fix(vite): keep require calls in ESM file (#3433)
Detect if a file is ESM based on file extension or finding ESM-only statements. When we know that it's an ESM file we should keep `require()` calls as is, because some libraries use it as a conditional check like `npm:motion`. Fixes https://github.com/denoland/fresh/issues/3429
This commit is contained in:
@@ -11,9 +11,20 @@ export function cjsPlugin(
|
|||||||
const ALIASED = "aliased";
|
const ALIASED = "aliased";
|
||||||
const REEXPORT = "re-export";
|
const REEXPORT = "re-export";
|
||||||
const NEEDS_REQUIRE_IMPORT = "needsRequireImport";
|
const NEEDS_REQUIRE_IMPORT = "needsRequireImport";
|
||||||
|
const IS_ESM = "isESM";
|
||||||
|
|
||||||
return {
|
return {
|
||||||
name: "fresh-cjs-esm",
|
name: "fresh-cjs-esm",
|
||||||
|
pre(file) {
|
||||||
|
const filename = file.opts.filename;
|
||||||
|
if (filename) {
|
||||||
|
if (filename.endsWith(".mjs") || filename.endsWith(".cts")) {
|
||||||
|
this.set(IS_ESM, true);
|
||||||
|
} else if (filename.endsWith(".cjs") || filename.endsWith(".cts")) {
|
||||||
|
this.set(IS_ESM, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
visitor: {
|
visitor: {
|
||||||
Program: {
|
Program: {
|
||||||
enter(path, state) {
|
enter(path, state) {
|
||||||
@@ -21,6 +32,24 @@ export function cjsPlugin(
|
|||||||
state.set(EXPORTED, new Set<string>());
|
state.set(EXPORTED, new Set<string>());
|
||||||
state.set(EXPORTED_NAMESPACES, new Set<string>());
|
state.set(EXPORTED_NAMESPACES, new Set<string>());
|
||||||
state.set(REEXPORT, null);
|
state.set(REEXPORT, null);
|
||||||
|
|
||||||
|
path.traverse({
|
||||||
|
Import(_path, state) {
|
||||||
|
state.set(IS_ESM, true);
|
||||||
|
},
|
||||||
|
ImportDeclaration(_path, state) {
|
||||||
|
state.set(IS_ESM, true);
|
||||||
|
},
|
||||||
|
ExportAllDeclaration(_path, state) {
|
||||||
|
state.set(IS_ESM, true);
|
||||||
|
},
|
||||||
|
ExportDefaultDeclaration(_path, state) {
|
||||||
|
state.set(IS_ESM, true);
|
||||||
|
},
|
||||||
|
ExportNamedDeclaration(_path, state) {
|
||||||
|
state.set(IS_ESM, true);
|
||||||
|
},
|
||||||
|
}, state);
|
||||||
},
|
},
|
||||||
exit(path, state) {
|
exit(path, state) {
|
||||||
const body = path.get("body");
|
const body = path.get("body");
|
||||||
@@ -42,8 +71,9 @@ 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 (needsRequireImport) {
|
if (!isESM && needsRequireImport) {
|
||||||
// Inject:
|
// Inject:
|
||||||
// ```ts
|
// ```ts
|
||||||
// import { createRequire } from "node:module";
|
// import { createRequire } from "node:module";
|
||||||
|
|||||||
@@ -2,9 +2,11 @@ import { expect } from "@std/expect/expect";
|
|||||||
import * as babel from "@babel/core";
|
import * as babel from "@babel/core";
|
||||||
import { cjsPlugin } from "../patches/commonjs.ts";
|
import { cjsPlugin } from "../patches/commonjs.ts";
|
||||||
|
|
||||||
function runTest(options: { input: string; expected: string }) {
|
function runTest(
|
||||||
|
options: { input: string; expected: string; filename?: string },
|
||||||
|
) {
|
||||||
const res = babel.transformSync(options.input, {
|
const res = babel.transformSync(options.input, {
|
||||||
filename: "foo.js",
|
filename: options.filename ?? "foo.js",
|
||||||
babelrc: false,
|
babelrc: false,
|
||||||
plugins: [cjsPlugin],
|
plugins: [cjsPlugin],
|
||||||
});
|
});
|
||||||
@@ -585,3 +587,25 @@ ${DEFAULT_EXPORT}
|
|||||||
${DEFAULT_EXPORT_END}`,
|
${DEFAULT_EXPORT_END}`,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Deno.test("commonjs - keep require() in .mjs", () => {
|
||||||
|
runTest({
|
||||||
|
filename: "foo.mjs",
|
||||||
|
input: `try { require("foo"); } catch {}`,
|
||||||
|
expected: `try {
|
||||||
|
require("foo");
|
||||||
|
} catch {}`,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
Deno.test("commonjs - keep conditional require() in ESM file", () => {
|
||||||
|
runTest({
|
||||||
|
filename: "foo.mjs",
|
||||||
|
input: `try { require("foo"); } catch {};
|
||||||
|
export {};`,
|
||||||
|
expected: `try {
|
||||||
|
require("foo");
|
||||||
|
} catch {}
|
||||||
|
export {};`,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user