feat: support universalFetch during generate

This commit is contained in:
Pooya Parsa 2020-11-14 01:03:26 +01:00
parent c8f4957520
commit 9e638e96fa
3 changed files with 32 additions and 28 deletions

View File

@ -26,10 +26,6 @@ export async function build (options: SLSOptions) {
await hooks.callHook('template:document', htmlTemplate)
await writeFile(htmlTemplate.dst, htmlTemplate.compiled)
if (options.targetDir.startsWith(options.slsDir)) {
emptyDir(options.slsDir)
}
options.rollupConfig = getRollupConfig(options)
await hooks.callHook('rollup:before', options)
const build = await rollup(options.rollupConfig).catch((error) => {
@ -45,4 +41,8 @@ export async function build (options: SLSOptions) {
)
await hooks.callHook('done', options)
return {
entry: options.rollupConfig.output.file
}
}

View File

@ -31,7 +31,7 @@ export interface SLSOptions {
entry: UnresolvedPath
outName: string
node: false
node: false | true
target: string
minify: boolean
rollupConfig?: any
@ -57,23 +57,16 @@ export interface SLSConfig extends Omit<Partial<SLSOptions>, 'targetDir'> {
export type SLSTargetFn = (config: SLSConfig) => SLSConfig
export type SLSTarget = SLSConfig | SLSTargetFn
interface SLSNuxt extends Nuxt {
options: Nuxt['options'] & {
generate: Nuxt['options']['generate'] & {
staticAssets: string[]
}
}
}
export function getoptions (nuxt: SLSNuxt): SLSOptions {
export function getoptions (nuxtOptions: Nuxt['options'], serverless: SLSConfig): SLSOptions {
const defaults: SLSConfig = {
rootDir: nuxt.options.rootDir,
buildDir: nuxt.options.buildDir,
publicDir: nuxt.options.generate.dir,
routerBase: nuxt.options.router.base,
publicPath: nuxt.options.build.publicPath,
fullStatic: nuxt.options.target === 'static' && !nuxt.options._legacyGenerate,
staticAssets: nuxt.options.generate.staticAssets,
rootDir: nuxtOptions.rootDir,
buildDir: nuxtOptions.buildDir,
publicDir: nuxtOptions.generate.dir,
routerBase: nuxtOptions.router.base,
publicPath: nuxtOptions.build.publicPath,
fullStatic: nuxtOptions.target === 'static' && !nuxtOptions._legacyGenerate,
// @ts-ignore
staticAssets: nuxtOptions.generate.staticAssets,
outName: '_nuxt.js',
logStartup: true,
@ -83,23 +76,23 @@ export function getoptions (nuxt: SLSNuxt): SLSOptions {
slsDir: '{{ rootDir }}/.nuxt/serverless',
targetDir: '{{ slsDir }}/{{ target }}',
serverMiddleware: [],
serverMiddleware: serverless.serverMiddleware || [],
static: [],
generateIgnore: []
}
const target = process.env.NUXT_SLS_TARGET || (nuxt.options.serverless || {}).target || detectTarget()
let targetDefaults = TARGETS[target] || tryImport(nuxt.options.rootDir, target)
const target = serverless.target || process.env.NUXT_SLS_TARGET || detectTarget()
let targetDefaults = TARGETS[target] || tryImport(nuxtOptions.rootDir, target)
if (!targetDefaults) {
throw new Error('Cannot resolve target: ' + target)
}
targetDefaults = targetDefaults.default || targetDefaults
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)
const _targetInput = defu(nuxtOptions.serverless, _defaults)
const _target = extendTarget(nuxtOptions.serverless, targetDefaults)(_targetInput)
const options: SLSOptions = defu(nuxtOptions.serverless, _target, _defaults)
options.slsDir = resolvePath(options, options.slsDir)
options.targetDir = resolvePath(options, options.targetDir)

View File

@ -10,7 +10,7 @@ export default <Module> function slsModule () {
}
// Config
const options = getoptions(nuxt)
const options = getoptions(nuxt.options, nuxt.options.serverless || {})
// Tune webpack config
if (options.minify !== false) {
@ -71,5 +71,16 @@ export default <Module> function slsModule () {
}
})
nuxt.hook('generate:before', async () => {
const { entry } = await build(getoptions(nuxt.options, {
target: 'node',
serverMiddleware: options.serverMiddleware,
node: true,
minify: false,
analyze: false
}))
require(entry)
})
nuxt.hook('generate:done', () => build(options))
}