Skip to content

Commit

Permalink
refactor: utilize knitwork to generate safe variable names (nitrojs#447)
Browse files Browse the repository at this point in the history
  • Loading branch information
manniL authored and WinterYukky committed Nov 1, 2022
1 parent 7f172cc commit 6fa05eb
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 10 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/rollup/plugins/dynamic-require.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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)

Expand Down
6 changes: 3 additions & 3 deletions src/rollup/plugins/storage.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -44,15 +44,15 @@ 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({})
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 : ''}
`
Expand Down
5 changes: 0 additions & 5 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 6fa05eb

Please sign in to comment.