Skip to content

Commit fa47a39

Browse files
committed
Add strictExportPresence option
1 parent e618bdc commit fa47a39

File tree

5 files changed

+31
-5
lines changed

5 files changed

+31
-5
lines changed

lib/WebpackOptionsDefaulter.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class WebpackOptionsDefaulter extends OptionsDefaulter {
2727
this.set("module.wrappedContextRegExp", /.*/);
2828
this.set("module.wrappedContextRecursive", true);
2929
this.set("module.wrappedContextCritical", false);
30+
this.set("module.strictExportPresence", false);
3031

3132
this.set("module.unsafeCache", true);
3233

lib/dependencies/HarmonyImportDependencyParserPlugin.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ const HarmonyAcceptDependency = require("./HarmonyAcceptDependency");
1111
const HarmonyModulesHelpers = require("./HarmonyModulesHelpers");
1212

1313
module.exports = class HarmonyImportDependencyParserPlugin {
14+
constructor(moduleOptions) {
15+
this.strictExportPresence = moduleOptions.strictExportPresence;
16+
}
17+
1418
apply(parser) {
1519
parser.plugin("import", (statement, source) => {
1620
const dep = new HarmonyImportDependency(source, HarmonyModulesHelpers.getNewModuleVar(parser.state, source), statement.range);
@@ -29,7 +33,7 @@ module.exports = class HarmonyImportDependencyParserPlugin {
2933
parser.plugin("expression imported var", (expr) => {
3034
const name = expr.name;
3135
const settings = parser.state.harmonySpecifier[`$${name}`];
32-
const dep = new HarmonyImportSpecifierDependency(settings[0], settings[1], settings[2], name, expr.range);
36+
const dep = new HarmonyImportSpecifierDependency(settings[0], settings[1], settings[2], name, expr.range, this.strictExportPresence);
3337
dep.shorthand = parser.scope.inShorthand;
3438
dep.directImport = true;
3539
dep.loc = expr.loc;
@@ -41,7 +45,7 @@ module.exports = class HarmonyImportDependencyParserPlugin {
4145
const settings = parser.state.harmonySpecifier[`$${name}`];
4246
if(settings[2] !== null)
4347
return false;
44-
const dep = new HarmonyImportSpecifierDependency(settings[0], settings[1], expr.property.name || expr.property.value, name, expr.range);
48+
const dep = new HarmonyImportSpecifierDependency(settings[0], settings[1], expr.property.name || expr.property.value, name, expr.range, this.strictExportPresence);
4549
dep.shorthand = parser.scope.inShorthand;
4650
dep.directImport = false;
4751
dep.loc = expr.loc;
@@ -54,7 +58,7 @@ module.exports = class HarmonyImportDependencyParserPlugin {
5458
expr = expr.callee;
5559
const name = expr.name;
5660
const settings = parser.state.harmonySpecifier[`$${name}`];
57-
const dep = new HarmonyImportSpecifierDependency(settings[0], settings[1], settings[2], name, expr.range);
61+
const dep = new HarmonyImportSpecifierDependency(settings[0], settings[1], settings[2], name, expr.range, this.strictExportPresence);
5862
dep.directImport = true;
5963
dep.callArgs = args;
6064
dep.call = fullExpr;

lib/dependencies/HarmonyImportSpecifierDependency.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
const NullDependency = require("./NullDependency");
77

88
class HarmonyImportSpecifierDependency extends NullDependency {
9-
constructor(importDependency, importedVar, id, name, range) {
9+
constructor(importDependency, importedVar, id, name, range, strictExportPresence) {
1010
super();
1111
this.importDependency = importDependency;
1212
this.importedVar = importedVar;
1313
this.id = id;
1414
this.name = name;
1515
this.range = range;
16+
this.strictExportPresence = strictExportPresence;
1617
}
1718

1819
get type() {
@@ -28,6 +29,20 @@ class HarmonyImportSpecifierDependency extends NullDependency {
2829
}
2930

3031
getWarnings() {
32+
if(this.strictExportPresence) {
33+
return [];
34+
}
35+
return this._getErrors();
36+
}
37+
38+
getErrors() {
39+
if(this.strictExportPresence) {
40+
return this._getErrors();
41+
}
42+
return [];
43+
}
44+
45+
_getErrors() {
3146
const importedModule = this.importDependency.module;
3247
if(!importedModule || !importedModule.meta || !importedModule.meta.harmonyModule) {
3348
return;

lib/dependencies/HarmonyModulesPlugin.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ const HarmonyImportDependencyParserPlugin = require("./HarmonyImportDependencyPa
2020
const HarmonyExportDependencyParserPlugin = require("./HarmonyExportDependencyParserPlugin");
2121

2222
class HarmonyModulesPlugin {
23+
constructor(options) {
24+
this.options = options;
25+
}
2326

2427
apply(compiler) {
2528
compiler.plugin("compilation", (compilation, params) => {
@@ -59,7 +62,7 @@ class HarmonyModulesPlugin {
5962

6063
parser.apply(
6164
new HarmonyDetectionParserPlugin(),
62-
new HarmonyImportDependencyParserPlugin(),
65+
new HarmonyImportDependencyParserPlugin(this.options),
6366
new HarmonyExportDependencyParserPlugin()
6467
);
6568
});

schemas/webpackOptionsSchema.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@
218218
},
219219
"wrappedContextRegExp": {
220220
"instanceof": "RegExp"
221+
},
222+
"strictExportPresence": {
223+
"type": "boolean"
221224
}
222225
},
223226
"type": "object"

0 commit comments

Comments
 (0)