rolldown-plugin-require-cjs 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -12,7 +12,6 @@ interface Options {
12
12
  include?: FilterPattern;
13
13
  exclude?: FilterPattern;
14
14
  order?: "pre" | "post" | undefined;
15
- cwd?: string;
16
15
  /**
17
16
  * A function to determine whether a module should be transformed.
18
17
  * Return `true` to force transformation, `false` to skip transformation,
package/dist/index.js CHANGED
@@ -1,13 +1,13 @@
1
- import { builtinModules, createRequire } from "node:module";
1
+ import { createRequire, isBuiltin } from "node:module";
2
2
  import { readFile } from "node:fs/promises";
3
3
  import path from "node:path";
4
+ import process from "node:process";
4
5
  import { init, parse } from "cjs-module-lexer";
5
6
  import { up } from "empathic/package";
6
7
  import { MagicStringAST, generateTransform } from "magic-string-ast";
7
8
  import { resolvePathSync } from "mlly";
8
9
  import { parseAst } from "rolldown/parseAst";
9
10
  import { createFilter } from "unplugin-utils";
10
- import process from "node:process";
11
11
 
12
12
  //#region rolldown:runtime
13
13
  var __require = /* @__PURE__ */ createRequire(import.meta.url);
@@ -20,10 +20,9 @@ function resolveOptions(options) {
20
20
  options.shouldTransform = (id) => shouldTransform.includes(id);
21
21
  }
22
22
  return {
23
- include: options.include || [/\.[cm]?[jt]sx?$/],
23
+ include: options.include || [/\.m?[jt]sx?$/],
24
24
  exclude: options.exclude || [/node_modules/, /\.d\.[cm]?ts$/],
25
25
  order: "order" in options ? options.order : "pre",
26
- cwd: options.cwd || process.cwd(),
27
26
  shouldTransform: options.shouldTransform,
28
27
  builtinNodeModules: !!options.builtinNodeModules
29
28
  };
@@ -34,8 +33,9 @@ function resolveOptions(options) {
34
33
  let initted = false;
35
34
  const REQUIRE = `__cjs_require`;
36
35
  function RequireCJS(userOptions = {}) {
37
- const { include, exclude, order, cwd, shouldTransform, builtinNodeModules } = resolveOptions(userOptions);
36
+ const { include, exclude, order, shouldTransform, builtinNodeModules } = resolveOptions(userOptions);
38
37
  const filter = createFilter(include, exclude);
38
+ let cwd;
39
39
  return {
40
40
  name: "rolldown-plugin-require-cjs",
41
41
  async buildStart() {
@@ -46,6 +46,7 @@ function RequireCJS(userOptions = {}) {
46
46
  },
47
47
  options(options) {
48
48
  if (options.platform !== "node") this.error("`rolldown-plugin-require-cjs` plugin is designed only for the Node.js environment. Please make sure to set `platform: \"node\"` in the options.");
49
+ cwd = options.cwd || process.cwd();
49
50
  },
50
51
  outputOptions(options) {
51
52
  if (![
@@ -56,7 +57,7 @@ function RequireCJS(userOptions = {}) {
56
57
  },
57
58
  renderChunk: {
58
59
  order,
59
- async handler(code, { fileName }) {
60
+ async handler(code, { fileName }, { file, dir }) {
60
61
  if (!filter(fileName)) return;
61
62
  const { body } = parseAst(code, { lang: void 0 }, fileName);
62
63
  const s = new MagicStringAST(code);
@@ -64,8 +65,10 @@ function RequireCJS(userOptions = {}) {
64
65
  for (const stmt of body) if (stmt.type === "ImportDeclaration") {
65
66
  if (stmt.importKind === "type") continue;
66
67
  const source = stmt.source.value;
67
- const isBuiltinModule = builtinNodeModules && (builtinModules.includes(source) || source.startsWith("node:"));
68
- if (!(isBuiltinModule || (await shouldTransform?.(source, cwd) ?? await isPureCJS(source, cwd)))) continue;
68
+ const isBuiltinModule = builtinNodeModules && isBuiltin(source);
69
+ const distFilename = file || (dir ? path.join(dir, fileName) : fileName);
70
+ const importer = cwd ? path.resolve(cwd, distFilename) : distFilename;
71
+ if (!(isBuiltinModule || (await shouldTransform?.(source, importer) ?? await isPureCJS(source, importer)))) continue;
69
72
  if (stmt.specifiers.length === 0) {
70
73
  if (isBuiltinModule) s.removeNode(stmt);
71
74
  else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rolldown-plugin-require-cjs",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "Transform ESM imports to CJS requires when the imported module is pure CJS.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -33,23 +33,23 @@
33
33
  "dependencies": {
34
34
  "cjs-module-lexer": "^2.1.0",
35
35
  "empathic": "^2.0.0",
36
- "magic-string-ast": "^1.0.2",
36
+ "magic-string-ast": "^1.0.3",
37
37
  "mlly": "^1.8.0",
38
- "unplugin-utils": "^0.3.0"
38
+ "unplugin-utils": "^0.3.1"
39
39
  },
40
40
  "devDependencies": {
41
41
  "@babel/parser": "^7.28.4",
42
- "@sxzz/eslint-config": "^7.2.5",
42
+ "@sxzz/eslint-config": "^7.2.7",
43
43
  "@sxzz/prettier-config": "^2.2.4",
44
44
  "@sxzz/test-utils": "^0.5.11",
45
- "@types/node": "^24.5.2",
46
- "bumpp": "^10.2.3",
47
- "eslint": "^9.36.0",
45
+ "@types/node": "^24.7.0",
46
+ "bumpp": "^10.3.1",
47
+ "eslint": "^9.37.0",
48
48
  "eslint-plugin-vue": "^10.5.0",
49
49
  "prettier": "^3.6.2",
50
- "rolldown": "1.0.0-beta.40",
51
- "tsdown": "^0.15.4",
52
- "typescript": "^5.9.2",
50
+ "rolldown": "1.0.0-beta.41",
51
+ "tsdown": "^0.15.6",
52
+ "typescript": "^5.9.3",
53
53
  "vitest": "^3.2.4"
54
54
  },
55
55
  "engines": {