Skip to content

Commit

Permalink
Update webpack to v3 (#1329)
Browse files Browse the repository at this point in the history
  • Loading branch information
frenzzy authored Jun 26, 2017
1 parent 5c0e011 commit 42ec072
Show file tree
Hide file tree
Showing 4 changed files with 284 additions and 134 deletions.
33 changes: 20 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"babel-cli": "^6.24.1",
"babel-core": "^6.25.0",
"babel-eslint": "^7.2.3",
"babel-loader": "^7.0.0",
"babel-loader": "^7.1.0",
"babel-plugin-istanbul": "^4.1.4",
"babel-plugin-rewire": "^1.1.0",
"babel-preset-env": "^1.5.2",
Expand All @@ -63,20 +63,19 @@
"cross-env": "^5.0.1",
"css-loader": "^0.28.4",
"editorconfig-tools": "^0.1.1",
"enzyme": "^2.8.2",
"enzyme": "^2.9.1",
"eslint": "^3.19.0",
"eslint-config-airbnb": "^15.0.1",
"eslint-import-resolver-node": "^0.3.0",
"eslint-loader": "^1.7.1",
"eslint-import-resolver-node": "^0.3.1",
"eslint-loader": "^1.8.0",
"eslint-plugin-css-modules": "^2.7.1",
"eslint-plugin-import": "^2.3.0",
"eslint-plugin-import": "^2.6.0",
"eslint-plugin-jsx-a11y": "^5.0.3",
"eslint-plugin-react": "^7.0.1",
"file-loader": "^0.11.2",
"front-matter": "^2.1.2",
"glob": "^7.1.2",
"json-loader": "^0.5.4",
"lint-staged": "^3.6.1",
"lint-staged": "^4.0.0",
"markdown-it": "^8.3.1",
"mkdirp": "^0.5.1",
"mocha": "^3.4.2",
Expand All @@ -85,15 +84,15 @@
"opn-cli": "^3.1.0",
"pixrem": "^3.0.2",
"pleeease-filters": "^4.0.0",
"postcss": "^6.0.1",
"postcss": "^6.0.3",
"postcss-calc": "^6.0.0",
"postcss-color-function": "^4.0.0",
"postcss-custom-media": "^6.0.0",
"postcss-custom-properties": "^6.0.1",
"postcss-custom-selectors": "^4.0.1",
"postcss-flexbugs-fixes": "^3.0.0",
"postcss-import": "^10.0.0",
"postcss-loader": "^2.0.5",
"postcss-loader": "^2.0.6",
"postcss-media-minmax": "^3.0.0",
"postcss-nested": "^2.0.2",
"postcss-nesting": "^4.0.1",
Expand All @@ -111,10 +110,11 @@
"stylelint": "^7.11.0",
"stylelint-config-standard": "^16.0.0",
"stylelint-order": "^0.5.0",
"url-loader": "^0.5.8",
"webpack": "^2.6.1",
"svg-url-loader": "^2.0.2",
"url-loader": "^0.5.9",
"webpack": "^3.0.0",
"webpack-bundle-analyzer": "^2.8.2",
"webpack-dev-middleware": "^1.10.2",
"webpack-dev-middleware": "^1.11.0",
"webpack-hot-middleware": "^2.18.0",
"webpack-node-externals": "^1.6.0"
},
Expand All @@ -138,7 +138,14 @@
"rewire"
]
}
}
},
"ignore": [
"/node_modules/",
"/build/chunks/",
"/build/public/",
"/build/updates/",
"/build/server.js"
]
},
"lint-staged": {
"*.{cmd,html,json,md,sh,txt,xml,yml}": [
Expand Down
42 changes: 26 additions & 16 deletions tools/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import path from 'path';
import express from 'express';
import browserSync from 'browser-sync';
import webpack from 'webpack';
import logApplyResult from 'webpack/hot/log-apply-result';
import webpackDevMiddleware from 'webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';
import createLaunchEditorMiddleware from 'react-error-overlay/middleware';
Expand All @@ -24,7 +23,7 @@ const isDebug = !process.argv.includes('--release');
// https://webpack.js.org/configuration/watch/#watchoptions
const watchOptions = {
// Watching may not work with NFS and machines in VirtualBox
// Uncomment next line if it's your case (use true or interval in milliseconds)
// Uncomment next line if it is your case (use true or interval in milliseconds)
// poll: true,

// Decrease CPU or memory usage in some file systems
Expand Down Expand Up @@ -75,11 +74,12 @@ async function start() {
clientConfig.output.filename = clientConfig.output.filename.replace('chunkhash', 'hash');
clientConfig.output.chunkFilename = clientConfig.output.chunkFilename.replace('chunkhash', 'hash');
clientConfig.module.rules = clientConfig.module.rules.filter(x => x.loader !== 'null-loader');
const { query } = clientConfig.module.rules.find(x => x.loader === 'babel-loader');
query.plugins = ['react-hot-loader/babel'].concat(query.plugins || []);
const { options } = clientConfig.module.rules.find(x => x.loader === 'babel-loader');
options.plugins = ['react-hot-loader/babel'].concat(options.plugins || []);
clientConfig.plugins.push(
new webpack.HotModuleReplacementPlugin(),
new webpack.NoEmitOnErrorsPlugin(),
new webpack.NamedModulesPlugin(),
);

// Configure server-side hot module replacement
Expand Down Expand Up @@ -126,26 +126,36 @@ async function start() {
});

function checkForUpdate(fromUpdate) {
return app.hot.check().then((updatedModules) => {
if (updatedModules) {
return app.hot.apply().then((renewedModules) => {
logApplyResult(updatedModules, renewedModules);
checkForUpdate(true);
});
const hmrPrefix = '[\x1b[35mHMR\x1b[0m] ';
if (!app.hot) {
throw new Error(`${hmrPrefix}Hot Module Replacement is disabled.`);
}
if (app.hot.status() !== 'idle') {
return Promise.resolve();
}
return app.hot.check(true).then((updatedModules) => {
if (!updatedModules) {
if (fromUpdate) {
console.info(`${hmrPrefix}Update applied.`);
}
return;
}
if (fromUpdate) {
return console.info('[HMR] Update applied.');
if (updatedModules.length === 0) {
console.info(`${hmrPrefix}Nothing hot updated.`);
} else {
console.info(`${hmrPrefix}Updated modules:`);
updatedModules.forEach(moduleId => console.info(`${hmrPrefix} - ${moduleId}`));
checkForUpdate(true);
}
return console.warn('[HMR] Cannot find update.');
}).catch((error) => {
if (['abort', 'fail'].includes(app.hot.status())) {
console.warn('[HMR] Cannot apply update.');
console.warn(`${hmrPrefix}Cannot apply update.`);
delete require.cache[require.resolve('../build/server')];
// eslint-disable-next-line global-require, import/no-unresolved
app = require('../build/server').default;
console.warn('[HMR] App has been reloaded.');
console.warn(`${hmrPrefix}App has been reloaded.`);
} else {
console.warn(`[HMR] Update failed: ${error.stack || error.message}`);
console.warn(`${hmrPrefix}Update failed: ${error.stack || error.message}`);
}
});
}
Expand Down
Loading

0 comments on commit 42ec072

Please sign in to comment.