mirror of https://github.com/webpack/webpack.git
allow to use "old" modules from harmony modules
This commit is contained in:
parent
91621a7c4b
commit
03ecbd50f8
|
@ -15,6 +15,7 @@ module.exports = AbstractPlugin.create({
|
||||||
var dep = new HarmonyExportHeaderDependency(statement.declaration && statement.declaration.range, statement.range);
|
var dep = new HarmonyExportHeaderDependency(statement.declaration && statement.declaration.range, statement.range);
|
||||||
dep.loc = statement.loc;
|
dep.loc = statement.loc;
|
||||||
this.state.current.addDependency(dep);
|
this.state.current.addDependency(dep);
|
||||||
|
this.state.module.meta.harmonyModule = true;
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
"export import": function(statement, source) {
|
"export import": function(statement, source) {
|
||||||
|
@ -22,6 +23,7 @@ module.exports = AbstractPlugin.create({
|
||||||
dep.loc = statement.loc;
|
dep.loc = statement.loc;
|
||||||
this.state.current.addDependency(dep);
|
this.state.current.addDependency(dep);
|
||||||
this.state.lastHarmoryImport = dep;
|
this.state.lastHarmoryImport = dep;
|
||||||
|
this.state.module.meta.harmonyModule = true;
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
"export expression": function(statement, expr) {
|
"export expression": function(statement, expr) {
|
||||||
|
|
|
@ -37,6 +37,8 @@ HarmonyExportImportedSpecifierDependency.Template.prototype.apply = function(dep
|
||||||
var content;
|
var content;
|
||||||
if(!used) {
|
if(!used) {
|
||||||
content = "/* ununsed harmony reexport " + (dep.name || "namespace") + " */;";
|
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) {
|
} 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) + "]") + "; }});";
|
content = "/* harmony reexport */ Object.defineProperty(exports, " + JSON.stringify(dep.name) + ", {configurable: false, enumerable: true, get: function() { return " + (name + "[" + JSON.stringify(dep.id) + "]") + "; }});";
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -34,6 +34,10 @@ HarmonyImportDependency.Template.prototype.apply = function(dep, source, outputO
|
||||||
content = "throw new Error(" + JSON.stringify("Cannot find module \"" + dep.request + "\"") + ");\n";
|
content = "throw new Error(" + JSON.stringify("Cannot find module \"" + dep.request + "\"") + ");\n";
|
||||||
} else if(dep.importedVar) {
|
} else if(dep.importedVar) {
|
||||||
content = "/* harmony import */ var " + dep.importedVar + " = __webpack_require__(" + comment + JSON.stringify(dep.module.id) + ");\n";
|
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 {
|
} else {
|
||||||
content = "";
|
content = "";
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,7 +31,9 @@ HarmonyImportSpecifierDependency.Template = function HarmonyImportSpecifierDepen
|
||||||
|
|
||||||
HarmonyImportSpecifierDependency.Template.prototype.apply = function(dep, source) {
|
HarmonyImportSpecifierDependency.Template.prototype.apply = function(dep, source) {
|
||||||
var content;
|
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) + "]";
|
content = "/* harmony import */ " + dep.importedVar + "[" + JSON.stringify(dep.id) + "]";
|
||||||
} else {
|
} else {
|
||||||
content = "/* harmony namespace import */ " + dep.importedVar;
|
content = "/* harmony namespace import */ " + dep.importedVar;
|
||||||
|
|
|
@ -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 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() {
|
it("should import an identifier from a module", function() {
|
||||||
a.should.be.eql("a");
|
a.should.be.eql("a");
|
||||||
B.should.be.eql("b");
|
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() {
|
it("should export functions", function() {
|
||||||
fn.should.have.type("function");
|
fn.should.have.type("function");
|
||||||
fn().should.be.eql("fn");
|
fn().should.be.eql("fn");
|
||||||
|
@ -43,4 +54,17 @@ it("should reexport a module", function() {
|
||||||
it("should support circular dependencies", function() {
|
it("should support circular dependencies", function() {
|
||||||
threeIsOdd.should.be.eql(true);
|
threeIsOdd.should.be.eql(true);
|
||||||
even(4).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");
|
||||||
|
});
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
exports.default = function Thing() {
|
||||||
|
this.value = "thing";
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.Other = "other";
|
|
@ -0,0 +1,5 @@
|
||||||
|
module.exports = function Thing() {
|
||||||
|
return "thing";
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports.Other = "other";
|
Loading…
Reference in New Issue