2023-03-03 17:52:55 +00:00
|
|
|
import { join, normalize, relative, resolve } from 'pathe'
|
2023-04-07 16:02:47 +00:00
|
|
|
import { createDebugger, createHooks } from 'hookable'
|
2022-12-11 21:44:52 +00:00
|
|
|
import type { LoadNuxtOptions } from '@nuxt/kit'
|
2023-06-29 09:14:35 +00:00
|
|
|
import { addBuildPlugin, addComponent, addPlugin, addVitePlugin, addWebpackPlugin, installModule, loadNuxtConfig, logger, nuxtCtx, resolveAlias, resolveFiles, resolvePath, tryResolveModule, useNitro } from '@nuxt/kit'
|
2023-04-07 16:02:47 +00:00
|
|
|
import type { Nuxt, NuxtHooks, NuxtOptions } from 'nuxt/schema'
|
2023-01-20 12:10:58 +00:00
|
|
|
|
2022-06-10 13:33:16 +00:00
|
|
|
import escapeRE from 'escape-string-regexp'
|
2022-09-05 13:46:47 +00:00
|
|
|
import fse from 'fs-extra'
|
|
|
|
import { withoutLeadingSlash } from 'ufo'
|
2022-11-24 12:24:14 +00:00
|
|
|
/* eslint-disable import/no-restricted-paths */
|
2021-08-11 20:28:38 +00:00
|
|
|
import pagesModule from '../pages/module'
|
2022-04-05 14:02:29 +00:00
|
|
|
import metaModule from '../head/module'
|
2021-08-11 20:28:38 +00:00
|
|
|
import componentsModule from '../components/module'
|
2022-08-23 14:22:11 +00:00
|
|
|
import importsModule from '../imports/module'
|
2022-02-17 12:01:24 +00:00
|
|
|
/* eslint-enable */
|
2021-08-11 21:26:47 +00:00
|
|
|
import { distDir, pkgDir } from '../dirs'
|
2021-10-02 18:31:00 +00:00
|
|
|
import { version } from '../../package.json'
|
2022-01-24 13:25:23 +00:00
|
|
|
import { ImportProtectionPlugin, vueAppPatterns } from './plugins/import-protection'
|
2022-04-01 09:55:23 +00:00
|
|
|
import { UnctxTransformPlugin } from './plugins/unctx'
|
2023-03-07 09:30:05 +00:00
|
|
|
import type { TreeShakeComposablesPluginOptions } from './plugins/tree-shake'
|
|
|
|
import { TreeShakeComposablesPlugin } from './plugins/tree-shake'
|
2022-10-26 12:43:37 +00:00
|
|
|
import { DevOnlyPlugin } from './plugins/dev-only'
|
2023-04-03 13:18:24 +00:00
|
|
|
import { LayerAliasingPlugin } from './plugins/layer-aliasing'
|
2021-10-06 17:59:35 +00:00
|
|
|
import { addModuleTranspiles } from './modules'
|
2022-04-07 11:28:04 +00:00
|
|
|
import { initNitro } from './nitro'
|
2023-01-23 18:07:21 +00:00
|
|
|
import schemaModule from './schema'
|
2023-06-19 23:00:03 +00:00
|
|
|
import { RemovePluginMetadataPlugin } from './plugins/plugin-metadata'
|
2020-07-02 13:02:35 +00:00
|
|
|
|
2021-04-02 11:47:01 +00:00
|
|
|
export function createNuxt (options: NuxtOptions): Nuxt {
|
2021-08-27 12:51:40 +00:00
|
|
|
const hooks = createHooks<NuxtHooks>()
|
2021-04-02 11:47:01 +00:00
|
|
|
|
2021-04-15 19:17:44 +00:00
|
|
|
const nuxt: Nuxt = {
|
2021-10-02 18:31:00 +00:00
|
|
|
_version: version,
|
2021-04-02 11:47:01 +00:00
|
|
|
options,
|
|
|
|
hooks,
|
|
|
|
callHook: hooks.callHook,
|
2021-08-27 12:51:40 +00:00
|
|
|
addHooks: hooks.addHooks,
|
2021-04-15 19:17:44 +00:00
|
|
|
hook: hooks.hook,
|
|
|
|
ready: () => initNuxt(nuxt),
|
2021-07-15 10:18:34 +00:00
|
|
|
close: () => Promise.resolve(hooks.callHook('close', nuxt)),
|
2023-05-09 22:46:03 +00:00
|
|
|
vfs: {},
|
|
|
|
apps: {}
|
2020-08-17 19:12:34 +00:00
|
|
|
}
|
2021-04-15 19:17:44 +00:00
|
|
|
|
|
|
|
return nuxt
|
2020-08-02 15:50:35 +00:00
|
|
|
}
|
|
|
|
|
2021-04-02 11:47:01 +00:00
|
|
|
async function initNuxt (nuxt: Nuxt) {
|
|
|
|
// Register user hooks
|
|
|
|
nuxt.hooks.addHooks(nuxt.options.hooks)
|
2020-07-02 13:02:35 +00:00
|
|
|
|
2021-06-24 14:06:16 +00:00
|
|
|
// Set nuxt instance for useNuxt
|
|
|
|
nuxtCtx.set(nuxt)
|
|
|
|
nuxt.hook('close', () => nuxtCtx.unset())
|
|
|
|
|
2022-04-20 08:52:39 +00:00
|
|
|
// Add nuxt types
|
2021-10-07 10:09:09 +00:00
|
|
|
nuxt.hook('prepare:types', (opts) => {
|
2022-04-20 08:52:39 +00:00
|
|
|
opts.references.push({ types: 'nuxt' })
|
2022-02-07 10:20:01 +00:00
|
|
|
opts.references.push({ path: resolve(nuxt.options.buildDir, 'types/plugins.d.ts') })
|
2022-01-19 18:10:38 +00:00
|
|
|
// Add vue shim
|
|
|
|
if (nuxt.options.typescript.shim) {
|
2022-02-07 10:20:01 +00:00
|
|
|
opts.references.push({ path: resolve(nuxt.options.buildDir, 'types/vue-shim.d.ts') })
|
2022-01-19 18:10:38 +00:00
|
|
|
}
|
2022-02-08 19:09:44 +00:00
|
|
|
// Add module augmentations directly to NuxtConfig
|
|
|
|
opts.references.push({ path: resolve(nuxt.options.buildDir, 'types/schema.d.ts') })
|
2022-08-17 15:23:13 +00:00
|
|
|
opts.references.push({ path: resolve(nuxt.options.buildDir, 'types/app.config.d.ts') })
|
2022-09-12 13:41:15 +00:00
|
|
|
|
|
|
|
for (const layer of nuxt.options._layers) {
|
|
|
|
const declaration = join(layer.cwd, 'index.d.ts')
|
|
|
|
if (fse.existsSync(declaration)) {
|
|
|
|
opts.references.push({ path: declaration })
|
|
|
|
}
|
|
|
|
}
|
2021-10-07 10:09:09 +00:00
|
|
|
})
|
|
|
|
|
2023-06-19 23:00:03 +00:00
|
|
|
// Add plugin normalisation plugin
|
|
|
|
addBuildPlugin(RemovePluginMetadataPlugin(nuxt))
|
|
|
|
|
2022-01-24 13:25:23 +00:00
|
|
|
// Add import protection
|
|
|
|
const config = {
|
|
|
|
rootDir: nuxt.options.rootDir,
|
2022-09-10 11:51:17 +00:00
|
|
|
// Exclude top-level resolutions by plugins
|
|
|
|
exclude: [join(nuxt.options.rootDir, 'index.html')],
|
2022-01-24 13:25:23 +00:00
|
|
|
patterns: vueAppPatterns(nuxt)
|
|
|
|
}
|
2023-05-02 11:17:41 +00:00
|
|
|
addVitePlugin(() => ImportProtectionPlugin.vite(config))
|
|
|
|
addWebpackPlugin(() => ImportProtectionPlugin.webpack(config))
|
2022-01-24 13:25:23 +00:00
|
|
|
|
2023-04-03 13:18:24 +00:00
|
|
|
if (nuxt.options.experimental.localLayerAliases) {
|
|
|
|
// Add layer aliasing support for ~, ~~, @ and @@ aliases
|
2023-05-02 11:17:41 +00:00
|
|
|
addVitePlugin(() => LayerAliasingPlugin.vite({
|
2023-04-03 13:18:24 +00:00
|
|
|
sourcemap: nuxt.options.sourcemap.server || nuxt.options.sourcemap.client,
|
2023-05-18 14:10:12 +00:00
|
|
|
dev: nuxt.options.dev,
|
|
|
|
root: nuxt.options.srcDir,
|
2023-04-03 13:18:24 +00:00
|
|
|
// skip top-level layer (user's project) as the aliases will already be correctly resolved
|
|
|
|
layers: nuxt.options._layers.slice(1)
|
|
|
|
}))
|
2023-05-02 11:17:41 +00:00
|
|
|
addWebpackPlugin(() => LayerAliasingPlugin.webpack({
|
2023-04-03 13:18:24 +00:00
|
|
|
sourcemap: nuxt.options.sourcemap.server || nuxt.options.sourcemap.client,
|
2023-05-18 14:10:12 +00:00
|
|
|
dev: nuxt.options.dev,
|
|
|
|
root: nuxt.options.srcDir,
|
2023-04-03 13:18:24 +00:00
|
|
|
// skip top-level layer (user's project) as the aliases will already be correctly resolved
|
|
|
|
layers: nuxt.options._layers.slice(1),
|
|
|
|
transform: true
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2023-01-25 12:52:00 +00:00
|
|
|
nuxt.hook('modules:done', () => {
|
2023-03-07 09:30:05 +00:00
|
|
|
// Add unctx transform
|
2023-04-10 21:57:13 +00:00
|
|
|
const options = {
|
|
|
|
sourcemap: nuxt.options.sourcemap.server || nuxt.options.sourcemap.client,
|
|
|
|
transformerOptions: nuxt.options.optimization.asyncTransforms
|
|
|
|
}
|
2023-05-02 11:17:41 +00:00
|
|
|
addVitePlugin(() => UnctxTransformPlugin.vite(options))
|
|
|
|
addWebpackPlugin(() => UnctxTransformPlugin.webpack(options))
|
2023-03-07 09:30:05 +00:00
|
|
|
|
|
|
|
// Add composable tree-shaking optimisations
|
2023-04-03 13:18:24 +00:00
|
|
|
const serverTreeShakeOptions: TreeShakeComposablesPluginOptions = {
|
2023-03-07 09:30:05 +00:00
|
|
|
sourcemap: nuxt.options.sourcemap.server,
|
|
|
|
composables: nuxt.options.optimization.treeShake.composables.server
|
|
|
|
}
|
|
|
|
if (Object.keys(serverTreeShakeOptions.composables).length) {
|
2023-05-02 11:17:41 +00:00
|
|
|
addVitePlugin(() => TreeShakeComposablesPlugin.vite(serverTreeShakeOptions), { client: false })
|
|
|
|
addWebpackPlugin(() => TreeShakeComposablesPlugin.webpack(serverTreeShakeOptions), { client: false })
|
2023-03-07 09:30:05 +00:00
|
|
|
}
|
2023-04-03 13:18:24 +00:00
|
|
|
const clientTreeShakeOptions: TreeShakeComposablesPluginOptions = {
|
2023-03-07 09:30:05 +00:00
|
|
|
sourcemap: nuxt.options.sourcemap.client,
|
|
|
|
composables: nuxt.options.optimization.treeShake.composables.client
|
|
|
|
}
|
|
|
|
if (Object.keys(clientTreeShakeOptions.composables).length) {
|
2023-05-02 11:17:41 +00:00
|
|
|
addVitePlugin(() => TreeShakeComposablesPlugin.vite(clientTreeShakeOptions), { server: false })
|
|
|
|
addWebpackPlugin(() => TreeShakeComposablesPlugin.webpack(clientTreeShakeOptions), { server: false })
|
2023-03-07 09:30:05 +00:00
|
|
|
}
|
2023-01-25 12:52:00 +00:00
|
|
|
})
|
2022-04-01 09:55:23 +00:00
|
|
|
|
2022-07-07 16:04:38 +00:00
|
|
|
if (!nuxt.options.dev) {
|
2022-10-26 12:43:37 +00:00
|
|
|
// DevOnly component tree-shaking - build time only
|
2023-05-02 11:17:41 +00:00
|
|
|
addVitePlugin(() => DevOnlyPlugin.vite({ sourcemap: nuxt.options.sourcemap.server || nuxt.options.sourcemap.client }))
|
|
|
|
addWebpackPlugin(() => DevOnlyPlugin.webpack({ sourcemap: nuxt.options.sourcemap.server || nuxt.options.sourcemap.client }))
|
2022-07-07 16:04:38 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 13:46:47 +00:00
|
|
|
// TODO: [Experimental] Avoid emitting assets when flag is enabled
|
2022-09-13 21:57:58 +00:00
|
|
|
if (nuxt.options.experimental.noScripts && !nuxt.options.dev) {
|
2022-09-05 13:46:47 +00:00
|
|
|
nuxt.hook('build:manifest', async (manifest) => {
|
|
|
|
for (const file in manifest) {
|
|
|
|
if (manifest[file].resourceType === 'script') {
|
|
|
|
await fse.rm(resolve(nuxt.options.buildDir, 'dist/client', withoutLeadingSlash(nuxt.options.app.buildAssetsDir), manifest[file].file), { force: true })
|
|
|
|
manifest[file].file = ''
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-02-14 00:23:12 +00:00
|
|
|
// Transpile #app if it is imported directly from subpath export
|
|
|
|
nuxt.options.build.transpile.push('nuxt/app')
|
|
|
|
|
2022-06-10 13:33:16 +00:00
|
|
|
// Transpile layers within node_modules
|
|
|
|
nuxt.options.build.transpile.push(
|
2022-08-12 17:47:58 +00:00
|
|
|
...nuxt.options._layers.filter(i => i.cwd.includes('node_modules')).map(i => i.cwd as string)
|
2022-06-10 13:33:16 +00:00
|
|
|
)
|
|
|
|
|
2021-04-02 11:47:01 +00:00
|
|
|
// Init user modules
|
2022-10-27 10:36:37 +00:00
|
|
|
await nuxt.callHook('modules:before')
|
2023-03-03 17:52:55 +00:00
|
|
|
const modulesToInstall = []
|
|
|
|
|
|
|
|
const watchedPaths = new Set<string>()
|
|
|
|
const specifiedModules = new Set<string>()
|
|
|
|
|
|
|
|
for (const _mod of nuxt.options.modules) {
|
|
|
|
const mod = Array.isArray(_mod) ? _mod[0] : _mod
|
|
|
|
if (typeof mod !== 'string') { continue }
|
|
|
|
const modPath = await resolvePath(resolveAlias(mod))
|
|
|
|
specifiedModules.add(modPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Automatically register user modules
|
|
|
|
for (const config of nuxt.options._layers.map(layer => layer.config).reverse()) {
|
|
|
|
const layerModules = await resolveFiles(config.srcDir, [
|
|
|
|
`${config.dir?.modules || 'modules'}/*{${nuxt.options.extensions.join(',')}}`,
|
|
|
|
`${config.dir?.modules || 'modules'}/*/index{${nuxt.options.extensions.join(',')}}`
|
|
|
|
])
|
|
|
|
for (const mod of layerModules) {
|
|
|
|
watchedPaths.add(relative(config.srcDir, mod))
|
|
|
|
if (specifiedModules.has(mod)) { continue }
|
|
|
|
specifiedModules.add(mod)
|
|
|
|
modulesToInstall.push(mod)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register user and then ad-hoc modules
|
|
|
|
modulesToInstall.push(...nuxt.options.modules, ...nuxt.options._modules)
|
|
|
|
|
2021-11-02 11:27:25 +00:00
|
|
|
// Add <NuxtWelcome>
|
|
|
|
addComponent({
|
|
|
|
name: 'NuxtWelcome',
|
2023-03-06 11:33:40 +00:00
|
|
|
priority: 10, // built-in that we do not expect the user to override
|
2023-05-27 16:24:21 +00:00
|
|
|
filePath: (await tryResolveModule('@nuxt/ui-templates/templates/welcome.vue', nuxt.options.modulesDir))!
|
2021-11-02 11:27:25 +00:00
|
|
|
})
|
|
|
|
|
2022-03-14 10:47:24 +00:00
|
|
|
addComponent({
|
|
|
|
name: 'NuxtLayout',
|
2023-03-06 11:33:40 +00:00
|
|
|
priority: 10, // built-in that we do not expect the user to override
|
2022-03-14 10:47:24 +00:00
|
|
|
filePath: resolve(nuxt.options.appDir, 'components/layout')
|
|
|
|
})
|
|
|
|
|
2022-03-16 15:49:53 +00:00
|
|
|
// Add <NuxtErrorBoundary>
|
|
|
|
addComponent({
|
|
|
|
name: 'NuxtErrorBoundary',
|
2023-03-06 11:33:40 +00:00
|
|
|
priority: 10, // built-in that we do not expect the user to override
|
2022-03-16 15:49:53 +00:00
|
|
|
filePath: resolve(nuxt.options.appDir, 'components/nuxt-error-boundary')
|
|
|
|
})
|
|
|
|
|
2021-11-15 11:57:38 +00:00
|
|
|
// Add <ClientOnly>
|
|
|
|
addComponent({
|
|
|
|
name: 'ClientOnly',
|
2023-03-06 11:33:40 +00:00
|
|
|
priority: 10, // built-in that we do not expect the user to override
|
2021-11-15 11:57:38 +00:00
|
|
|
filePath: resolve(nuxt.options.appDir, 'components/client-only')
|
|
|
|
})
|
|
|
|
|
2022-10-26 12:43:37 +00:00
|
|
|
// Add <DevOnly>
|
|
|
|
addComponent({
|
|
|
|
name: 'DevOnly',
|
2023-03-06 11:33:40 +00:00
|
|
|
priority: 10, // built-in that we do not expect the user to override
|
2022-10-26 12:43:37 +00:00
|
|
|
filePath: resolve(nuxt.options.appDir, 'components/dev-only')
|
|
|
|
})
|
|
|
|
|
2022-04-19 19:13:55 +00:00
|
|
|
// Add <ServerPlaceholder>
|
|
|
|
addComponent({
|
|
|
|
name: 'ServerPlaceholder',
|
2023-03-06 11:33:40 +00:00
|
|
|
priority: 10, // built-in that we do not expect the user to override
|
2022-04-19 19:13:55 +00:00
|
|
|
filePath: resolve(nuxt.options.appDir, 'components/server-placeholder')
|
|
|
|
})
|
|
|
|
|
2022-03-14 13:36:32 +00:00
|
|
|
// Add <NuxtLink>
|
|
|
|
addComponent({
|
|
|
|
name: 'NuxtLink',
|
2023-03-06 11:33:40 +00:00
|
|
|
priority: 10, // built-in that we do not expect the user to override
|
2022-03-14 13:36:32 +00:00
|
|
|
filePath: resolve(nuxt.options.appDir, 'components/nuxt-link')
|
|
|
|
})
|
|
|
|
|
2022-07-07 17:59:55 +00:00
|
|
|
// Add <NuxtLoadingIndicator>
|
|
|
|
addComponent({
|
|
|
|
name: 'NuxtLoadingIndicator',
|
2023-03-06 11:33:40 +00:00
|
|
|
priority: 10, // built-in that we do not expect the user to override
|
2022-07-07 17:59:55 +00:00
|
|
|
filePath: resolve(nuxt.options.appDir, 'components/nuxt-loading-indicator')
|
|
|
|
})
|
|
|
|
|
2023-03-08 21:13:06 +00:00
|
|
|
// Add <NuxtClientFallback>
|
|
|
|
if (nuxt.options.experimental.clientFallback) {
|
|
|
|
addComponent({
|
|
|
|
name: 'NuxtClientFallback',
|
|
|
|
priority: 10, // built-in that we do not expect the user to override
|
|
|
|
filePath: resolve(nuxt.options.appDir, 'components/client-fallback.client'),
|
|
|
|
mode: 'client'
|
|
|
|
})
|
|
|
|
|
|
|
|
addComponent({
|
|
|
|
name: 'NuxtClientFallback',
|
|
|
|
priority: 10, // built-in that we do not expect the user to override
|
|
|
|
filePath: resolve(nuxt.options.appDir, 'components/client-fallback.server'),
|
|
|
|
mode: 'server'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-11-24 12:24:14 +00:00
|
|
|
// Add <NuxtIsland>
|
|
|
|
if (nuxt.options.experimental.componentIslands) {
|
|
|
|
addComponent({
|
|
|
|
name: 'NuxtIsland',
|
2023-03-06 11:33:40 +00:00
|
|
|
priority: 10, // built-in that we do not expect the user to override
|
2022-11-24 12:24:14 +00:00
|
|
|
filePath: resolve(nuxt.options.appDir, 'components/nuxt-island')
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-17 11:15:29 +00:00
|
|
|
// Add experimental cross-origin prefetch support using Speculation Rules API
|
|
|
|
if (nuxt.options.experimental.crossOriginPrefetch) {
|
|
|
|
addPlugin(resolve(nuxt.options.appDir, 'plugins/cross-origin-prefetch.client'))
|
|
|
|
}
|
|
|
|
|
2023-02-16 12:43:58 +00:00
|
|
|
// Add experimental page reload support
|
2023-03-08 12:17:22 +00:00
|
|
|
if (nuxt.options.experimental.emitRouteChunkError === 'automatic') {
|
2023-02-16 12:43:58 +00:00
|
|
|
addPlugin(resolve(nuxt.options.appDir, 'plugins/chunk-reload.client'))
|
|
|
|
}
|
2023-03-08 12:17:22 +00:00
|
|
|
// Add experimental session restoration support
|
|
|
|
if (nuxt.options.experimental.restoreState) {
|
|
|
|
addPlugin(resolve(nuxt.options.appDir, 'plugins/restore-state.client'))
|
|
|
|
}
|
2023-02-16 12:43:58 +00:00
|
|
|
|
2023-04-10 11:33:14 +00:00
|
|
|
// Add experimental automatic view transition api support
|
|
|
|
if (nuxt.options.experimental.viewTransition) {
|
|
|
|
addPlugin(resolve(nuxt.options.appDir, 'plugins/view-transitions.client'))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add experimental support for custom types in JSON payload
|
2023-04-07 10:34:35 +00:00
|
|
|
if (nuxt.options.experimental.renderJsonPayloads) {
|
2023-04-11 11:58:43 +00:00
|
|
|
nuxt.hooks.hook('modules:done', () => {
|
|
|
|
addPlugin(resolve(nuxt.options.appDir, 'plugins/revive-payload.client'))
|
|
|
|
addPlugin(resolve(nuxt.options.appDir, 'plugins/revive-payload.server'))
|
2023-04-07 10:34:35 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-13 10:57:14 +00:00
|
|
|
// Track components used to render for webpack
|
|
|
|
if (nuxt.options.builder === '@nuxt/webpack-builder') {
|
|
|
|
addPlugin(resolve(nuxt.options.appDir, 'plugins/preload.server'))
|
|
|
|
}
|
|
|
|
|
2023-05-15 16:17:39 +00:00
|
|
|
const envMap = {
|
|
|
|
// defaults from `builder` based on package name
|
|
|
|
'@nuxt/vite-builder': 'vite/client',
|
|
|
|
'@nuxt/webpack-builder': 'webpack/module',
|
|
|
|
// simpler overrides from `typescript.builder` for better DX
|
|
|
|
vite: 'vite/client',
|
|
|
|
webpack: 'webpack/module',
|
|
|
|
// default 'merged' builder environment for module authors
|
|
|
|
shared: '@nuxt/schema/builder-env'
|
|
|
|
}
|
|
|
|
|
|
|
|
nuxt.hook('prepare:types', ({ references }) => {
|
|
|
|
// Disable entirely if `typescript.builder` is false
|
|
|
|
if (nuxt.options.typescript.builder === false) { return }
|
|
|
|
|
|
|
|
const overrideEnv = nuxt.options.typescript.builder && envMap[nuxt.options.typescript.builder]
|
|
|
|
// If there's no override, infer based on builder. If a custom builder is provided, we disable shared types
|
|
|
|
const defaultEnv = typeof nuxt.options.builder === 'string' ? envMap[nuxt.options.builder] : false
|
|
|
|
const types = overrideEnv || defaultEnv
|
|
|
|
|
|
|
|
if (types) { references.push({ types }) }
|
|
|
|
})
|
|
|
|
|
2022-10-15 10:56:15 +00:00
|
|
|
// Add nuxt app debugger
|
|
|
|
if (nuxt.options.debug) {
|
|
|
|
addPlugin(resolve(nuxt.options.appDir, 'plugins/debug'))
|
|
|
|
}
|
|
|
|
|
2021-04-02 11:47:01 +00:00
|
|
|
for (const m of modulesToInstall) {
|
2021-12-21 21:26:03 +00:00
|
|
|
if (Array.isArray(m)) {
|
2022-02-08 19:09:44 +00:00
|
|
|
await installModule(m[0], m[1])
|
2021-12-21 21:26:03 +00:00
|
|
|
} else {
|
2022-02-08 19:09:44 +00:00
|
|
|
await installModule(m, {})
|
2021-12-21 21:26:03 +00:00
|
|
|
}
|
2020-07-02 13:02:35 +00:00
|
|
|
}
|
|
|
|
|
2022-10-27 10:36:37 +00:00
|
|
|
await nuxt.callHook('modules:done')
|
2020-07-02 13:02:35 +00:00
|
|
|
|
2023-03-09 11:46:08 +00:00
|
|
|
nuxt.hooks.hook('builder:watch', (event, path) => {
|
|
|
|
// Local module patterns
|
|
|
|
if (watchedPaths.has(path)) {
|
|
|
|
return nuxt.callHook('restart', { hard: true })
|
|
|
|
}
|
|
|
|
|
|
|
|
// User provided patterns
|
|
|
|
for (const pattern of nuxt.options.watch) {
|
|
|
|
if (typeof pattern === 'string') {
|
|
|
|
if (pattern === path) { return nuxt.callHook('restart') }
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if (pattern.test(path)) { return nuxt.callHook('restart') }
|
|
|
|
}
|
|
|
|
|
|
|
|
// Core Nuxt files: app.vue, error.vue and app.config.ts
|
|
|
|
const isFileChange = ['add', 'unlink'].includes(event)
|
2023-05-22 20:25:42 +00:00
|
|
|
if (isFileChange && RESTART_RE.test(path)) {
|
2023-03-09 11:46:08 +00:00
|
|
|
console.info(`\`${path}\` ${event === 'add' ? 'created' : 'removed'}`)
|
|
|
|
return nuxt.callHook('restart')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-06-15 11:52:46 +00:00
|
|
|
// Normalize windows transpile paths added by modules
|
|
|
|
nuxt.options.build.transpile = nuxt.options.build.transpile.map(t => typeof t === 'string' ? normalize(t) : t)
|
|
|
|
|
2022-06-08 19:49:11 +00:00
|
|
|
addModuleTranspiles()
|
2021-10-06 17:59:35 +00:00
|
|
|
|
2022-04-07 11:28:04 +00:00
|
|
|
// Init nitro
|
|
|
|
await initNitro(nuxt)
|
|
|
|
|
2023-06-29 09:14:35 +00:00
|
|
|
// TODO: remove when app manifest support is landed in https://github.com/nuxt/nuxt/pull/21641
|
|
|
|
// Add prerender payload support
|
|
|
|
if (useNitro().options.static && nuxt.options.experimental.payloadExtraction === undefined) {
|
|
|
|
console.warn('Using experimental payload extraction for full-static output. You can opt-out by setting `experimental.payloadExtraction` to `false`.')
|
|
|
|
nuxt.options.experimental.payloadExtraction = true
|
|
|
|
}
|
|
|
|
if (!nuxt.options.dev && nuxt.options.experimental.payloadExtraction) {
|
|
|
|
addPlugin(resolve(nuxt.options.appDir, 'plugins/payload.client'))
|
|
|
|
}
|
|
|
|
|
2021-04-02 11:47:01 +00:00
|
|
|
await nuxt.callHook('ready', nuxt)
|
|
|
|
}
|
2020-07-02 13:02:35 +00:00
|
|
|
|
2021-04-15 19:17:44 +00:00
|
|
|
export async function loadNuxt (opts: LoadNuxtOptions): Promise<Nuxt> {
|
2021-10-18 09:03:39 +00:00
|
|
|
const options = await loadNuxtConfig(opts)
|
2021-03-28 21:12:16 +00:00
|
|
|
|
2021-11-02 11:27:25 +00:00
|
|
|
// Temporary until finding better placement for each
|
2021-08-11 20:28:38 +00:00
|
|
|
options.appDir = options.alias['#app'] = resolve(distDir, 'app')
|
2021-03-28 21:12:16 +00:00
|
|
|
options._majorVersion = 3
|
2023-04-30 09:06:43 +00:00
|
|
|
|
|
|
|
// Nuxt DevTools is currently opt-in
|
|
|
|
if (options.devtools === true || (options.devtools && options.devtools.enabled !== false)) {
|
|
|
|
if (await import('./features').then(r => r.ensurePackageInstalled(options.rootDir, '@nuxt/devtools', options.modulesDir))) {
|
|
|
|
options._modules.push('@nuxt/devtools')
|
|
|
|
} else {
|
|
|
|
logger.warn('Failed to install `@nuxt/devtools`, please install it manually, or disable `devtools` in `nuxt.config`')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add core modules
|
2022-06-10 13:33:16 +00:00
|
|
|
options._modules.push(pagesModule, metaModule, componentsModule)
|
2022-08-23 14:22:11 +00:00
|
|
|
options._modules.push([importsModule, {
|
2022-06-10 13:33:16 +00:00
|
|
|
transform: {
|
|
|
|
include: options._layers
|
2022-06-12 20:12:43 +00:00
|
|
|
.filter(i => i.cwd && i.cwd.includes('node_modules'))
|
2022-08-12 17:47:58 +00:00
|
|
|
.map(i => new RegExp(`(^|\\/)${escapeRE(i.cwd!.split('node_modules/').pop()!)}(\\/|$)(?!node_modules\\/)`))
|
2022-06-10 13:33:16 +00:00
|
|
|
}
|
|
|
|
}])
|
2023-01-23 18:07:21 +00:00
|
|
|
options._modules.push(schemaModule)
|
2022-09-12 20:06:17 +00:00
|
|
|
options.modulesDir.push(resolve(options.workspaceDir, 'node_modules'))
|
2021-08-11 21:26:47 +00:00
|
|
|
options.modulesDir.push(resolve(pkgDir, 'node_modules'))
|
2023-06-05 18:23:38 +00:00
|
|
|
options.build.transpile.push(
|
|
|
|
'@nuxt/ui-templates', // this exposes vue SFCs
|
|
|
|
'std-env' // we need to statically replace process.env when used in runtime code
|
|
|
|
)
|
2021-11-15 10:25:50 +00:00
|
|
|
options.alias['vue-demi'] = resolve(options.appDir, 'compat/vue-demi')
|
|
|
|
options.alias['@vue/composition-api'] = resolve(options.appDir, 'compat/capi')
|
2022-04-20 18:25:09 +00:00
|
|
|
if (options.telemetry !== false && !process.env.NUXT_TELEMETRY_DISABLED) {
|
|
|
|
options._modules.push('@nuxt/telemetry')
|
|
|
|
}
|
2021-03-28 21:12:16 +00:00
|
|
|
|
2021-04-02 11:47:01 +00:00
|
|
|
const nuxt = createNuxt(options)
|
|
|
|
|
2022-10-15 10:56:15 +00:00
|
|
|
if (nuxt.options.debug) {
|
|
|
|
createDebugger(nuxt.hooks, { tag: 'nuxt' })
|
|
|
|
}
|
|
|
|
|
2021-04-15 19:17:44 +00:00
|
|
|
if (opts.ready !== false) {
|
|
|
|
await nuxt.ready()
|
|
|
|
}
|
2021-04-02 11:47:01 +00:00
|
|
|
|
2021-03-28 21:12:16 +00:00
|
|
|
return nuxt
|
|
|
|
}
|
2023-05-22 20:25:42 +00:00
|
|
|
|
|
|
|
const RESTART_RE = /^(app|error|app\.config)\.(js|ts|mjs|jsx|tsx|vue)$/i
|