Skip to content

Commit

Permalink
star import a non-harmony modules behaves like require
Browse files Browse the repository at this point in the history
fixes #2732
  • Loading branch information
sokra committed Jul 4, 2016
1 parent 1ee3585 commit 3a6b649
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/dependencies/HarmonyImportDependencyParserPlugin.js
Expand Up @@ -30,6 +30,7 @@ module.exports = AbstractPlugin.create({
var settings = this.state.harmonySpecifier["$" + name];
var dep = new HarmonyImportSpecifierDependency(settings[0], settings[1], settings[2], name, expr.range);
dep.shorthand = this.scope.inShorthand;
dep.directImport = true;
dep.loc = expr.loc;
this.state.current.addDependency(dep);
return true;
Expand All @@ -41,6 +42,7 @@ module.exports = AbstractPlugin.create({
return false;
var dep = new HarmonyImportSpecifierDependency(settings[0], settings[1], expr.property.name, name, expr.range);
dep.shorthand = this.scope.inShorthand;
dep.directImport = false;
dep.loc = expr.loc;
this.state.current.addDependency(dep);
return true;
Expand All @@ -52,6 +54,7 @@ module.exports = AbstractPlugin.create({
var name = expr.name;
var settings = this.state.harmonySpecifier["$" + name];
var dep = new HarmonyImportSpecifierDependency(settings[0], settings[1], settings[2], name, expr.range);
dep.directImport = true;
dep.callArgs = args;
dep.call = fullExpr;
dep.loc = expr.loc;
Expand Down
4 changes: 2 additions & 2 deletions lib/dependencies/HarmonyImportSpecifierDependency.js
Expand Up @@ -56,7 +56,7 @@ HarmonyImportSpecifierDependency.Template = function HarmonyImportSpecifierDepen
HarmonyImportSpecifierDependency.Template.prototype.apply = function(dep, source) {
var content;
var importedModule = dep.importDependency.module;
var defaultImport = dep.id === "default" && !(importedModule && (!importedModule.meta || importedModule.meta.harmonyModule));
var defaultImport = dep.directImport && dep.id === "default" && !(importedModule && (!importedModule.meta || importedModule.meta.harmonyModule));
if(defaultImport) {
content = dep.importedVar + "_default.a";
} else if(dep.id) {
Expand All @@ -68,7 +68,7 @@ HarmonyImportSpecifierDependency.Template.prototype.apply = function(dep, source
if(dep.call) {
if(defaultImport) {
content = dep.importedVar + "_default()";
} else {
} else if(dep.id) {
content = "__webpack_require__.i(" + content + ")";
}
}
Expand Down
15 changes: 15 additions & 0 deletions test/cases/parsing/harmony/index.js
Expand Up @@ -16,7 +16,9 @@ import threeIsOdd, { even } from "circularEven";
import { specA, specB } from "exports-specifier";

import Thing, { Other } from "commonjs";
import * as commonjs from "commonjs";
import Thing2, { Other as Other2 } from "commonjs-trans";
import * as commonjsTrans from "commonjs-trans";

import defaultExport from "def";

Expand Down Expand Up @@ -97,3 +99,16 @@ it("should be able to import commonjs", function() {
Other2.should.be.eql("other");
Thing3().should.be.eql("thing");
});

it("should be able to import commonjs with star import", function() {
var copyOfCommonjs = commonjs;
commonjs().should.be.eql("thing");
commonjs.Other.should.be.eql("other");
copyOfCommonjs().should.be.eql("thing");
copyOfCommonjs.Other.should.be.eql("other");
var copyOfCommonjsTrans = commonjsTrans;
new commonjsTrans.default().value.should.be.eql("thing");
commonjsTrans.Other.should.be.eql("other");
new copyOfCommonjsTrans.default().value.should.be.eql("thing");
copyOfCommonjsTrans.Other.should.be.eql("other");
});

0 comments on commit 3a6b649

Please sign in to comment.