Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: disable HMR in dev mode #11

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions packages/next/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export default async function getBaseWebpackConfig(
): Promise<webpack.Configuration> {
let plugins: PluginMetaData[] = []
let babelPresetPlugins: { dir: string; config: any }[] = []

if (config.experimental.plugins) {
plugins = await collectPlugins(dir, config.env, config.plugins)
pluginLoaderOptions.plugins = plugins
Expand Down Expand Up @@ -268,7 +269,7 @@ export default async function getBaseWebpackConfig(
cache: true,
cpus: config.experimental.cpus,
distDir: distDir,
parallel: false,
parallel: true,
sourceMap: false,
workerThreads: config.experimental.workerThreads,
}
Expand Down Expand Up @@ -459,7 +460,6 @@ export default async function getBaseWebpackConfig(
? undefined
: !isServerless
? [
{ 'next/router': 'next/dist/client/router.js' },
(context, request, callback) => {
const notExternalModules = [
'next/app',
Expand Down Expand Up @@ -559,7 +559,6 @@ export default async function getBaseWebpackConfig(
webpack5Experiential
? [
'enhanced-resolve',
{ 'next/router': 'next/dist/client/router.js' },
]
: []
),
Expand Down Expand Up @@ -814,11 +813,8 @@ export default async function getBaseWebpackConfig(
})
)
}
devPlugins.push(
new webpack.HotModuleReplacementPlugin({
multiStep: true,
})
)
!webpack5Experiential &&
devPlugins.push(new webpack.HotModuleReplacementPlugin())
}

return devPlugins
Expand Down
61 changes: 6 additions & 55 deletions packages/next/server/on-demand-entry-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const ADDED = Symbol('added')
const BUILDING = Symbol('building')
const BUILT = Symbol('built')

const IS_WEBPACK_5 = true
const webpack5Experiential = parseInt(require('webpack').version) === 5

// Based on https://github.com/webpack/webpack/blob/master/lib/DynamicEntryPlugin.js#L29-L37
function addEntry(
Expand Down Expand Up @@ -96,29 +96,9 @@ export default function onDemandEntryHandler(
let reloading = false
let stopped = false
let reloadCallbacks: EventEmitter | null = new EventEmitter()
let multiStepBuild = { promises: [] }

multiStepBuild.promises.push(
new Promise(resolve => {
multiStepBuild.additionalPass = resolve
})
)
multiStepBuild.promises.push(
new Promise(resolve => {
multiStepBuild.done = resolve
})
)

multiStepBuild.promises.push(
new Promise(resolve => {
multiStepBuild.lastDoneEvent = resolve
})
)

let callbackSet = []
let callCount = []
for (const compiler of compilers) {
IS_WEBPACK_5 &&
webpack5Experiential &&
new DynamicEntryPlugin(compiler.context, async () => {
const theEntries = await Promise.all(getAllEntries(entries, compiler))
const allEntries = theEntries
Expand All @@ -135,15 +115,14 @@ export default function onDemandEntryHandler(
return allEntries
}).apply(compiler)

IS_WEBPACK_5 &&
webpack5Experiential &&
compiler.hooks.make.intercept({
register(tap) {
const initialFn = tap.fn
const cb = (compilation, callback) => {
const res = initialFn(compilation, callback)
if (tap.name === 'DynamicEntryPlugin') {
invalidator.startBuilding()
callbackSet.push(res)
}
return res
}
Expand All @@ -152,24 +131,7 @@ export default function onDemandEntryHandler(
},
})

if (compiler.name === 'client') {
compiler.hooks.additionalPass.tap('NextJsOnDemandEntries', () => {
multiStepBuild.additionalPass()
clearTimeout(multiStepBuild.timeout)
})
}
compiler.hooks.afterDone.tap('NextJsOnDemandEntries', () => {
if (callbackSet.length) {
// additionalPass initiated.
multiStepBuild.promises[0].then(() => {
clearInterval(multiStepBuild.timeout)
multiStepBuild.timeout = setTimeout(() => {
multiStepBuild.done()
}, 600)
})
}
})
!IS_WEBPACK_5 &&
!webpack5Experiential &&
compiler.hooks.make.tapPromise(
'NextJsOnDemandEntries',
(compilation: webpack.compilation.Compilation) => {
Expand Down Expand Up @@ -261,13 +223,6 @@ export default function onDemandEntryHandler(
}

invalidator.doneBuilding()
clearTimeout(multiStepBuild.timeout)

Promise.all([multiStepBuild.promises[0], multiStepBuild.promises[1]]).then(
() => {
multiStepBuild.lastDoneEvent()
}
)

if (hardFailedPages.length > 0 && !reloading) {
console.log(
Expand Down Expand Up @@ -420,12 +375,7 @@ export default function onDemandEntryHandler(

function handleCallback(err: Error) {
if (err) return reject(err)
Promise.all(multiStepBuild.promises).then(() => {
Promise.all(callbackSet).then(() => {
callbackSet = []
resolve()
})
})
resolve()
}
})
},
Expand All @@ -449,6 +399,7 @@ export default function onDemandEntryHandler(
})
} else {
if (!/^\/_next\/webpack-hmr/.test(req.url!)) return next()

const { query } = parse(req.url!, true)
const page = query.page
if (!page) return next()
Expand Down