Repository URL to install this package:
|
Version:
1.2.10 ▾
|
import {
HotModuleReplacementPlugin,
WatchIgnorePlugin,
NamedModulesPlugin,
} from 'webpack'
import { resolveToRoot } from '../../../config/resolveToRoot'
import { Configuration, Options } from '../../../typings'
const IS_WEBPACK4 = true
const IS_BABEL_CACHE_ENABLED = false
const IGNORE_SOME = process.env.BABEL_CACHE || IS_BABEL_CACHE_ENABLED
/**
* @see https://github.com/webpack/webpack/issues/1151#issuecomment-138809417
* @see https://github.com/insin/nwb/blob/master/docs/Configuration.md#webpack-configuration
* @see http://stackoverflow.com/questions/32296967/webpack-dev-server-doesnt-generate-source-maps
* @see https://webpack.github.io/docs/configuration.html#devtool
* @see https://github.com/gaearon/react-hot-loader/blob/master/docs/Troubleshooting.md
* @see https://github.com/glenjamin/ultimate-hot-reloading-example
* @see https://hackernoon.com/hot-reload-all-the-things-ec0fed8ab0
* @see https://medium.com/@rajaraodv/webpacks-hmr-react-hot-loader-the-missing-manual-232336dc0d96
*/
export default function hmr(config: Configuration, options: Options) {
const { isDev, isServer } = options
if (IS_WEBPACK4) {
// @todo improve
config.mode = isDev ? 'development' : 'production'
}
if (isServer) {
// ignoring HMR on server
return
}
if (isDev) {
// webpack v4 - https://github.com/webpack/webpack/issues/6357
config.plugins.push(new NamedModulesPlugin())
// https://github.com/gaearon/react-hot-loader/tree/next
// removed for hmr 3 => 4
// const hotWebpackLoader = {
// // test: /\.tsx?$/,
// test: /\.(j|t)sx?$/,
// use: [
// 'react-hot-loader/webpack',
// // 'babel-loader', 'awesome-typescript-loader'
// ],
// // exclude: /node_modules/,
// // @todo - check HOT
// exclude: /node_modules|modules/,
// }
// config.module.rules.unshift(hotWebpackLoader)
// @see https://github.com/gaearon/react-hot-loader/blob/master/docs/Troubleshooting.md#uncaught-rangeerror-maximum-call-stack-size-exceeded !!!!!!
// @see scripts/development/hotClientServer
// // @todo https://webpack.js.org/configuration/output/#output-hotupdatefunction
const hotModuleReplacementConfig = {
/**
* @see https://github.com/webpack/webpack/issues/2985
* @see https://github.com/webpack/webpack/issues/6693
*/
multiStep: IS_WEBPACK4,
requestTimeout: 1000000,
// fullBuildTimeout: 10000,
}
if (process.env.IS_HMR_ENABLED) {
console.log('[bs] IS_HMR_ENABLED == true')
const plugin = new HotModuleReplacementPlugin(hotModuleReplacementConfig)
config.plugins.unshift(plugin)
// config.plugins.push(plugin)
} else {
console.log('[bs] IS_HMR_ENABLED == false')
}
// @NOTE !!!!!!!!!!!!! ignoring these makes it take 1/2 the time to HMR
const ignorePaths = [
'./modules/',
'./node_modules',
'./views/structured',
'./config',
'./scripts',
// @todo needs more ignoring...
'./client/workers',
].map(resolveToRoot)
// THIS BREAKS WHAT "HOT" does...
if (IGNORE_SOME) {
const ignore = new WatchIgnorePlugin(ignorePaths)
config.plugins.push(ignore)
}
}
}