fix(vite): cover more commonjs cases (#3465)
Fixes https://github.com/denoland/fresh/issues/3463 Fixes https://github.com/denoland/fresh/issues/3449
This commit is contained in:
@@ -53,7 +53,8 @@ export function deno(): Plugin {
|
||||
},
|
||||
async resolveId(id, importer, options) {
|
||||
if (BUILTINS.has(id)) {
|
||||
if (!id.startsWith("node:") && BUILTINS.has(`node:${id}`)) {
|
||||
// `node:` prefix is not included in builtins list.
|
||||
if (!id.startsWith("node:")) {
|
||||
id = `node:${id}`;
|
||||
}
|
||||
return {
|
||||
|
||||
@@ -34,32 +34,6 @@ export function codeEvalPlugin(
|
||||
}
|
||||
},
|
||||
},
|
||||
CallExpression: {
|
||||
enter(path) {
|
||||
if (
|
||||
t.isMemberExpression(path.node.callee) &&
|
||||
t.isIdentifier(path.node.callee.object) &&
|
||||
t.isIdentifier(path.node.callee.property) &&
|
||||
path.node.callee.object.name === "Object" &&
|
||||
path.node.callee.property.name === "defineProperty" &&
|
||||
path.node.arguments.length >= 2
|
||||
) {
|
||||
const args = path.node.arguments;
|
||||
|
||||
if (
|
||||
t.isIdentifier(args[0]) && args[0].name === "exports" ||
|
||||
t.isMemberExpression(args[0]) &&
|
||||
t.isIdentifier(args[0].object) &&
|
||||
t.isIdentifier(args[0].property) &&
|
||||
args[0].object.name === "module" &&
|
||||
args[0].property.name === "exports" &&
|
||||
t.isStringLiteral(args[1]) && args[1].value === "native"
|
||||
) {
|
||||
path.remove();
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
import type { NodePath, PluginObj, types } from "@babel/core";
|
||||
import { builtinModules } from "node:module";
|
||||
|
||||
const BUILTINS = new Set(builtinModules);
|
||||
|
||||
export function cjsPlugin(
|
||||
{ types: t }: { types: typeof types },
|
||||
@@ -74,6 +77,7 @@ export function cjsPlugin(
|
||||
const exported = state.get(EXPORTED);
|
||||
const exportedNs = state.get(EXPORTED_NAMESPACES);
|
||||
const needsRequireImport = state.get(NEEDS_REQUIRE_IMPORT);
|
||||
const hasEsModule = state.get(HAS_ES_MODULE);
|
||||
|
||||
if (needsRequireImport) {
|
||||
// Inject:
|
||||
@@ -223,7 +227,7 @@ export function cjsPlugin(
|
||||
);
|
||||
}
|
||||
|
||||
if (exportNamed.size > 0 || exportedNs.size > 0) {
|
||||
if (exportNamed.size > 0 || exportedNs.size > 0 || hasEsModule) {
|
||||
const id = path.scope.generateUidIdentifier("__default");
|
||||
|
||||
path.pushContainer(
|
||||
@@ -260,15 +264,60 @@ export function cjsPlugin(
|
||||
|
||||
for (let i = 0; i < mappedNs.length; i++) {
|
||||
const mapped = mappedNs[i];
|
||||
|
||||
const key = path.scope.generateUid("k");
|
||||
path.pushContainer(
|
||||
"body",
|
||||
t.expressionStatement(
|
||||
t.callExpression(
|
||||
t.memberExpression(
|
||||
t.identifier("Object"),
|
||||
t.identifier("assign"),
|
||||
t.forInStatement(
|
||||
t.variableDeclaration("var", [
|
||||
t.variableDeclarator(t.identifier(key)),
|
||||
]),
|
||||
t.identifier(mapped),
|
||||
t.ifStatement(
|
||||
t.logicalExpression(
|
||||
"&&",
|
||||
t.logicalExpression(
|
||||
"&&",
|
||||
t.binaryExpression(
|
||||
"!==",
|
||||
t.identifier(key),
|
||||
t.stringLiteral("default"),
|
||||
),
|
||||
t.binaryExpression(
|
||||
"!==",
|
||||
t.identifier(key),
|
||||
t.stringLiteral("__esModule"),
|
||||
),
|
||||
),
|
||||
t.callExpression(
|
||||
t.memberExpression(
|
||||
t.memberExpression(
|
||||
t.memberExpression(
|
||||
t.identifier("Object"),
|
||||
t.identifier("prototype"),
|
||||
),
|
||||
t.identifier("hasOwnProperty"),
|
||||
),
|
||||
t.identifier("call"),
|
||||
),
|
||||
[t.identifier(mapped), t.identifier(key)],
|
||||
),
|
||||
),
|
||||
t.expressionStatement(
|
||||
t.assignmentExpression(
|
||||
"=",
|
||||
t.memberExpression(
|
||||
t.cloneNode(id, true),
|
||||
t.identifier(key),
|
||||
true,
|
||||
),
|
||||
t.memberExpression(
|
||||
t.identifier(mapped),
|
||||
t.identifier(key),
|
||||
true,
|
||||
),
|
||||
),
|
||||
),
|
||||
[t.cloneNode(id, true), t.identifier(mapped)],
|
||||
),
|
||||
),
|
||||
);
|
||||
@@ -288,8 +337,24 @@ export function cjsPlugin(
|
||||
);
|
||||
}
|
||||
|
||||
if (body.length === 0 && state.get(HAS_ES_MODULE)) {
|
||||
if (body.length === 0 && hasEsModule) {
|
||||
path.pushContainer("body", t.exportNamedDeclaration(null));
|
||||
} else if (hasEsModule) {
|
||||
path.pushContainer(
|
||||
"body",
|
||||
t.exportNamedDeclaration(
|
||||
t.variableDeclaration(
|
||||
"var",
|
||||
[t.variableDeclarator(
|
||||
t.identifier("__esModule"),
|
||||
t.memberExpression(
|
||||
t.identifier("exports"),
|
||||
t.identifier("__esModule"),
|
||||
),
|
||||
)],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
},
|
||||
@@ -299,7 +364,6 @@ export function cjsPlugin(
|
||||
|
||||
if (isObjEsModuleFlag(t, path.node)) {
|
||||
state.set(HAS_ES_MODULE, true);
|
||||
path.remove();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -313,7 +377,8 @@ export function cjsPlugin(
|
||||
const mods = state.get(REQUIRE_CALLS) ?? [];
|
||||
state.set(REQUIRE_CALLS, mods);
|
||||
|
||||
if (t.isStringLiteral(path.node.arguments[0])) {
|
||||
const source = path.node.arguments[0];
|
||||
if (t.isStringLiteral(source)) {
|
||||
// Check if we can hoist it or if we need to keep it.
|
||||
let canImport = true;
|
||||
let parent: NodePath | null = path.parentPath;
|
||||
@@ -343,13 +408,9 @@ export function cjsPlugin(
|
||||
path.parentPath?.get("id").isIdentifier() ||
|
||||
path.parentPath?.isCallExpression()
|
||||
) {
|
||||
path.replaceWith(
|
||||
t.logicalExpression(
|
||||
"??",
|
||||
t.memberExpression(
|
||||
t.cloneNode(id, true),
|
||||
t.identifier("__require"),
|
||||
),
|
||||
// Vite json processing always adds a default property.
|
||||
if (source.value.endsWith(".json")) {
|
||||
path.replaceWith(
|
||||
t.logicalExpression(
|
||||
"??",
|
||||
t.memberExpression(
|
||||
@@ -358,8 +419,48 @@ export function cjsPlugin(
|
||||
),
|
||||
t.cloneNode(id, true),
|
||||
),
|
||||
),
|
||||
);
|
||||
);
|
||||
} else if (
|
||||
path.parentPath?.isCallExpression() &&
|
||||
t.isIdentifier(path.parentPath.node.callee) &&
|
||||
path.parentPath.node.callee.name === "__importDefault"
|
||||
) {
|
||||
if (
|
||||
BUILTINS.has(source.value) ||
|
||||
source.value.startsWith("node:")
|
||||
? BUILTINS.has(source.value.slice("node:".length))
|
||||
: BUILTINS.has(`node:${source.value}`)
|
||||
) {
|
||||
path.replaceWith(t.logicalExpression(
|
||||
"??",
|
||||
t.memberExpression(
|
||||
t.cloneNode(id, true),
|
||||
t.identifier("default"),
|
||||
),
|
||||
t.cloneNode(id, true),
|
||||
));
|
||||
} else {
|
||||
path.replaceWith(t.cloneNode(id, true));
|
||||
}
|
||||
} else {
|
||||
path.replaceWith(
|
||||
t.logicalExpression(
|
||||
"??",
|
||||
t.memberExpression(
|
||||
t.cloneNode(id, true),
|
||||
t.identifier("__require"),
|
||||
),
|
||||
t.logicalExpression(
|
||||
"??",
|
||||
t.memberExpression(
|
||||
t.cloneNode(id, true),
|
||||
t.identifier("default"),
|
||||
),
|
||||
t.cloneNode(id, true),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -406,19 +507,14 @@ export function cjsPlugin(
|
||||
if (state.get(IS_ESM)) return;
|
||||
// Check: Object.defineProperty(module.exports) "__esModule" ...)
|
||||
// Check: Object.defineProperty(exports) "__esModule" ...)
|
||||
// Check: a({}, "__esModule", ...)
|
||||
if (
|
||||
t.isCallExpression(path.node.expression) &&
|
||||
t.isMemberExpression(path.node.expression.callee) &&
|
||||
t.isIdentifier(path.node.expression.callee.object) &&
|
||||
t.isIdentifier(path.node.expression.callee.property) &&
|
||||
path.node.expression.callee.object.name === "Object" &&
|
||||
path.node.expression.callee.property.name === "defineProperty" &&
|
||||
path.node.expression.arguments.length >= 2 &&
|
||||
path.node.expression.arguments.length === 3 &&
|
||||
t.isStringLiteral(path.node.expression.arguments[1]) &&
|
||||
path.node.expression.arguments[1].value === "__esModule"
|
||||
) {
|
||||
state.set(HAS_ES_MODULE, true);
|
||||
path.remove();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -565,7 +661,6 @@ export function cjsPlugin(
|
||||
|
||||
if (isEsModuleFlag(t, expr.node)) {
|
||||
state.set(HAS_ES_MODULE, true);
|
||||
path.remove();
|
||||
} else if (left.isMemberExpression()) {
|
||||
if (isModuleExports(t, left.node)) {
|
||||
exported.add("default");
|
||||
@@ -594,7 +689,6 @@ export function cjsPlugin(
|
||||
} else if (expr.isCallExpression()) {
|
||||
if (isObjEsModuleFlag(t, expr.node)) {
|
||||
state.set(HAS_ES_MODULE, true);
|
||||
path.remove();
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -687,16 +781,7 @@ function isObjEsModuleFlag(
|
||||
t: typeof types,
|
||||
node: types.CallExpression,
|
||||
): boolean {
|
||||
return t.isMemberExpression(node.callee) &&
|
||||
t.isIdentifier(node.callee.object) &&
|
||||
node.callee.object.name === "Object" &&
|
||||
t.isIdentifier(node.callee.property) &&
|
||||
node.callee.property.name === "defineProperty" &&
|
||||
node.arguments.length === 3 &&
|
||||
(t.isMemberExpression(node.arguments[0]) &&
|
||||
isModuleExports(t, node.arguments[0]) ||
|
||||
t.isIdentifier(node.arguments[0]) &&
|
||||
node.arguments[0].name === "exports") &&
|
||||
return node.arguments.length === 3 &&
|
||||
t.isStringLiteral(node.arguments[1]) &&
|
||||
node.arguments[1].value === "__esModule" &&
|
||||
t.isObjectExpression(node.arguments[2]);
|
||||
|
||||
@@ -31,6 +31,7 @@ const DEFAULT_EXPORT_END = `export default _default;
|
||||
export var __require = exports;`;
|
||||
const IMPORT_REQUIRE = `import { createRequire } from "node:module";
|
||||
const require = createRequire(import.meta.url);`;
|
||||
const EXPORT_ES_MODULE = `export var __esModule = exports.__esModule;`;
|
||||
|
||||
Deno.test("commonjs - module.exports default", () => {
|
||||
runTest({
|
||||
@@ -58,13 +59,15 @@ Deno.test("commonjs - exports with default + named", () => {
|
||||
exports.default = 'x';
|
||||
exports.foo = 'foo';`,
|
||||
expected: `${INIT}
|
||||
exports.__esModule = true;
|
||||
exports.default = 'x';
|
||||
exports.foo = 'foo';
|
||||
var _foo = exports.foo;
|
||||
export { _foo as foo };
|
||||
${DEFAULT_EXPORT}
|
||||
_default.foo = _foo;
|
||||
${DEFAULT_EXPORT_END}`,
|
||||
${DEFAULT_EXPORT_END}
|
||||
${EXPORT_ES_MODULE}`,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -74,13 +77,15 @@ Deno.test("commonjs - module.exports with default + named", () => {
|
||||
module.exports.default = 'x';
|
||||
module.exports.foo = 'foo';`,
|
||||
expected: `${INIT}
|
||||
module.exports.__esModule = true;
|
||||
module.exports.default = 'x';
|
||||
module.exports.foo = 'foo';
|
||||
var _foo = exports.foo;
|
||||
export { _foo as foo };
|
||||
${DEFAULT_EXPORT}
|
||||
_default.foo = _foo;
|
||||
${DEFAULT_EXPORT_END}`,
|
||||
${DEFAULT_EXPORT_END}
|
||||
${EXPORT_ES_MODULE}`,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -91,13 +96,17 @@ exports.foo = 'bar';
|
||||
const foo = 'also bar';
|
||||
`,
|
||||
expected: `${INIT}
|
||||
Object.defineProperty(exports, '__esModule', {
|
||||
value: true
|
||||
});
|
||||
exports.foo = 'bar';
|
||||
const foo = 'also bar';
|
||||
var _foo = exports.foo;
|
||||
export { _foo as foo };
|
||||
${DEFAULT_EXPORT}
|
||||
_default.foo = _foo;
|
||||
${DEFAULT_EXPORT_END}`,
|
||||
${DEFAULT_EXPORT_END}
|
||||
${EXPORT_ES_MODULE}`,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -108,20 +117,30 @@ exports.default = 'foo';
|
||||
exports.foo = 'bar';
|
||||
`,
|
||||
expected: `${INIT}
|
||||
Object.defineProperty(exports, '__esModule', {
|
||||
value: true
|
||||
});
|
||||
exports.default = 'foo';
|
||||
exports.foo = 'bar';
|
||||
var _foo = exports.foo;
|
||||
export { _foo as foo };
|
||||
${DEFAULT_EXPORT}
|
||||
_default.foo = _foo;
|
||||
${DEFAULT_EXPORT_END}`,
|
||||
${DEFAULT_EXPORT_END}
|
||||
${EXPORT_ES_MODULE}`,
|
||||
});
|
||||
});
|
||||
|
||||
Deno.test("commonjs - esModule flag only", () => {
|
||||
runTest({
|
||||
input: `Object.defineProperty(exports, "__esModule", { value: true });`,
|
||||
expected: `export {};`,
|
||||
expected: `Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
const _default = exports.default ?? exports;
|
||||
export default _default;
|
||||
export var __require = exports;
|
||||
${EXPORT_ES_MODULE}`,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -129,14 +148,26 @@ Deno.test("commonjs - esModule flag only #2", () => {
|
||||
runTest({
|
||||
input:
|
||||
`Object.defineProperty(module.exports, "__esModule", { value: true });`,
|
||||
expected: `export {};`,
|
||||
expected: `Object.defineProperty(module.exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
const _default = exports.default ?? exports;
|
||||
export default _default;
|
||||
export var __require = exports;
|
||||
${EXPORT_ES_MODULE}`,
|
||||
});
|
||||
});
|
||||
|
||||
Deno.test("commonjs - esModule flag only minified #3", () => {
|
||||
runTest({
|
||||
input: `Object.defineProperty(exports, '__esModule', { value: !0 });`,
|
||||
expected: `export {};`,
|
||||
expected: `Object.defineProperty(exports, '__esModule', {
|
||||
value: !0
|
||||
});
|
||||
const _default = exports.default ?? exports;
|
||||
export default _default;
|
||||
export var __require = exports;
|
||||
${EXPORT_ES_MODULE}`,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -147,6 +178,9 @@ exports.foo = 'bar';
|
||||
exports.bar = 'foo';
|
||||
`,
|
||||
expected: `${INIT}
|
||||
Object.defineProperty(exports, '__esModule', {
|
||||
value: true
|
||||
});
|
||||
exports.foo = 'bar';
|
||||
exports.bar = 'foo';
|
||||
var _foo = exports.foo;
|
||||
@@ -155,7 +189,8 @@ export { _foo as foo, _bar as bar };
|
||||
${DEFAULT_EXPORT}
|
||||
_default.foo = _foo;
|
||||
_default.bar = _bar;
|
||||
${DEFAULT_EXPORT_END}`,
|
||||
${DEFAULT_EXPORT_END}
|
||||
${EXPORT_ES_MODULE}`,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -228,13 +263,17 @@ Deno.test("commonjs - duplicate exports", () => {
|
||||
exports.trace = void 0;
|
||||
exports.trace = 'foo'`,
|
||||
expected: `${INIT}
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.trace = void 0;
|
||||
exports.trace = 'foo';
|
||||
var _trace = exports.trace;
|
||||
export { _trace as trace };
|
||||
${DEFAULT_EXPORT}
|
||||
_default.trace = _trace;
|
||||
${DEFAULT_EXPORT_END}`,
|
||||
${DEFAULT_EXPORT_END}
|
||||
${EXPORT_ES_MODULE}`,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -244,12 +283,16 @@ Deno.test("commonjs - cleared exports", () => {
|
||||
exports.foo = exports.bar = void 0;
|
||||
exports.foo = 'foo'`,
|
||||
expected: `${INIT}
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.foo = 'foo';
|
||||
var _foo = exports.foo;
|
||||
export { _foo as foo };
|
||||
${DEFAULT_EXPORT}
|
||||
_default.foo = _foo;
|
||||
${DEFAULT_EXPORT_END}`,
|
||||
${DEFAULT_EXPORT_END}
|
||||
${EXPORT_ES_MODULE}`,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -291,13 +334,17 @@ Deno.test("commonjs - define exports #3", () => {
|
||||
exports._globalThis = void 0;
|
||||
exports._globalThis = typeof globalThis === 'object' ? globalThis : global;`,
|
||||
expected: `${INIT}
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports._globalThis = void 0;
|
||||
exports._globalThis = typeof globalThis === 'object' ? globalThis : global;
|
||||
var _globalThis = exports._globalThis;
|
||||
export { _globalThis };
|
||||
${DEFAULT_EXPORT}
|
||||
_default._globalThis = _globalThis;
|
||||
${DEFAULT_EXPORT_END}`,
|
||||
${DEFAULT_EXPORT_END}
|
||||
${EXPORT_ES_MODULE}`,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -307,13 +354,17 @@ Deno.test("commonjs - named function", () => {
|
||||
function foo() {};
|
||||
exports.foo = foo;`,
|
||||
expected: `${INIT}
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
function foo() {}
|
||||
exports.foo = foo;
|
||||
var _foo = exports.foo;
|
||||
export { _foo as foo };
|
||||
${DEFAULT_EXPORT}
|
||||
_default.foo = _foo;
|
||||
${DEFAULT_EXPORT_END}`,
|
||||
${DEFAULT_EXPORT_END}
|
||||
${EXPORT_ES_MODULE}`,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -324,7 +375,7 @@ Deno.test("commonjs - detect esbuild shims", () => {
|
||||
import * as _ns from "./globalThis";
|
||||
export * from "./globalThis";
|
||||
${DEFAULT_EXPORT}
|
||||
Object.assign(_default, _ns);
|
||||
for (var _k in _ns) if (_k !== "default" && _k !== "__esModule" && Object.prototype.hasOwnProperty.call(_ns, _k)) _default[_k] = _ns[_k];
|
||||
${DEFAULT_EXPORT_END}`,
|
||||
});
|
||||
});
|
||||
@@ -363,6 +414,9 @@ var DiagLogLevel;
|
||||
DiagLogLevel[DiagLogLevel["ALL"] = 9999] = "ALL";
|
||||
})(DiagLogLevel = exports.DiagLogLevel || (exports.DiagLogLevel = {}));`,
|
||||
expected: `${INIT}
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.DiagLogLevel = void 0;
|
||||
var DiagLogLevel;
|
||||
(function (DiagLogLevel) {
|
||||
@@ -372,7 +426,8 @@ var _DiagLogLevel = exports.DiagLogLevel;
|
||||
export { _DiagLogLevel as DiagLogLevel };
|
||||
${DEFAULT_EXPORT}
|
||||
_default.DiagLogLevel = _DiagLogLevel;
|
||||
${DEFAULT_EXPORT_END}`,
|
||||
${DEFAULT_EXPORT_END}
|
||||
${EXPORT_ES_MODULE}`,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -397,6 +452,9 @@ Deno.test("commonjs - export default object", () => {
|
||||
module.exports = { foo: 'bar' };
|
||||
`,
|
||||
expected: `${INIT}
|
||||
Object.defineProperty(exports, '__esModule', {
|
||||
value: true
|
||||
});
|
||||
module.exports = {
|
||||
foo: 'bar'
|
||||
};
|
||||
@@ -404,7 +462,8 @@ var _foo = exports.foo;
|
||||
export { _foo as foo };
|
||||
${DEFAULT_EXPORT}
|
||||
_default.foo = _foo;
|
||||
${DEFAULT_EXPORT_END}`,
|
||||
${DEFAULT_EXPORT_END}
|
||||
${EXPORT_ES_MODULE}`,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -457,10 +516,14 @@ var __createBinding = this && this.__createBinding || (Object.create ? function
|
||||
var __exportStar = this && this.__exportStar || function (m, exports) {
|
||||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
export * from "./node";
|
||||
${DEFAULT_EXPORT}
|
||||
Object.assign(_default, _ns);
|
||||
${DEFAULT_EXPORT_END}`,
|
||||
for (var _k in _ns) if (_k !== "default" && _k !== "__esModule" && Object.prototype.hasOwnProperty.call(_ns, _k)) _default[_k] = _ns[_k];
|
||||
${DEFAULT_EXPORT_END}
|
||||
${EXPORT_ES_MODULE}`,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -576,7 +639,7 @@ ${DEFAULT_EXPORT_END}`,
|
||||
});
|
||||
});
|
||||
|
||||
Deno.test("commonjs - re-export", () => {
|
||||
Deno.test("commonjs - re-export #2", () => {
|
||||
runTest({
|
||||
input: `module.exports = require("foo");`,
|
||||
expected: `${INIT}
|
||||
@@ -621,3 +684,38 @@ export default module.exports;
|
||||
export var create = module.exports.create;`,
|
||||
});
|
||||
});
|
||||
|
||||
Deno.test("commonjs - minified __esModule", () => {
|
||||
runTest({
|
||||
filename: "foo.js",
|
||||
input: `
|
||||
const m = module.exports;
|
||||
const a = Object.defineProperty;
|
||||
a(m, "__esModule", { value: !0 });`,
|
||||
expected: `const m = module.exports;
|
||||
const a = Object.defineProperty;
|
||||
a(m, "__esModule", {
|
||||
value: !0
|
||||
});
|
||||
${DEFAULT_EXPORT}
|
||||
${DEFAULT_EXPORT_END}
|
||||
export var __esModule = exports.__esModule;`,
|
||||
});
|
||||
});
|
||||
|
||||
Deno.test("commonjs - esbuild __importDefault", () => {
|
||||
runTest({
|
||||
input:
|
||||
`var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
const node_events_1 = __importDefault(require("node:events"));`,
|
||||
expected: `import * as _mod from "node:events";
|
||||
var __importDefault = this && this.__importDefault || function (mod) {
|
||||
return mod && mod.__esModule ? mod : {
|
||||
"default": mod
|
||||
};
|
||||
};
|
||||
const node_events_1 = __importDefault(_mod.default ?? _mod);`,
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user