Skip to content

Commit 12108bd

Browse files
committed
First version of WatchPackageJsonPlugin. It takes care of recompiling erroneous files when package.json is changed, because it might resolve the issue.
1 parent 2a29503 commit 12108bd

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use strict';
2+
3+
// This Webpack plugin ensures that package.json is watched for changes and
4+
// that appropriate actions are triggered, e.g. an eslint-loader recheck.
5+
6+
class WatchPackageJsonPlugin {
7+
constructor(packageJsonPath) {
8+
this.packageJsonPath = packageJsonPath;
9+
this.erroneousFiles = [];
10+
}
11+
12+
apply(compiler) {
13+
compiler.plugin('compilation', compilation => {
14+
const timestamp = compilation.fileTimestamps[this.packageJsonPath] || 0;
15+
16+
if (timestamp > this.previousTimestamp && this.erroneousFiles.length) {
17+
this.erroneousFiles.forEach(filename => {
18+
compilation.fileTimestamps[filename] = timestamp;
19+
});
20+
}
21+
});
22+
23+
compiler.plugin('emit', (compilation, callback) => {
24+
// Add package.json to the list of watched files. This needs to be done
25+
// for every compilation run since the list is rebuilt every time.
26+
compilation.fileDependencies.push(this.packageJsonPath);
27+
28+
this.previousTimestamp = compilation.fileTimestamps[
29+
this.packageJsonPath
30+
] || 0;
31+
32+
// First we extract all files related to any occurred errors. Then
33+
// we remove any request params that could have been added by a plugin,
34+
// loader or the user.
35+
this.erroneousFiles = compilation.errors
36+
.reduce(
37+
(acc, error) => {
38+
acc.push.apply(acc, error.dependencies);
39+
40+
return acc;
41+
},
42+
[]
43+
)
44+
.map(entry => entry.request.replace(/\?.*$/));
45+
46+
callback();
47+
});
48+
}
49+
}
50+
51+
module.exports = WatchPackageJsonPlugin;

packages/react-dev-utils/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"openChrome.applescript",
2424
"prompt.js",
2525
"WatchMissingNodeModulesPlugin.js",
26+
"WatchPackageJsonPlugin.js",
2627
"webpackHotDevClient.js"
2728
],
2829
"dependencies": {

packages/react-scripts/config/webpack.config.dev.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const HtmlWebpackPlugin = require('html-webpack-plugin');
1616
const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
1717
const InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
1818
const WatchMissingNodeModulesPlugin = require('react-dev-utils/WatchMissingNodeModulesPlugin');
19+
const WatchPackageJsonPlugin = require('react-dev-utils/WatchPackageJsonPlugin');
1920
const getClientEnvironment = require('./env');
2021
const paths = require('./paths');
2122

@@ -244,6 +245,9 @@ module.exports = {
244245
// makes the discovery automatic so you don't have to restart.
245246
// See https://github.com/facebookincubator/create-react-app/issues/186
246247
new WatchMissingNodeModulesPlugin(paths.appNodeModules),
248+
// This Webpack plugin ensures that package.json is watched for changes and
249+
// that appropriate actions are triggered, e.g. an eslint-loader recheck.
250+
new WatchPackageJsonPlugin(paths.appPackageJson),
247251
],
248252
// Some libraries import Node modules but don't use them in the browser.
249253
// Tell Webpack to provide empty mocks for them so importing them works.

0 commit comments

Comments
 (0)