From 91caf2c4709a5cb0687bf71ec37248acbd773e02 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 6 Nov 2020 13:55:30 +0100 Subject: [PATCH] feat: support targer functions to consume nuxtOptions --- packages/nitro/src/config.ts | 10 ++++++++-- packages/nitro/src/utils.ts | 18 ++++++++++++++++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/packages/nitro/src/config.ts b/packages/nitro/src/config.ts index 01699cbf56..07a4cd2a17 100644 --- a/packages/nitro/src/config.ts +++ b/packages/nitro/src/config.ts @@ -1,12 +1,13 @@ import { resolve } from 'path' import defu from 'defu' -import { NuxtOptions } from '@nuxt/types' +import type { NuxtOptions } from '@nuxt/types' import { tryImport, resolvePath, detectTarget } from './utils' import * as TARGETS from './targets' export type UnresolvedPath = string | ((SLSOptions) => string) export interface SLSOptions { + nuxtOptions: NuxtOptions node: false target: string entry: UnresolvedPath @@ -42,7 +43,7 @@ export interface SLSConfig extends Omit, 'targetDir'> { targetDir: UnresolvedPath } -export type SLSTarget = Partial +export type SLSTarget = Partial | ((NuxtOptions) => Partial) export function getoptions (nuxtOptions: NuxtOptions): SLSOptions { const defaults: SLSConfig = { @@ -55,6 +56,7 @@ export function getoptions (nuxtOptions: NuxtOptions): SLSOptions { outName: '_nuxt.js', runtimeDir: resolve(__dirname, '../runtime'), static: [], + generateIgnore: [], nuxt: 2, logStartup: true, inlineChunks: true, @@ -65,11 +67,15 @@ export function getoptions (nuxtOptions: NuxtOptions): SLSOptions { if (typeof target === 'function') { target = target(nuxtOptions) } + let targetDefaults = TARGETS[target] || tryImport(nuxtOptions.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 }) diff --git a/packages/nitro/src/utils.ts b/packages/nitro/src/utils.ts index 3afe5661af..a615509b8e 100644 --- a/packages/nitro/src/utils.ts +++ b/packages/nitro/src/utils.ts @@ -2,6 +2,8 @@ import { relative, dirname, resolve } from 'path' 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' export function hl (str: string) { @@ -60,6 +62,18 @@ export function detectTarget () { } export function extendTarget (base: SLSTarget, target: SLSTarget): SLSTarget { - // TODO: merge hooks - return defu(target, base) + return (nuxtOptions: NuxtOptions) => { + if (typeof target === 'function') { + target = target(nuxtOptions) + } + + if (typeof base === 'function') { + base = base(base) + } + + return defu({ + hooks: Hookable.mergeHooks(base.hooks, target.hooks), + nuxtHooks: Hookable.mergeHooks(base.nuxtHooks as any, target.nuxtHooks as any) + }, target, base) + } }