fix(vite): improve cjs esm detection + builtins resolution (#3457)
This commit is contained in:
@@ -11,6 +11,9 @@ import * as babel from "@babel/core";
|
|||||||
import babelReact from "@babel/preset-react";
|
import babelReact from "@babel/preset-react";
|
||||||
import { httpAbsolute } from "./patches/http_absolute.ts";
|
import { httpAbsolute } from "./patches/http_absolute.ts";
|
||||||
import { JS_REG, JSX_REG } from "../utils.ts";
|
import { JS_REG, JSX_REG } from "../utils.ts";
|
||||||
|
import { builtinModules } from "node:module";
|
||||||
|
|
||||||
|
const BUILTINS = new Set(builtinModules);
|
||||||
|
|
||||||
interface DenoState {
|
interface DenoState {
|
||||||
type: RequestedModuleType;
|
type: RequestedModuleType;
|
||||||
@@ -49,6 +52,15 @@ export function deno(): Plugin {
|
|||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
async resolveId(id, importer, options) {
|
async resolveId(id, importer, options) {
|
||||||
|
if (BUILTINS.has(id)) {
|
||||||
|
if (!id.startsWith("node:") && BUILTINS.has(`node:${id}`)) {
|
||||||
|
id = `node:${id}`;
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
id,
|
||||||
|
external: true,
|
||||||
|
};
|
||||||
|
}
|
||||||
const loader = options?.ssr ? ssrLoader : browserLoader;
|
const loader = options?.ssr ? ssrLoader : browserLoader;
|
||||||
|
|
||||||
const original = id;
|
const original = id;
|
||||||
|
|||||||
@@ -52,6 +52,9 @@ export function cjsPlugin(
|
|||||||
}, state);
|
}, state);
|
||||||
},
|
},
|
||||||
exit(path, state) {
|
exit(path, state) {
|
||||||
|
const isESM = state.get(IS_ESM);
|
||||||
|
if (isESM) return;
|
||||||
|
|
||||||
const body = path.get("body");
|
const body = path.get("body");
|
||||||
const requires = state.get(REQUIRE_CALLS);
|
const requires = state.get(REQUIRE_CALLS);
|
||||||
if (requires !== undefined) {
|
if (requires !== undefined) {
|
||||||
@@ -71,9 +74,8 @@ export function cjsPlugin(
|
|||||||
const exported = state.get(EXPORTED);
|
const exported = state.get(EXPORTED);
|
||||||
const exportedNs = state.get(EXPORTED_NAMESPACES);
|
const exportedNs = state.get(EXPORTED_NAMESPACES);
|
||||||
const needsRequireImport = state.get(NEEDS_REQUIRE_IMPORT);
|
const needsRequireImport = state.get(NEEDS_REQUIRE_IMPORT);
|
||||||
const isESM = state.get(IS_ESM);
|
|
||||||
|
|
||||||
if (!isESM && needsRequireImport) {
|
if (needsRequireImport) {
|
||||||
// Inject:
|
// Inject:
|
||||||
// ```ts
|
// ```ts
|
||||||
// import { createRequire } from "node:module";
|
// import { createRequire } from "node:module";
|
||||||
@@ -292,6 +294,7 @@ export function cjsPlugin(
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
CallExpression(path, state) {
|
CallExpression(path, state) {
|
||||||
|
if (state.get(IS_ESM)) return;
|
||||||
const exported = state.get(EXPORTED);
|
const exported = state.get(EXPORTED);
|
||||||
|
|
||||||
if (isObjEsModuleFlag(t, path.node)) {
|
if (isObjEsModuleFlag(t, path.node)) {
|
||||||
@@ -384,6 +387,7 @@ export function cjsPlugin(
|
|||||||
},
|
},
|
||||||
MemberExpression: {
|
MemberExpression: {
|
||||||
exit(path, state) {
|
exit(path, state) {
|
||||||
|
if (state.get(IS_ESM)) return;
|
||||||
if (
|
if (
|
||||||
t.isIdentifier(path.node.object) &&
|
t.isIdentifier(path.node.object) &&
|
||||||
path.node.object.name === "exports" &&
|
path.node.object.name === "exports" &&
|
||||||
@@ -399,6 +403,7 @@ export function cjsPlugin(
|
|||||||
},
|
},
|
||||||
ExpressionStatement: {
|
ExpressionStatement: {
|
||||||
enter(path, state) {
|
enter(path, state) {
|
||||||
|
if (state.get(IS_ESM)) return;
|
||||||
// Check: Object.defineProperty(module.exports) "__esModule" ...)
|
// Check: Object.defineProperty(module.exports) "__esModule" ...)
|
||||||
// Check: Object.defineProperty(exports) "__esModule" ...)
|
// Check: Object.defineProperty(exports) "__esModule" ...)
|
||||||
if (
|
if (
|
||||||
@@ -551,6 +556,7 @@ export function cjsPlugin(
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
exit(path, state) {
|
exit(path, state) {
|
||||||
|
if (state.get(IS_ESM)) return;
|
||||||
const exported = state.get(EXPORTED);
|
const exported = state.get(EXPORTED);
|
||||||
const expr = path.get("expression");
|
const expr = path.get("expression");
|
||||||
|
|
||||||
@@ -598,7 +604,9 @@ export function cjsPlugin(
|
|||||||
path.remove();
|
path.remove();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
ConditionalExpression(path) {
|
ConditionalExpression(path, state) {
|
||||||
|
if (state.get(IS_ESM)) return;
|
||||||
|
|
||||||
if (
|
if (
|
||||||
t.isBinaryExpression(path.node.test) &&
|
t.isBinaryExpression(path.node.test) &&
|
||||||
t.isUnaryExpression(path.node.test.left) &&
|
t.isUnaryExpression(path.node.test.left) &&
|
||||||
@@ -611,6 +619,8 @@ export function cjsPlugin(
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
AssignmentExpression(path, state) {
|
AssignmentExpression(path, state) {
|
||||||
|
if (state.get(IS_ESM)) return;
|
||||||
|
|
||||||
const exported = state.get(EXPORTED);
|
const exported = state.get(EXPORTED);
|
||||||
const aliased = state.get(ALIASED);
|
const aliased = state.get(ALIASED);
|
||||||
if (aliased === undefined) return;
|
if (aliased === undefined) return;
|
||||||
|
|||||||
@@ -609,3 +609,15 @@ export {};`,
|
|||||||
export {};`,
|
export {};`,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Deno.test("commonjs - CJS turned ESM module", () => {
|
||||||
|
runTest({
|
||||||
|
filename: "foo.mjs",
|
||||||
|
input: `module.exports.create = confettiCannon;
|
||||||
|
export default module.exports;
|
||||||
|
export var create = module.exports.create;`,
|
||||||
|
expected: `module.exports.create = confettiCannon;
|
||||||
|
export default module.exports;
|
||||||
|
export var create = module.exports.create;`,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user