fix(vite): unable to load transpiled commonjs with node builtins (#3541)
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
"use strict";
|
||||
// deno-lint-ignore no-var
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const assert_1 = __importDefault(require("assert"));
|
||||
(0, assert_1.default)(true);
|
||||
@@ -0,0 +1,7 @@
|
||||
import * as maxmind from "../../fixtures/maxmind.cjs";
|
||||
|
||||
export default function Page() {
|
||||
// deno-lint-ignore no-console
|
||||
console.log(maxmind);
|
||||
return <h1>maxmind</h1>;
|
||||
}
|
||||
@@ -425,20 +425,24 @@ export function cjsPlugin(
|
||||
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"),
|
||||
if (isNodeBuiltin(source.value)) {
|
||||
path.replaceWith(t.objectExpression([
|
||||
t.objectProperty(
|
||||
t.identifier("__esModule"),
|
||||
t.booleanLiteral(true),
|
||||
),
|
||||
t.cloneNode(id, true),
|
||||
));
|
||||
t.objectProperty(
|
||||
t.identifier("default"),
|
||||
t.logicalExpression(
|
||||
"??",
|
||||
t.memberExpression(
|
||||
t.cloneNode(id, true),
|
||||
t.identifier("default"),
|
||||
),
|
||||
t.cloneNode(id, true),
|
||||
),
|
||||
),
|
||||
]));
|
||||
} else {
|
||||
path.replaceWith(t.cloneNode(id, true));
|
||||
}
|
||||
@@ -786,3 +790,11 @@ function isObjEsModuleFlag(
|
||||
node.arguments[1].value === "__esModule" &&
|
||||
t.isObjectExpression(node.arguments[2]);
|
||||
}
|
||||
|
||||
function isNodeBuiltin(specifier: string): boolean {
|
||||
return BUILTINS.has(specifier) || (
|
||||
specifier.startsWith("node:")
|
||||
? BUILTINS.has(specifier.slice("node:".length))
|
||||
: BUILTINS.has(`node:${specifier}`)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -720,6 +720,9 @@ var __importDefault = this && this.__importDefault || function (mod) {
|
||||
"default": mod
|
||||
};
|
||||
};
|
||||
const node_events_1 = __importDefault(_mod.default ?? _mod);`,
|
||||
const node_events_1 = __importDefault({
|
||||
__esModule: true,
|
||||
default: _mod.default ?? _mod
|
||||
});`,
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user