rolldown-require 1.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
+ MIT License
2
+
3
+ Copyright (c) 2024 ice breaker
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,126 @@
1
+ # rolldown-require
2
+
3
+ [English](./README.md) | [中文](./README-cn.md)
4
+
5
+ [![npm version](https://badgen.net/npm/v/rolldown-require)](https://npm.im/rolldown-require) [![npm downloads](https://badgen.net/npm/dm/rolldown-require)](https://npm.im/rolldown-require) [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/rolldown-require)
6
+
7
+ ## Use Case
8
+
9
+ Recently, the [rolldown-vite](https://github.com/vite/rolldown-vite) project needed to load user-provided configuration files. However, simply using `require()` was not sufficient, because the configuration files might not be `CommonJS` modules—they could be in `.mjs` format or even written in `TypeScript`. That’s where the `rolldown-require` package comes in, enabling you to load configuration files of any format.
10
+
11
+ Previously, [vite](https://vitejs.dev/) introduced the [bundle-require](https://www.npmjs.com/package/bundle-require) solution based on `esbuild`. Meanwhile, `rolldown-require` is an implementation based on [rolldown](https://rolldown.rs/), providing a similar API but with a significantly different implementation.
12
+
13
+ ## How it works
14
+
15
+ - Bundles your file using `rolldown`, excluding `node_modules` (since bundling those can cause problems)
16
+
17
+ - `__filename`, `__dirname`, and `import.meta.url` are replaced with the source file’s value rather than the temporary output file’s value.
18
+
19
+ - Outputs the file in `esm` format if possible (for `.ts` and `.js` inputs).
20
+ - Loads the output file using `import()` when possible.
21
+ - Returns the loaded module and its dependencies (imported files).
22
+
23
+ ## Install
24
+
25
+ ```bash
26
+ npm i rolldown-require rolldown
27
+ ```
28
+
29
+ Note: `rolldown` is a peer dependency.
30
+
31
+ ## Usage
32
+
33
+ ```ts
34
+ import { bundleRequire } from 'rolldown-require'
35
+
36
+ const { mod } = await bundleRequire({
37
+ filepath: './project/vite.config.ts',
38
+ })
39
+ ```
40
+
41
+ ## Options
42
+
43
+ ```ts
44
+ export type RequireFunction = (
45
+ outfile: string,
46
+ ctx: { format: 'cjs' | 'esm' },
47
+ ) => any
48
+
49
+ export type GetOutputFile = (filepath: string, format: 'esm' | 'cjs') => string
50
+
51
+ export type RebuildCallback = (
52
+ error: RollupError | null,
53
+ result: RolldownOutput | null,
54
+ ) => void
55
+
56
+ export type ReadFile = (filepath: string) => string
57
+
58
+ export interface Options {
59
+ cwd?: string
60
+ /**
61
+ * The filepath to bundle and require
62
+ */
63
+ filepath: string
64
+ /**
65
+ * The `require` function used to load the output file
66
+ * Defaults to the global `require` function.
67
+ * This function can be asynchronous and return a Promise.
68
+ */
69
+ require?: RequireFunction
70
+ /**
71
+ * esbuild options
72
+ *
73
+ */
74
+ rolldownOptions?: InputOptions
75
+ /**
76
+ * Get the path to the output file
77
+ * By default, we simply replace the extension with `.bundled_{randomId}.js`
78
+ */
79
+ getOutputFile?: GetOutputFile
80
+ /**
81
+ * Enable watching and call the callback after each rebuild
82
+ */
83
+
84
+ /** External packages */
85
+ external?: ExternalOption
86
+
87
+ /**
88
+ * A custom tsconfig path to read the `paths` option
89
+ *
90
+ * Set to `false` to disable tsconfig.
91
+ */
92
+ tsconfig?: string | false
93
+
94
+ /**
95
+ * Preserve the compiled temporary file for debugging
96
+ * Defaults to `process.env.BUNDLE_REQUIRE_PRESERVE`
97
+ */
98
+ preserveTemporaryFile?: boolean
99
+
100
+ /**
101
+ * Provide the bundle format explicitly
102
+ * to skip the default format inference
103
+ */
104
+ format?: 'cjs' | 'esm'
105
+ }
106
+ ```
107
+
108
+ ## API
109
+
110
+ [https://www.jsdocs.io/package/rolldown-require](https://www.jsdocs.io/package/rolldown-require)
111
+
112
+ ## Inspirations
113
+
114
+ - Thanks to [rolldown](https://rolldown.rs/) for providing an excellent bundling experience.
115
+ - Thanks to [rolldown-vite](https://github.com/vite/rolldown-vite) for offering a direct file loading solution.
116
+ - Thanks to [bundle-require](https://www.npmjs.com/package/bundle-require) for providing inspiration for the API design and configuration options.
117
+
118
+ ## Contributing
119
+
120
+ This library is published as part of [weapp-vite](https://github.com/weapp-vite/weapp-vite).
121
+
122
+ The library is under active development. If you encounter any issues or have suggestions, please feel free to open an issue on [GitHub Issues](https://github.com/weapp-vite/weapp-vite/issues).
123
+
124
+ ## License
125
+
126
+ MIT © [sonofmagic](https://github.com/sonofmagic)
@@ -0,0 +1,15 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
+ }) : x)(function(x) {
4
+ if (typeof require !== "undefined") return require.apply(this, arguments);
5
+ throw Error('Dynamic require of "' + x + '" is not supported');
6
+ });
7
+
8
+ // ../../node_modules/.pnpm/tsup@8.5.0_@swc+core@1.12.0_jiti@2.4.2_postcss@8.5.4_tsx@4.20.1_typescript@5.8.3_yaml@2.8.0/node_modules/tsup/assets/cjs_shims.js
9
+ var getImportMetaUrl = () => typeof document === "undefined" ? new URL(`file:${__filename}`).href : document.currentScript && document.currentScript.src || new URL("main.js", document.baseURI).href;
10
+ var importMetaUrl = /* @__PURE__ */ getImportMetaUrl();
11
+
12
+
13
+
14
+
15
+ exports.__require = __require; exports.importMetaUrl = importMetaUrl;
File without changes
@@ -0,0 +1,7 @@
1
+ import "./chunk-WUKYLWAZ.mjs";
2
+
3
+ // misc/false.js
4
+ var false_default = false;
5
+ export {
6
+ false_default as default
7
+ };
@@ -0,0 +1,9 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});require('./chunk-Q6XJHTDB.cjs');
2
+
3
+ // misc/false.js
4
+ var false_default = false;
5
+
6
+
7
+ exports.default = false_default;
8
+
9
+ module.exports = exports.default;