Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/plugin-vite/src/plugins/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import * as babel from "@babel/core";
import babelReact from "@babel/preset-react";
import { httpAbsolute } from "./patches/http_absolute.ts";
import { JS_REG, JSX_REG } from "../utils.ts";
import { builtinModules } from "node:module";

const BUILTINS = new Set(builtinModules);

interface DenoState {
type: RequestedModuleType;
Expand Down Expand Up @@ -49,6 +52,15 @@ export function deno(): Plugin {
return true;
},
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 original = id;
Expand Down
16 changes: 13 additions & 3 deletions packages/plugin-vite/src/plugins/patches/commonjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ export function cjsPlugin(
}, state);
},
exit(path, state) {
const isESM = state.get(IS_ESM);
if (isESM) return;

const body = path.get("body");
const requires = state.get(REQUIRE_CALLS);
if (requires !== undefined) {
Expand All @@ -71,9 +74,8 @@ export function cjsPlugin(
const exported = state.get(EXPORTED);
const exportedNs = state.get(EXPORTED_NAMESPACES);
const needsRequireImport = state.get(NEEDS_REQUIRE_IMPORT);
const isESM = state.get(IS_ESM);

if (!isESM && needsRequireImport) {
if (needsRequireImport) {
// Inject:
// ```ts
// import { createRequire } from "node:module";
Expand Down Expand Up @@ -292,6 +294,7 @@ export function cjsPlugin(
},
},
CallExpression(path, state) {
if (state.get(IS_ESM)) return;
const exported = state.get(EXPORTED);

if (isObjEsModuleFlag(t, path.node)) {
Expand Down Expand Up @@ -384,6 +387,7 @@ export function cjsPlugin(
},
MemberExpression: {
exit(path, state) {
if (state.get(IS_ESM)) return;
if (
t.isIdentifier(path.node.object) &&
path.node.object.name === "exports" &&
Expand All @@ -399,6 +403,7 @@ export function cjsPlugin(
},
ExpressionStatement: {
enter(path, state) {
if (state.get(IS_ESM)) return;
// Check: Object.defineProperty(module.exports) "__esModule" ...)
// Check: Object.defineProperty(exports) "__esModule" ...)
if (
Expand Down Expand Up @@ -551,6 +556,7 @@ export function cjsPlugin(
}
},
exit(path, state) {
if (state.get(IS_ESM)) return;
const exported = state.get(EXPORTED);
const expr = path.get("expression");

Expand Down Expand Up @@ -598,7 +604,9 @@ export function cjsPlugin(
path.remove();
}
},
ConditionalExpression(path) {
ConditionalExpression(path, state) {
if (state.get(IS_ESM)) return;

if (
t.isBinaryExpression(path.node.test) &&
t.isUnaryExpression(path.node.test.left) &&
Expand All @@ -611,6 +619,8 @@ export function cjsPlugin(
}
},
AssignmentExpression(path, state) {
if (state.get(IS_ESM)) return;

const exported = state.get(EXPORTED);
const aliased = state.get(ALIASED);
if (aliased === undefined) return;
Expand Down
12 changes: 12 additions & 0 deletions packages/plugin-vite/src/plugins/patches/commonjs_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -609,3 +609,15 @@ 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;`,
});
});
Loading