feat: support serverMiddleware

This commit is contained in:
Pooya Parsa 2020-11-11 23:06:29 +01:00
parent b258c2af2c
commit 75ed762192
3 changed files with 47 additions and 5 deletions

View File

@ -12,6 +12,11 @@ export interface Nuxt extends Hookable{
options: NuxtOptions
}
export interface ServerMiddleware {
route: string
handle: string
}
export interface SLSOptions {
hooks: configHooksT
nuxtHooks: configHooksT
@ -39,6 +44,8 @@ export interface SLSOptions {
slsDir: string
targetDir: string
serverMiddleware: ServerMiddleware[],
static: string[]
generateIgnore: string[]
}
@ -76,6 +83,8 @@ export function getoptions (nuxt: SLSNuxt): SLSOptions {
slsDir: '{{ rootDir }}/.nuxt/serverless',
targetDir: '{{ slsDir }}/{{ target }}',
serverMiddleware: [],
static: [],
generateIgnore: []
}

View File

@ -1,3 +1,4 @@
import hasha from 'hasha'
import type { Module } from '@nuxt/types'
import { build } from './build'
import { getoptions } from './config'
@ -12,12 +13,13 @@ export default <Module> function slsModule () {
// Config
const options = getoptions(nuxt)
// Tune webpack config
if (options.minify !== false) {
nuxt.options.build._minifyServer = true
}
nuxt.options.build.standalone = true
// Tune generator
nuxt.options.generate.crawler = false
if (Array.isArray(nuxt.options.generate.routes)) {
nuxt.options.generate.routes = Array.from(new Set([
@ -25,9 +27,28 @@ export default <Module> function slsModule () {
...options.static
]))
}
nuxt.options.generate.dir = options.publicDir
// serverMiddleware
// TODO: render:setupMiddleware hook
// TODO: support m.prefix and m.route
nuxt.hook('modules:done', () => {
for (let m of nuxt.options.serverMiddleware) {
if (typeof m === 'string') {
m = { handler: m }
}
if (typeof m.handler !== 'string') {
console.warn('[Serverless] Unsupported serverMiddleware format:', m)
continue
}
const route = m.path || m.route || '/'
const handle = nuxt.resolver.resolvePath(m.handler || m.handle)
const id = '_' + hasha(handle).substr(0, 6)
options.serverMiddleware.push({ route, id, handle })
}
})
if (options.nuxtHooks) {
nuxt.addHooks(options.nuxtHooks)
}

View File

@ -7,8 +7,10 @@ import nodeResolve from '@rollup/plugin-node-resolve'
import alias from '@rollup/plugin-alias'
import json from '@rollup/plugin-json'
import replace from '@rollup/plugin-replace'
import virtual from '@rollup/plugin-virtual'
import analyze from 'rollup-plugin-analyzer'
import hasha from 'hasha'
import { SLSOptions } from '../config'
import { resolvePath } from '../utils'
import dynamicRequire from './dynamic-require'
@ -97,6 +99,18 @@ export const getRollupConfig = (config: SLSOptions) => {
}
}))
// Provide serverMiddleware
const getImportId = p => '_' + hasha(p).substr(0, 6)
options.plugins.push(virtual({
'~serverMiddleware': `
${config.serverMiddleware.map(m => `import ${getImportId(m.handle)} from '${m.handle}';`).join('\n')}
export default [
${config.serverMiddleware.map(m => `{ route: '${m.route}', handle: ${getImportId(m.handle)} }`).join(',\n')}
];
`
}))
// https://github.com/rollup/plugins/tree/master/packages/alias
const renderer = config.renderer || 'vue2'
options.plugins.push(alias({
@ -116,9 +130,7 @@ export const getRollupConfig = (config: SLSOptions) => {
preferBuiltins: true,
rootDir: config.rootDir,
// https://www.npmjs.com/package/resolve
customResolveOptions: {
basedir: config.rootDir
},
customResolveOptions: { basedir: config.rootDir },
mainFields: ['main'] // Force resolve CJS (@vue/runtime-core ssrUtils)
}))