safer common interop - closes #493
This commit is contained in:
@@ -1,3 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
module.exports = require("./_strict")(require("./amd"));
|
||||
@@ -107,7 +107,7 @@ AMDFormatter.prototype.importSpecifier = function (specifier, node, nodes) {
|
||||
};
|
||||
|
||||
AMDFormatter.prototype.exportDeclaration = function (node) {
|
||||
if (node.default) {
|
||||
if (node.default && !this.noInteropRequire) {
|
||||
this.passModuleArg = true;
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ function CommonJSFormatter(file) {
|
||||
var state = { hasNonDefaultExports: false };
|
||||
traverse(file.ast, visitor, file.scope, state);
|
||||
|
||||
this.insertedModuleDeclaration = false;
|
||||
this.hasNonDefaultExports = state.hasNonDefaultExports;
|
||||
}
|
||||
|
||||
@@ -71,33 +72,23 @@ CommonJSFormatter.prototype.exportDeclaration = function (node, nodes) {
|
||||
var declar = node.declaration;
|
||||
var assign;
|
||||
|
||||
// module.exports = VALUE;
|
||||
var templateName = "exports-default-module";
|
||||
|
||||
// exports = module.exports = VALUE;
|
||||
if (this.hasNonDefaultExports) templateName = "exports-default-module-override";
|
||||
|
||||
if (t.isFunctionDeclaration(declar) || !this.hasNonDefaultExports) {
|
||||
assign = util.template(templateName, {
|
||||
if (this.hasNonDefaultExports) {
|
||||
if (!this.insertedModuleDeclaration) {
|
||||
nodes.push(util.template("exports-module-declaration", true));
|
||||
this.insertedModuleDeclaration = true;
|
||||
}
|
||||
} else {
|
||||
var assign = util.template("exports-default-assign", {
|
||||
VALUE: this._pushStatement(declar, nodes)
|
||||
}, true);
|
||||
|
||||
// hoist to the top if this default is a function
|
||||
nodes.push(this._hoistExport(declar, assign, 3));
|
||||
return;
|
||||
} else {
|
||||
// this export isn't a function so we can't hoist it to the top so we need to set it
|
||||
// at the very end of the file with something like:
|
||||
//
|
||||
// module.exports = _extends(exports["default"], exports)
|
||||
//
|
||||
|
||||
assign = util.template("common-export-default-assign", {
|
||||
EXTENDS_HELPER: this.file.addHelper("extends")
|
||||
}, true);
|
||||
assign._blockHoist = 0;
|
||||
if (t.isFunctionDeclaration(declar)) {
|
||||
// we can hoist this assignment to the top of the file
|
||||
assign._blockHoist = 3;
|
||||
}
|
||||
|
||||
nodes.push(assign);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
module.exports = EXTENDS_HELPER(exports["default"], exports);
|
||||
@@ -1 +0,0 @@
|
||||
exports = module.exports = VALUE;
|
||||
@@ -0,0 +1 @@
|
||||
exports.__esModule = true;
|
||||
@@ -1,3 +1,3 @@
|
||||
(function (obj) {
|
||||
return obj && obj.constructor === Object ? obj : { default: obj };
|
||||
return obj && obj.__esModule ? obj.default : { default: obj };
|
||||
})
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
(function (obj) {
|
||||
return obj && (obj["default"] || obj);
|
||||
return obj && obj.__esModule ? obj.default : obj;
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user