rolldown-plugin-require-cjs 0.3.1 → 0.3.3

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.
@@ -2,11 +2,12 @@ import { createRequire, isBuiltin } from "node:module";
2
2
  import { readFile } from "node:fs/promises";
3
3
  import path from "node:path";
4
4
  import process from "node:process";
5
+ import { fileURLToPath, pathToFileURL } from "node:url";
5
6
  import { init, parse } from "cjs-module-lexer";
6
7
  import { up } from "empathic/package";
8
+ import { resolve } from "import-meta-resolve";
7
9
  import { MagicStringAST, generateTransform } from "magic-string-ast";
8
- import { resolvePathSync } from "mlly";
9
- import { parseAst } from "rolldown/parseAst";
10
+ import { parseSync } from "rolldown/experimental";
10
11
  import { createFilter } from "unplugin-utils";
11
12
 
12
13
  //#region rolldown:runtime
@@ -53,16 +54,16 @@ function RequireCJS(userOptions = {}) {
53
54
  "es",
54
55
  "esm",
55
56
  "module"
56
- ].includes(options.format)) throw new Error("`rolldown-plugin-require-cjs` plugin is only necessary for ESM output");
57
+ ].includes(options.format)) this.error(/* @__PURE__ */ new Error("`rolldown-plugin-require-cjs` plugin is only necessary for ESM output"));
57
58
  },
58
59
  renderChunk: {
59
60
  order,
60
61
  async handler(code, { fileName }, { file, dir }) {
61
62
  if (!filter(fileName)) return;
62
- const { body } = parseAst(code, { lang: void 0 }, fileName);
63
+ const { program } = parseSync(fileName, code);
63
64
  const s = new MagicStringAST(code);
64
65
  let usingRequire = false;
65
- for (const stmt of body) if (stmt.type === "ImportDeclaration") {
66
+ for (const stmt of program.body) if (stmt.type === "ImportDeclaration") {
66
67
  if (stmt.importKind === "type") continue;
67
68
  const source = stmt.source.value;
68
69
  const isBuiltinModule = builtinNodeModules && isBuiltin(source);
@@ -113,9 +114,11 @@ const ${REQUIRE} = __cjs_createRequire(import.meta.url);\n`;
113
114
  }
114
115
  async function isPureCJS(id, importer) {
115
116
  if (!initted) await init();
116
- if (id.startsWith("node:")) return false;
117
+ if (isBuiltin(id)) return false;
117
118
  try {
118
- const importResolved = resolvePathSync(id, { url: importer });
119
+ let importResolved = resolve(id, pathToFileURL(importer).href);
120
+ if (!importResolved.startsWith("file://")) return false;
121
+ importResolved = fileURLToPath(importResolved);
119
122
  const requireResolved = __require.resolve(id, { paths: [importer] });
120
123
  if (path.resolve(importResolved) !== path.resolve(requireResolved)) return false;
121
124
  if (importResolved.endsWith(".cjs")) return true;
package/package.json CHANGED
@@ -1,68 +1,73 @@
1
1
  {
2
2
  "name": "rolldown-plugin-require-cjs",
3
- "version": "0.3.1",
4
- "description": "Transform ESM imports to CJS requires when the imported module is pure CJS.",
5
3
  "type": "module",
4
+ "version": "0.3.3",
5
+ "description": "Transform ESM imports to CJS requires when the imported module is pure CJS.",
6
+ "author": "Kevin Deng <sxzz@sxzz.moe>",
6
7
  "license": "MIT",
8
+ "funding": "https://github.com/sponsors/sxzz",
7
9
  "homepage": "https://github.com/sxzz/rolldown-plugin-require-cjs#readme",
8
- "bugs": {
9
- "url": "https://github.com/sxzz/rolldown-plugin-require-cjs/issues"
10
- },
11
10
  "repository": {
12
11
  "type": "git",
13
12
  "url": "git+https://github.com/sxzz/rolldown-plugin-require-cjs.git"
14
13
  },
15
- "author": "Kevin Deng <sxzz@sxzz.moe>",
16
- "funding": "https://github.com/sponsors/sxzz",
17
- "files": [
18
- "dist"
19
- ],
20
- "main": "./dist/index.js",
21
- "module": "./dist/index.js",
22
- "types": "./dist/index.d.ts",
14
+ "bugs": {
15
+ "url": "https://github.com/sxzz/rolldown-plugin-require-cjs/issues"
16
+ },
23
17
  "exports": {
24
- ".": "./dist/index.js",
18
+ ".": "./dist/index.mjs",
25
19
  "./package.json": "./package.json"
26
20
  },
21
+ "main": "./dist/index.mjs",
22
+ "module": "./dist/index.mjs",
23
+ "types": "./dist/index.d.mts",
24
+ "files": [
25
+ "dist"
26
+ ],
27
27
  "publishConfig": {
28
28
  "access": "public"
29
29
  },
30
+ "engines": {
31
+ "node": ">=20.19.0"
32
+ },
30
33
  "peerDependencies": {
31
34
  "rolldown": "*"
32
35
  },
33
36
  "dependencies": {
34
- "cjs-module-lexer": "^2.1.0",
37
+ "cjs-module-lexer": "^2.1.1",
35
38
  "empathic": "^2.0.0",
39
+ "import-meta-resolve": "^4.2.0",
36
40
  "magic-string-ast": "^1.0.3",
37
- "mlly": "^1.8.0",
38
41
  "unplugin-utils": "^0.3.1"
39
42
  },
40
43
  "devDependencies": {
41
- "@babel/parser": "^7.28.4",
42
- "@sxzz/eslint-config": "^7.2.7",
43
- "@sxzz/prettier-config": "^2.2.4",
44
- "@sxzz/test-utils": "^0.5.11",
45
- "@types/node": "^24.7.0",
46
- "bumpp": "^10.3.1",
47
- "eslint": "^9.37.0",
48
- "eslint-plugin-vue": "^10.5.0",
49
- "prettier": "^3.6.2",
50
- "rolldown": "1.0.0-beta.41",
51
- "tsdown": "^0.15.6",
44
+ "@babel/parser": "^7.28.5",
45
+ "@sxzz/eslint-config": "^7.4.1",
46
+ "@sxzz/prettier-config": "^2.2.6",
47
+ "@sxzz/test-utils": "^0.5.13",
48
+ "@types/node": "^24.10.1",
49
+ "@typescript/native-preview": "7.0.0-dev.20251203.1",
50
+ "bumpp": "^10.3.2",
51
+ "eslint": "^9.39.1",
52
+ "eslint-plugin-vue": "^10.6.2",
53
+ "prettier": "^3.7.4",
54
+ "rolldown": "1.0.0-beta.53",
55
+ "tsdown": "^0.17.0-beta.6",
52
56
  "typescript": "^5.9.3",
53
- "vitest": "^3.2.4"
54
- },
55
- "engines": {
56
- "node": ">=20.19.0"
57
+ "vitest": "^4.0.15"
57
58
  },
58
59
  "prettier": "@sxzz/prettier-config",
60
+ "tsdown": {
61
+ "exports": true,
62
+ "inlineOnly": []
63
+ },
59
64
  "scripts": {
60
65
  "lint": "eslint --cache .",
61
66
  "lint:fix": "pnpm run lint --fix",
62
67
  "build": "tsdown",
63
68
  "dev": "tsdown --watch",
64
69
  "test": "vitest",
65
- "typecheck": "tsc --noEmit",
70
+ "typecheck": "tsgo --noEmit",
66
71
  "format": "prettier --cache --write .",
67
72
  "release": "bumpp"
68
73
  }
File without changes