Skip to content

Commit 3a6b649

Browse files
committed
star import a non-harmony modules behaves like require
fixes webpack#2732
1 parent 1ee3585 commit 3a6b649

3 files changed

Lines changed: 20 additions & 2 deletions

File tree

‎lib/dependencies/HarmonyImportDependencyParserPlugin.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ module.exports = AbstractPlugin.create({
3030
var settings = this.state.harmonySpecifier["$" + name];
3131
var dep = new HarmonyImportSpecifierDependency(settings[0], settings[1], settings[2], name, expr.range);
3232
dep.shorthand = this.scope.inShorthand;
33+
dep.directImport = true;
3334
dep.loc = expr.loc;
3435
this.state.current.addDependency(dep);
3536
return true;
@@ -41,6 +42,7 @@ module.exports = AbstractPlugin.create({
4142
return false;
4243
var dep = new HarmonyImportSpecifierDependency(settings[0], settings[1], expr.property.name, name, expr.range);
4344
dep.shorthand = this.scope.inShorthand;
45+
dep.directImport = false;
4446
dep.loc = expr.loc;
4547
this.state.current.addDependency(dep);
4648
return true;
@@ -52,6 +54,7 @@ module.exports = AbstractPlugin.create({
5254
var name = expr.name;
5355
var settings = this.state.harmonySpecifier["$" + name];
5456
var dep = new HarmonyImportSpecifierDependency(settings[0], settings[1], settings[2], name, expr.range);
57+
dep.directImport = true;
5558
dep.callArgs = args;
5659
dep.call = fullExpr;
5760
dep.loc = expr.loc;

‎lib/dependencies/HarmonyImportSpecifierDependency.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ HarmonyImportSpecifierDependency.Template = function HarmonyImportSpecifierDepen
5656
HarmonyImportSpecifierDependency.Template.prototype.apply = function(dep, source) {
5757
var content;
5858
var importedModule = dep.importDependency.module;
59-
var defaultImport = dep.id === "default" && !(importedModule && (!importedModule.meta || importedModule.meta.harmonyModule));
59+
var defaultImport = dep.directImport && dep.id === "default" && !(importedModule && (!importedModule.meta || importedModule.meta.harmonyModule));
6060
if(defaultImport) {
6161
content = dep.importedVar + "_default.a";
6262
} else if(dep.id) {
@@ -68,7 +68,7 @@ HarmonyImportSpecifierDependency.Template.prototype.apply = function(dep, source
6868
if(dep.call) {
6969
if(defaultImport) {
7070
content = dep.importedVar + "_default()";
71-
} else {
71+
} else if(dep.id) {
7272
content = "__webpack_require__.i(" + content + ")";
7373
}
7474
}

‎test/cases/parsing/harmony/index.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ import threeIsOdd, { even } from "circularEven";
1616
import { specA, specB } from "exports-specifier";
1717

1818
import Thing, { Other } from "commonjs";
19+
import * as commonjs from "commonjs";
1920
import Thing2, { Other as Other2 } from "commonjs-trans";
21+
import * as commonjsTrans from "commonjs-trans";
2022

2123
import defaultExport from "def";
2224

@@ -97,3 +99,16 @@ it("should be able to import commonjs", function() {
9799
Other2.should.be.eql("other");
98100
Thing3().should.be.eql("thing");
99101
});
102+
103+
it("should be able to import commonjs with star import", function() {
104+
var copyOfCommonjs = commonjs;
105+
commonjs().should.be.eql("thing");
106+
commonjs.Other.should.be.eql("other");
107+
copyOfCommonjs().should.be.eql("thing");
108+
copyOfCommonjs.Other.should.be.eql("other");
109+
var copyOfCommonjsTrans = commonjsTrans;
110+
new commonjsTrans.default().value.should.be.eql("thing");
111+
commonjsTrans.Other.should.be.eql("other");
112+
new copyOfCommonjsTrans.default().value.should.be.eql("thing");
113+
copyOfCommonjsTrans.Other.should.be.eql("other");
114+
});

0 commit comments

Comments
 (0)