Skip to content

Commit

Permalink
fixes for dev and static target
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Nov 20, 2020
1 parent d4d5285 commit 21bf7c2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export interface SigmaContext {
staticDir: string
routerBase: string
publicPath: string
isStatic: boolean
fullStatic: boolean
staticAssets: any
}
Expand Down Expand Up @@ -85,7 +86,8 @@ export function getsigmaContext (nuxtOptions: NuxtOptions, input: SigmaInput): S
staticDir: nuxtOptions.dir.static,
routerBase: nuxtOptions.router.base,
publicPath: nuxtOptions.build.publicPath,
fullStatic: nuxtOptions.preset === 'static' && !nuxtOptions._legacyGenerate,
isStatic: nuxtOptions.target === 'static' && !nuxtOptions.dev,
fullStatic: nuxtOptions.target === 'static' && !nuxtOptions._legacyGenerate,
// @ts-ignore
staticAssets: nuxtOptions.generate.staticAssets
},
Expand Down
25 changes: 18 additions & 7 deletions src/module/nuxt2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@ export default function (nuxt) {
const sigmaContext = getsigmaContext(nuxt.options, nuxt.options.sigma || {})
const sigmaDevContext = getsigmaContext(nuxt.options, { preset: 'dev' })

// Use nuxt as main hooks host
// Connect hooks
nuxt.addHooks(sigmaContext.nuxtHooks)
nuxt.hook('close', () => sigmaContext._internal.hooks.callHook('close'))

nuxt.addHooks(sigmaDevContext.nuxtHooks)
nuxt.hook('close', () => sigmaDevContext._internal.hooks.callHook('close'))
sigmaDevContext._internal.hooks.hook('renderLoading',
(req, res) => nuxt.callHook('server:nuxt:renderLoading', req, res))

// Replace nuxt server
if (nuxt.server) {
nuxt.server.__closed = true
nuxt.server = createNuxt2DevServer(sigmaDevContext)
nuxt.addHooks(sigmaDevContext.nuxtHooks)
}

// serverMiddleware bridge
Expand Down Expand Up @@ -60,12 +65,16 @@ export default function (nuxt) {
nuxt.options.build._minifyServer = false
nuxt.options.build.standalone = false
nuxt.hook('build:done', async () => {
await build(nuxt.options.dev ? sigmaDevContext : sigmaContext)
if (nuxt.options.dev) {
await build(sigmaDevContext)
} else if (!sigmaContext._nuxt.isStatic) {
await build(sigmaContext)
}
})

// nude dev
if (nuxt.options.dev) {
nuxt.hook('sigma:compiled', () => { nuxt.server.watch() })
sigmaDevContext._internal.hooks.hook('sigma:compiled', () => { nuxt.server.watch() })
nuxt.hook('build:compile', ({ compiler }) => { compiler.outputFileSystem = wpfs })
nuxt.hook('server:devMiddleware', (m) => { nuxt.server.setDevMiddleware(m) })
}
Expand All @@ -74,17 +83,19 @@ export default function (nuxt) {
nuxt.options.generate.dir = sigmaContext.output.publicDir
nuxt.hook('generate:cache:ignore', (ignore: string[]) => {
ignore.push(sigmaContext.output.dir)
ignore.push(sigmaContext.output.serverDir)
if (sigmaContext.output.publicDir) {
ignore.push(sigmaContext.output.publicDir)
}
ignore.push(...sigmaContext.ignore)
})

// generate:bfore is before webpack build that we need!
nuxt.hook('generate:extendRoutes', async () => {
await build(sigmaDevContext)
await nuxt.server.reload()
})

nuxt.hook('generate:done', async () => {
await nuxt.server.close()
await build(sigmaContext)
})
}

Expand Down
6 changes: 4 additions & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@ export function createDevServer (sigmaContext: SigmaContext) {
const proxy = createProxy()
app.use((req, res) => {
if (workerAddress) {
proxy.web(req, res, { target: workerAddress })
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('server:nuxt:renderLoading', req, res)
sigmaContext._internal.hooks.callHook('renderLoading', req, res)
} else {
res.end('Worker not ready!')
}
Expand Down

0 comments on commit 21bf7c2

Please sign in to comment.