fix(vite): mark transformed cjs with __esModule (#3575)

Fixes https://github.com/denoland/fresh/issues/3565
This commit is contained in:
Marvin Hagemeister
2025-10-24 19:54:37 +02:00
committed by GitHub
parent 5a228625ea
commit 7b6d6315d1
7 changed files with 192 additions and 33 deletions
@@ -0,0 +1,7 @@
import * as qs from "qs";
export default function Page() {
// deno-lint-ignore no-console
console.log(qs);
return <h1>qs</h1>;
}
@@ -0,0 +1,7 @@
import * as stripe from "stripe";
export default function Page() {
// deno-lint-ignore no-console
console.log(stripe);
return <h1>stripe</h1>;
}
+3
View File
@@ -31,13 +31,16 @@
"@tailwindcss/vite": "npm:@tailwindcss/vite@^4.1.12",
"@types/babel__core": "npm:@types/babel__core@^7.20.5",
"@types/node": "npm:@types/node@^24.1.0",
"@types/qs": "npm:@types/qs@^6.14.0",
"feed": "npm:feed@^5.1.0",
"fresh": "jsr:@fresh/core@^2.0.0",
"ioredis": "npm:ioredis@^5.7.0",
"mime-db": "npm:mime-db@^1.54.0",
"pg": "npm:pg@^8.16.3",
"preact": "npm:preact@^10.26.9",
"qs": "npm:qs@^6.14.0",
"rollup-plugin-visualizer": "npm:rollup-plugin-visualizer@^6.0.3",
"stripe": "npm:stripe@^19.1.0",
"vite": "npm:vite@^7.1.4",
"vite-plugin-inspect": "npm:vite-plugin-inspect@^11.3.2"
}
@@ -135,6 +135,27 @@ export function cjsPlugin(
}
if (exported.size > 0 || exportedNs.size > 0 || hasEsModule) {
path.unshiftContainer(
"body",
t.expressionStatement(
t.callExpression(
t.memberExpression(
t.identifier("Object"),
t.identifier("defineProperty"),
),
[
t.identifier("exports"),
t.stringLiteral("__esModule"),
t.objectExpression([
t.objectProperty(
t.identifier("value"),
t.booleanLiteral(true),
),
]),
],
),
),
);
path.unshiftContainer(
"body",
t.expressionStatement(
@@ -247,21 +268,6 @@ export function cjsPlugin(
]),
);
for (const [local, exported] of exportNamed.entries()) {
if (exported === "default") continue;
path.pushContainer(
"body",
t.expressionStatement(
t.assignmentExpression(
"=",
t.memberExpression(id, t.identifier(exported)),
t.identifier(local),
),
),
);
}
for (let i = 0; i < mappedNs.length; i++) {
const mapped = mappedNs[i];
@@ -24,6 +24,9 @@ Object.defineProperty(module, "exports", {
set(value) {
exports = value;
}
});
Object.defineProperty(exports, "__esModule", {
value: true
});`;
const DEFAULT_EXPORT = `const _default = exports.default ?? exports;`;
@@ -65,7 +68,6 @@ exports.foo = 'foo';
var _foo = exports.foo;
export { _foo as foo };
${DEFAULT_EXPORT}
_default.foo = _foo;
${DEFAULT_EXPORT_END}
${EXPORT_ES_MODULE}`,
});
@@ -83,7 +85,6 @@ module.exports.foo = 'foo';
var _foo = exports.foo;
export { _foo as foo };
${DEFAULT_EXPORT}
_default.foo = _foo;
${DEFAULT_EXPORT_END}
${EXPORT_ES_MODULE}`,
});
@@ -104,7 +105,6 @@ const foo = 'also bar';
var _foo = exports.foo;
export { _foo as foo };
${DEFAULT_EXPORT}
_default.foo = _foo;
${DEFAULT_EXPORT_END}
${EXPORT_ES_MODULE}`,
});
@@ -125,7 +125,6 @@ exports.foo = 'bar';
var _foo = exports.foo;
export { _foo as foo };
${DEFAULT_EXPORT}
_default.foo = _foo;
${DEFAULT_EXPORT_END}
${EXPORT_ES_MODULE}`,
});
@@ -190,8 +189,6 @@ var _foo = exports.foo;
var _bar = exports.bar;
export { _foo as foo, _bar as bar };
${DEFAULT_EXPORT}
_default.foo = _foo;
_default.bar = _bar;
${DEFAULT_EXPORT_END}
${EXPORT_ES_MODULE}`,
});
@@ -274,7 +271,6 @@ exports.trace = 'foo';
var _trace = exports.trace;
export { _trace as trace };
${DEFAULT_EXPORT}
_default.trace = _trace;
${DEFAULT_EXPORT_END}
${EXPORT_ES_MODULE}`,
});
@@ -293,7 +289,6 @@ exports.foo = 'foo';
var _foo = exports.foo;
export { _foo as foo };
${DEFAULT_EXPORT}
_default.foo = _foo;
${DEFAULT_EXPORT_END}
${EXPORT_ES_MODULE}`,
});
@@ -310,7 +305,6 @@ exports.foo = utils_1.foo;
var _foo = exports.foo;
export { _foo as foo };
${DEFAULT_EXPORT}
_default.foo = _foo;
${DEFAULT_EXPORT_END}`,
});
});
@@ -326,7 +320,6 @@ exports.foo = utils_1.foo;
var _foo = exports.foo;
export { _foo as foo };
${DEFAULT_EXPORT}
_default.foo = _foo;
${DEFAULT_EXPORT_END}`,
});
});
@@ -345,7 +338,6 @@ exports._globalThis = typeof globalThis === 'object' ? globalThis : global;
var _globalThis = exports._globalThis;
export { _globalThis };
${DEFAULT_EXPORT}
_default._globalThis = _globalThis;
${DEFAULT_EXPORT_END}
${EXPORT_ES_MODULE}`,
});
@@ -365,7 +357,6 @@ exports.foo = foo;
var _foo = exports.foo;
export { _foo as foo };
${DEFAULT_EXPORT}
_default.foo = _foo;
${DEFAULT_EXPORT_END}
${EXPORT_ES_MODULE}`,
});
@@ -403,7 +394,6 @@ exports.VERSION = '1.9.0';
var _VERSION = exports.VERSION;
export { _VERSION as VERSION };
${DEFAULT_EXPORT}
_default.VERSION = _VERSION;
${DEFAULT_EXPORT_END}`,
});
});
@@ -428,7 +418,6 @@ var DiagLogLevel;
var _DiagLogLevel = exports.DiagLogLevel;
export { _DiagLogLevel as DiagLogLevel };
${DEFAULT_EXPORT}
_default.DiagLogLevel = _DiagLogLevel;
${DEFAULT_EXPORT_END}
${EXPORT_ES_MODULE}`,
});
@@ -464,7 +453,6 @@ module.exports = {
var _foo = exports.foo;
export { _foo as foo };
${DEFAULT_EXPORT}
_default.foo = _foo;
${DEFAULT_EXPORT_END}
${EXPORT_ES_MODULE}`,
});
@@ -482,7 +470,6 @@ Deno.test("commonjs - detect iife wrapper", () => {
var _foo = exports.foo;
export { _foo as foo };
${DEFAULT_EXPORT}
_default.foo = _foo;
${DEFAULT_EXPORT_END}`,
});
});
@@ -540,7 +527,6 @@ module.exports = {
var _foo = exports.foo;
export { _foo as foo };
${DEFAULT_EXPORT}
_default.foo = _foo;
${DEFAULT_EXPORT_END}`,
});
});
@@ -637,7 +623,6 @@ Deno.test("commonjs - wrapped iife binding", () => {
var _foo = exports.foo;
export { _foo as foo };
${DEFAULT_EXPORT}
_default.foo = _foo;
${DEFAULT_EXPORT_END}`,
});
});
@@ -453,6 +453,28 @@ Deno.test({
sanitizeResources: false,
});
Deno.test({
name: "vite dev - qs",
fn: async () => {
const res = await fetch(`${demoServer.address()}/tests/qs`);
const text = await res.text();
expect(text).toContain("<h1>qs</h1>");
},
sanitizeOps: false,
sanitizeResources: false,
});
Deno.test({
name: "vite dev - stripe",
fn: async () => {
const res = await fetch(`${demoServer.address()}/tests/stripe`);
const text = await res.text();
expect(text).toContain("<h1>stripe</h1>");
},
sanitizeOps: false,
sanitizeResources: false,
});
Deno.test({
name: "vite dev - static index.html",
fn: async () => {