1 /**
2 * Guards the CJS build of @medusajs/admin-bundler against esbuild rewriting
3 * `await import("<pkg>")` into `require("<pkg>")`.
4 *
5 * This package is published as CommonJS (tsup `format: ["cjs"]`), but some of
6 * its dependencies are ESM-only and can therefore only be loaded through a
7 * genuine dynamic `import()`:
8 *
9 * - `vite` dropped its CJS Node API in v6
10 * - `@vitejs/plugin-react` dropped its `require` export condition in v5
11 *
12 * If esbuild downlevels those dynamic imports, the bundle still typechecks and
13 * still builds — it only fails at runtime, with `ERR_REQUIRE_ESM`, for every
14 * `medusa develop` / `medusa build` / `medusa plugin:build` invocation. This
15 * check turns that silent, ship-able failure into a build error.
16 */
17 import { readFileSync } from "node:fs"
18 import path from "node:path"
19 import { fileURLToPath } from "node:url"
20
21 const ESM_ONLY_DEPENDENCIES = ["vite", "@vitejs/plugin-react"]
22
23 const bundlePath = path.resolve(
24 path.dirname(fileURLToPath(import.meta.url)),
25 "../dist/index.js"
26 )
27
28 let bundle
29 try {
30 bundle = readFileSync(bundlePath, "utf-8")
31 } catch {
32 console.error(
33 `[assert-esm-imports] Could not read ${bundlePath}. Run the build first.`
34 )
35 process.exit(1)
36 }
37
38 /** Matches `require("pkg")` / `require('pkg')`, tolerating inner whitespace. */
39 const requireCall = (pkg) =>
40 new RegExp(`require\\(\\s*["']${escape(pkg)}["']\\s*\\)`)
41
42 /** Matches `import("pkg")` / `import('pkg')`, tolerating inner whitespace. */
43 const importCall = (pkg) =>
44 new RegExp(`import\\(\\s*["']${escape(pkg)}["']\\s*\\)`)
45
46 function escape(value) {
47 return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
48 }
49
50 const failures = []
51
52 for (const dependency of ESM_ONLY_DEPENDENCIES) {
53 if (requireCall(dependency).test(bundle)) {
54 failures.push(
55 ` - \`${dependency}\` is loaded with require(). It is ESM-only, so this ` +
56 `throws ERR_REQUIRE_ESM at runtime.`
57 )
58 continue
59 }
60
61 if (!importCall(dependency).test(bundle)) {
62 failures.push(
63 ` - \`${dependency}\` is not loaded with a dynamic import(). Either the ` +
64 `import was removed, or esbuild rewrote it into a form this check ` +
65 `does not recognise.`
66 )
67 }
68 }
69
70 if (failures.length) {
71 console.error(
72 [
73 `[assert-esm-imports] ${bundlePath} does not load its ESM-only dependencies correctly:`,
74 ...failures,
75 "",
76 "Fix by keeping these as `await import(...)` in src/ and ensuring tsup does",
77 "not downlevel them (see `target` in tsup.config.cjs). If esbuild insists on",
78 "rewriting them, route the imports through a helper esbuild cannot analyse:",
79 ' const importESM = (s) => (new Function("s", "return import(s)"))(s)',
80 "",
81 "Do NOT convert this package to ESM — @medusajs/framework requires it from CJS.",
82 ].join("\n")
83 )
84 process.exit(1)
85 }
86
87 console.log(
88 `[assert-esm-imports] OK — ${ESM_ONLY_DEPENDENCIES.join(
89 ", "
90 )} are loaded via dynamic import().`
91 )