From af92c53dcdb7a70cd83eab084961f9f1de7505d4 Mon Sep 17 00:00:00 2001 From: pooya parsa Date: Thu, 12 Nov 2020 19:20:55 +0100 Subject: [PATCH] feat: support serverMiddleware (#26) * feat: support node req, res claases in all envs * feat: support serverMiddleware --- packages/nitro/src/config.ts | 9 +++++++++ packages/nitro/src/index.ts | 25 +++++++++++++++++++++++-- packages/nitro/src/rollup/config.ts | 18 +++++++++++++++--- 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/packages/nitro/src/config.ts b/packages/nitro/src/config.ts index 72343bddd3..70533398aa 100644 --- a/packages/nitro/src/config.ts +++ b/packages/nitro/src/config.ts @@ -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: [] } diff --git a/packages/nitro/src/index.ts b/packages/nitro/src/index.ts index 6a4680561f..e473f98fa5 100644 --- a/packages/nitro/src/index.ts +++ b/packages/nitro/src/index.ts @@ -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 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 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) } diff --git a/packages/nitro/src/rollup/config.ts b/packages/nitro/src/rollup/config.ts index eadcfe8f85..e79aeeb9da 100644 --- a/packages/nitro/src/rollup/config.ts +++ b/packages/nitro/src/rollup/config.ts @@ -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) }))