Skip to content

Commit efef517

Browse files
fix(vite): unable to load transpiled commonjs with node builtins (#3541)
1 parent 4b069ee commit efef517

File tree

4 files changed

+43
-13
lines changed

4 files changed

+43
-13
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"use strict";
2+
// deno-lint-ignore no-var
3+
var __importDefault = (this && this.__importDefault) || function (mod) {
4+
return (mod && mod.__esModule) ? mod : { "default": mod };
5+
};
6+
Object.defineProperty(exports, "__esModule", { value: true });
7+
const assert_1 = __importDefault(require("assert"));
8+
(0, assert_1.default)(true);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import * as maxmind from "../../fixtures/maxmind.cjs";
2+
3+
export default function Page() {
4+
// deno-lint-ignore no-console
5+
console.log(maxmind);
6+
return <h1>maxmind</h1>;
7+
}

packages/plugin-vite/src/plugins/patches/commonjs.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -425,20 +425,24 @@ export function cjsPlugin(
425425
t.isIdentifier(path.parentPath.node.callee) &&
426426
path.parentPath.node.callee.name === "__importDefault"
427427
) {
428-
if (
429-
BUILTINS.has(source.value) ||
430-
source.value.startsWith("node:")
431-
? BUILTINS.has(source.value.slice("node:".length))
432-
: BUILTINS.has(`node:${source.value}`)
433-
) {
434-
path.replaceWith(t.logicalExpression(
435-
"??",
436-
t.memberExpression(
437-
t.cloneNode(id, true),
428+
if (isNodeBuiltin(source.value)) {
429+
path.replaceWith(t.objectExpression([
430+
t.objectProperty(
431+
t.identifier("__esModule"),
432+
t.booleanLiteral(true),
433+
),
434+
t.objectProperty(
438435
t.identifier("default"),
436+
t.logicalExpression(
437+
"??",
438+
t.memberExpression(
439+
t.cloneNode(id, true),
440+
t.identifier("default"),
441+
),
442+
t.cloneNode(id, true),
443+
),
439444
),
440-
t.cloneNode(id, true),
441-
));
445+
]));
442446
} else {
443447
path.replaceWith(t.cloneNode(id, true));
444448
}
@@ -786,3 +790,11 @@ function isObjEsModuleFlag(
786790
node.arguments[1].value === "__esModule" &&
787791
t.isObjectExpression(node.arguments[2]);
788792
}
793+
794+
function isNodeBuiltin(specifier: string): boolean {
795+
return BUILTINS.has(specifier) || (
796+
specifier.startsWith("node:")
797+
? BUILTINS.has(specifier.slice("node:".length))
798+
: BUILTINS.has(`node:${specifier}`)
799+
);
800+
}

packages/plugin-vite/src/plugins/patches/commonjs_test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,9 @@ var __importDefault = this && this.__importDefault || function (mod) {
720720
"default": mod
721721
};
722722
};
723-
const node_events_1 = __importDefault(_mod.default ?? _mod);`,
723+
const node_events_1 = __importDefault({
724+
__esModule: true,
725+
default: _mod.default ?? _mod
726+
});`,
724727
});
725728
});

0 commit comments

Comments
 (0)