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