11import { promises as fsp } from 'node:fs'
22import 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
85import type { NuxtTemplate } from '@nuxt/schema'
96
@@ -26,18 +23,16 @@ export async function compileTemplate (template: NuxtTemplate, ctx: any) {
2623
2724const 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 - z A - Z ? \d \s : ] / g, '' ) } _${ hash ( src ) } `
30-
3126const 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 }
0 commit comments