rolldown-plugin-require-cjs 0.3.3 → 0.4.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/README.md +3 -10
- package/dist/index.d.mts +0 -7
- package/dist/index.mjs +12 -22
- package/package.json +20 -21
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@ Transform ESM imports to CJS requires when the imported module is pure CJS.
|
|
|
8
8
|
|
|
9
9
|
## Why?
|
|
10
10
|
|
|
11
|
-
Some packages only provide CJS builds (e.g., [`typescript`](https://
|
|
11
|
+
Some packages only provide CJS builds (e.g., [`typescript`](https://npmx.dev/package/typescript), [`@babel/parser`](https://npmx.dev/package/@babel/parser)), and importing them using ESM syntax increases Node's `cjs-module-lexer` overhead. This plugin converts ESM imports to CJS requires for such pure CJS packages, allowing Node to skip the cjs-module-lexer step and improve performance.
|
|
12
12
|
|
|
13
13
|
If performance is insignificant for your project, please do not use this plugin, as it introduces additional complexity and maintenance overhead.
|
|
14
14
|
|
|
@@ -21,7 +21,7 @@ We encourage the JavaScript ecosystem to continue its transition toward ESM. If
|
|
|
21
21
|
## Install
|
|
22
22
|
|
|
23
23
|
```bash
|
|
24
|
-
npm i rolldown-plugin-require-cjs
|
|
24
|
+
npm i -D rolldown-plugin-require-cjs
|
|
25
25
|
```
|
|
26
26
|
|
|
27
27
|
## Options
|
|
@@ -37,13 +37,6 @@ export interface Options {
|
|
|
37
37
|
* or `undefined` to let the plugin decide automatically.
|
|
38
38
|
*/
|
|
39
39
|
shouldTransform?: string[] | TransformFn
|
|
40
|
-
/**
|
|
41
|
-
* Whether to transform Node.js built-in modules (e.g., `fs`, `path`)
|
|
42
|
-
* to `process.getBuiltinModule()` calls, which has the best performance.
|
|
43
|
-
*
|
|
44
|
-
* Note: `process.getBuiltinModule` is available since Node.js 20.16.0 and 22.3.0.
|
|
45
|
-
*/
|
|
46
|
-
builtinNodeModules?: boolean
|
|
47
40
|
}
|
|
48
41
|
|
|
49
42
|
/**
|
|
@@ -92,7 +85,7 @@ RequireCJS({
|
|
|
92
85
|
<!-- Badges -->
|
|
93
86
|
|
|
94
87
|
[npm-version-src]: https://img.shields.io/npm/v/rolldown-plugin-require-cjs.svg
|
|
95
|
-
[npm-version-href]: https://
|
|
88
|
+
[npm-version-href]: https://npmx.dev/package/rolldown-plugin-require-cjs
|
|
96
89
|
[npm-downloads-src]: https://img.shields.io/npm/dm/rolldown-plugin-require-cjs
|
|
97
90
|
[npm-downloads-href]: https://www.npmcharts.com/compare/rolldown-plugin-require-cjs?interval=30
|
|
98
91
|
[unit-test-src]: https://github.com/sxzz/rolldown-plugin-require-cjs/actions/workflows/unit-test.yml/badge.svg
|
package/dist/index.d.mts
CHANGED
|
@@ -18,13 +18,6 @@ interface Options {
|
|
|
18
18
|
* or `undefined` to let the plugin decide automatically.
|
|
19
19
|
*/
|
|
20
20
|
shouldTransform?: string[] | TransformFn;
|
|
21
|
-
/**
|
|
22
|
-
* Whether to transform Node.js built-in modules (e.g., `fs`, `path`)
|
|
23
|
-
* to `process.getBuiltinModule()` calls, which has the best performance.
|
|
24
|
-
*
|
|
25
|
-
* Note: `process.getBuiltinModule` is available since Node.js 20.16.0 and 22.3.0.
|
|
26
|
-
*/
|
|
27
|
-
builtinNodeModules?: boolean;
|
|
28
21
|
}
|
|
29
22
|
type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U;
|
|
30
23
|
type OptionsResolved = Overwrite<Required<Options>, Pick<Options, "order"> & {
|
package/dist/index.mjs
CHANGED
|
@@ -7,12 +7,10 @@ import { init, parse } from "cjs-module-lexer";
|
|
|
7
7
|
import { up } from "empathic/package";
|
|
8
8
|
import { resolve } from "import-meta-resolve";
|
|
9
9
|
import { MagicStringAST, generateTransform } from "magic-string-ast";
|
|
10
|
-
import { parseSync } from "rolldown/
|
|
10
|
+
import { parseSync } from "rolldown/utils";
|
|
11
11
|
import { createFilter } from "unplugin-utils";
|
|
12
|
-
|
|
13
|
-
//#region rolldown:runtime
|
|
12
|
+
//#region \0rolldown/runtime.js
|
|
14
13
|
var __require = /* @__PURE__ */ createRequire(import.meta.url);
|
|
15
|
-
|
|
16
14
|
//#endregion
|
|
17
15
|
//#region src/options.ts
|
|
18
16
|
function resolveOptions(options) {
|
|
@@ -24,17 +22,15 @@ function resolveOptions(options) {
|
|
|
24
22
|
include: options.include || [/\.m?[jt]sx?$/],
|
|
25
23
|
exclude: options.exclude || [/node_modules/, /\.d\.[cm]?ts$/],
|
|
26
24
|
order: "order" in options ? options.order : "pre",
|
|
27
|
-
shouldTransform: options.shouldTransform
|
|
28
|
-
builtinNodeModules: !!options.builtinNodeModules
|
|
25
|
+
shouldTransform: options.shouldTransform
|
|
29
26
|
};
|
|
30
27
|
}
|
|
31
|
-
|
|
32
28
|
//#endregion
|
|
33
29
|
//#region src/index.ts
|
|
34
30
|
let initted = false;
|
|
35
31
|
const REQUIRE = `__cjs_require`;
|
|
36
32
|
function RequireCJS(userOptions = {}) {
|
|
37
|
-
const { include, exclude, order, shouldTransform
|
|
33
|
+
const { include, exclude, order, shouldTransform } = resolveOptions(userOptions);
|
|
38
34
|
const filter = createFilter(include, exclude);
|
|
39
35
|
let cwd;
|
|
40
36
|
return {
|
|
@@ -66,12 +62,11 @@ function RequireCJS(userOptions = {}) {
|
|
|
66
62
|
for (const stmt of program.body) if (stmt.type === "ImportDeclaration") {
|
|
67
63
|
if (stmt.importKind === "type") continue;
|
|
68
64
|
const source = stmt.source.value;
|
|
69
|
-
const isBuiltinModule = builtinNodeModules && isBuiltin(source);
|
|
70
65
|
const distFilename = file || (dir ? path.join(dir, fileName) : fileName);
|
|
71
66
|
const importer = cwd ? path.resolve(cwd, distFilename) : distFilename;
|
|
72
|
-
if (!(
|
|
67
|
+
if (!(await shouldTransform?.(source, importer) ?? await isPureCJS(source, importer))) continue;
|
|
73
68
|
if (stmt.specifiers.length === 0) {
|
|
74
|
-
if (
|
|
69
|
+
if (isBuiltin(source)) s.removeNode(stmt);
|
|
75
70
|
else {
|
|
76
71
|
s.overwriteNode(stmt, `${REQUIRE}(${JSON.stringify(source)});`);
|
|
77
72
|
usingRequire = true;
|
|
@@ -86,12 +81,8 @@ function RequireCJS(userOptions = {}) {
|
|
|
86
81
|
if (specifier.importKind === "type") continue;
|
|
87
82
|
mapping.push([s.sliceNode(specifier.imported), specifier.local.name]);
|
|
88
83
|
} else defaultId = specifier.local.name;
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
else {
|
|
92
|
-
requireCode = `__cjs_require(${JSON.stringify(source)})`;
|
|
93
|
-
usingRequire = true;
|
|
94
|
-
}
|
|
84
|
+
const requireCode = `__cjs_require(${JSON.stringify(source)})`;
|
|
85
|
+
usingRequire = true;
|
|
95
86
|
const codes = [];
|
|
96
87
|
if (namespaceId) defaultId ||= `_cjs_${namespaceId}_default`;
|
|
97
88
|
if (defaultId) codes.push(`const ${defaultId} = ${requireCode};`);
|
|
@@ -100,7 +91,7 @@ function RequireCJS(userOptions = {}) {
|
|
|
100
91
|
s.overwriteNode(stmt, codes.join("\n"));
|
|
101
92
|
}
|
|
102
93
|
if (usingRequire) {
|
|
103
|
-
const preamble =
|
|
94
|
+
const preamble = `import { createRequire as __cjs_createRequire } from "node:module";
|
|
104
95
|
const ${REQUIRE} = __cjs_createRequire(import.meta.url);\n`;
|
|
105
96
|
if (code[0] === "#") {
|
|
106
97
|
const firstNewLineIndex = code.indexOf("\n") + 1;
|
|
@@ -138,12 +129,11 @@ async function isPureCJS(id, importer) {
|
|
|
138
129
|
} catch {}
|
|
139
130
|
return false;
|
|
140
131
|
}
|
|
141
|
-
async function getPackageType(path
|
|
142
|
-
const contents = await readFile(path
|
|
132
|
+
async function getPackageType(path) {
|
|
133
|
+
const contents = await readFile(path, "utf8");
|
|
143
134
|
try {
|
|
144
135
|
return JSON.parse(contents).type;
|
|
145
136
|
} catch {}
|
|
146
137
|
}
|
|
147
|
-
|
|
148
138
|
//#endregion
|
|
149
|
-
export { RequireCJS, isPureCJS, resolveOptions };
|
|
139
|
+
export { RequireCJS, isPureCJS, resolveOptions };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rolldown-plugin-require-cjs",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.4.1",
|
|
5
5
|
"description": "Transform ESM imports to CJS requires when the imported module is pure CJS.",
|
|
6
6
|
"author": "Kevin Deng <sxzz@sxzz.moe>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -18,8 +18,6 @@
|
|
|
18
18
|
".": "./dist/index.mjs",
|
|
19
19
|
"./package.json": "./package.json"
|
|
20
20
|
},
|
|
21
|
-
"main": "./dist/index.mjs",
|
|
22
|
-
"module": "./dist/index.mjs",
|
|
23
21
|
"types": "./dist/index.d.mts",
|
|
24
22
|
"files": [
|
|
25
23
|
"dist"
|
|
@@ -28,33 +26,34 @@
|
|
|
28
26
|
"access": "public"
|
|
29
27
|
},
|
|
30
28
|
"engines": {
|
|
31
|
-
"node": ">=
|
|
29
|
+
"node": "^22.18.0 || >=24.0.0"
|
|
32
30
|
},
|
|
33
31
|
"peerDependencies": {
|
|
34
32
|
"rolldown": "*"
|
|
35
33
|
},
|
|
36
34
|
"dependencies": {
|
|
37
|
-
"cjs-module-lexer": "^2.
|
|
38
|
-
"empathic": "^2.0.
|
|
35
|
+
"cjs-module-lexer": "^2.2.0",
|
|
36
|
+
"empathic": "^2.0.1",
|
|
39
37
|
"import-meta-resolve": "^4.2.0",
|
|
40
|
-
"magic-string-ast": "^
|
|
38
|
+
"magic-string-ast": "^2.0.0",
|
|
41
39
|
"unplugin-utils": "^0.3.1"
|
|
42
40
|
},
|
|
43
41
|
"devDependencies": {
|
|
44
|
-
"@babel/parser": "^7.
|
|
45
|
-
"@sxzz/eslint-config": "^
|
|
46
|
-
"@sxzz/prettier-config": "^2.
|
|
47
|
-
"@sxzz/test-utils": "^0.5.
|
|
48
|
-
"@types/node": "^
|
|
49
|
-
"@typescript/native-preview": "7.0.0-dev.
|
|
50
|
-
"bumpp": "^
|
|
51
|
-
"eslint": "^
|
|
52
|
-
"eslint-plugin-vue": "^10.
|
|
53
|
-
"prettier": "^3.
|
|
54
|
-
"rolldown": "1.
|
|
55
|
-
"tsdown": "^0.
|
|
56
|
-
"
|
|
57
|
-
"
|
|
42
|
+
"@babel/parser": "^7.29.7",
|
|
43
|
+
"@sxzz/eslint-config": "^8.1.0",
|
|
44
|
+
"@sxzz/prettier-config": "^2.3.1",
|
|
45
|
+
"@sxzz/test-utils": "^0.5.18",
|
|
46
|
+
"@types/node": "^25.9.3",
|
|
47
|
+
"@typescript/native-preview": "7.0.0-dev.20260612.1",
|
|
48
|
+
"bumpp": "^11.1.0",
|
|
49
|
+
"eslint": "^10.4.1",
|
|
50
|
+
"eslint-plugin-vue": "^10.9.2",
|
|
51
|
+
"prettier": "^3.8.4",
|
|
52
|
+
"rolldown": "1.1.1",
|
|
53
|
+
"tsdown": "^0.22.2",
|
|
54
|
+
"tsdown-preset-sxzz": "^0.6.0",
|
|
55
|
+
"typescript": "^6.0.3",
|
|
56
|
+
"vitest": "^4.1.8"
|
|
58
57
|
},
|
|
59
58
|
"prettier": "@sxzz/prettier-config",
|
|
60
59
|
"tsdown": {
|