ExcludeModulePlugin.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. const pluginCompat = require('./util/plugin-compat');
  2. const matchTest = (test, source) => {
  3. if (Array.isArray(test)) {
  4. return test.some(subtest => matchTest(subtest, source));
  5. } else if (test instanceof RegExp) {
  6. return test.test(source);
  7. } else if (typeof test === 'string') {
  8. return source.startsWith(test);
  9. } else if (typeof test === 'function') {
  10. return test(source);
  11. }
  12. return false;
  13. };
  14. const matchOne = ({ test, include, exclude }, source) => {
  15. return (
  16. (test ? matchTest(test, source) : true) &&
  17. (include ? matchTest(include, source) : true) &&
  18. (exclude ? !matchTest(exclude, source) : true)
  19. );
  20. };
  21. const matchAny = (test, source) => {
  22. if (Array.isArray(test)) {
  23. return test.some(subtest => matchOne(subtest, source));
  24. }
  25. return matchOne(test, source);
  26. };
  27. class ExcludeModulePlugin {
  28. constructor(match) {
  29. this.match = match;
  30. }
  31. apply(compiler) {
  32. const compilerHooks = pluginCompat.hooks(compiler);
  33. compilerHooks.afterPlugins.tap('HardSource - ExcludeModulePlugin', () => {
  34. compilerHooks._hardSourceAfterFreezeModule.tap(
  35. 'HardSource - ExcludeModulePlugin',
  36. (frozen, module, extra) => {
  37. if (matchAny(this.match, module.identifier())) {
  38. return null;
  39. }
  40. return frozen;
  41. },
  42. );
  43. });
  44. }
  45. }
  46. module.exports = ExcludeModulePlugin;