feat: support serverMiddleware (#26)

* feat: support node req, res claases in all envs

* feat: support serverMiddleware
This commit is contained in:
pooya parsa 2020-11-12 19:20:55 +01:00 committed by GitHub
parent b258c2af2c
commit af92c53dcd
3 changed files with 47 additions and 5 deletions

View File

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

View File

@ -1,3 +1,4 @@
import hasha from 'hasha'
import type { Module } from '@nuxt/types' import type { Module } from '@nuxt/types'
import { build } from './build' import { build } from './build'
import { getoptions } from './config' import { getoptions } from './config'
@ -12,12 +13,13 @@ export default <Module> function slsModule () {
// Config // Config
const options = getoptions(nuxt) const options = getoptions(nuxt)
// Tune webpack config
if (options.minify !== false) { if (options.minify !== false) {
nuxt.options.build._minifyServer = true nuxt.options.build._minifyServer = true
} }
nuxt.options.build.standalone = true nuxt.options.build.standalone = true
// Tune generator
nuxt.options.generate.crawler = false nuxt.options.generate.crawler = false
if (Array.isArray(nuxt.options.generate.routes)) { if (Array.isArray(nuxt.options.generate.routes)) {
nuxt.options.generate.routes = Array.from(new Set([ nuxt.options.generate.routes = Array.from(new Set([
@ -25,9 +27,28 @@ export default <Module> function slsModule () {
...options.static ...options.static
])) ]))
} }
nuxt.options.generate.dir = options.publicDir 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) { if (options.nuxtHooks) {
nuxt.addHooks(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 alias from '@rollup/plugin-alias'
import json from '@rollup/plugin-json' import json from '@rollup/plugin-json'
import replace from '@rollup/plugin-replace' import replace from '@rollup/plugin-replace'
import virtual from '@rollup/plugin-virtual'
import analyze from 'rollup-plugin-analyzer' import analyze from 'rollup-plugin-analyzer'
import hasha from 'hasha'
import { SLSOptions } from '../config' import { SLSOptions } from '../config'
import { resolvePath } from '../utils' import { resolvePath } from '../utils'
import dynamicRequire from './dynamic-require' 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 // https://github.com/rollup/plugins/tree/master/packages/alias
const renderer = config.renderer || 'vue2' const renderer = config.renderer || 'vue2'
options.plugins.push(alias({ options.plugins.push(alias({
@ -116,9 +130,7 @@ export const getRollupConfig = (config: SLSOptions) => {
preferBuiltins: true, preferBuiltins: true,
rootDir: config.rootDir, rootDir: config.rootDir,
// https://www.npmjs.com/package/resolve // https://www.npmjs.com/package/resolve
customResolveOptions: { customResolveOptions: { basedir: config.rootDir },
basedir: config.rootDir
},
mainFields: ['main'] // Force resolve CJS (@vue/runtime-core ssrUtils) mainFields: ['main'] // Force resolve CJS (@vue/runtime-core ssrUtils)
})) }))