allow to use "old" modules from harmony modules

This commit is contained in:
Tobias Koppers 2015-10-31 15:28:13 +01:00
parent 91621a7c4b
commit 03ecbd50f8
7 changed files with 46 additions and 2 deletions

View File

@ -15,6 +15,7 @@ module.exports = AbstractPlugin.create({
var dep = new HarmonyExportHeaderDependency(statement.declaration && statement.declaration.range, statement.range);
dep.loc = statement.loc;
this.state.current.addDependency(dep);
this.state.module.meta.harmonyModule = true;
return true;
},
"export import": function(statement, source) {
@ -22,6 +23,7 @@ module.exports = AbstractPlugin.create({
dep.loc = statement.loc;
this.state.current.addDependency(dep);
this.state.lastHarmoryImport = dep;
this.state.module.meta.harmonyModule = true;
return true;
},
"export expression": function(statement, expr) {

View File

@ -37,6 +37,8 @@ HarmonyExportImportedSpecifierDependency.Template.prototype.apply = function(dep
var content;
if(!used) {
content = "/* ununsed harmony reexport " + (dep.name || "namespace") + " */;";
} else if(dep.name === "default" && !dep.importDependency.module.meta.harmonyModule) {
content = "/* harmony reexport */ Object.defineProperty(exports, " + JSON.stringify(dep.name) + ", {configurable: false, enumerable: true, get: function() { return " + name + "_default.a; }});";
} else if(dep.name) {
content = "/* harmony reexport */ Object.defineProperty(exports, " + JSON.stringify(dep.name) + ", {configurable: false, enumerable: true, get: function() { return " + (name + "[" + JSON.stringify(dep.id) + "]") + "; }});";
} else {

View File

@ -34,6 +34,10 @@ HarmonyImportDependency.Template.prototype.apply = function(dep, source, outputO
content = "throw new Error(" + JSON.stringify("Cannot find module \"" + dep.request + "\"") + ");\n";
} else if(dep.importedVar) {
content = "/* harmony import */ var " + dep.importedVar + " = __webpack_require__(" + comment + JSON.stringify(dep.module.id) + ");\n";
if(!dep.module.meta.harmonyModule) {
content += "/* harmony import */ function " + dep.importedVar + "_default() { return " + dep.importedVar + " && typeof " + dep.importedVar + " === 'object' && 'default' in " + dep.importedVar + " ? " + dep.importedVar + "['default'] : " + dep.importedVar + "; }\n";
content += "/* harmony import */ Object.defineProperty(" + dep.importedVar + "_default, 'a', { get: function() { return " + dep.importedVar + "_default(); }});\n";
}
} else {
content = "";
}

View File

@ -31,7 +31,9 @@ HarmonyImportSpecifierDependency.Template = function HarmonyImportSpecifierDepen
HarmonyImportSpecifierDependency.Template.prototype.apply = function(dep, source) {
var content;
if(dep.id) {
if(dep.id === "default" && !dep.importDependency.module.meta.harmonyModule) {
content = "/* harmony import */ " + dep.importedVar + "_default.a";
} else if(dep.id) {
content = "/* harmony import */ " + dep.importedVar + "[" + JSON.stringify(dep.id) + "]";
} else {
content = "/* harmony namespace import */ " + dep.importedVar;

View File

@ -12,11 +12,22 @@ import { a as rea, b as reb, c as rec, o as reo, two as retwo } from "reexport";
import threeIsOdd, { even } from "circularEven";
import Thing, { Other } from "commonjs";
import Thing2, { Other as Other2 } from "commonjs-trans";
it("should import an identifier from a module", function() {
a.should.be.eql("a");
B.should.be.eql("b");
});
it("should import a whole module", function() {
abc.a.should.be.eql("a");
abc.b.should.be.eql("b");
var copy = (function(a) { return a; }(abc));
copy.a.should.be.eql("a");
copy.b.should.be.eql("b");
});
it("should export functions", function() {
fn.should.have.type("function");
fn().should.be.eql("fn");
@ -43,4 +54,17 @@ it("should reexport a module", function() {
it("should support circular dependencies", function() {
threeIsOdd.should.be.eql(true);
even(4).should.be.eql(true);
})
});
it("should be able to import commonjs", function() {
function x() { throw new Error("should not be executed"); }
// next line doesn't end with semicolon
x
Thing.should.have.type("function");
Thing().should.be.eql("thing");
Other.should.be.eql("other");
Thing2.should.have.type("function");
new Thing2().value.should.be.eql("thing");
Other2.should.be.eql("other");
});

View File

@ -0,0 +1,5 @@
exports.default = function Thing() {
this.value = "thing";
};
exports.Other = "other";

5
test/cases/parsing/harmony/node_modules/commonjs.js generated vendored Normal file
View File

@ -0,0 +1,5 @@
module.exports = function Thing() {
return "thing";
}
module.exports.Other = "other";