Skip to content

Commit

Permalink
esm tdz fix
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Oct 30, 2023
1 parent 13b814e commit c4b7b85
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 24 deletions.
10 changes: 8 additions & 2 deletions test/fixtures/requireesm-brand-nowrap/expected.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import * as _nobinding2 from "nobinding";
var _nobinding = "default" in _nobinding2 ? _nobinding2.default : _nobinding2;
var _nobinding = _nobinding2;
try {
if ("default" in _nobinding2) _nobinding = _nobinding2.default;
} catch (e) {}
import * as _fs2 from "fs";
var _fs = "default" in _fs2 ? _fs2.default : _fs2;
var _fs = _fs2;
try {
if ("default" in _fs2) _fs = _fs2.default;
} catch (e) {}
import * as _thing from "thing";
_nobinding;
const {
Expand Down
10 changes: 8 additions & 2 deletions test/fixtures/requireesm-brand/expected.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import * as _nobinding2 from "nobinding";
var _nobinding = "default" in _nobinding2 ? _nobinding2.default : _nobinding2;
var _nobinding = _nobinding2;
try {
if ("default" in _nobinding2) _nobinding = _nobinding2.default;
} catch (e) {}
import * as _fs2 from "fs";
var _fs = "default" in _fs2 ? _fs2.default : _fs2;
var _fs = _fs2;
try {
if ("default" in _fs2) _fs = _fs2.default;
} catch (e) {}
import * as _thing from "thing";
var exports = {},
_dewExec = false;
Expand Down
38 changes: 18 additions & 20 deletions transform-cjs-dew.js
Original file line number Diff line number Diff line change
Expand Up @@ -764,17 +764,15 @@ module.exports = function ({ types: t }) {
)]));
for (let i = state.deps.length - 1; i >= 0; i--) {
const dep = state.deps[i];
if (dep.ns && dep.mid.name !== dep.id.name)
unshiftBody(path,
t.variableDeclaration('var', [t.variableDeclarator(
dep.id,
t.conditionalExpression(
t.binaryExpression('in', t.stringLiteral('default'), dep.mid),
t.memberExpression(dep.mid, defaultIdentifier),
dep.mid
)
)])
);
if (dep.ns && dep.mid.name !== dep.id.name) {
unshiftBody(path, t.tryStatement(t.blockStatement([
t.ifStatement(
t.binaryExpression('in', t.stringLiteral('default'), dep.mid),
t.expressionStatement(t.assignmentExpression('=', dep.id, t.memberExpression(dep.mid, defaultIdentifier)))
)
]), t.catchClause(t.identifier('e'), t.blockStatement([]))));
unshiftBody(path, t.variableDeclaration('var', [t.variableDeclarator(dep.id, dep.mid)]));
}
unshiftBody(path,
t.importDeclaration([
dep.dew && !dep.ns ? t.importSpecifier(dep.mid, dewIdentifier) : (dep.ns ? t.importNamespaceSpecifier : t.importDefaultSpecifier)(dep.mid)
Expand Down Expand Up @@ -832,17 +830,17 @@ module.exports = function ({ types: t }) {
dep.dew && !dep.ns ? t.importSpecifier(dep.mid, dewIdentifier) : (dep.ns ? t.importNamespaceSpecifier : t.importDefaultSpecifier)(dep.mid)
], dep.literal)
);
if (dep.ns && dep.mid.name !== dep.id.name)
if (dep.ns && dep.mid.name !== dep.id.name) {
dewBodyWrapper.push(
t.variableDeclaration('var', [t.variableDeclarator(
dep.id,
t.conditionalExpression(
t.binaryExpression('in', t.stringLiteral('default'), dep.mid),
t.memberExpression(dep.mid, defaultIdentifier),
dep.mid
)
)])
t.variableDeclaration('var', [t.variableDeclarator(dep.id, dep.mid)])
);
dewBodyWrapper.push(t.tryStatement(t.blockStatement([
t.ifStatement(
t.binaryExpression('in', t.stringLiteral('default'), dep.mid),
t.expressionStatement(t.assignmentExpression('=', dep.id, t.memberExpression(dep.mid, defaultIdentifier)))
)
]), t.catchClause(t.identifier('e'), t.blockStatement([]))));
}
});

const execIdentifier = path.scope.generateUidIdentifier('dewExec');
Expand Down

0 comments on commit c4b7b85

Please sign in to comment.