2023-05-20 22:14:52 +00:00
|
|
|
import { existsSync } from 'node:fs'
|
2023-04-07 16:02:47 +00:00
|
|
|
import { genArrayFromRaw, genDynamicImport, genExport, genImport, genObjectFromRawEntries, genSafeVariableName, genString } from 'knitwork'
|
2022-10-03 13:32:01 +00:00
|
|
|
import { isAbsolute, join, relative, resolve } from 'pathe'
|
2023-07-30 09:16:13 +00:00
|
|
|
import type { JSValue } from 'untyped'
|
2023-04-07 16:02:47 +00:00
|
|
|
import { generateTypes, resolveSchema } from 'untyped'
|
2022-01-27 11:13:32 +00:00
|
|
|
import escapeRE from 'escape-string-regexp'
|
2022-08-08 11:03:44 +00:00
|
|
|
import { hash } from 'ohash'
|
2022-08-23 14:24:20 +00:00
|
|
|
import { camelCase } from 'scule'
|
2023-01-30 20:24:58 +00:00
|
|
|
import { filename } from 'pathe/utils'
|
2024-01-12 10:47:23 +00:00
|
|
|
import type { NuxtTemplate } from 'nuxt/schema'
|
2023-12-14 17:11:08 +00:00
|
|
|
import { annotatePlugins, checkForCircularDependencies } from './app'
|
2021-11-18 13:11:34 +00:00
|
|
|
|
2022-08-12 17:47:58 +00:00
|
|
|
export const vueShim: NuxtTemplate = {
|
2022-02-07 10:20:01 +00:00
|
|
|
filename: 'types/vue-shim.d.ts',
|
2023-09-22 07:20:29 +00:00
|
|
|
getContents: ({ nuxt }) => {
|
|
|
|
if (!nuxt.options.typescript.shim) {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
2022-01-19 18:10:38 +00:00
|
|
|
'declare module \'*.vue\' {',
|
2023-01-25 15:59:02 +00:00
|
|
|
' import { DefineComponent } from \'vue\'',
|
2022-01-19 18:10:38 +00:00
|
|
|
' const component: DefineComponent<{}, {}, any>',
|
|
|
|
' export default component',
|
|
|
|
'}'
|
|
|
|
].join('\n')
|
2023-09-22 07:20:29 +00:00
|
|
|
}
|
2022-01-19 18:10:38 +00:00
|
|
|
}
|
|
|
|
|
2021-10-12 12:51:41 +00:00
|
|
|
// TODO: Use an alias
|
2024-01-12 10:47:23 +00:00
|
|
|
export const appComponentTemplate: NuxtTemplate = {
|
2021-10-12 12:51:41 +00:00
|
|
|
filename: 'app-component.mjs',
|
2022-08-12 17:47:58 +00:00
|
|
|
getContents: ctx => genExport(ctx.app.mainComponent!, ['default'])
|
2021-10-12 12:51:41 +00:00
|
|
|
}
|
|
|
|
// TODO: Use an alias
|
2024-01-12 10:47:23 +00:00
|
|
|
export const rootComponentTemplate: NuxtTemplate = {
|
2021-10-12 12:51:41 +00:00
|
|
|
filename: 'root-component.mjs',
|
2023-12-19 10:07:34 +00:00
|
|
|
// TODO: fix upstream in vite - this ensures that vite generates a module graph for islands
|
|
|
|
// but should not be necessary (and has a warmup performance cost). See https://github.com/nuxt/nuxt/pull/24584.
|
|
|
|
getContents: ctx => (ctx.nuxt.options.dev ? "import '#build/components.islands.mjs';\n" : '') + genExport(ctx.app.rootComponent!, ['default'])
|
2021-09-21 14:55:07 +00:00
|
|
|
}
|
2022-03-11 08:22:16 +00:00
|
|
|
// TODO: Use an alias
|
2024-01-12 10:47:23 +00:00
|
|
|
export const errorComponentTemplate: NuxtTemplate = {
|
2022-03-11 08:22:16 +00:00
|
|
|
filename: 'error-component.mjs',
|
2022-08-12 17:47:58 +00:00
|
|
|
getContents: ctx => genExport(ctx.app.errorComponent!, ['default'])
|
2022-03-11 08:22:16 +00:00
|
|
|
}
|
2023-04-06 12:07:22 +00:00
|
|
|
// TODO: Use an alias
|
2024-01-12 10:47:23 +00:00
|
|
|
export const testComponentWrapperTemplate: NuxtTemplate = {
|
2023-04-06 12:07:22 +00:00
|
|
|
filename: 'test-component-wrapper.mjs',
|
2024-01-12 10:47:23 +00:00
|
|
|
getContents: (ctx) => genExport(resolve(ctx.nuxt.options.appDir, 'components/test-component-wrapper'), ['default'])
|
2023-04-06 12:07:22 +00:00
|
|
|
}
|
2021-09-21 14:55:07 +00:00
|
|
|
|
2024-01-12 10:47:23 +00:00
|
|
|
export const cssTemplate: NuxtTemplate = {
|
2021-09-21 14:55:07 +00:00
|
|
|
filename: 'css.mjs',
|
2022-08-12 17:47:58 +00:00
|
|
|
getContents: ctx => ctx.nuxt.options.css.map(i => genImport(i)).join('\n')
|
2021-09-21 14:55:07 +00:00
|
|
|
}
|
|
|
|
|
2024-01-12 10:47:23 +00:00
|
|
|
export const clientPluginTemplate: NuxtTemplate = {
|
2021-09-21 14:55:07 +00:00
|
|
|
filename: 'plugins/client.mjs',
|
2023-06-19 23:00:03 +00:00
|
|
|
async getContents (ctx) {
|
|
|
|
const clientPlugins = await annotatePlugins(ctx.nuxt, ctx.app.plugins.filter(p => !p.mode || p.mode !== 'server'))
|
2023-12-14 17:11:08 +00:00
|
|
|
checkForCircularDependencies(clientPlugins)
|
2022-08-08 11:03:44 +00:00
|
|
|
const exports: string[] = []
|
|
|
|
const imports: string[] = []
|
|
|
|
for (const plugin of clientPlugins) {
|
|
|
|
const path = relative(ctx.nuxt.options.rootDir, plugin.src)
|
2023-01-30 20:24:58 +00:00
|
|
|
const variable = genSafeVariableName(filename(plugin.src)).replace(/_(45|46|47)/g, '_') + '_' + hash(path)
|
2022-08-08 11:03:44 +00:00
|
|
|
exports.push(variable)
|
|
|
|
imports.push(genImport(plugin.src, variable))
|
|
|
|
}
|
2021-09-21 14:55:07 +00:00
|
|
|
return [
|
2022-07-21 18:09:27 +00:00
|
|
|
...imports,
|
|
|
|
`export default ${genArrayFromRaw(exports)}`
|
2021-09-21 14:55:07 +00:00
|
|
|
].join('\n')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-12 10:47:23 +00:00
|
|
|
export const serverPluginTemplate: NuxtTemplate = {
|
2021-09-21 14:55:07 +00:00
|
|
|
filename: 'plugins/server.mjs',
|
2023-06-19 23:00:03 +00:00
|
|
|
async getContents (ctx) {
|
|
|
|
const serverPlugins = await annotatePlugins(ctx.nuxt, ctx.app.plugins.filter(p => !p.mode || p.mode !== 'client'))
|
2023-12-14 17:11:08 +00:00
|
|
|
checkForCircularDependencies(serverPlugins)
|
2022-09-13 10:57:14 +00:00
|
|
|
const exports: string[] = []
|
|
|
|
const imports: string[] = []
|
2022-08-08 11:03:44 +00:00
|
|
|
for (const plugin of serverPlugins) {
|
|
|
|
const path = relative(ctx.nuxt.options.rootDir, plugin.src)
|
2023-01-30 20:24:58 +00:00
|
|
|
const variable = genSafeVariableName(filename(path)).replace(/_(45|46|47)/g, '_') + '_' + hash(path)
|
2022-08-08 11:03:44 +00:00
|
|
|
exports.push(variable)
|
|
|
|
imports.push(genImport(plugin.src, variable))
|
|
|
|
}
|
2021-09-21 14:55:07 +00:00
|
|
|
return [
|
2022-07-21 18:09:27 +00:00
|
|
|
...imports,
|
2022-08-08 11:03:44 +00:00
|
|
|
`export default ${genArrayFromRaw(exports)}`
|
2021-09-21 14:55:07 +00:00
|
|
|
].join('\n')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-12 10:47:23 +00:00
|
|
|
export const pluginsDeclaration: NuxtTemplate = {
|
2022-02-07 10:20:01 +00:00
|
|
|
filename: 'types/plugins.d.ts',
|
2023-12-14 17:11:08 +00:00
|
|
|
getContents: async (ctx) => {
|
2022-01-27 16:02:25 +00:00
|
|
|
const EXTENSION_RE = new RegExp(`(?<=\\w)(${ctx.nuxt.options.extensions.map(e => escapeRE(e)).join('|')})$`, 'g')
|
2023-11-09 11:25:37 +00:00
|
|
|
const tsImports: string[] = []
|
|
|
|
for (const p of ctx.app.plugins) {
|
|
|
|
const sources = [p.src, p.src.replace(EXTENSION_RE, '.d.ts')]
|
|
|
|
if (!isAbsolute(p.src)) {
|
|
|
|
tsImports.push(p.src.replace(EXTENSION_RE, ''))
|
|
|
|
} else if (ctx.app.templates.some(t => t.write && t.dst && sources.includes(t.dst)) || sources.some(s => existsSync(s))) {
|
|
|
|
tsImports.push(relative(join(ctx.nuxt.options.buildDir, 'types'), p.src).replace(EXTENSION_RE, ''))
|
|
|
|
}
|
|
|
|
}
|
2021-11-18 13:11:34 +00:00
|
|
|
|
2023-12-14 17:11:08 +00:00
|
|
|
const pluginsName = (await annotatePlugins(ctx.nuxt, ctx.app.plugins)).filter(p => p.name).map(p => `'${p.name}'`)
|
|
|
|
|
2022-04-20 08:52:39 +00:00
|
|
|
return `// Generated by Nuxt'
|
2021-11-18 13:11:34 +00:00
|
|
|
import type { Plugin } from '#app'
|
|
|
|
|
|
|
|
type Decorate<T extends Record<string, any>> = { [K in keyof T as K extends string ? \`$\${K}\` : never]: T[K] }
|
|
|
|
|
2024-01-02 16:53:36 +00:00
|
|
|
type IsAny<T> = 0 extends 1 & T ? true : false
|
|
|
|
type InjectionType<A extends Plugin> = IsAny<A> extends true ? unknown : A extends Plugin<infer T> ? Decorate<T> : unknown
|
2021-11-18 13:11:34 +00:00
|
|
|
|
2022-02-07 13:45:47 +00:00
|
|
|
type NuxtAppInjections = \n ${tsImports.map(p => `InjectionType<typeof ${genDynamicImport(p, { wrapper: false })}.default>`).join(' &\n ')}
|
2021-11-18 13:11:34 +00:00
|
|
|
|
|
|
|
declare module '#app' {
|
|
|
|
interface NuxtApp extends NuxtAppInjections { }
|
2023-12-14 17:11:08 +00:00
|
|
|
|
|
|
|
interface NuxtAppLiterals {
|
|
|
|
pluginName: ${pluginsName.join(' | ')}
|
|
|
|
}
|
2021-11-18 13:11:34 +00:00
|
|
|
}
|
|
|
|
|
2023-01-25 15:59:02 +00:00
|
|
|
declare module 'vue' {
|
2021-11-18 13:11:34 +00:00
|
|
|
interface ComponentCustomProperties extends NuxtAppInjections { }
|
|
|
|
}
|
|
|
|
|
|
|
|
export { }
|
|
|
|
`
|
|
|
|
}
|
|
|
|
}
|
2022-02-08 19:09:44 +00:00
|
|
|
|
2023-03-11 22:11:48 +00:00
|
|
|
const adHocModules = ['router', 'pages', 'imports', 'meta', 'components', 'nuxt-config-schema']
|
2024-01-12 10:47:23 +00:00
|
|
|
export const schemaTemplate: NuxtTemplate = {
|
2022-02-08 19:09:44 +00:00
|
|
|
filename: 'types/schema.d.ts',
|
2022-09-12 18:22:41 +00:00
|
|
|
getContents: async ({ nuxt }) => {
|
2022-02-08 19:09:44 +00:00
|
|
|
const moduleInfo = nuxt.options._installedModules.map(m => ({
|
2023-11-09 17:01:13 +00:00
|
|
|
...m.meta,
|
2022-02-08 19:09:44 +00:00
|
|
|
importName: m.entryPath || m.meta?.name
|
|
|
|
})).filter(m => m.configKey && m.name && !adHocModules.includes(m.name))
|
|
|
|
|
2022-10-03 13:32:01 +00:00
|
|
|
const relativeRoot = relative(resolve(nuxt.options.buildDir, 'types'), nuxt.options.rootDir)
|
2023-12-29 08:47:11 +00:00
|
|
|
const getImportName = (name: string) => (name[0] === '.' ? './' + join(relativeRoot, name) : name).replace(/\.\w+$/, '')
|
2023-01-21 16:55:44 +00:00
|
|
|
const modules = moduleInfo.map(meta => [genString(meta.configKey), getImportName(meta.importName)])
|
2024-01-09 09:56:40 +00:00
|
|
|
const privateRuntimeConfig = Object.create(null)
|
|
|
|
for (const key in nuxt.options.runtimeConfig) {
|
|
|
|
if (key !== 'public') {
|
|
|
|
privateRuntimeConfig[key] = nuxt.options.runtimeConfig[key]
|
|
|
|
}
|
|
|
|
}
|
2022-02-08 19:09:44 +00:00
|
|
|
return [
|
2023-04-04 13:34:39 +00:00
|
|
|
"import { NuxtModule, RuntimeConfig } from 'nuxt/schema'",
|
2023-02-13 22:42:04 +00:00
|
|
|
"declare module 'nuxt/schema' {",
|
2022-02-08 19:09:44 +00:00
|
|
|
' interface NuxtConfig {',
|
2023-01-21 16:55:44 +00:00
|
|
|
...modules.map(([configKey, importName]) =>
|
|
|
|
` [${configKey}]?: typeof ${genDynamicImport(importName, { wrapper: false })}.default extends NuxtModule<infer O> ? Partial<O> : Record<string, any>`
|
2022-02-08 19:09:44 +00:00
|
|
|
),
|
2023-03-19 23:16:13 +00:00
|
|
|
modules.length > 0 ? ` modules?: (undefined | null | false | NuxtModule | string | [NuxtModule | string, Record<string, any>] | ${modules.map(([configKey, importName]) => `[${genString(importName)}, Exclude<NuxtConfig[${configKey}], boolean>]`).join(' | ')})[],` : '',
|
2022-02-08 19:09:44 +00:00
|
|
|
' }',
|
2024-01-09 09:56:40 +00:00
|
|
|
generateTypes(await resolveSchema(privateRuntimeConfig as Record<string, JSValue>),
|
2022-03-09 13:41:24 +00:00
|
|
|
{
|
2022-04-11 14:34:23 +00:00
|
|
|
interfaceName: 'RuntimeConfig',
|
2022-03-09 13:41:24 +00:00
|
|
|
addExport: false,
|
|
|
|
addDefaults: false,
|
|
|
|
allowExtraKeys: false,
|
|
|
|
indentation: 2
|
|
|
|
}),
|
2023-07-30 09:16:13 +00:00
|
|
|
generateTypes(await resolveSchema(nuxt.options.runtimeConfig.public as Record<string, JSValue>),
|
2022-05-06 13:27:42 +00:00
|
|
|
{
|
|
|
|
interfaceName: 'PublicRuntimeConfig',
|
|
|
|
addExport: false,
|
|
|
|
addDefaults: false,
|
|
|
|
allowExtraKeys: false,
|
|
|
|
indentation: 2
|
|
|
|
}),
|
2023-04-04 13:34:39 +00:00
|
|
|
'}',
|
|
|
|
`declare module 'vue' {
|
|
|
|
interface ComponentCustomProperties {
|
|
|
|
$config: RuntimeConfig
|
|
|
|
}
|
|
|
|
}`
|
2022-02-08 19:09:44 +00:00
|
|
|
].join('\n')
|
|
|
|
}
|
|
|
|
}
|
2022-03-14 10:47:24 +00:00
|
|
|
|
|
|
|
// Add layouts template
|
2024-01-12 10:47:23 +00:00
|
|
|
export const layoutTemplate: NuxtTemplate = {
|
2022-03-14 10:47:24 +00:00
|
|
|
filename: 'layouts.mjs',
|
2022-08-12 17:47:58 +00:00
|
|
|
getContents ({ app }) {
|
2022-03-14 10:47:24 +00:00
|
|
|
const layoutsObject = genObjectFromRawEntries(Object.values(app.layouts).map(({ name, file }) => {
|
2023-07-19 06:55:53 +00:00
|
|
|
return [name, genDynamicImport(file, { interopDefault: true })]
|
2022-03-14 10:47:24 +00:00
|
|
|
}))
|
|
|
|
return [
|
2022-08-17 15:23:13 +00:00
|
|
|
`export default ${layoutsObject}`
|
2022-03-14 10:47:24 +00:00
|
|
|
].join('\n')
|
|
|
|
}
|
|
|
|
}
|
2022-03-22 15:51:26 +00:00
|
|
|
|
2022-06-27 12:10:29 +00:00
|
|
|
// Add middleware template
|
2024-01-12 10:47:23 +00:00
|
|
|
export const middlewareTemplate: NuxtTemplate = {
|
2022-06-27 12:10:29 +00:00
|
|
|
filename: 'middleware.mjs',
|
2022-08-12 17:47:58 +00:00
|
|
|
getContents ({ app }) {
|
2022-06-27 12:10:29 +00:00
|
|
|
const globalMiddleware = app.middleware.filter(mw => mw.global)
|
|
|
|
const namedMiddleware = app.middleware.filter(mw => !mw.global)
|
|
|
|
const namedMiddlewareObject = genObjectFromRawEntries(namedMiddleware.map(mw => [mw.name, genDynamicImport(mw.path)]))
|
|
|
|
return [
|
|
|
|
...globalMiddleware.map(mw => genImport(mw.path, genSafeVariableName(mw.name))),
|
|
|
|
`export const globalMiddleware = ${genArrayFromRaw(globalMiddleware.map(mw => genSafeVariableName(mw.name)))}`,
|
|
|
|
`export const namedMiddleware = ${namedMiddlewareObject}`
|
|
|
|
].join('\n')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-25 16:40:53 +00:00
|
|
|
export const nitroSchemaTemplate: NuxtTemplate = {
|
|
|
|
filename: 'types/nitro-nuxt.d.ts',
|
|
|
|
getContents: () => {
|
|
|
|
return /* typescript */`
|
|
|
|
/// <reference path="./schema.d.ts" />
|
|
|
|
|
|
|
|
import type { RuntimeConfig } from 'nuxt/schema'
|
|
|
|
import type { H3Event } from 'h3'
|
|
|
|
import type { NuxtIslandContext, NuxtIslandResponse, NuxtRenderHTMLContext } from 'nuxt/dist/core/runtime/nitro/renderer'
|
|
|
|
|
|
|
|
declare module 'nitropack' {
|
|
|
|
interface NitroRuntimeConfigApp {
|
|
|
|
buildAssetsDir: string
|
|
|
|
cdnURL: string
|
|
|
|
}
|
|
|
|
interface NitroRuntimeConfig extends RuntimeConfig {}
|
|
|
|
interface NitroRouteConfig {
|
|
|
|
ssr?: boolean
|
|
|
|
experimentalNoScripts?: boolean
|
|
|
|
}
|
|
|
|
interface NitroRouteRules {
|
|
|
|
ssr?: boolean
|
|
|
|
experimentalNoScripts?: boolean
|
|
|
|
}
|
|
|
|
interface NitroRuntimeHooks {
|
|
|
|
'render:html': (htmlContext: NuxtRenderHTMLContext, context: { event: H3Event }) => void | Promise<void>
|
|
|
|
'render:island': (islandResponse: NuxtIslandResponse, context: { event: H3Event, islandContext: NuxtIslandContext }) => void | Promise<void>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:51:26 +00:00
|
|
|
export const clientConfigTemplate: NuxtTemplate = {
|
2022-04-07 11:28:04 +00:00
|
|
|
filename: 'nitro.client.mjs',
|
|
|
|
getContents: () => `
|
|
|
|
export const useRuntimeConfig = () => window?.__NUXT__?.config || {}
|
|
|
|
`
|
2022-03-22 15:51:26 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 15:23:13 +00:00
|
|
|
export const appConfigDeclarationTemplate: NuxtTemplate = {
|
|
|
|
filename: 'types/app.config.d.ts',
|
|
|
|
getContents: ({ app, nuxt }) => {
|
|
|
|
return `
|
2023-03-11 22:53:28 +00:00
|
|
|
import type { CustomAppConfig } from 'nuxt/schema'
|
2022-08-17 15:23:13 +00:00
|
|
|
import type { Defu } from 'defu'
|
|
|
|
${app.configs.map((id: string, index: number) => `import ${`cfg${index}`} from ${JSON.stringify(id.replace(/(?<=\w)\.\w+$/g, ''))}`).join('\n')}
|
|
|
|
|
|
|
|
declare const inlineConfig = ${JSON.stringify(nuxt.options.appConfig, null, 2)}
|
|
|
|
type ResolvedAppConfig = Defu<typeof inlineConfig, [${app.configs.map((_id: string, index: number) => `typeof cfg${index}`).join(', ')}]>
|
2023-03-22 17:08:27 +00:00
|
|
|
type IsAny<T> = 0 extends 1 & T ? true : false
|
2022-08-17 15:23:13 +00:00
|
|
|
|
2023-04-26 13:30:05 +00:00
|
|
|
type MergedAppConfig<Resolved extends Record<string, unknown>, Custom extends Record<string, unknown>> = {
|
|
|
|
[K in keyof (Resolved & Custom)]: K extends keyof Custom
|
|
|
|
? unknown extends Custom[K]
|
2023-04-06 12:33:54 +00:00
|
|
|
? Resolved[K]
|
2023-04-26 13:30:05 +00:00
|
|
|
: IsAny<Custom[K]> extends true
|
|
|
|
? Resolved[K]
|
|
|
|
: Custom[K] extends Record<string, any>
|
|
|
|
? Resolved[K] extends Record<string, any>
|
|
|
|
? MergedAppConfig<Resolved[K], Custom[K]>
|
|
|
|
: Exclude<Custom[K], undefined>
|
|
|
|
: Exclude<Custom[K], undefined>
|
2023-04-06 12:33:54 +00:00
|
|
|
: Resolved[K]
|
2023-03-11 22:53:28 +00:00
|
|
|
}
|
|
|
|
|
2023-02-13 22:42:04 +00:00
|
|
|
declare module 'nuxt/schema' {
|
2023-03-11 22:53:28 +00:00
|
|
|
interface AppConfig extends MergedAppConfig<ResolvedAppConfig, CustomAppConfig> { }
|
|
|
|
}
|
|
|
|
declare module '@nuxt/schema' {
|
|
|
|
interface AppConfig extends MergedAppConfig<ResolvedAppConfig, CustomAppConfig> { }
|
2022-08-17 15:23:13 +00:00
|
|
|
}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const appConfigTemplate: NuxtTemplate = {
|
|
|
|
filename: 'app.config.mjs',
|
|
|
|
write: true,
|
2023-11-01 15:55:34 +00:00
|
|
|
getContents ({ app, nuxt }) {
|
2022-08-17 15:23:13 +00:00
|
|
|
return `
|
2023-10-30 21:05:02 +00:00
|
|
|
import { updateAppConfig } from '#app/config'
|
2023-11-01 15:55:34 +00:00
|
|
|
import { defuFn } from 'defu'
|
2022-08-17 15:23:13 +00:00
|
|
|
|
|
|
|
const inlineConfig = ${JSON.stringify(nuxt.options.appConfig, null, 2)}
|
|
|
|
|
2023-08-07 22:23:31 +00:00
|
|
|
// Vite - webpack is handled directly in #app/config
|
|
|
|
if (import.meta.hot) {
|
|
|
|
import.meta.hot.accept((newModule) => {
|
|
|
|
updateAppConfig(newModule.default)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-17 15:23:13 +00:00
|
|
|
${app.configs.map((id: string, index: number) => `import ${`cfg${index}`} from ${JSON.stringify(id)}`).join('\n')}
|
2022-08-24 16:35:02 +00:00
|
|
|
|
2023-11-28 23:20:54 +00:00
|
|
|
export default /*@__PURE__*/ defuFn(${app.configs.map((_id: string, index: number) => `cfg${index}`).concat(['inlineConfig']).join(', ')})
|
2022-08-17 15:23:13 +00:00
|
|
|
`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const publicPathTemplate: NuxtTemplate = {
|
2022-03-22 15:51:26 +00:00
|
|
|
filename: 'paths.mjs',
|
2023-11-01 15:55:34 +00:00
|
|
|
getContents ({ nuxt }) {
|
2022-03-22 15:51:26 +00:00
|
|
|
return [
|
2023-11-01 15:55:34 +00:00
|
|
|
'import { joinURL } from \'ufo\'',
|
2022-04-19 19:10:32 +00:00
|
|
|
!nuxt.options.dev && 'import { useRuntimeConfig } from \'#internal/nitro\'',
|
2022-03-22 15:51:26 +00:00
|
|
|
|
|
|
|
nuxt.options.dev
|
|
|
|
? `const appConfig = ${JSON.stringify(nuxt.options.app)}`
|
2022-04-07 11:28:04 +00:00
|
|
|
: 'const appConfig = useRuntimeConfig().app',
|
2022-03-22 15:51:26 +00:00
|
|
|
|
|
|
|
'export const baseURL = () => appConfig.baseURL',
|
|
|
|
'export const buildAssetsDir = () => appConfig.buildAssetsDir',
|
|
|
|
|
|
|
|
'export const buildAssetsURL = (...path) => joinURL(publicAssetsURL(), buildAssetsDir(), ...path)',
|
|
|
|
|
|
|
|
'export const publicAssetsURL = (...path) => {',
|
|
|
|
' const publicBase = appConfig.cdnURL || appConfig.baseURL',
|
|
|
|
' return path.length ? joinURL(publicBase, ...path) : publicBase',
|
2022-07-21 10:44:33 +00:00
|
|
|
'}',
|
|
|
|
|
2022-10-18 16:13:50 +00:00
|
|
|
// On server these are registered directly in packages/nuxt/src/core/runtime/nitro/renderer.ts
|
2023-08-07 22:03:40 +00:00
|
|
|
'if (import.meta.client) {',
|
2022-10-18 16:13:50 +00:00
|
|
|
' globalThis.__buildAssetsURL = buildAssetsURL',
|
|
|
|
' globalThis.__publicAssetsURL = publicAssetsURL',
|
|
|
|
'}'
|
2022-03-25 11:38:36 +00:00
|
|
|
].filter(Boolean).join('\n')
|
2022-03-22 15:51:26 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-23 14:24:20 +00:00
|
|
|
|
2023-10-25 00:31:25 +00:00
|
|
|
export const dollarFetchTemplate: NuxtTemplate = {
|
|
|
|
filename: 'fetch.mjs',
|
|
|
|
getContents () {
|
|
|
|
return [
|
2023-11-01 15:55:34 +00:00
|
|
|
'import { $fetch } from \'ofetch\'',
|
2023-10-25 00:31:25 +00:00
|
|
|
"import { baseURL } from '#build/paths.mjs'",
|
|
|
|
'if (!globalThis.$fetch) {',
|
|
|
|
' globalThis.$fetch = $fetch.create({',
|
|
|
|
' baseURL: baseURL()',
|
|
|
|
' })',
|
|
|
|
'}'
|
|
|
|
].join('\n')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-23 14:24:20 +00:00
|
|
|
// Allow direct access to specific exposed nuxt.config
|
2024-01-12 10:47:23 +00:00
|
|
|
export const nuxtConfigTemplate: NuxtTemplate = {
|
2022-08-23 14:24:20 +00:00
|
|
|
filename: 'nuxt.config.mjs',
|
2024-01-12 10:47:23 +00:00
|
|
|
getContents: (ctx) => {
|
2023-10-18 14:25:25 +00:00
|
|
|
const fetchDefaults = {
|
|
|
|
...ctx.nuxt.options.experimental.defaults.useFetch,
|
|
|
|
baseURL: undefined,
|
|
|
|
headers: undefined
|
|
|
|
}
|
2023-04-04 13:18:29 +00:00
|
|
|
return [
|
|
|
|
...Object.entries(ctx.nuxt.options.app).map(([k, v]) => `export const ${camelCase('app-' + k)} = ${JSON.stringify(v)}`),
|
2023-04-07 10:34:35 +00:00
|
|
|
`export const renderJsonPayloads = ${!!ctx.nuxt.options.experimental.renderJsonPayloads}`,
|
2023-06-14 09:09:27 +00:00
|
|
|
`export const componentIslands = ${!!ctx.nuxt.options.experimental.componentIslands}`,
|
2023-09-19 21:31:18 +00:00
|
|
|
`export const payloadExtraction = ${!!ctx.nuxt.options.experimental.payloadExtraction}`,
|
|
|
|
`export const appManifest = ${!!ctx.nuxt.options.experimental.appManifest}`,
|
2023-12-19 12:21:29 +00:00
|
|
|
`export const remoteComponentIslands = ${typeof ctx.nuxt.options.experimental.componentIslands === 'object' && ctx.nuxt.options.experimental.componentIslands.remoteIsland}`,
|
|
|
|
`export const selectiveClient = ${typeof ctx.nuxt.options.experimental.componentIslands === 'object' && ctx.nuxt.options.experimental.componentIslands.selectiveClient}`,
|
2023-04-27 10:31:52 +00:00
|
|
|
`export const devPagesDir = ${ctx.nuxt.options.dev ? JSON.stringify(ctx.nuxt.options.dir.pages) : 'null'}`,
|
2023-08-12 07:19:36 +00:00
|
|
|
`export const devRootDir = ${ctx.nuxt.options.dev ? JSON.stringify(ctx.nuxt.options.rootDir) : 'null'}`,
|
2023-10-18 12:43:42 +00:00
|
|
|
`export const nuxtLinkDefaults = ${JSON.stringify(ctx.nuxt.options.experimental.defaults.nuxtLink)}`,
|
2023-10-18 14:25:25 +00:00
|
|
|
`export const asyncDataDefaults = ${JSON.stringify(ctx.nuxt.options.experimental.defaults.useAsyncData)}`,
|
|
|
|
`export const fetchDefaults = ${JSON.stringify(fetchDefaults)}`,
|
2023-08-12 07:19:36 +00:00
|
|
|
`export const vueAppRootContainer = ${ctx.nuxt.options.app.rootId ? `'#${ctx.nuxt.options.app.rootId}'` : `'body > ${ctx.nuxt.options.app.rootTag}'`}`
|
2023-04-04 13:18:29 +00:00
|
|
|
].join('\n\n')
|
2022-08-23 14:24:20 +00:00
|
|
|
}
|
|
|
|
}
|