refactor(app): remove lodash template files (#543)

This commit is contained in:
Xin Du (Clark) 2021-09-21 15:55:07 +01:00 committed by GitHub
parent 3671cfc772
commit f4cf2199a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 75 additions and 38 deletions

View File

@ -1,6 +1,5 @@
dist dist
node_modules node_modules
_templates
schema schema
**/*.tmpl.* **/*.tmpl.*
sw.js sw.js

View File

@ -1 +0,0 @@
export { default } from '<%= app.main %>'

View File

@ -1 +0,0 @@
<%= nuxt.options.css.map(i => `import '${i.src || i}';`).join('') %>

View File

@ -1,5 +0,0 @@
<%= utils.importSources(app.plugins.filter(p => !p.mode || p.mode !== 'server').map(p => p.src)) %>
export default [
<%= app.plugins.filter(p => !p.mode || p.mode !== 'server').map(p => utils.importName(p.src)).join(',\n ') %>
]

View File

@ -1,8 +0,0 @@
import preload from '#app/plugins/preload.server'
<%= utils.importSources(app.plugins.filter(p => !p.mode || p.mode !== 'client').map(p => p.src)) %>
export default [
preload,
<%= app.plugins.filter(p => !p.mode || p.mode !== 'client').map(p => utils.importName(p.src)).join(',\n ') %>
]

View File

@ -1,12 +0,0 @@
<!DOCTYPE html>
<html {{ HTML_ATTRS }}>
<head {{ HEAD_ATTRS }}>
{{ HEAD }}
</head>
<body {{ BODY_ATTRS }}>
{{ APP }}
</body>
</html>

View File

@ -0,0 +1,71 @@
import type { Nuxt, NuxtApp } from '@nuxt/kit'
import * as templateUtils from '../core/template.utils'
type TemplateContext = {
nuxt: Nuxt;
app: NuxtApp;
utils: typeof templateUtils
}
export const appTemplate = {
filename: 'app.mjs',
getContents (ctx: TemplateContext) {
return `export { default } from '${ctx.app.main}'`
}
}
export const cssTemplate = {
filename: 'css.mjs',
getContents (ctx: TemplateContext) {
return ctx.nuxt.options.css.map(i => `import '${i.src || i}';`).join('\n')
}
}
export const clientPluginTemplate = {
filename: 'plugins/client.mjs',
getContents (ctx: TemplateContext) {
const { app, utils } = ctx
return [
utils.importSources(app.plugins.filter(p => !p.mode || p.mode !== 'server').map(p => p.src)),
'export default [',
app.plugins.filter(p => !p.mode || p.mode !== 'server').map(p => utils.importName(p.src)).join(',\n '),
']'
].join('\n')
}
}
export const serverPluginTemplate = {
filename: 'plugins/server.mjs',
getContents (ctx: TemplateContext) {
const { app, utils } = ctx
return [
"import preload from '#app/plugins/preload.server'",
utils.importSources(app.plugins.filter(p => !p.mode || p.mode !== 'client').map(p => p.src)),
'export default [',
' preload,',
app.plugins.filter(p => !p.mode || p.mode !== 'client').map(p => utils.importName(p.src)).join(',\n '),
']'
].join('\n')
}
}
export const appViewTemplate = {
filename: 'views/app.template.html',
getContents () {
return `<!DOCTYPE html>
<html {{ HTML_ATTRS }}>
<head {{ HEAD_ATTRS }}>
{{ HEAD }}
</head>
<body {{ BODY_ATTRS }}>
{{ APP }}
</body>
</html>
`
}
}

View File

@ -1,9 +1,10 @@
import { resolve, join, relative } from 'upath' import { resolve } from 'upath'
import globby from 'globby'
import lodashTemplate from 'lodash/template' import lodashTemplate from 'lodash/template'
import defu from 'defu' import defu from 'defu'
import { tryResolvePath, resolveFiles, Nuxt, NuxtApp, NuxtTemplate, normalizePlugin, normalizeTemplate } from '@nuxt/kit' import { tryResolvePath, resolveFiles, Nuxt, NuxtApp, NuxtTemplate, normalizePlugin, normalizeTemplate } from '@nuxt/kit'
import { readFile, writeFile } from 'fs-extra' import { readFile, writeFile } from 'fs-extra'
import * as defaultTemplates from '../app/templates'
import * as templateUtils from './template.utils' import * as templateUtils from './template.utils'
export function createApp (nuxt: Nuxt, options: Partial<NuxtApp> = {}): NuxtApp { export function createApp (nuxt: Nuxt, options: Partial<NuxtApp> = {}): NuxtApp {
@ -19,15 +20,8 @@ export async function generateApp (nuxt: Nuxt, app: NuxtApp) {
// Resolve app // Resolve app
await resolveApp(nuxt, app) await resolveApp(nuxt, app)
// Scan app templates
const templatesDir = join(nuxt.options.appDir, '_templates')
const templateFiles = await globby(join(templatesDir, '/**'))
app.templates = templateFiles
.filter(src => !src.endsWith('.d.ts'))
.map(src => ({ src, filename: relative(templatesDir, src) } as NuxtTemplate))
// User templates from options.build.templates // User templates from options.build.templates
app.templates = app.templates.concat(nuxt.options.build.templates) app.templates = Object.values(defaultTemplates).concat(nuxt.options.build.templates)
// Extend templates with hook // Extend templates with hook
await nuxt.callHook('app:templates', app) await nuxt.callHook('app:templates', app)