Skip to content

Commit 12b7c91

Browse files
committed
Rebuild project on package update (facebook#2956)
1 parent 5348d6e commit 12b7c91

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
// This Webpack plugin ensures that any change in node_modules package.json forces a project rebuild.
9+
10+
'use strict';
11+
12+
var fs = require('fs');
13+
14+
var timeout;
15+
16+
class WatchChangedNodeModulesPlugin {
17+
constructor(nodeModulesPath) {
18+
this.nodeModulesPath = nodeModulesPath;
19+
}
20+
21+
apply(compiler) {
22+
fs.watch(
23+
this.nodeModulesPath,
24+
{ persistent: false, recursive: true },
25+
() => {
26+
clearTimeout(timeout);
27+
timeout = setTimeout(() => compiler.run(() => {}), 1000);
28+
}
29+
);
30+
}
31+
}
32+
33+
module.exports = WatchChangedNodeModulesPlugin;

packages/react-dev-utils/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"openChrome.applescript",
3434
"printHostingInstructions.js",
3535
"WatchMissingNodeModulesPlugin.js",
36+
"WatchChangedNodeModulesPlugin.js",
3637
"WebpackDevServerUtils.js",
3738
"webpackHotDevClient.js"
3839
],

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const HtmlWebpackPlugin = require('html-webpack-plugin');
1515
const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
1616
const InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
1717
const WatchMissingNodeModulesPlugin = require('react-dev-utils/WatchMissingNodeModulesPlugin');
18+
const WatchChangedNodeModulesPlugin = require('react-dev-utils/WatchChangedNodeModulesPlugin');
1819
const eslintFormatter = require('react-dev-utils/eslintFormatter');
1920
const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin');
2021
const getClientEnvironment = require('./env');
@@ -333,11 +334,16 @@ module.exports = {
333334
// a plugin that prints an error when you attempt to do this.
334335
// See https://github.com/facebook/create-react-app/issues/240
335336
new CaseSensitivePathsPlugin(),
336-
// If you require a missing module and then `npm install` it, you still have
337+
// If you require a missing module and then `yarn add` it, you still have
337338
// to restart the development server for Webpack to discover it. This plugin
338339
// makes the discovery automatic so you don't have to restart.
339340
// See https://github.com/facebook/create-react-app/issues/186
340341
new WatchMissingNodeModulesPlugin(paths.appNodeModules),
342+
// If you upgrade package version, you still have to restart the development
343+
// server for Webpack to discover it. This plugin makes the discovery automatic
344+
// so you don't have to restart.
345+
// and https://github.com/facebook/create-react-app/issues/2956
346+
new WatchChangedNodeModulesPlugin(paths.appNodeModules),
341347
// Moment.js is an extremely popular library that bundles large locale files
342348
// by default due to how Webpack interprets its code. This is a practical
343349
// solution that requires the user to opt into importing specific locales.

0 commit comments

Comments
 (0)