fix(storage): replace non-word characters with underscore (#542)

This commit is contained in:
Ahad Birang 2021-09-21 19:35:36 +04:30 committed by GitHub
parent f4cf2199a5
commit 5394aef2f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 7 deletions

View File

@ -1,6 +1,7 @@
import { resolve } from 'upath' import { resolve } from 'upath'
import globby from 'globby' import globby from 'globby'
import type { Plugin } from 'rollup' import type { Plugin } from 'rollup'
import { serializeImportName } from '../../utils'
const PLUGIN_NAME = 'dynamic-require' const PLUGIN_NAME = 'dynamic-require'
const HELPER_DYNAMIC = `\0${PLUGIN_NAME}.js` const HELPER_DYNAMIC = `\0${PLUGIN_NAME}.js`
@ -63,7 +64,7 @@ export function dynamicRequire ({ dir, ignore, inline }: Options): Plugin {
const chunks = files.map(id => ({ const chunks = files.map(id => ({
id, id,
src: resolve(dir, id).replace(/\\/g, '/'), src: resolve(dir, id).replace(/\\/g, '/'),
name: '_' + id.replace(/[^a-zA-Z0-9_]/g, '_'), name: serializeImportName(id),
meta: getWebpackChunkMeta(resolve(dir, id)) meta: getWebpackChunkMeta(resolve(dir, id))
})).filter(chunk => chunk.meta) })).filter(chunk => chunk.meta)

View File

@ -1,4 +1,5 @@
import virtual from '@rollup/plugin-virtual' import virtual from '@rollup/plugin-virtual'
import { serializeImportName } from '../../utils'
export interface StorageOptions { export interface StorageOptions {
mounts: { mounts: {
@ -34,17 +35,13 @@ export function storage (opts: StorageOptions) {
import { createStorage } from 'unstorage' import { createStorage } from 'unstorage'
import { assets } from '#assets' import { assets } from '#assets'
${driverImports.map(i => `import ${getImportName(i)} from '${i}'`).join('\n')} ${driverImports.map(i => `import ${serializeImportName(i)} from '${i}'`).join('\n')}
export const storage = createStorage({}) export const storage = createStorage({})
storage.mount('/assets', assets) storage.mount('/assets', assets)
${mounts.map(m => `storage.mount('${m.path}', ${getImportName(m.driver)}(${JSON.stringify(m.opts)}))`).join('\n')} ${mounts.map(m => `storage.mount('${m.path}', ${serializeImportName(m.driver)}(${JSON.stringify(m.opts)}))`).join('\n')}
` `
}) })
} }
function getImportName (id: string) {
return '_' + id.replace(/[\\/.]/g, '_')
}

View File

@ -120,3 +120,8 @@ export function getDependencies (dir: string, mode: keyof typeof _getDependencie
} }
return dependencies 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, '_')
}