Skip to content

Commit 5d1759f

Browse files
committed
modified import context filtering logic to filter on file names.
1 parent 4c08560 commit 5d1759f

8 files changed

Lines changed: 38 additions & 13 deletions

File tree

‎examples/code-splitting-native-import-context-filter/example.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
async function getTemplate(templateName) {
22
try {
33
let template = await import(
4-
/* webpackFilter: "^(?:[^.]*(?:\\.(?!noimport(?:\\.js)?$))?)*$" */
4+
/* webpackFilter: "\\.js$" */
5+
/* webpackExclude: "\\.noimport\\.js$" */
56
`./templates/${templateName}`
67
);
78
console.log(template);

‎lib/ContextModule.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const DepBlockHelpers = require("./dependencies/DepBlockHelpers");
1212
const Template = require("./Template");
1313

1414
class ContextModule extends Module {
15-
constructor(resolveDependencies, context, recursive, regExp, addon, asyncMode, chunkName) {
15+
constructor(resolveDependencies, context, recursive, regExp, addon, asyncMode, chunkName, contextDependency) {
1616
super();
1717
this.resolveDependencies = resolveDependencies;
1818
this.context = context;
@@ -24,6 +24,7 @@ class ContextModule extends Module {
2424
this.contextDependencies = [context];
2525
this.built = false;
2626
this.chunkName = chunkName;
27+
this.contextDependency = contextDependency;
2728
}
2829

2930
prettyRegExp(regexString) {
@@ -102,7 +103,7 @@ class ContextModule extends Module {
102103
build(options, compilation, resolver, fs, callback) {
103104
this.built = true;
104105
this.builtTime = Date.now();
105-
this.resolveDependencies(fs, this.context, this.recursive, this.regExp, (err, dependencies) => {
106+
this.resolveDependencies(fs, this.context, this.recursive, this.regExp, this.contextDependency, (err, dependencies) => {
106107
if(err) return callback(err);
107108

108109
// Reset children

‎lib/ContextModuleFactory.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,20 +92,20 @@ module.exports = class ContextModuleFactory extends Tapable {
9292
// Ignored
9393
if(!result) return callback();
9494

95-
return callback(null, new ContextModule(result.resolveDependencies, result.resource, result.recursive, result.regExp, result.loaders, result.async, dependency.chunkName));
95+
return callback(null, new ContextModule(result.resolveDependencies, result.resource, result.recursive, result.regExp, result.loaders, result.async, dependency.chunkName, dependency));
9696
});
9797
});
9898
});
9999
}
100100

101-
resolveDependencies(fs, resource, recursive, regExp, callback) {
101+
resolveDependencies(fs, resource, recursive, regExp, contextDependency, callback) {
102102
const cmf = this;
103103
if(!regExp || !resource)
104104
return callback(null, []);
105105
(function addDirectory(directory, callback) {
106106
fs.readdir(directory, (err, files) => {
107107
if(err) return callback(err);
108-
files = cmf.applyPluginsWaterfall("context-module-files", files);
108+
files = cmf.applyPluginsWaterfall("context-module-files", files, contextDependency);
109109
if(!files || files.length === 0) return callback(null, []);
110110
asyncLib.map(files.filter(function(p) {
111111
return p.indexOf(".") !== 0;

‎lib/dependencies/ContextDependencyHelpers.js‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function quotemeta(str) {
1515
return str.replace(/[-[\]\\/{}()*+?.^$|]/g, "\\$&");
1616
}
1717

18-
ContextDependencyHelpers.create = function(Dep, range, param, expr, options, chunkName, filter) {
18+
ContextDependencyHelpers.create = function(Dep, range, param, expr, options, chunkName) {
1919
let dep;
2020
let prefix;
2121
let postfix;
@@ -35,9 +35,8 @@ ContextDependencyHelpers.create = function(Dep, range, param, expr, options, chu
3535
context = prefix.substr(0, idx);
3636
prefix = `.${prefix.substr(idx)}`;
3737
}
38-
// If filter exists, don't attempt to merge prefix/postfix with filter. we've already pulled out the context
3938
// If there are more than two quasis, maybe the generated RegExp can be more precise?
40-
regExp = filter || new RegExp(`^${quotemeta(prefix)}${options.wrappedContextRegExp.source}${quotemeta(postfix)}$`);
39+
regExp = new RegExp(`^${quotemeta(prefix)}${options.wrappedContextRegExp.source}${quotemeta(postfix)}$`);
4140
dep = new Dep(context, options.wrappedContextRecursive, regExp, range, valueRange, chunkName);
4241
dep.loc = expr.loc;
4342
dep.replaces = [{
@@ -57,8 +56,7 @@ ContextDependencyHelpers.create = function(Dep, range, param, expr, options, chu
5756
context = prefix.substr(0, idx);
5857
prefix = `.${prefix.substr(idx)}`;
5958
}
60-
// If filter exists, don't attempt to merge prefix/postfix with filter. we've already pulled out the context
61-
regExp = filter || new RegExp(`^${quotemeta(prefix)}${options.wrappedContextRegExp.source}${quotemeta(postfix)}$`);
59+
regExp = new RegExp(`^${quotemeta(prefix)}${options.wrappedContextRegExp.source}${quotemeta(postfix)}$`);
6260
dep = new Dep(context, options.wrappedContextRecursive, regExp, range, valueRange, chunkName);
6361
dep.loc = expr.loc;
6462
dep.prepend = param.prefix && param.prefix.isString() ? prefix : null;

‎lib/dependencies/ImportContextDependency.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class ImportContextDependency extends ContextDependency {
1212
this.range = range;
1313
this.valueRange = valueRange;
1414
this.chunkName = chunkName;
15+
this.filter = null;
16+
this.exclude = null;
1517
}
1618

1719
get type() {

‎lib/dependencies/ImportParserPlugin.js‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class ImportParserPlugin {
3131
let chunkName = null;
3232
let mode = "lazy";
3333
let filter = null;
34+
let exclude = null;
3435

3536
const importOptions = parser.getCommentOptions(expr.range);
3637
if(importOptions) {
@@ -53,6 +54,13 @@ class ImportParserPlugin {
5354
filter = new RegExp(importOptions.webpackFilter);
5455
}
5556
}
57+
if(typeof importOptions.webpackExclude !== "undefined") {
58+
if(typeof importOptions.webpackExclude !== "string") {
59+
parser.state.module.warnings.push(new UnsupportedFeatureWarning(parser.state.module, `\`webpackExclude\` expected a string, but received: ${importOptions.webpackExclude}.`));
60+
} else {
61+
exclude = new RegExp(importOptions.webpackExclude);
62+
}
63+
}
5664
}
5765

5866
if(param.isString()) {
@@ -84,10 +92,12 @@ class ImportParserPlugin {
8492
} else if(mode === "lazy-once") {
8593
Dep = ImportLazyOnceContextDependency;
8694
}
87-
const dep = ContextDependencyHelpers.create(Dep, expr.range, param, expr, options, chunkName, filter);
95+
const dep = ContextDependencyHelpers.create(Dep, expr.range, param, expr, options, chunkName);
8896
if(!dep) return;
8997
dep.loc = expr.loc;
9098
dep.optional = !!parser.scope.inTry;
99+
dep.filter = filter;
100+
dep.exclude = exclude;
91101
parser.state.current.addDependency(dep);
92102
return true;
93103
}

‎lib/dependencies/ImportPlugin.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
"use strict";
66

7+
const ImportContextDependency = require("./ImportContextDependency");
78
const ImportDependency = require("./ImportDependency");
89
const ImportEagerDependency = require("./ImportEagerDependency");
910
const ImportWeakDependency = require("./ImportWeakDependency");
@@ -54,6 +55,18 @@ class ImportPlugin {
5455
new ImportParserPlugin(options)
5556
);
5657
});
58+
59+
contextModuleFactory.plugin("context-module-files", (files, contextDependency) => {
60+
if(contextDependency instanceof ImportContextDependency) {
61+
if(contextDependency.filter) {
62+
files = files.filter(file => !!file.match(contextDependency.filter));
63+
}
64+
if(contextDependency.exclude) {
65+
files = files.filter(file => !file.match(contextDependency.exclude));
66+
}
67+
}
68+
return files;
69+
});
5770
});
5871
}
5972
}

‎test/statsCases/import-context-filter/expected.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ entry.js 6.82 kB 3 [emitted] entry
99
[1] (webpack)/test/statsCases/import-context-filter/templates/baz.js 38 bytes {1} [optional] [built]
1010
[2] (webpack)/test/statsCases/import-context-filter/templates/foo.js 38 bytes {0} [optional] [built]
1111
[3] (webpack)/test/statsCases/import-context-filter/entry.js 444 bytes {3} [built]
12-
[4] (webpack)/test/statsCases/import-context-filter/templates lazy ^(?:[^.]*(?:\.(?!noimport(?:\.js)?$))?)*$ 160 bytes {3} [optional] [built]
12+
[4] (webpack)/test/statsCases/import-context-filter/templates lazy ^\.\/.*$ 160 bytes {3} [optional] [built]

0 commit comments

Comments
 (0)