Handle more edgecases in our CJS2ESM conversion code

This commit is contained in:
Jarred Sumner
2021-09-23 21:45:15 -07:00
parent ff01dfa03d
commit f78f4854a4
4 changed files with 57 additions and 9 deletions

View File

@@ -12453,6 +12453,7 @@ pub fn NewParser(
switch (s2.data) {
.s_function => |func| {
var name: string = "";
const had_name = func.func.name != null;
if (func.func.name) |func_loc| {
name = p.loadNameFromRef(func_loc.ref.?);
} else {
@@ -12465,7 +12466,15 @@ pub fn NewParser(
if (p.options.enable_bundling) {
var export_default_args = p.allocator.alloc(Expr, 2) catch unreachable;
export_default_args[0] = p.e(E.Identifier{ .ref = p.exports_ref }, s2.loc);
export_default_args[1] = p.e(E.Function{ .func = func.func }, s2.loc);
if (had_name) {
export_default_args[1] = p.e(E.Identifier{ .ref = func.func.name.?.ref.? }, s2.loc);
stmts.ensureUnusedCapacity(2) catch unreachable;
stmts.appendAssumeCapacity(s2);
} else {
export_default_args[1] = p.e(E.Function{ .func = func.func }, s2.loc);
}
stmts.append(p.s(S.SExpr{ .value = p.callRuntime(s2.loc, "__exportDefault", export_default_args) }, s2.loc)) catch unreachable;
return;
@@ -12481,10 +12490,26 @@ pub fn NewParser(
},
.s_class => |class| {
var shadow_ref = p.visitClass(s2.loc, &class.class);
if (p.options.enable_bundling) {
var export_default_args = p.allocator.alloc(Expr, 2) catch unreachable;
export_default_args[0] = p.e(E.Identifier{ .ref = p.exports_ref }, s2.loc);
export_default_args[1] = p.e(class.class, s2.loc);
const class_name_ref = brk: {
if (class.class.class_name) |class_name_ref| {
if (class_name_ref.ref) |ref| {
break :brk ref;
}
}
break :brk null;
};
if (class_name_ref) |ref| {
stmts.ensureUnusedCapacity(2) catch unreachable;
stmts.appendAssumeCapacity(s2);
export_default_args[1] = p.e(E.Identifier{ .ref = ref }, s2.loc);
} else {
export_default_args[1] = p.e(class.class, s2.loc);
}
stmts.append(p.s(S.SExpr{ .value = p.callRuntime(s2.loc, "__exportDefault", export_default_args) }, s2.loc)) catch unreachable;
return;

View File

@@ -2485,7 +2485,7 @@ pub fn NewPrinter(
// Object.assign(__export, {prop1, prop2, prop3});
else => {
if (comptime is_inside_bundle) {
p.printSymbol(p.options.runtime_imports.__exportValue.?.ref);
p.printSymbol(p.options.runtime_imports.__export.?.ref);
} else {
p.print("Object.assign");
}
@@ -2497,8 +2497,12 @@ pub fn NewPrinter(
for (s.items) |item, i| {
const name = p.renamer.nameForSymbol(item.name.ref.?);
p.printClauseAlias(item.alias);
if (!strings.eql(name, item.alias)) {
if (comptime is_inside_bundle) {
p.print(":");
p.printSpace();
p.print("() => ");
p.printIdentifier(name);
} else if (!strings.eql(name, item.alias)) {
p.print(":");
p.printSpace();
p.printIdentifier(name);

View File

@@ -54,9 +54,24 @@ export var __commonJS = (cb, name) => {
cb(mod, mod.exports);
const kind = typeof mod.exports;
// If it's a default-only export, don't crash if they call .default on the module
if (
(kind === "function" || kind === "object") &&
"__esModule" in mod.exports &&
!mod.exports[tagSymbol]
) {
if (!("default" in mod.exports)) {
Object.defineProperty(mod.exports, "default", {
get() {
return mod.exports;
},
set(v) {
mod.exports = v;
},
enumerable: true,
configurable: true,
});
}
} else if (
kind === "object" &&
"default" in mod.exports &&
!mod.exports[tagSymbol] &&
@@ -73,6 +88,9 @@ export var __commonJS = (cb, name) => {
get() {
return mod.exports;
},
set(v) {
mod.exports = v;
},
enumerable: true,
configurable: true,
});
@@ -168,13 +186,14 @@ export var __export = (target, all) => {
};
export var __exportValue = (target, all) => {
for (var name in all)
for (var name in all) {
__defProp(target, name, {
get: () => all[name],
set: (newValue) => (all[name] = newValue),
enumerable: true,
configurable: true,
});
}
};
export var __exportDefault = (target, value) => {

View File

@@ -1 +1 @@
c92310420c760f74
80c43f840c4f57eb