2020-11-05 12:26:00 +00:00
|
|
|
import type { Module } from '@nuxt/types'
|
2020-11-05 21:59:14 +00:00
|
|
|
import { build } from './build'
|
2020-11-05 13:38:15 +00:00
|
|
|
import { getoptions } from './config'
|
2020-11-05 11:28:39 +00:00
|
|
|
|
2020-11-05 12:26:00 +00:00
|
|
|
export default <Module> function slsModule () {
|
2020-11-05 11:28:39 +00:00
|
|
|
const { nuxt } = this
|
|
|
|
|
|
|
|
if (nuxt.options.dev) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Config
|
2020-11-14 00:03:26 +00:00
|
|
|
const options = getoptions(nuxt.options, nuxt.options.serverless || {})
|
2020-11-05 11:28:39 +00:00
|
|
|
|
2020-11-11 22:06:29 +00:00
|
|
|
// Tune webpack config
|
2020-11-14 22:01:57 +00:00
|
|
|
nuxt.options.build._minifyServer = false
|
|
|
|
nuxt.options.build.standalone = false
|
2020-11-05 11:28:39 +00:00
|
|
|
|
2020-11-11 22:06:29 +00:00
|
|
|
// Tune generator
|
2020-11-05 19:13:50 +00:00
|
|
|
nuxt.options.generate.crawler = false
|
2020-11-05 20:42:40 +00:00
|
|
|
if (Array.isArray(nuxt.options.generate.routes)) {
|
|
|
|
nuxt.options.generate.routes = Array.from(new Set([
|
|
|
|
...nuxt.options.generate.routes,
|
|
|
|
...options.static
|
|
|
|
]))
|
|
|
|
}
|
2020-11-10 18:19:24 +00:00
|
|
|
nuxt.options.generate.dir = options.publicDir
|
|
|
|
|
2020-11-11 22:06:29 +00:00
|
|
|
// serverMiddleware
|
|
|
|
// TODO: render:setupMiddleware hook
|
|
|
|
// TODO: support m.prefix and m.route
|
|
|
|
nuxt.hook('modules:done', () => {
|
2020-11-12 18:18:33 +00:00
|
|
|
const unsupported = []
|
2020-11-11 22:06:29 +00:00
|
|
|
for (let m of nuxt.options.serverMiddleware) {
|
|
|
|
if (typeof m === 'string') {
|
|
|
|
m = { handler: m }
|
|
|
|
}
|
|
|
|
|
|
|
|
const route = m.path || m.route || '/'
|
|
|
|
const handle = nuxt.resolver.resolvePath(m.handler || m.handle)
|
2020-11-12 18:18:33 +00:00
|
|
|
|
|
|
|
if (typeof handle !== 'string' || typeof route !== 'string') {
|
|
|
|
unsupported.push(m)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-11-14 13:05:09 +00:00
|
|
|
options.serverMiddleware.push({ ...m, route, handle })
|
2020-11-12 18:18:33 +00:00
|
|
|
}
|
|
|
|
if (unsupported.length) {
|
|
|
|
console.warn('[serverless] Unsupported Server middleware used: ', unsupported)
|
|
|
|
console.info('Supported format is `{ path: string, handler: string }` and handler should export `(req, res) => {}`')
|
2020-11-11 22:06:29 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-11-05 21:40:25 +00:00
|
|
|
if (options.nuxtHooks) {
|
|
|
|
nuxt.addHooks(options.nuxtHooks)
|
|
|
|
}
|
|
|
|
|
2020-11-08 23:19:23 +00:00
|
|
|
nuxt.hook('generate:cache:ignore', (ignore: string[]) => {
|
2020-11-05 13:38:15 +00:00
|
|
|
ignore.push(options.slsDir)
|
2020-11-06 12:14:49 +00:00
|
|
|
ignore.push(options.targetDir)
|
2020-11-06 12:41:33 +00:00
|
|
|
ignore.push(...options.generateIgnore)
|
2020-11-05 11:28:39 +00:00
|
|
|
})
|
|
|
|
|
2020-11-05 12:02:57 +00:00
|
|
|
nuxt.hook('generate:page', (page) => {
|
|
|
|
// TODO: Use ssrContext
|
2020-11-05 13:38:15 +00:00
|
|
|
if (!options.static.includes(page.route)) {
|
2020-11-05 12:02:57 +00:00
|
|
|
page.exclude = true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-11-14 00:03:26 +00:00
|
|
|
nuxt.hook('generate:before', async () => {
|
2020-11-14 13:05:09 +00:00
|
|
|
console.info('Building light version for `nuxt generate`')
|
2020-11-14 00:03:26 +00:00
|
|
|
const { entry } = await build(getoptions(nuxt.options, {
|
2020-11-14 13:05:09 +00:00
|
|
|
target: 'cjs',
|
2020-11-14 00:23:22 +00:00
|
|
|
serverMiddleware: options.serverMiddleware
|
2020-11-14 00:03:26 +00:00
|
|
|
}))
|
2020-11-14 13:05:09 +00:00
|
|
|
console.info('Loading lambda')
|
2020-11-14 00:03:26 +00:00
|
|
|
require(entry)
|
|
|
|
})
|
|
|
|
|
2020-11-14 13:05:09 +00:00
|
|
|
nuxt.hook('generate:done', async () => {
|
|
|
|
await build(options)
|
|
|
|
})
|
2020-11-05 11:28:39 +00:00
|
|
|
}
|