refactor: move nuxt2 plugin to compat

This commit is contained in:
Pooya Parsa 2021-01-22 20:53:40 +01:00
parent d94aec4841
commit 41951c8862
5 changed files with 81 additions and 76 deletions

View File

@ -1,23 +1,17 @@
import fetch from 'node-fetch'
import { resolve } from 'upath'
import { build, generate, prepare } from '../build'
import { getSigmaContext, SigmaContext } from '../context'
import { createDevServer } from '../server'
import wpfs from '../utils/wpfs'
import { build, generate, prepare } from './build'
import { getSigmaContext, SigmaContext } from './context'
import { createDevServer } from './server'
import { wpfs } from './utils/wpfs'
import { resolveMiddleware } from './middleware'
export default function (nuxt, moduleContainer) {
// Build in node_modules/.cache/nuxt
const oldBuildDir = nuxt.options.buildDir
if (!nuxt.options.dev) {
nuxt.options.buildDir = resolve(nuxt.options.rootDir, 'node_modules/.cache/nuxt')
}
nuxt.options.build.transpile = nuxt.options.build.transpile || []
nuxt.options.build.transpile.push(nuxt.options.buildDir)
export default function nuxt2CompatModule () {
const { nuxt } = this
for (const pathKey of ['appTemplatePath', 'documentPath']) {
nuxt.options[pathKey] = (nuxt.options[pathKey] || '')
.replace(oldBuildDir, nuxt.options.buildDir)
}
// Disable loading-screen
nuxt.options.build.loadingScreen = false
nuxt.options.build.indicator = false
// Create contexts
const sigmaContext = getSigmaContext(nuxt.options, nuxt.options.sigma || {})
@ -45,41 +39,18 @@ export default function (nuxt, moduleContainer) {
}
// Sigma client plugin
moduleContainer.addPlugin({
this.addPlugin({
fileName: 'sigma.client.js',
src: resolve(sigmaContext._internal.runtimeDir, 'app/sigma.client.js')
})
// serverMiddleware bridge
// TODO: render:setupMiddleware hook
// TODO: support m.prefix and m.route
// Resolve middleware
nuxt.hook('modules:done', () => {
const unsupported = []
for (let m of nuxt.options.serverMiddleware) {
if (typeof m === 'string') { m = { handler: m } }
const route = m.path || m.route || '/'
let handle = m.handler || m.handle
if (typeof handle !== 'string' || typeof route !== 'string') {
if (route === '/_loading') {
nuxt.server.setLoadingMiddleware(handle)
continue
}
// Temporary hide for @nuxt/pwa module
if (route === '/_nuxt/' && process.env.NODE_ENV === 'development') {
continue
}
unsupported.push(m)
continue
}
handle = nuxt.resolver.resolvePath(handle)
sigmaContext.middleware.push({ ...m, route, handle })
sigmaDevContext.middleware.push({ ...m, route, handle })
}
nuxt.options.serverMiddleware = [...unsupported]
if (unsupported.length) {
console.warn('[sigma] Unsupported Server middleware used: \n', ...unsupported)
console.info('Supported format is `{ path: string, handler: string }` and handler should export `(req, res) => {}`')
}
const { middleware, legacyMiddleware } =
resolveMiddleware(nuxt.options.serverMiddleware, nuxt.resolver.resolvePath)
nuxt.server.setLegacyMiddleware(legacyMiddleware)
sigmaContext.middleware.push(...middleware)
sigmaDevContext.middleware.push(...middleware)
})
// nuxt build/dev
@ -154,7 +125,7 @@ function createNuxt2DevServer (sigmaContext: SigmaContext) {
renderRoute,
listen,
serverMiddlewarePaths () { return [] },
ready () {}
ready () { }
}
}

View File

@ -1,7 +1,6 @@
import nuxt2 from './module/nuxt2'
export * from './build'
export * from './context'
export * from './middleware'
export * from './server'
export * from './types'
export default function () {
const { nuxt } = this
return nuxt2(nuxt, this)
}
export { wpfs } from './utils/wpfs'

View File

@ -0,0 +1,29 @@
export interface Middleware {
handle: string
route: string
}
export function resolveMiddleware (serverMiddleware: any[], resolvePath: (string) => string) {
const middleware: Middleware[] = []
const legacyMiddleware: Middleware[] = []
for (let m of serverMiddleware) {
if (typeof m === 'string') { m = { handler: m } }
const route = m.path || m.route || '/'
const handle = m.handler || m.handle
if (typeof handle !== 'string' || typeof route !== 'string') {
legacyMiddleware.push(m)
} else {
middleware.push({
...m,
handle: resolvePath(handle),
route
})
}
}
return {
middleware,
legacyMiddleware
}
}

View File

@ -1,5 +1,5 @@
import { Worker } from 'worker_threads'
import connect from 'connect'
import { createApp } from 'h3'
import { resolve } from 'upath'
import debounce from 'debounce'
import chokidar from 'chokidar'
@ -48,26 +48,17 @@ export function createDevServer (sigmaContext: SigmaContext) {
}
// App
const app = connect()
const app = createApp()
// _nuxt and static
app.use(sigmaContext._nuxt.publicPath, serveStatic(resolve(sigmaContext._nuxt.buildDir, 'dist/client')))
app.use(sigmaContext._nuxt.routerBase, serveStatic(resolve(sigmaContext._nuxt.staticDir)))
// Dev Middleware
let loadingMiddleware, devMiddleware
const setLoadingMiddleware = (m) => { loadingMiddleware = m }
const setDevMiddleware = (m) => { devMiddleware = m }
app.use((req, res, next) => {
if (loadingMiddleware && req.url.startsWith('/_loading')) {
req.url = req.url.replace('/_loading', '')
return loadingMiddleware(req, res)
}
if (devMiddleware) {
return devMiddleware(req, res, next)
}
return next()
})
// Dynamic Middlwware
const legacyMiddleware = createDynamicMiddleware()
const devMiddleware = createDynamicMiddleware()
app.use(legacyMiddleware.middleware)
app.use(devMiddleware.middleware)
// serve placeholder 404 assets instead of hitting SSR
app.use(sigmaContext._nuxt.publicPath, servePlaceholder())
@ -80,10 +71,6 @@ export function createDevServer (sigmaContext: SigmaContext) {
proxy.web(req, res, { target: workerAddress }, (_err) => {
// console.error('[proxy]', err)
})
} else if (loadingMiddleware) {
// TODO:serverIndex method is not exposed
// loadingMiddleware(req, res)
sigmaContext._internal.hooks.callHook('renderLoading', req, res)
} else {
res.end('Worker not ready!')
}
@ -131,7 +118,26 @@ export function createDevServer (sigmaContext: SigmaContext) {
listen: _listen,
close,
watch,
setLoadingMiddleware,
setDevMiddleware
setLegacyMiddleware: legacyMiddleware.set,
setDevMiddleware: devMiddleware.set
}
}
function createDynamicMiddleware () {
let middleware
return {
set: (input) => {
if (!Array.isArray(input)) {
middleware = input
return
}
const app = require('connect')()
for (const m of input) {
app.use(m.path || m.route || '/', m.handler || m.handle)
}
middleware = app
},
middleware: (req, res, next) =>
middleware ? middleware(req, res, next) : next()
}
}

View File

@ -1,7 +1,7 @@
import { join } from 'upath'
import fsExtra from 'fs-extra'
export default {
export const wpfs = {
...fsExtra,
join
}