Skip to content

Commit 03ecbd5

Browse files
committed
allow to use "old" modules from harmony modules
1 parent 91621a7 commit 03ecbd5

File tree

7 files changed

+46
-2
lines changed

7 files changed

+46
-2
lines changed

lib/dependencies/HarmonyExportDependencyParserPlugin.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ module.exports = AbstractPlugin.create({
1515
var dep = new HarmonyExportHeaderDependency(statement.declaration && statement.declaration.range, statement.range);
1616
dep.loc = statement.loc;
1717
this.state.current.addDependency(dep);
18+
this.state.module.meta.harmonyModule = true;
1819
return true;
1920
},
2021
"export import": function(statement, source) {
2122
var dep = new HarmonyImportDependency(source, HarmonyModulesHelpers.getNewModuleVar(this.state, source), statement.range);
2223
dep.loc = statement.loc;
2324
this.state.current.addDependency(dep);
2425
this.state.lastHarmoryImport = dep;
26+
this.state.module.meta.harmonyModule = true;
2527
return true;
2628
},
2729
"export expression": function(statement, expr) {

lib/dependencies/HarmonyExportImportedSpecifierDependency.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ HarmonyExportImportedSpecifierDependency.Template.prototype.apply = function(dep
3737
var content;
3838
if(!used) {
3939
content = "/* ununsed harmony reexport " + (dep.name || "namespace") + " */;";
40+
} else if(dep.name === "default" && !dep.importDependency.module.meta.harmonyModule) {
41+
content = "/* harmony reexport */ Object.defineProperty(exports, " + JSON.stringify(dep.name) + ", {configurable: false, enumerable: true, get: function() { return " + name + "_default.a; }});";
4042
} else if(dep.name) {
4143
content = "/* harmony reexport */ Object.defineProperty(exports, " + JSON.stringify(dep.name) + ", {configurable: false, enumerable: true, get: function() { return " + (name + "[" + JSON.stringify(dep.id) + "]") + "; }});";
4244
} else {

lib/dependencies/HarmonyImportDependency.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ HarmonyImportDependency.Template.prototype.apply = function(dep, source, outputO
3434
content = "throw new Error(" + JSON.stringify("Cannot find module \"" + dep.request + "\"") + ");\n";
3535
} else if(dep.importedVar) {
3636
content = "/* harmony import */ var " + dep.importedVar + " = __webpack_require__(" + comment + JSON.stringify(dep.module.id) + ");\n";
37+
if(!dep.module.meta.harmonyModule) {
38+
content += "/* harmony import */ function " + dep.importedVar + "_default() { return " + dep.importedVar + " && typeof " + dep.importedVar + " === 'object' && 'default' in " + dep.importedVar + " ? " + dep.importedVar + "['default'] : " + dep.importedVar + "; }\n";
39+
content += "/* harmony import */ Object.defineProperty(" + dep.importedVar + "_default, 'a', { get: function() { return " + dep.importedVar + "_default(); }});\n";
40+
}
3741
} else {
3842
content = "";
3943
}

lib/dependencies/HarmonyImportSpecifierDependency.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ HarmonyImportSpecifierDependency.Template = function HarmonyImportSpecifierDepen
3131

3232
HarmonyImportSpecifierDependency.Template.prototype.apply = function(dep, source) {
3333
var content;
34-
if(dep.id) {
34+
if(dep.id === "default" && !dep.importDependency.module.meta.harmonyModule) {
35+
content = "/* harmony import */ " + dep.importedVar + "_default.a";
36+
} else if(dep.id) {
3537
content = "/* harmony import */ " + dep.importedVar + "[" + JSON.stringify(dep.id) + "]";
3638
} else {
3739
content = "/* harmony namespace import */ " + dep.importedVar;

test/cases/parsing/harmony/index.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,22 @@ import { a as rea, b as reb, c as rec, o as reo, two as retwo } from "reexport";
1212

1313
import threeIsOdd, { even } from "circularEven";
1414

15+
import Thing, { Other } from "commonjs";
16+
import Thing2, { Other as Other2 } from "commonjs-trans";
17+
1518
it("should import an identifier from a module", function() {
1619
a.should.be.eql("a");
1720
B.should.be.eql("b");
1821
});
1922

23+
it("should import a whole module", function() {
24+
abc.a.should.be.eql("a");
25+
abc.b.should.be.eql("b");
26+
var copy = (function(a) { return a; }(abc));
27+
copy.a.should.be.eql("a");
28+
copy.b.should.be.eql("b");
29+
});
30+
2031
it("should export functions", function() {
2132
fn.should.have.type("function");
2233
fn().should.be.eql("fn");
@@ -43,4 +54,17 @@ it("should reexport a module", function() {
4354
it("should support circular dependencies", function() {
4455
threeIsOdd.should.be.eql(true);
4556
even(4).should.be.eql(true);
46-
})
57+
});
58+
59+
it("should be able to import commonjs", function() {
60+
function x() { throw new Error("should not be executed"); }
61+
// next line doesn't end with semicolon
62+
x
63+
Thing.should.have.type("function");
64+
Thing().should.be.eql("thing");
65+
Other.should.be.eql("other");
66+
67+
Thing2.should.have.type("function");
68+
new Thing2().value.should.be.eql("thing");
69+
Other2.should.be.eql("other");
70+
});

test/cases/parsing/harmony/node_modules/commonjs-trans.js

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/cases/parsing/harmony/node_modules/commonjs.js

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)