Skip to content
This repository was archived by the owner on Apr 6, 2023. It is now read-only.

Commit be5829c

Browse files
committed
fix: replace other locations of custom import name
1 parent 6420716 commit be5829c

File tree

2 files changed

+7
-12
lines changed

2 files changed

+7
-12
lines changed

packages/kit/src/internal/template.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { promises as fsp } from 'node:fs'
22
import lodashTemplate from 'lodash.template'
3-
import hash from 'hash-sum'
4-
import { camelCase } from 'scule'
5-
import { basename, extname } from 'pathe'
6-
import { genDynamicImport, genImport } from 'knitwork'
3+
import { genSafeVariableName, genDynamicImport, genImport } from 'knitwork'
74

85
import type { NuxtTemplate } from '@nuxt/schema'
96

@@ -26,18 +23,16 @@ export async function compileTemplate (template: NuxtTemplate, ctx: any) {
2623

2724
const serialize = (data: any) => JSON.stringify(data, null, 2).replace(/"{(.+)}"(?=,?$)/gm, r => JSON.parse(r).replace(/^{(.*)}$/, '$1'))
2825

29-
const importName = (src: string) => `${camelCase(basename(src, extname(src))).replace(/[^a-zA-Z?\d\s:]/g, '')}_${hash(src)}`
30-
3126
const importSources = (sources: string | string[], { lazy = false } = {}) => {
3227
if (!Array.isArray(sources)) {
3328
sources = [sources]
3429
}
3530
return sources.map((src) => {
3631
if (lazy) {
37-
return `const ${importName(src)} = ${genDynamicImport(src, { comment: `webpackChunkName: ${JSON.stringify(src)}` })}`
32+
return `const ${genSafeVariableName(src)} = ${genDynamicImport(src, { comment: `webpackChunkName: ${JSON.stringify(src)}` })}`
3833
}
39-
return genImport(src, importName(src))
34+
return genImport(src, genSafeVariableName(src))
4035
}).join('\n')
4136
}
4237

43-
export const templateUtils = { serialize, importName, importSources }
38+
export const templateUtils = { serialize, importName: genSafeVariableName, importSources }

packages/nuxt/src/core/templates.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { templateUtils } from '@nuxt/kit'
22
import type { Nuxt, NuxtApp, NuxtTemplate } from '@nuxt/schema'
3-
import { genArrayFromRaw, genDynamicImport, genExport, genImport, genObjectFromRawEntries, genString } from 'knitwork'
3+
import { genArrayFromRaw, genDynamicImport, genExport, genImport, genObjectFromRawEntries, genString, genSafeVariableName } from 'knitwork'
44

55
import { isAbsolute, join, relative } from 'pathe'
66
import { resolveSchema, generateTypes } from 'untyped'
@@ -50,7 +50,7 @@ export const clientPluginTemplate = {
5050
const clientPlugins = ctx.app.plugins.filter(p => !p.mode || p.mode !== 'server')
5151
return [
5252
templateUtils.importSources(clientPlugins.map(p => p.src)),
53-
`export default ${genArrayFromRaw(clientPlugins.map(p => templateUtils.importName(p.src)))}`
53+
`export default ${genArrayFromRaw(clientPlugins.map(p => genSafeVariableName(p.src)))}`
5454
].join('\n')
5555
}
5656
}
@@ -64,7 +64,7 @@ export const serverPluginTemplate = {
6464
templateUtils.importSources(serverPlugins.map(p => p.src)),
6565
`export default ${genArrayFromRaw([
6666
'preload',
67-
...serverPlugins.map(p => templateUtils.importName(p.src))
67+
...serverPlugins.map(p => genSafeVariableName(p.src))
6868
])}`
6969
].join('\n')
7070
}

0 commit comments

Comments
 (0)