Skip to content

Commit

Permalink
fix: Intercept requests earlier as webpack ended up NOT serving some …
Browse files Browse the repository at this point in the history
…requests
  • Loading branch information
marcbachmann committed Feb 18, 2021
1 parent 903713b commit 001a9bd
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,34 +137,33 @@ async function webpackPlugin (fastify, opts) {
}

if (opts.watch) {
const webpackConfig = opts.webpackConfig || require(path.resolve('./webpack.config'))
fastify.register(module.parent.require('fastify-webpack-hmr'), {
config: webpackConfig,
webpackDev: {
watchOptions: {
ignored: [distDir]
},
...opts.webpackDev,
publicPath: publicAssetsPath
}
}).after((err) => {
if (err) throw err
let compiledPromise = new Promise((resolve) => {
fastify.addHook('onRequest', (req, rep, next) => {
if (!compiledPromise) return next()
compiledPromise.then(() => next())
})

let hasCompiled = false
const compiled = new Promise((resolve) => {
fastify.webpack.compiler.hooks.afterEmit.tap('WebpackDevMiddleware', (compilation) => {
if (!hasCompiled) resolve()
hasCompiled = true
const webpackConfig = opts.webpackConfig || require(path.resolve('./webpack.config'))
fastify.register(module.parent.require('fastify-webpack-hmr'), {
config: webpackConfig,
webpackDev: {
watchOptions: {
ignored: [distDir]
},
...opts.webpackDev,
publicPath: publicAssetsPath
}
}).after((err) => {
if (err) throw err

fastify.webpack.compiler.hooks.afterEmit.tap('WebpackDevMiddleware', (compilation) => {
const newAssetsMap = JSON.parse(compilation.assets['.assets.json']._value)
for (const key in assetsMap) delete assetsMap[key]
Object.assign(assetsMap, newAssetsMap)
})
})

fastify.addHook('onRequest', async () => {
if (hasCompiled) return
await compiled
if (compiledPromise) resolve()
compiledPromise = undefined
})
})
})
}
Expand Down

0 comments on commit 001a9bd

Please sign in to comment.