From 3fa6717882c1f9da0269e3666a1a6e94e82a2b34 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 6 Nov 2020 14:46:17 +0100 Subject: [PATCH] improve config extending --- packages/nitro/src/build.ts | 2 +- packages/nitro/src/config.ts | 103 +++++++++++++------------- packages/nitro/src/index.ts | 2 +- packages/nitro/src/rollup/config.ts | 2 +- packages/nitro/src/targets/browser.ts | 46 ++++++------ packages/nitro/src/utils.ts | 17 +++-- 6 files changed, 88 insertions(+), 84 deletions(-) diff --git a/packages/nitro/src/build.ts b/packages/nitro/src/build.ts index 34cd512d61..15926de966 100644 --- a/packages/nitro/src/build.ts +++ b/packages/nitro/src/build.ts @@ -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') diff --git a/packages/nitro/src/config.ts b/packages/nitro/src/config.ts index 07a4cd2a17..4aed031fe5 100644 --- a/packages/nitro/src/config.ts +++ b/packages/nitro/src/config.ts @@ -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, 'targetDir'> { - targetDir: UnresolvedPath + targetDir?: UnresolvedPath } -export type SLSTarget = Partial | ((NuxtOptions) => Partial) +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) diff --git a/packages/nitro/src/index.ts b/packages/nitro/src/index.ts index 6459c03659..25451179be 100644 --- a/packages/nitro/src/index.ts +++ b/packages/nitro/src/index.ts @@ -10,7 +10,7 @@ export default function slsModule () { } // Config - const options = getoptions(nuxt.options) + const options = getoptions(nuxt) if (options.minify !== false) { nuxt.options.build._minifyServer = true diff --git a/packages/nitro/src/rollup/config.ts b/packages/nitro/src/rollup/config.ts index 6dde1e80d5..9325f5d6e9 100644 --- a/packages/nitro/src/rollup/config.ts +++ b/packages/nitro/src/rollup/config.ts @@ -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, diff --git a/packages/nitro/src/targets/browser.ts b/packages/nitro/src/targets/browser.ts index 2f43aac1bd..61369b0839 100644 --- a/packages/nitro/src/targets/browser.ts +++ b/packages/nitro/src/targets/browser.ts @@ -5,36 +5,38 @@ import { extendTarget } from '../utils' import { SLSTarget } from '../config' import { worker } from './worker' -const getScriptTag = () => ``.replace(/\n| +/g, '') -export const browser: SLSTarget = extendTarget(worker, { - targetDir: ({ publicDir }) => publicDir, - nuxtHooks: { - 'vue-renderer:ssr:templateParams' (params) { - params.APP += getScriptTag() - }, - 'vue-renderer:spa:templateParams' (params) { - params.APP += getScriptTag() - } - }, - hooks: { - 'template:document' (tmpl) { - tmpl.compiled = tmpl.compiled.replace('', getScriptTag() + '') - }, - async done ({ targetDir, publicDir }) { - const rootIndex = resolve(publicDir, 'index.html') - const rootFallback = resolve(publicDir, '200.html') - if (!existsSync(rootIndex) && existsSync(rootFallback)) { - await copy(rootFallback, rootIndex) + return { + targetDir: '{{ publicDir }}', + nuxtHooks: { + 'vue-renderer:ssr:templateParams' (params) { + params.APP += script + }, + 'vue-renderer:spa:templateParams' (params) { + params.APP += script } + }, + hooks: { + 'template:document' (tmpl) { + tmpl.compiled = tmpl.compiled.replace('', script + '') + }, + async done ({ targetDir, publicDir }) { + const rootIndex = resolve(publicDir, 'index.html') + const rootFallback = resolve(publicDir, '200.html') + if (!existsSync(rootIndex) && existsSync(rootFallback)) { + await copy(rootFallback, rootIndex) + } - consola.info(`Try with \`npx serve ${relative(process.cwd(), targetDir)}\``) + consola.info(`Try with \`npx serve ${relative(process.cwd(), targetDir)}\``) + } } } }) diff --git a/packages/nitro/src/utils.ts b/packages/nitro/src/utils.ts index a615509b8e..f5e2b54735 100644 --- a/packages/nitro/src/utils.ts +++ b/packages/nitro/src/utils.ts @@ -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)