mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-18 09:25:54 +00:00
refactor(nuxt3): use the @nuxt/kit
template utilites (#1908)
This commit is contained in:
parent
a7eacfed88
commit
c53b6a5f2e
@ -1,19 +0,0 @@
|
||||
import { basename, extname } from 'pathe'
|
||||
import hash from 'hash-sum'
|
||||
import { camelCase } from 'scule'
|
||||
|
||||
export const serialize = data => JSON.stringify(data, null, 2).replace(/"{(.+)}"/g, '$1')
|
||||
|
||||
export const importName = (src: string) => `${camelCase(basename(src, extname(src))).replace(/[^a-zA-Z?\d\s:]/g, '')}_${hash(src)}`
|
||||
|
||||
export const importSources = (sources: string | string[], { lazy = false } = {}) => {
|
||||
if (!Array.isArray(sources)) {
|
||||
sources = [sources]
|
||||
}
|
||||
return sources.map((src) => {
|
||||
if (lazy) {
|
||||
return `const ${importName(src)} = () => import('${src}' /* webpackChunkName: '${src}' */)`
|
||||
}
|
||||
return `import ${importName(src)} from '${src}'`
|
||||
}).join('\n')
|
||||
}
|
@ -1,8 +1,6 @@
|
||||
|
||||
import { templateUtils } from '@nuxt/kit'
|
||||
import type { Nuxt, NuxtApp } from '@nuxt/kit'
|
||||
|
||||
import { importName, importSources } from './template.utils'
|
||||
|
||||
type TemplateContext = {
|
||||
nuxt: Nuxt;
|
||||
app: NuxtApp;
|
||||
@ -35,9 +33,9 @@ export const clientPluginTemplate = {
|
||||
getContents (ctx: TemplateContext) {
|
||||
const clientPlugins = ctx.app.plugins.filter(p => !p.mode || p.mode !== 'server')
|
||||
return [
|
||||
importSources(clientPlugins.map(p => p.src)),
|
||||
templateUtils.importSources(clientPlugins.map(p => p.src)),
|
||||
'export default [',
|
||||
clientPlugins.map(p => importName(p.src)).join(',\n '),
|
||||
clientPlugins.map(p => templateUtils.importName(p.src)).join(',\n '),
|
||||
']'
|
||||
].join('\n')
|
||||
}
|
||||
@ -49,10 +47,10 @@ export const serverPluginTemplate = {
|
||||
const serverPlugins = ctx.app.plugins.filter(p => !p.mode || p.mode !== 'client')
|
||||
return [
|
||||
"import preload from '#app/plugins/preload.server'",
|
||||
importSources(serverPlugins.map(p => p.src)),
|
||||
templateUtils.importSources(serverPlugins.map(p => p.src)),
|
||||
'export default [',
|
||||
' preload,',
|
||||
serverPlugins.map(p => importName(p.src)).join(',\n '),
|
||||
serverPlugins.map(p => templateUtils.importName(p.src)).join(',\n '),
|
||||
']'
|
||||
].join('\n')
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { existsSync } from 'fs'
|
||||
import { defineNuxtModule, addTemplate, addPlugin } from '@nuxt/kit'
|
||||
import { defineNuxtModule, addTemplate, addPlugin, templateUtils } from '@nuxt/kit'
|
||||
import { resolve } from 'pathe'
|
||||
import { distDir } from '../dirs'
|
||||
import { resolveLayouts, resolvePagesRoutes, addComponentToRoutes } from './utils'
|
||||
@ -45,7 +45,7 @@ export default defineNuxtModule({
|
||||
const pages = await resolvePagesRoutes(nuxt)
|
||||
await nuxt.callHook('pages:extend', pages)
|
||||
const serializedRoutes = addComponentToRoutes(pages)
|
||||
return `export default ${JSON.stringify(serializedRoutes, null, 2).replace(/"{(.+)}"/g, '$1')}`
|
||||
return `export default ${templateUtils.serialize(serializedRoutes)}`
|
||||
}
|
||||
})
|
||||
|
||||
@ -59,7 +59,7 @@ export default defineNuxtModule({
|
||||
}))
|
||||
return [
|
||||
'import { defineAsyncComponent } from \'vue\'',
|
||||
`export default ${JSON.stringify(layoutsObject, null, 2).replace(/"{(.+)}"/g, '$1')}`
|
||||
`export default ${templateUtils.serialize(layoutsObject)}`
|
||||
].join('\n')
|
||||
}
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user