mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-29 09:02:03 +00:00
refactor: move nuxt2 plugin to compat
This commit is contained in:
parent
d94aec4841
commit
41951c8862
@ -1,23 +1,17 @@
|
|||||||
import fetch from 'node-fetch'
|
import fetch from 'node-fetch'
|
||||||
import { resolve } from 'upath'
|
import { resolve } from 'upath'
|
||||||
import { build, generate, prepare } from '../build'
|
import { build, generate, prepare } from './build'
|
||||||
import { getSigmaContext, SigmaContext } from '../context'
|
import { getSigmaContext, SigmaContext } from './context'
|
||||||
import { createDevServer } from '../server'
|
import { createDevServer } from './server'
|
||||||
import wpfs from '../utils/wpfs'
|
import { wpfs } from './utils/wpfs'
|
||||||
|
import { resolveMiddleware } from './middleware'
|
||||||
|
|
||||||
export default function (nuxt, moduleContainer) {
|
export default function nuxt2CompatModule () {
|
||||||
// Build in node_modules/.cache/nuxt
|
const { nuxt } = this
|
||||||
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)
|
|
||||||
|
|
||||||
for (const pathKey of ['appTemplatePath', 'documentPath']) {
|
// Disable loading-screen
|
||||||
nuxt.options[pathKey] = (nuxt.options[pathKey] || '')
|
nuxt.options.build.loadingScreen = false
|
||||||
.replace(oldBuildDir, nuxt.options.buildDir)
|
nuxt.options.build.indicator = false
|
||||||
}
|
|
||||||
|
|
||||||
// Create contexts
|
// Create contexts
|
||||||
const sigmaContext = getSigmaContext(nuxt.options, nuxt.options.sigma || {})
|
const sigmaContext = getSigmaContext(nuxt.options, nuxt.options.sigma || {})
|
||||||
@ -45,41 +39,18 @@ export default function (nuxt, moduleContainer) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Sigma client plugin
|
// Sigma client plugin
|
||||||
moduleContainer.addPlugin({
|
this.addPlugin({
|
||||||
fileName: 'sigma.client.js',
|
fileName: 'sigma.client.js',
|
||||||
src: resolve(sigmaContext._internal.runtimeDir, 'app/sigma.client.js')
|
src: resolve(sigmaContext._internal.runtimeDir, 'app/sigma.client.js')
|
||||||
})
|
})
|
||||||
|
|
||||||
// serverMiddleware bridge
|
// Resolve middleware
|
||||||
// TODO: render:setupMiddleware hook
|
|
||||||
// TODO: support m.prefix and m.route
|
|
||||||
nuxt.hook('modules:done', () => {
|
nuxt.hook('modules:done', () => {
|
||||||
const unsupported = []
|
const { middleware, legacyMiddleware } =
|
||||||
for (let m of nuxt.options.serverMiddleware) {
|
resolveMiddleware(nuxt.options.serverMiddleware, nuxt.resolver.resolvePath)
|
||||||
if (typeof m === 'string') { m = { handler: m } }
|
nuxt.server.setLegacyMiddleware(legacyMiddleware)
|
||||||
const route = m.path || m.route || '/'
|
sigmaContext.middleware.push(...middleware)
|
||||||
let handle = m.handler || m.handle
|
sigmaDevContext.middleware.push(...middleware)
|
||||||
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) => {}`')
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// nuxt build/dev
|
// nuxt build/dev
|
||||||
@ -154,7 +125,7 @@ function createNuxt2DevServer (sigmaContext: SigmaContext) {
|
|||||||
renderRoute,
|
renderRoute,
|
||||||
listen,
|
listen,
|
||||||
serverMiddlewarePaths () { return [] },
|
serverMiddlewarePaths () { return [] },
|
||||||
ready () {}
|
ready () { }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -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 * from './types'
|
||||||
|
export { wpfs } from './utils/wpfs'
|
||||||
export default function () {
|
|
||||||
const { nuxt } = this
|
|
||||||
return nuxt2(nuxt, this)
|
|
||||||
}
|
|
||||||
|
29
packages/nitro/src/middleware.ts
Normal file
29
packages/nitro/src/middleware.ts
Normal 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
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,5 @@
|
|||||||
import { Worker } from 'worker_threads'
|
import { Worker } from 'worker_threads'
|
||||||
import connect from 'connect'
|
import { createApp } from 'h3'
|
||||||
import { resolve } from 'upath'
|
import { resolve } from 'upath'
|
||||||
import debounce from 'debounce'
|
import debounce from 'debounce'
|
||||||
import chokidar from 'chokidar'
|
import chokidar from 'chokidar'
|
||||||
@ -48,26 +48,17 @@ export function createDevServer (sigmaContext: SigmaContext) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// App
|
// App
|
||||||
const app = connect()
|
const app = createApp()
|
||||||
|
|
||||||
// _nuxt and static
|
// _nuxt and static
|
||||||
app.use(sigmaContext._nuxt.publicPath, serveStatic(resolve(sigmaContext._nuxt.buildDir, 'dist/client')))
|
app.use(sigmaContext._nuxt.publicPath, serveStatic(resolve(sigmaContext._nuxt.buildDir, 'dist/client')))
|
||||||
app.use(sigmaContext._nuxt.routerBase, serveStatic(resolve(sigmaContext._nuxt.staticDir)))
|
app.use(sigmaContext._nuxt.routerBase, serveStatic(resolve(sigmaContext._nuxt.staticDir)))
|
||||||
|
|
||||||
// Dev Middleware
|
// Dynamic Middlwware
|
||||||
let loadingMiddleware, devMiddleware
|
const legacyMiddleware = createDynamicMiddleware()
|
||||||
const setLoadingMiddleware = (m) => { loadingMiddleware = m }
|
const devMiddleware = createDynamicMiddleware()
|
||||||
const setDevMiddleware = (m) => { devMiddleware = m }
|
app.use(legacyMiddleware.middleware)
|
||||||
app.use((req, res, next) => {
|
app.use(devMiddleware.middleware)
|
||||||
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()
|
|
||||||
})
|
|
||||||
|
|
||||||
// serve placeholder 404 assets instead of hitting SSR
|
// serve placeholder 404 assets instead of hitting SSR
|
||||||
app.use(sigmaContext._nuxt.publicPath, servePlaceholder())
|
app.use(sigmaContext._nuxt.publicPath, servePlaceholder())
|
||||||
@ -80,10 +71,6 @@ export function createDevServer (sigmaContext: SigmaContext) {
|
|||||||
proxy.web(req, res, { target: workerAddress }, (_err) => {
|
proxy.web(req, res, { target: workerAddress }, (_err) => {
|
||||||
// console.error('[proxy]', 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 {
|
} else {
|
||||||
res.end('Worker not ready!')
|
res.end('Worker not ready!')
|
||||||
}
|
}
|
||||||
@ -131,7 +118,26 @@ export function createDevServer (sigmaContext: SigmaContext) {
|
|||||||
listen: _listen,
|
listen: _listen,
|
||||||
close,
|
close,
|
||||||
watch,
|
watch,
|
||||||
setLoadingMiddleware,
|
setLegacyMiddleware: legacyMiddleware.set,
|
||||||
setDevMiddleware
|
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()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { join } from 'upath'
|
import { join } from 'upath'
|
||||||
import fsExtra from 'fs-extra'
|
import fsExtra from 'fs-extra'
|
||||||
|
|
||||||
export default {
|
export const wpfs = {
|
||||||
...fsExtra,
|
...fsExtra,
|
||||||
join
|
join
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user