WatchMissingNodeModulesPlugin.js 1.1 KB

123456789101112131415161718192021222324252627282930313233
  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. // This webpack plugin ensures `npm install <library>` forces a project rebuild.
  8. // We’re not sure why this isn't webpack's default behavior.
  9. // See https://github.com/facebook/create-react-app/issues/186.
  10. 'use strict';
  11. class WatchMissingNodeModulesPlugin {
  12. constructor(nodeModulesPath) {
  13. this.nodeModulesPath = nodeModulesPath;
  14. }
  15. apply(compiler) {
  16. compiler.hooks.emit.tap('WatchMissingNodeModulesPlugin', compilation => {
  17. var missingDeps = Array.from(compilation.missingDependencies);
  18. var nodeModulesPath = this.nodeModulesPath;
  19. // If any missing files are expected to appear in node_modules...
  20. if (missingDeps.some(file => file.includes(nodeModulesPath))) {
  21. // ...tell webpack to watch node_modules recursively until they appear.
  22. compilation.contextDependencies.add(nodeModulesPath);
  23. }
  24. });
  25. }
  26. }
  27. module.exports = WatchMissingNodeModulesPlugin;