2021-09-27 12:49:36 +00:00
|
|
|
import { resolve } from 'pathe'
|
2021-08-27 12:51:40 +00:00
|
|
|
import { createHooks } from 'hookable'
|
2021-11-21 16:14:46 +00:00
|
|
|
import type { Nuxt, NuxtOptions, NuxtConfig, ModuleContainer, NuxtHooks } from '@nuxt/schema'
|
2022-03-11 08:22:16 +00:00
|
|
|
import { loadNuxtConfig, LoadNuxtOptions, nuxtCtx, installModule, addComponent, addVitePlugin, addWebpackPlugin, tryResolveModule } from '@nuxt/kit'
|
2022-02-17 12:01:24 +00:00
|
|
|
// Temporary until finding better placement
|
|
|
|
/* eslint-disable import/no-restricted-paths */
|
2021-08-11 20:28:38 +00:00
|
|
|
import pagesModule from '../pages/module'
|
|
|
|
import metaModule from '../meta/module'
|
|
|
|
import componentsModule from '../components/module'
|
2021-10-11 08:07:27 +00:00
|
|
|
import autoImportsModule from '../auto-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'
|
2021-10-06 17:59:35 +00:00
|
|
|
import { addModuleTranspiles } from './modules'
|
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)),
|
|
|
|
vfs: {}
|
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())
|
|
|
|
|
2021-04-02 11:47:01 +00:00
|
|
|
// Init nitro
|
2022-02-10 17:29:59 +00:00
|
|
|
const { initNitro } = await import(nuxt.options.experimentNitropack ? './nitro-nitropack' : './nitro-legacy')
|
2021-04-02 11:47:01 +00:00
|
|
|
await initNitro(nuxt)
|
2020-07-02 13:02:35 +00:00
|
|
|
|
2021-10-07 10:09:09 +00:00
|
|
|
// Add nuxt3 types
|
|
|
|
nuxt.hook('prepare:types', (opts) => {
|
|
|
|
opts.references.push({ types: 'nuxt3' })
|
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') })
|
2021-10-07 10:09:09 +00:00
|
|
|
})
|
|
|
|
|
2022-01-24 13:25:23 +00:00
|
|
|
// Add import protection
|
|
|
|
|
|
|
|
const config = {
|
|
|
|
rootDir: nuxt.options.rootDir,
|
|
|
|
patterns: vueAppPatterns(nuxt)
|
|
|
|
}
|
|
|
|
addVitePlugin(ImportProtectionPlugin.vite(config))
|
|
|
|
addWebpackPlugin(ImportProtectionPlugin.webpack(config))
|
|
|
|
|
2021-04-02 11:47:01 +00:00
|
|
|
// Init user modules
|
2021-04-15 19:17:44 +00:00
|
|
|
await nuxt.callHook('modules:before', { nuxt } as ModuleContainer)
|
2021-04-02 11:47:01 +00:00
|
|
|
const modulesToInstall = [
|
|
|
|
...nuxt.options.buildModules,
|
|
|
|
...nuxt.options.modules,
|
|
|
|
...nuxt.options._modules
|
|
|
|
]
|
2020-07-02 13:02:35 +00:00
|
|
|
|
2021-11-02 11:27:25 +00:00
|
|
|
// Add <NuxtWelcome>
|
|
|
|
addComponent({
|
|
|
|
name: 'NuxtWelcome',
|
2022-03-11 08:22:16 +00:00
|
|
|
filePath: tryResolveModule('@nuxt/ui-templates/templates/welcome.vue')
|
2021-11-02 11:27:25 +00:00
|
|
|
})
|
|
|
|
|
2022-03-14 10:47:24 +00:00
|
|
|
addComponent({
|
|
|
|
name: 'NuxtLayout',
|
|
|
|
filePath: resolve(nuxt.options.appDir, 'components/layout')
|
|
|
|
})
|
|
|
|
|
2021-11-15 11:57:38 +00:00
|
|
|
// Add <ClientOnly>
|
|
|
|
addComponent({
|
|
|
|
name: 'ClientOnly',
|
|
|
|
filePath: resolve(nuxt.options.appDir, 'components/client-only')
|
|
|
|
})
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-04-15 19:17:44 +00:00
|
|
|
await nuxt.callHook('modules:done', { nuxt } as ModuleContainer)
|
2020-07-02 13:02:35 +00:00
|
|
|
|
2021-10-06 17:59:35 +00:00
|
|
|
await addModuleTranspiles()
|
|
|
|
|
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
|
2022-01-25 16:45:00 +00:00
|
|
|
options._modules.push(pagesModule, metaModule, componentsModule, autoImportsModule)
|
2021-08-11 21:26:47 +00:00
|
|
|
options.modulesDir.push(resolve(pkgDir, 'node_modules'))
|
2022-03-11 08:22:16 +00:00
|
|
|
options.build.transpile.push('@nuxt/ui-templates')
|
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')
|
2021-03-28 21:12:16 +00:00
|
|
|
|
2021-04-02 11:47:01 +00:00
|
|
|
const nuxt = createNuxt(options)
|
|
|
|
|
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
|
|
|
|
}
|
2021-10-06 12:31:52 +00:00
|
|
|
|
|
|
|
export function defineNuxtConfig (config: NuxtConfig): NuxtConfig {
|
|
|
|
return config
|
|
|
|
}
|
2021-10-14 15:22:37 +00:00
|
|
|
|
|
|
|
// For a convenience import together with `defineNuxtConfig`
|
|
|
|
export type { NuxtConfig }
|