-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ExcludeModulePlugin (#381)
* feat: add ExcludeModulePlugin * fix: add ExcludeModulePlugin info to README
- Loading branch information
Showing
8 changed files
with
125 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const pluginCompat = require('./util/plugin-compat'); | ||
|
||
const matchTest = (test, source) => { | ||
if (Array.isArray(test)) { | ||
return test.some(subtest => matchTest(subtest, source)); | ||
} else if (test instanceof RegExp) { | ||
return test.test(source); | ||
} else if (typeof test === 'string') { | ||
return source.startsWith(test); | ||
} else if (typeof test === 'function') { | ||
return test(source); | ||
} | ||
return false; | ||
}; | ||
|
||
const matchOne = ({ test, include, exclude }, source) => { | ||
return ( | ||
(test ? matchTest(test, source) : true) && | ||
(include ? matchTest(include, source) : true) && | ||
(exclude ? !matchTest(exclude, source) : true) | ||
); | ||
}; | ||
|
||
const matchAny = (test, source) => { | ||
if (Array.isArray(test)) { | ||
return test.some(subtest => matchOne(subtest, source)); | ||
} | ||
return matchOne(test, source); | ||
}; | ||
|
||
class ExcludeModulePlugin { | ||
constructor(match) { | ||
this.match = match; | ||
} | ||
|
||
apply(compiler) { | ||
const compilerHooks = pluginCompat.hooks(compiler); | ||
|
||
compilerHooks.afterPlugins.tap('HardSource - ExcludeModulePlugin', () => { | ||
compilerHooks._hardSourceAfterFreezeModule.tap( | ||
'HardSource - ExcludeModulePlugin', | ||
(frozen, module, extra) => { | ||
if (matchAny(this.match, module.identifier())) { | ||
return null; | ||
} | ||
return frozen; | ||
}, | ||
); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = ExcludeModulePlugin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = function(n) { | ||
return n + (n > 0 ? n - 1 : 0); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var fib = require('./fib'); | ||
|
||
console.log(fib(3)); |
21 changes: 21 additions & 0 deletions
21
tests/fixtures/hard-source-exclude-plugin/webpack.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
var HardSourceWebpackPlugin = require('../../..'); | ||
|
||
module.exports = { | ||
context: __dirname, | ||
entry: './index.js', | ||
output: { | ||
path: __dirname + '/tmp', | ||
filename: 'main.js', | ||
}, | ||
plugins: [ | ||
new HardSourceWebpackPlugin({ | ||
cacheDirectory: 'cache', | ||
environmentHash: { | ||
root: __dirname + '/../../..', | ||
}, | ||
}), | ||
new HardSourceWebpackPlugin.ExcludeModulePlugin({ | ||
test: /fib\.js$/, | ||
}), | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters