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

View File

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

View File

@ -10,7 +10,7 @@ export default <Module> function slsModule () {
} }
// Config // Config
const options = getoptions(nuxt) const options = getoptions(nuxt.options, nuxt.options.serverless || {})
// Tune webpack config // Tune webpack config
if (options.minify !== false) { 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)) nuxt.hook('generate:done', () => build(options))
} }