rolldown-plugin-require-cjs 0.0.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright © 2025-PRESENT Kevin Deng (https://github.com/sxzz)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # rolldown-plugin-require-cjs
2
+
3
+ [![npm version][npm-version-src]][npm-version-href]
4
+ [![npm downloads][npm-downloads-src]][npm-downloads-href]
5
+ [![Unit Test][unit-test-src]][unit-test-href]
6
+
7
+ Transform ESM imports to CJS requires when the imported module is pure CJS.
8
+
9
+ ## Why?
10
+
11
+ Some packages only provide CJS builds (e.g., [`typescript`](https://npmjs.com/package/typescript), [`@babel/parser`](https://npmjs.com/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
+
13
+ See more: https://x.com/sanxiaozhizi/status/1968580207322808812
14
+
15
+ ## Install
16
+
17
+ ```bash
18
+ npm i rolldown-plugin-require-cjs
19
+ ```
20
+
21
+ ## Sponsors
22
+
23
+ <p align="center">
24
+ <a href="https://cdn.jsdelivr.net/gh/sxzz/sponsors/sponsors.svg">
25
+ <img src='https://cdn.jsdelivr.net/gh/sxzz/sponsors/sponsors.svg'/>
26
+ </a>
27
+ </p>
28
+
29
+ ## License
30
+
31
+ [MIT](./LICENSE) License © 2025-PRESENT [Kevin Deng](https://github.com/sxzz)
32
+
33
+ <!-- Badges -->
34
+
35
+ [npm-version-src]: https://img.shields.io/npm/v/rolldown-plugin-require-cjs.svg
36
+ [npm-version-href]: https://npmjs.com/package/rolldown-plugin-require-cjs
37
+ [npm-downloads-src]: https://img.shields.io/npm/dm/rolldown-plugin-require-cjs
38
+ [npm-downloads-href]: https://www.npmcharts.com/compare/rolldown-plugin-require-cjs?interval=30
39
+ [unit-test-src]: https://github.com/sxzz/rolldown-plugin-require-cjs/actions/workflows/unit-test.yml/badge.svg
40
+ [unit-test-href]: https://github.com/sxzz/rolldown-plugin-require-cjs/actions/workflows/unit-test.yml
@@ -0,0 +1,29 @@
1
+ import { Plugin } from "rolldown";
2
+
3
+ //#region src/options.d.ts
4
+ type FilterPattern = Array<string | RegExp> | string | RegExp;
5
+ type Awaitable<T> = T | Promise<T>;
6
+ interface Options {
7
+ include?: FilterPattern;
8
+ exclude?: FilterPattern;
9
+ order?: "pre" | "post" | undefined;
10
+ /**
11
+ * A function to determine whether a module should be transformed.
12
+ * Return `true` to force transformation, `false` to skip transformation,
13
+ * or `undefined` to let the plugin decide automatically.
14
+ *
15
+ * @param id The module ID (path) being imported.
16
+ * @param importer The module ID (path) of the importer.
17
+ * @returns A boolean or a promise that resolves to a boolean, or `undefined`.
18
+ */
19
+ shouldTransform?: (id: string, importer: string) => Awaitable<boolean | undefined>;
20
+ }
21
+ type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U;
22
+ type OptionsResolved = Overwrite<Required<Options>, Pick<Options, "order" | "shouldTransform">>;
23
+ declare function resolveOptions(options: Options): OptionsResolved;
24
+ //#endregion
25
+ //#region src/index.d.ts
26
+ declare function RequireCJS(userOptions?: Options): Plugin;
27
+ declare function isPureCJS(id: string, importer: string): Promise<boolean>;
28
+ //#endregion
29
+ export { Options, OptionsResolved, RequireCJS, isPureCJS, resolveOptions };