2021-11-15 19:20:51 +00:00
|
|
|
import { templateUtils } from '@nuxt/kit'
|
2021-11-21 16:14:46 +00:00
|
|
|
import type { Nuxt, NuxtApp } from '@nuxt/schema'
|
2021-09-21 14:55:07 +00:00
|
|
|
|
2021-11-18 13:11:34 +00:00
|
|
|
import { relative } from 'pathe'
|
2022-01-27 11:13:32 +00:00
|
|
|
import escapeRE from 'escape-string-regexp'
|
2021-11-18 13:11:34 +00:00
|
|
|
|
2021-09-21 14:55:07 +00:00
|
|
|
type TemplateContext = {
|
|
|
|
nuxt: Nuxt;
|
|
|
|
app: NuxtApp;
|
|
|
|
}
|
|
|
|
|
2022-01-19 18:10:38 +00:00
|
|
|
export const vueShim = {
|
|
|
|
filename: 'vue-shim.d.ts',
|
|
|
|
write: true,
|
|
|
|
getContents: () =>
|
|
|
|
[
|
|
|
|
'declare module \'*.vue\' {',
|
|
|
|
' import { DefineComponent } from \'@vue/runtime-core\'',
|
|
|
|
' const component: DefineComponent<{}, {}, any>',
|
|
|
|
' export default component',
|
|
|
|
'}'
|
|
|
|
].join('\n')
|
|
|
|
}
|
|
|
|
|
2021-10-12 12:51:41 +00:00
|
|
|
// TODO: Use an alias
|
|
|
|
export const appComponentTemplate = {
|
|
|
|
filename: 'app-component.mjs',
|
2021-09-21 14:55:07 +00:00
|
|
|
getContents (ctx: TemplateContext) {
|
2021-10-12 12:51:41 +00:00
|
|
|
return `export { default } from '${ctx.app.mainComponent}'`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO: Use an alias
|
|
|
|
export const rootComponentTemplate = {
|
|
|
|
filename: 'root-component.mjs',
|
|
|
|
getContents (ctx: TemplateContext) {
|
|
|
|
return `export { default } from '${ctx.app.rootComponent}'`
|
2021-09-21 14:55:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2021-11-04 08:16:54 +00:00
|
|
|
const clientPlugins = ctx.app.plugins.filter(p => !p.mode || p.mode !== 'server')
|
2021-09-21 14:55:07 +00:00
|
|
|
return [
|
2021-11-15 19:20:51 +00:00
|
|
|
templateUtils.importSources(clientPlugins.map(p => p.src)),
|
2021-09-21 14:55:07 +00:00
|
|
|
'export default [',
|
2021-11-15 19:20:51 +00:00
|
|
|
clientPlugins.map(p => templateUtils.importName(p.src)).join(',\n '),
|
2021-09-21 14:55:07 +00:00
|
|
|
']'
|
|
|
|
].join('\n')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const serverPluginTemplate = {
|
|
|
|
filename: 'plugins/server.mjs',
|
|
|
|
getContents (ctx: TemplateContext) {
|
2021-11-04 08:16:54 +00:00
|
|
|
const serverPlugins = ctx.app.plugins.filter(p => !p.mode || p.mode !== 'client')
|
2021-09-21 14:55:07 +00:00
|
|
|
return [
|
|
|
|
"import preload from '#app/plugins/preload.server'",
|
2021-11-15 19:20:51 +00:00
|
|
|
templateUtils.importSources(serverPlugins.map(p => p.src)),
|
2021-09-21 14:55:07 +00:00
|
|
|
'export default [',
|
|
|
|
' preload,',
|
2021-11-15 19:20:51 +00:00
|
|
|
serverPlugins.map(p => templateUtils.importName(p.src)).join(',\n '),
|
2021-09-21 14:55:07 +00:00
|
|
|
']'
|
|
|
|
].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>
|
|
|
|
`
|
|
|
|
}
|
|
|
|
}
|
2021-11-18 13:11:34 +00:00
|
|
|
|
|
|
|
export const pluginsDeclaration = {
|
|
|
|
filename: 'plugins.d.ts',
|
|
|
|
write: true,
|
|
|
|
getContents: (ctx: TemplateContext) => {
|
2022-01-27 16:02:25 +00:00
|
|
|
const EXTENSION_RE = new RegExp(`(?<=\\w)(${ctx.nuxt.options.extensions.map(e => escapeRE(e)).join('|')})$`, 'g')
|
2021-11-18 13:11:34 +00:00
|
|
|
const tsImports = ctx.app.plugins.map(p => relative(ctx.nuxt.options.buildDir, p.src).replace(EXTENSION_RE, ''))
|
|
|
|
|
|
|
|
return `// Generated by Nuxt3'
|
|
|
|
import type { Plugin } from '#app'
|
|
|
|
|
|
|
|
type Decorate<T extends Record<string, any>> = { [K in keyof T as K extends string ? \`$\${K}\` : never]: T[K] }
|
|
|
|
|
|
|
|
type InjectionType<A extends Plugin> = A extends Plugin<infer T> ? Decorate<T> : unknown
|
|
|
|
|
|
|
|
type NuxtAppInjections = \n ${tsImports.map(p => `InjectionType<typeof import('${p}').default>`).join(' &\n ')}
|
|
|
|
|
|
|
|
declare module '#app' {
|
|
|
|
interface NuxtApp extends NuxtAppInjections { }
|
|
|
|
}
|
|
|
|
|
|
|
|
declare module '@vue/runtime-core' {
|
|
|
|
interface ComponentCustomProperties extends NuxtAppInjections { }
|
|
|
|
}
|
|
|
|
|
|
|
|
export { }
|
|
|
|
`
|
|
|
|
}
|
|
|
|
}
|