Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions test/fixtures/requireesm-brand-nowrap/expected.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as _nobinding2 from "nobinding";
var _nobinding = _nobinding2;
try {
if ("default" in _nobinding2) _nobinding = _nobinding2.default;
} catch (e) {}
var _nobinding = _nobinding2.__cjsModule ? _nobinding2.default : "default" in _nobinding2 && !("__esModule" in _nobinding2) ? new Proxy(_nobinding2, {
get: (t, k, r) => k === "__esModule" || Reflect.get(t, k, r),
has: (t, k) => k === "__esModule" || Reflect.has(t, k)
}) : _nobinding2;
import * as _fs2 from "fs";
var _fs = _fs2;
try {
if ("default" in _fs2) _fs = _fs2.default;
} catch (e) {}
var _fs = _fs2.__cjsModule ? _fs2.default : "default" in _fs2 && !("__esModule" in _fs2) ? new Proxy(_fs2, {
get: (t, k, r) => k === "__esModule" || Reflect.get(t, k, r),
has: (t, k) => k === "__esModule" || Reflect.has(t, k)
}) : _fs2;
import * as _thing from "thing";
_nobinding;
const {
Expand All @@ -17,4 +17,5 @@ const {
thing
} = _thing;
readFileSync("asdf");
export default {};
export default {};
export const __cjsModule = true;
1 change: 1 addition & 0 deletions test/fixtures/requireesm-brand-nowrap/options.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
nowrap: true,
cjsMarker: true,
esmDependencies (x) {
if (x === 'thing')
return 'namespace';
Expand Down
16 changes: 8 additions & 8 deletions test/fixtures/requireesm-brand/expected.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import * as _nobinding2 from "nobinding";
var _nobinding = _nobinding2;
try {
if ("default" in _nobinding2) _nobinding = _nobinding2.default;
} catch (e) {}
import * as _fs2 from "fs";
var _fs = _fs2;
try {
if ("default" in _fs2) _fs = _fs2.default;
} catch (e) {}
import * as _thing from "thing";
var exports = {},
_dewExec = false;
export function dew() {
if (_dewExec) return exports;
_dewExec = true;
var _nobinding = _nobinding2.__cjsModule ? _nobinding2.default : "default" in _nobinding2 && !("__esModule" in _nobinding2) ? new Proxy(_nobinding2, {
get: (t, k, r) => k === "__esModule" || Reflect.get(t, k, r),
has: (t, k) => k === "__esModule" || Reflect.has(t, k)
}) : _nobinding2;
var _fs = _fs2.__cjsModule ? _fs2.default : "default" in _fs2 && !("__esModule" in _fs2) ? new Proxy(_fs2, {
get: (t, k, r) => k === "__esModule" || Reflect.get(t, k, r),
has: (t, k) => k === "__esModule" || Reflect.has(t, k)
}) : _fs2;
_nobinding;
const {
readFileSync
Expand Down
69 changes: 52 additions & 17 deletions transform-cjs-dew.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ module.exports = function ({ types: t }) {
const dewInterop = t.identifier('__dew');
const defaultIdentifier = t.identifier('default');

const esModuleLiteral = t.stringLiteral('__esModule');
const globalThis = t.identifier('globalThis');
const globalThisPredicate = t.binaryExpression('!==', t.unaryExpression('typeof', globalThis), t.stringLiteral('undefined'));
const selfIdentifier = t.identifier('self');
Expand Down Expand Up @@ -789,13 +790,26 @@ 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.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.variableDeclaration('var', [
t.variableDeclarator(dep.id, t.conditionalExpression(
t.memberExpression(dep.mid, t.identifier('__cjsModule')),
t.memberExpression(dep.mid, t.identifier('default')),
t.conditionalExpression(
t.logicalExpression('&&', t.binaryExpression('in', t.stringLiteral('default'), dep.mid), t.unaryExpression('!', t.binaryExpression('in', t.stringLiteral('__esModule'), dep.mid))),
t.newExpression(t.identifier('Proxy'), [dep.mid, t.objectExpression([
t.objectProperty(t.identifier('get'), t.arrowFunctionExpression([t.identifier('t'), t.identifier('k'), t.identifier('r')], t.logicalExpression('||',
t.binaryExpression('===', t.identifier('k'), esModuleLiteral),
t.callExpression(t.memberExpression(t.identifier('Reflect'), t.identifier('get')), [t.identifier('t'), t.identifier('k'), t.identifier('r')])
))),
t.objectProperty(t.identifier('has'), t.arrowFunctionExpression([t.identifier('t'), t.identifier('k')], t.logicalExpression('||',
t.binaryExpression('===', t.identifier('k'), esModuleLiteral),
t.callExpression(t.memberExpression(t.identifier('Reflect'), t.identifier('has')), [t.identifier('t'), t.identifier('k')])
)))
])]),
dep.mid
)
))
]));
}
unshiftBody(path,
t.importDeclaration([
Expand All @@ -817,11 +831,11 @@ module.exports = function ({ types: t }) {
t.exportDefaultDeclaration(exportsReturn)
);

if (state.opts.namedExports && state.opts.namedExports.length) {
if (state.opts.namedExports && state.opts.namedExports.length || state.opts.cjsMarker) {
const exportDeclarations = [];
const namedExports = [];
const varDeclarations = [];
for (const name of state.opts.namedExports) {
for (const name of state.opts.namedExports || []) {
const id = t.identifier(name);
if (name === 'default') {
if (exportsReturn)
Expand All @@ -836,6 +850,11 @@ module.exports = function ({ types: t }) {
namedExports.push(t.exportSpecifier(uid, id));
}
}

if (state.opts.cjsMarker) {
exportDeclarations.push(t.variableDeclarator(t.identifier('__cjsModule'), t.booleanLiteral(true)));
}

if (exportDeclarations.length)
pushBody(path, t.exportNamedDeclaration(t.variableDeclaration('const', exportDeclarations), []));
if (varDeclarations.length)
Expand All @@ -848,22 +867,37 @@ module.exports = function ({ types: t }) {

let dewBodyWrapper = [];

const innerWrapper = [];

state.deps.forEach(dep => {
dewBodyWrapper.push(
t.importDeclaration([
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) {
dewBodyWrapper.push(
t.variableDeclaration('var', [t.variableDeclarator(dep.id, dep.mid)])
innerWrapper.push(
t.variableDeclaration('var', [
t.variableDeclarator(dep.id, t.conditionalExpression(
t.memberExpression(dep.mid, t.identifier('__cjsModule')),
t.memberExpression(dep.mid, t.identifier('default')),
t.conditionalExpression(
t.logicalExpression('&&', t.binaryExpression('in', t.stringLiteral('default'), dep.mid), t.unaryExpression('!', t.binaryExpression('in', t.stringLiteral('__esModule'), dep.mid))),
t.newExpression(t.identifier('Proxy'), [dep.mid, t.objectExpression([
t.objectProperty(t.identifier('get'), t.arrowFunctionExpression([t.identifier('t'), t.identifier('k'), t.identifier('r')], t.logicalExpression('||',
t.binaryExpression('===', t.identifier('k'), esModuleLiteral),
t.callExpression(t.memberExpression(t.identifier('Reflect'), t.identifier('get')), [t.identifier('t'), t.identifier('k'), t.identifier('r')])
))),
t.objectProperty(t.identifier('has'), t.arrowFunctionExpression([t.identifier('t'), t.identifier('k')], t.logicalExpression('||',
t.binaryExpression('===', t.identifier('k'), esModuleLiteral),
t.callExpression(t.memberExpression(t.identifier('Reflect'), t.identifier('has')), [t.identifier('t'), t.identifier('k')])
)))
])]),
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([]))));
}
});

Expand All @@ -888,6 +922,7 @@ module.exports = function ({ types: t }) {
t.functionDeclaration(dewIdentifier, [], t.blockStatement([
t.ifStatement(execIdentifier, t.returnStatement(exportsReturn)),
t.expressionStatement(t.assignmentExpression('=', execIdentifier, t.booleanLiteral(true))),
...innerWrapper,
...path.node.body,
t.returnStatement(exportsReturn)
])),
Expand Down