feat: static asset handling

This commit is contained in:
alex8088
2023-01-04 22:53:22 +08:00
parent f7b4146c56
commit 336b4292eb
5 changed files with 193 additions and 2 deletions

View File

@@ -1,4 +1,6 @@
import { URL, URLSearchParams } from 'node:url'
import path from 'node:path'
import { createHash } from 'node:crypto'
import { loadEnv as viteLoadEnv } from 'vite'
export function isObject(value: unknown): value is Record<string, unknown> {
@@ -26,6 +28,15 @@ export function parseRequest(id: string): Record<string, string> | null {
return Object.fromEntries(new URLSearchParams(search))
}
export function getHash(text: Buffer | string): string {
return createHash('sha256').update(text).digest('hex').substring(0, 8)
}
export function toRelativePath(filename: string, importer: string): string {
const relPath = path.posix.relative(path.dirname(importer), filename)
return relPath.startsWith('.') ? relPath : `./${relPath}`
}
/**
* Load `.env` files within the `envDir`(default: `process.cwd()`).
* By default, only env variables prefixed with `MAIN_VITE_`, `PRELOAD_VITE_` and