Skip to content

Commit

Permalink
fix: compatibility with webpack v5
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Nov 7, 2022
1 parent c881af8 commit cecb3dd
Show file tree
Hide file tree
Showing 11 changed files with 10,301 additions and 12,702 deletions.
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ Next, enable hot reloading in your webpack config:
1. Add the following plugins to the `plugins` array:
```js
plugins: [
// OccurrenceOrderPlugin is needed for webpack 1.x only
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.HotModuleReplacementPlugin(),
// Use NoErrorsPlugin for webpack 1.x
new webpack.NoEmitOnErrorsPlugin()
]
```

Expand All @@ -47,7 +43,7 @@ Now add the middleware into your server:
var compiler = webpack(webpackConfig);

app.use(require("webpack-dev-middleware")(compiler, {
noInfo: true, publicPath: webpackConfig.output.publicPath
/* Options */
}));
```

Expand Down
2 changes: 1 addition & 1 deletion example/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ app.style.verticalAlign = 'middle';

// Uncomment this next line to trigger a warning
// require('Assert')
require('assert');
// require('assert');

if (module.hot) {
module.hot.accept();
Expand Down
11,132 changes: 3,299 additions & 7,833 deletions example/package-lock.json

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
"console-stamp": "^0.2.9",
"express": "^4.17.1",
"morgan": "^1.10.0",
"webpack": "^4.46.0",
"webpack-dev-middleware": "^3.7.3",
"webpack": "^5.74.0",
"webpack-dev-middleware": "^5.3.3",
"webpack-hot-middleware": "file:.."
},
"scripts": {
"start:base": "WEBPACK_CONFIG=./webpack.config.js node server.js",
"start:multientry": "WEBPACK_CONFIG=./webpack.config.multientry.js node server.js"
}
}
1 change: 0 additions & 1 deletion example/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ app.use(require('morgan')('short'));
// Step 2: Attach the dev middleware to the compiler & the server
app.use(
require('webpack-dev-middleware')(compiler, {
logLevel: 'warn',
publicPath: webpackConfig.output.publicPath,
})
);
Expand Down
7 changes: 2 additions & 5 deletions example/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ module.exports = {
publicPath: '/',
filename: 'bundle.js',
},
devtool: '#source-map',
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NoEmitOnErrorsPlugin(),
],
devtool: 'source-map',
plugins: [new webpack.HotModuleReplacementPlugin()],
};
8 changes: 2 additions & 6 deletions example/webpack.config.multientry.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ module.exports = {
publicPath: '/',
filename: '[name].js',
},
devtool: '#source-map',
plugins: [
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.HotModuleReplacementPlugin(),
new webpack.NoEmitOnErrorsPlugin(),
],
devtool: 'source-map',
plugins: [new webpack.HotModuleReplacementPlugin()],
};
22 changes: 20 additions & 2 deletions middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ function publishStats(action, statsResult, eventStream, log) {
modules: true,
timings: true,
hash: true,
errors: true,
warnings: true,
};

var bundles = [];
Expand Down Expand Up @@ -156,18 +158,34 @@ function publishStats(action, statsResult, eventStream, log) {
'ms'
);
}

eventStream.publish({
name: name,
action: action,
time: stats.time,
hash: stats.hash,
warnings: stats.warnings || [],
errors: stats.errors || [],
warnings: formatErrors(stats.warnings || []),
errors: formatErrors(stats.errors || []),
modules: buildModuleMap(stats.modules),
});
});
}

function formatErrors(errors) {
if (!errors || !errors.length) {
return [];
}

if (typeof errors[0] === 'string') {
return errors;
}

// Convert webpack@5 error info into a backwards-compatible flat string
return errors.map(function (error) {
return error.moduleName + ' ' + error.loc + '\n' + error.message;
});
}

function normalizeStats(stats, statsOptions) {
var statsJson = stats.toJson(statsOptions);

Expand Down
Loading

0 comments on commit cecb3dd

Please sign in to comment.