diff --git a/package.json b/package.json index 728121725b..79cdbbeb73 100644 --- a/package.json +++ b/package.json @@ -73,6 +73,7 @@ "is-primitive": "^3.0.1", "jiti": "^1.14.0", "klona": "^2.0.5", + "knitwork": "^0.1.2", "listhen": "^0.2.15", "mime": "^3.0.0", "mlly": "^0.5.14", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2b634ffef6..d5b88cdc8f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -54,6 +54,7 @@ importers: is-primitive: ^3.0.1 jiti: ^1.14.0 klona: ^2.0.5 + knitwork: ^0.1.2 listhen: ^0.2.15 mime: ^3.0.0 miniflare: ^2.7.1 @@ -118,6 +119,7 @@ importers: is-primitive: 3.0.1 jiti: 1.14.0 klona: 2.0.5 + knitwork: 0.1.2 listhen: 0.2.15 mime: 3.0.0 mlly: 0.5.14 @@ -4051,6 +4053,10 @@ packages: engines: {node: '>= 8'} dev: false + /knitwork/0.1.2: + resolution: {integrity: sha512-2ekmY2S/VB3YGVhrIFadyJQpkjMFSf48tsXCnA+kjs4FEQIT+5FLyOF0No/X58z/2E/VaMyeJfukRoVT4gMsfQ==} + dev: false + /lazystream/1.0.1: resolution: {integrity: sha512-b94GiNHQNy6JNTrt5w6zNyffMrNkXZb3KTkCZJb2V1xaEGCk093vkZ2jk3tpaeP33/OiXC+WvK9AxUebnf5nbw==} engines: {node: '>= 0.6.3'} diff --git a/src/rollup/plugins/dynamic-require.ts b/src/rollup/plugins/dynamic-require.ts index 3c5e2e80f6..da392f939a 100644 --- a/src/rollup/plugins/dynamic-require.ts +++ b/src/rollup/plugins/dynamic-require.ts @@ -2,7 +2,7 @@ import { pathToFileURL } from 'url' import { resolve } from 'pathe' import { globby } from 'globby' import type { Plugin } from 'rollup' -import { serializeImportName } from '../../utils' +import { genSafeVariableName } from 'knitwork' const PLUGIN_NAME = 'dynamic-require' const HELPER_DYNAMIC = `\0${PLUGIN_NAME}.mjs` @@ -66,7 +66,7 @@ export function dynamicRequire ({ dir, ignore, inline }: Options): Plugin { const chunks = (await Promise.all(files.map(async id => ({ id, src: resolve(dir, id).replace(/\\/g, '/'), - name: serializeImportName(id), + name: genSafeVariableName(id), meta: await getWebpackChunkMeta(resolve(dir, id)) })))).filter(chunk => chunk.meta) diff --git a/src/rollup/plugins/storage.ts b/src/rollup/plugins/storage.ts index 7866945bbd..530eb7c878 100644 --- a/src/rollup/plugins/storage.ts +++ b/src/rollup/plugins/storage.ts @@ -1,5 +1,5 @@ import { builtinDrivers } from 'unstorage' -import { serializeImportName } from '../../utils' +import { genImport, genSafeVariableName } from 'knitwork' import type { Nitro } from '../../types' import { virtual } from './virtual' @@ -44,7 +44,7 @@ for (const base of bundledStorage) { import { createStorage } from 'unstorage' import { assets } from '#internal/nitro/virtual/server-assets' -${driverImports.map(i => `import ${serializeImportName(i)} from '${i}'`).join('\n')} +${driverImports.map(i => genImport(i, genSafeVariableName(i))).join('\n')} const storage = createStorage({}) @@ -52,7 +52,7 @@ export const useStorage = () => storage storage.mount('/assets', assets) -${mounts.map(m => `storage.mount('${m.path}', ${serializeImportName(m.driver)}(${JSON.stringify(m.opts)}))`).join('\n')} +${mounts.map(m => `storage.mount('${m.path}', ${genSafeVariableName(m.driver)}(${JSON.stringify(m.opts)}))`).join('\n')} ${(!isDevOrPrerender && nitro.options.bundledStorage.length) ? bundledStorageCode : ''} ` diff --git a/src/utils/index.ts b/src/utils/index.ts index c552f9ad06..9e496390d3 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -105,11 +105,6 @@ export function getDependencies (dir: string, mode: keyof typeof _getDependencie return dependencies } -// TODO: Refactor to scule (https://github.com/unjs/scule/issues/6) -export function serializeImportName (id: string) { - return '_' + id.replace(/[^a-zA-Z0-9_$]/g, '_') -} - export function readPackageJson ( packageName: string, _require: NodeRequire = createRequire(import.meta.url)