Skip to content

Commit

Permalink
Merge pull request #75 from mcous/refactor_replace-whitelist
Browse files Browse the repository at this point in the history
Replace "whitelist" option with "allowlist"
  • Loading branch information
liady committed Jul 8, 2020
2 parents e1192f8 + f2d7565 commit c06c0f4
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ This library scans the `node_modules` folder for all node_modules names, and bui
### Configuration
This library accepts an `options` object.

#### `options.whitelist (=[])`
An array for the `externals` to whitelist, so they **will** be included in the bundle. Can accept exact strings (`'module_name'`), regex patterns (`/^module_name/`), or a function that accepts the module name and returns whether it should be included.
<br/>**Important** - if you have set aliases in your webpack config with the exact same names as modules in *node_modules*, you need to whitelist them so Webpack will know they should be bundled.
#### `options.allowlist (=[])`
An array for the `externals` to allow, so they **will** be included in the bundle. Can accept exact strings (`'module_name'`), regex patterns (`/^module_name/`), or a function that accepts the module name and returns whether it should be included.
<br/>**Important** - if you have set aliases in your webpack config with the exact same names as modules in *node_modules*, you need to allowlist them so Webpack will know they should be bundled.

#### `options.importType (='commonjs')`
The method in which unbundled modules will be required in the code. Best to leave as `commonjs` for node modules.
Expand Down Expand Up @@ -75,7 +75,7 @@ module.exports = {
target: 'node', // important in order not to bundle built-in modules like path, fs, etc.
externals: [nodeExternals({
// this WILL include `jquery` and `webpack/hot/dev-server` in the bundle, as well as `lodash/*`
whitelist: ['jquery', 'webpack/hot/dev-server', /^lodash/]
allowlist: ['jquery', 'webpack/hot/dev-server', /^lodash/]
})],
...
};
Expand All @@ -99,12 +99,12 @@ However, this will leave unbundled **all non-relative requires**, so it does not
This library scans the `node_modules` folder, so it only leaves unbundled the actual node modules that are being used.

#### How can I bundle required assets (i.e css files) from node_modules?
Using the `whitelist` option, this is possible. We can simply tell Webpack to bundle all files with extensions that are not js/jsx/json, using this [regex](https://regexper.com/#%5C.(%3F!(%3F%3Ajs%7Cjson)%24).%7B1%2C5%7D%24):
Using the `allowlist` option, this is possible. We can simply tell Webpack to bundle all files with extensions that are not js/jsx/json, using this [regex](https://regexper.com/#%5C.(%3F!(%3F%3Ajs%7Cjson)%24).%7B1%2C5%7D%24):
```js
...
nodeExternals({
// load non-javascript files with extensions, presumably via loaders
whitelist: [/\.(?!(?:jsx?|json)$).{1,5}$/i],
allowlist: [/\.(?!(?:jsx?|json)$).{1,5}$/i],
}),
...
```
Expand Down
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function getModuleName(request, includeAbsolutePaths) {

module.exports = function nodeExternals(options) {
options = options || {};
var whitelist = [].concat(options.whitelist || []);
var allowlist = [].concat(options.allowlist || []);
var binaryDirs = [].concat(options.binaryDirs || ['.bin']);
var importType = options.importType || 'commonjs';
var modulesDir = options.modulesDir || 'node_modules';
Expand All @@ -38,7 +38,7 @@ module.exports = function nodeExternals(options) {
// return an externals function
return function(context, request, callback){
var moduleName = getModuleName(request, includeAbsolutePaths);
if (utils.contains(nodeModules, moduleName) && !utils.containsPattern(whitelist, request)) {
if (utils.contains(nodeModules, moduleName) && !utils.containsPattern(allowlist, request)) {
if (typeof importType === 'function') {
return callback(null, importType(request));
}
Expand All @@ -48,4 +48,4 @@ module.exports = function nodeExternals(options) {
};
callback();
}
};
};
8 changes: 4 additions & 4 deletions test/library.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,13 @@ describe('reads from a file', function() {
});
});

// Test whitelist
describe('respects a whitelist', function() {
// Test allowlist
describe('respects a allowlist', function() {

before(function(){
mockNodeModules();
context.instance = nodeExternals({
whitelist: ['moduleA/sub-module', 'moduleA/another-sub/index.js', 'moduleC', function (m) {
allowlist: ['moduleA/sub-module', 'moduleA/another-sub/index.js', 'moduleC', function (m) {
return m == 'moduleF';
}, /^moduleD/]
});
Expand Down Expand Up @@ -244,4 +244,4 @@ describe('when modules dir does not exist', function() {
after(function(){
restoreMock()
});
})
})
6 changes: 3 additions & 3 deletions test/webpack.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ describe('actual webpack bundling', function() {

describe('basic tests', function() {
it('should output modules without bundling', webpackAssertion({}, ['module-a', 'module-b'], ['module-c']));
it('should honor a whitelist', webpackAssertion({ whitelist: ['module-a'] }, ['module-b'], ['module-a', 'module-c']));
it('should honor a allowlist', webpackAssertion({ allowlist: ['module-a'] }, ['module-b'], ['module-a', 'module-c']));
});

describe('with webpack aliased module in node_modules', function() {
before(function() {
return testUtils.copyModules(['module-c']);
});
it('should bundle aliased modules', webpackAssertion({ whitelist: ['module-c'] }, ['module-a', 'module-b'], ['module-c']));
it('should bundle aliased modules', webpackAssertion({ allowlist: ['module-c'] }, ['module-a', 'module-b'], ['module-c']));
})

after(function() {
testUtils.removeModules(['module-a', 'module-b', 'module-c']);
});
});
});

0 comments on commit c06c0f4

Please sign in to comment.