mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-25 15:15:19 +00:00
improve config extending
This commit is contained in:
parent
91caf2c470
commit
3fa6717882
@ -18,7 +18,7 @@ export async function build (options: SLSOptions) {
|
||||
hooks.addHooks(options.hooks)
|
||||
|
||||
// Compile html template
|
||||
const htmlSrc = resolve(options.buildDir, `views/${{ 2: 'app', 3: 'document' }[options.nuxt]}.template.html`)
|
||||
const htmlSrc = resolve(options.buildDir, `views/${{ 2: 'app', 3: 'document' }[2]}.template.html`)
|
||||
const htmlTemplate = { src: htmlSrc, contents: '', dst: '', compiled: '' }
|
||||
htmlTemplate.dst = htmlTemplate.src.replace(/.html$/, '.js').replace('app.', 'document.')
|
||||
htmlTemplate.contents = await readFile(htmlTemplate.src, 'utf-8')
|
||||
|
@ -1,86 +1,87 @@
|
||||
import { resolve } from 'path'
|
||||
import defu from 'defu'
|
||||
import type { NuxtOptions } from '@nuxt/types'
|
||||
import { tryImport, resolvePath, detectTarget } from './utils'
|
||||
import Hookable, { configHooksT } from 'hookable'
|
||||
import { tryImport, resolvePath, detectTarget, extendTarget } from './utils'
|
||||
import * as TARGETS from './targets'
|
||||
|
||||
export type UnresolvedPath = string | ((SLSOptions) => string)
|
||||
|
||||
export interface Nuxt extends Hookable{
|
||||
options: NuxtOptions
|
||||
}
|
||||
|
||||
export interface SLSOptions {
|
||||
nuxtOptions: NuxtOptions
|
||||
node: false
|
||||
target: string
|
||||
entry: UnresolvedPath
|
||||
slsDir: string
|
||||
outName: string
|
||||
logStartup: boolean
|
||||
inlineChunks: boolean
|
||||
hooks: configHooksT
|
||||
nuxtHooks: configHooksT
|
||||
|
||||
rootDir: string
|
||||
buildDir: string
|
||||
publicDir: string
|
||||
staticDir: string
|
||||
targetDir: string
|
||||
rootDir: string
|
||||
runtimeDir: string
|
||||
static: string[]
|
||||
renderer: string
|
||||
nuxt: 2 | 3
|
||||
analyze: boolean
|
||||
routerBase: string
|
||||
fullStatic: boolean
|
||||
staticAssets: any
|
||||
|
||||
entry: UnresolvedPath
|
||||
outName: string
|
||||
node: false
|
||||
target: string
|
||||
minify: boolean
|
||||
rollupConfig?: any
|
||||
fullStatic: boolean,
|
||||
logStartup: boolean
|
||||
inlineChunks: boolean
|
||||
renderer: string
|
||||
analyze: boolean
|
||||
|
||||
runtimeDir: string
|
||||
slsDir: string
|
||||
targetDir: string
|
||||
|
||||
static: string[]
|
||||
generateIgnore: string[]
|
||||
staticAssets: {
|
||||
base: string
|
||||
versionBase: string
|
||||
dir: string
|
||||
version: string
|
||||
}
|
||||
hooks: { [key: string]: any } // TODO: export from hookable
|
||||
nuxtHooks: { [key: string]: Function } // NuxtOptions['hooks']
|
||||
}
|
||||
|
||||
export interface SLSConfig extends Omit<Partial<SLSOptions>, 'targetDir'> {
|
||||
targetDir: UnresolvedPath
|
||||
targetDir?: UnresolvedPath
|
||||
}
|
||||
|
||||
export type SLSTarget = Partial<SLSConfig> | ((NuxtOptions) => Partial<SLSConfig>)
|
||||
export type SLSTargetFn = (SLSConfig) => SLSConfig
|
||||
export type SLSTarget = SLSConfig | SLSTargetFn
|
||||
|
||||
export function getoptions (nuxtOptions: NuxtOptions): SLSOptions {
|
||||
export function getoptions (nuxt: Nuxt): SLSOptions {
|
||||
const defaults: SLSConfig = {
|
||||
rootDir: nuxtOptions.rootDir,
|
||||
buildDir: nuxtOptions.buildDir,
|
||||
publicDir: nuxtOptions.generate.dir,
|
||||
fullStatic: nuxtOptions.target === 'static' && !nuxtOptions._legacyGenerate,
|
||||
rootDir: nuxt.options.rootDir,
|
||||
buildDir: nuxt.options.buildDir,
|
||||
publicDir: nuxt.options.generate.dir,
|
||||
routerBase: nuxt.options.router.base,
|
||||
fullStatic: nuxt.options.target === 'static' && !nuxt.options._legacyGenerate,
|
||||
// @ts-ignore
|
||||
staticAssets: nuxtOptions.generate.staticAssets,
|
||||
staticAssets: nuxt.options.generate.staticAssets,
|
||||
|
||||
outName: '_nuxt.js',
|
||||
runtimeDir: resolve(__dirname, '../runtime'),
|
||||
static: [],
|
||||
generateIgnore: [],
|
||||
nuxt: 2,
|
||||
logStartup: true,
|
||||
inlineChunks: true,
|
||||
targetDir: null
|
||||
|
||||
runtimeDir: resolve(__dirname, '../runtime'),
|
||||
slsDir: null,
|
||||
targetDir: null,
|
||||
|
||||
static: [],
|
||||
generateIgnore: []
|
||||
}
|
||||
|
||||
let target = process.env.NUXT_SLS_TARGET || nuxtOptions.serverless?.target || detectTarget()
|
||||
if (typeof target === 'function') {
|
||||
target = target(nuxtOptions)
|
||||
}
|
||||
|
||||
let targetDefaults = TARGETS[target] || tryImport(nuxtOptions.rootDir, target)
|
||||
const target = process.env.NUXT_SLS_TARGET || nuxt.options.serverless?.target || detectTarget()
|
||||
let targetDefaults = TARGETS[target] || tryImport(nuxt.options.rootDir, target)
|
||||
if (!targetDefaults) {
|
||||
throw new Error('Cannot resolve target: ' + target)
|
||||
}
|
||||
targetDefaults = targetDefaults.default || targetDefaults
|
||||
if (typeof targetDefaults === 'function') {
|
||||
targetDefaults = targetDefaults(nuxtOptions)
|
||||
}
|
||||
|
||||
const options: SLSOptions = defu(nuxtOptions.serverless, targetDefaults, defaults, { target })
|
||||
const _defaults = defu(defaults, { target })
|
||||
const _targetInput = defu(nuxt.options.serverless, _defaults)
|
||||
const _target = extendTarget(nuxt.options.serverless, targetDefaults)(_targetInput)
|
||||
const options: SLSOptions = defu(nuxt.options.serverless, _target, _defaults)
|
||||
|
||||
options.buildDir = resolve(options.rootDir, options.buildDir || '.nuxt')
|
||||
options.publicDir = resolve(options.rootDir, options.publicDir || 'dist')
|
||||
options.slsDir = resolve(options.rootDir, options.slsDir || '.sls')
|
||||
options.targetDir = options.targetDir ? resolvePath(options, options.targetDir) : resolve(options.slsDir, target)
|
||||
|
||||
|
@ -10,7 +10,7 @@ export default <Module> function slsModule () {
|
||||
}
|
||||
|
||||
// Config
|
||||
const options = getoptions(nuxt.options)
|
||||
const options = getoptions(nuxt)
|
||||
|
||||
if (options.minify !== false) {
|
||||
nuxt.options.build._minifyServer = true
|
||||
|
@ -93,7 +93,7 @@ export const getRollupConfig = (config: SLSOptions) => {
|
||||
}))
|
||||
|
||||
// https://github.com/rollup/plugins/tree/master/packages/alias
|
||||
const renderer = config.renderer || (config.nuxt === 2 ? 'vue2' : 'vue3')
|
||||
const renderer = config.renderer || 'vue2'
|
||||
options.plugins.push(alias({
|
||||
entries: {
|
||||
'~runtime': config.runtimeDir,
|
||||
|
@ -5,27 +5,28 @@ import { extendTarget } from '../utils'
|
||||
import { SLSTarget } from '../config'
|
||||
import { worker } from './worker'
|
||||
|
||||
const getScriptTag = () => `<script>
|
||||
export const browser: SLSTarget = extendTarget(worker, (options) => {
|
||||
const script = `<script>
|
||||
if ('serviceWorker' in navigator) {
|
||||
window.addEventListener('load', function () {
|
||||
navigator.serviceWorker.register('/server.js');
|
||||
navigator.serviceWorker.register('${options.routerBase}_nuxt.js');
|
||||
});
|
||||
}
|
||||
</script>`.replace(/\n| +/g, '')
|
||||
|
||||
export const browser: SLSTarget = extendTarget(worker, {
|
||||
targetDir: ({ publicDir }) => publicDir,
|
||||
return {
|
||||
targetDir: '{{ publicDir }}',
|
||||
nuxtHooks: {
|
||||
'vue-renderer:ssr:templateParams' (params) {
|
||||
params.APP += getScriptTag()
|
||||
params.APP += script
|
||||
},
|
||||
'vue-renderer:spa:templateParams' (params) {
|
||||
params.APP += getScriptTag()
|
||||
params.APP += script
|
||||
}
|
||||
},
|
||||
hooks: {
|
||||
'template:document' (tmpl) {
|
||||
tmpl.compiled = tmpl.compiled.replace('</body>', getScriptTag() + '</body>')
|
||||
tmpl.compiled = tmpl.compiled.replace('</body>', script + '</body>')
|
||||
},
|
||||
async done ({ targetDir, publicDir }) {
|
||||
const rootIndex = resolve(publicDir, 'index.html')
|
||||
@ -37,4 +38,5 @@ export const browser: SLSTarget = extendTarget(worker, {
|
||||
consola.info(`Try with \`npx serve ${relative(process.cwd(), targetDir)}\``)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
@ -3,8 +3,7 @@ import { writeFile, mkdirp } from 'fs-extra'
|
||||
import jiti from 'jiti'
|
||||
import defu from 'defu'
|
||||
import Hookable from 'hookable'
|
||||
import type { NuxtOptions } from '@nuxt/types'
|
||||
import { SLSOptions, UnresolvedPath, SLSTarget } from './config'
|
||||
import { SLSOptions, UnresolvedPath, SLSTarget, SLSTargetFn, SLSConfig } from './config'
|
||||
|
||||
export function hl (str: string) {
|
||||
return '`' + str + '`'
|
||||
@ -44,6 +43,10 @@ export function resolvePath (options: SLSOptions, path: UnresolvedPath, resolveB
|
||||
path = path(options)
|
||||
}
|
||||
|
||||
if (typeof path !== 'string') {
|
||||
throw new TypeError('Invalid path: ' + path)
|
||||
}
|
||||
|
||||
path = compileTemplate(path)(options)
|
||||
|
||||
return resolve(resolveBase, path)
|
||||
@ -61,16 +64,14 @@ export function detectTarget () {
|
||||
return 'node'
|
||||
}
|
||||
|
||||
export function extendTarget (base: SLSTarget, target: SLSTarget): SLSTarget {
|
||||
return (nuxtOptions: NuxtOptions) => {
|
||||
export function extendTarget (base: SLSTarget, target: SLSTarget): SLSTargetFn {
|
||||
return (config: SLSConfig) => {
|
||||
if (typeof target === 'function') {
|
||||
target = target(nuxtOptions)
|
||||
target = target(config)
|
||||
}
|
||||
|
||||
if (typeof base === 'function') {
|
||||
base = base(base)
|
||||
base = base(config)
|
||||
}
|
||||
|
||||
return defu({
|
||||
hooks: Hookable.mergeHooks(base.hooks, target.hooks),
|
||||
nuxtHooks: Hookable.mergeHooks(base.nuxtHooks as any, target.nuxtHooks as any)
|
||||
|
Loading…
Reference in New Issue
Block a user