rolldown-require 2.0.29 → 2.0.31
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-cn.md +156 -0
- package/package.json +2 -2
package/README-cn.md
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
# rolldown-require
|
|
2
|
+
|
|
3
|
+
[English](./README.md) | [中文](./README-cn.md)
|
|
4
|
+
|
|
5
|
+
[](https://npm.im/rolldown-require) [](https://npm.im/rolldown-require) [](https://www.jsdocs.io/package/rolldown-require)
|
|
6
|
+
|
|
7
|
+
## [主页](https://github.com/weapp-vite/weapp-vite/tree/main/packages/rolldown-require)
|
|
8
|
+
|
|
9
|
+
## 编写目的
|
|
10
|
+
|
|
11
|
+
最近 [rolldown-vite](https://github.com/vite/rolldown-vite) 项目需要加载用户提供的配置文件,但仅使用 `require()` 无法满足需求,因为配置文件不一定是 `CommonJS` 模块,它可能是 `.mjs` 格式,甚至可能是 `TypeScript` 编写的。这就是 `rolldown-require` 包的用武之地,它可以加载任何格式的配置文件。
|
|
12
|
+
|
|
13
|
+
之前的 `vite` 中已经诞生了基于 `esbuild` 的 [bundle-require](https://www.npmjs.com/package/bundle-require),而 `rolldown-require` 则是基于 [rolldown](https://rolldown.rs/) 的实现,提供了相似的API,但是实现有极大的不同。
|
|
14
|
+
|
|
15
|
+
## How it works
|
|
16
|
+
|
|
17
|
+
- Bundle your file with `rolldown`, `node_modules` are excluded because it's problematic to try to bundle it
|
|
18
|
+
- `__filename`, `__dirname` and `import.meta.url` are replaced with source file's value instead of the one from the temporary output file
|
|
19
|
+
- Output file in `esm` format if possible (for `.ts`, `.js` input files)
|
|
20
|
+
- Load output file with `import()` if possible
|
|
21
|
+
- Return the loaded module and its dependencies (imported files)
|
|
22
|
+
|
|
23
|
+
## 安装
|
|
24
|
+
|
|
25
|
+
```sh
|
|
26
|
+
npm i rolldown-require rolldown
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
`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
|
+
import type { ExternalOption, InputOptions, OutputOptions } from 'rolldown'
|
|
45
|
+
|
|
46
|
+
export type RequireFunction = (
|
|
47
|
+
outfile: string,
|
|
48
|
+
ctx: { format: 'cjs' | 'esm' },
|
|
49
|
+
) => any
|
|
50
|
+
|
|
51
|
+
export type GetOutputFile = (filepath: string, format: 'esm' | 'cjs') => string
|
|
52
|
+
|
|
53
|
+
export interface Options {
|
|
54
|
+
cwd?: string
|
|
55
|
+
/**
|
|
56
|
+
* The filepath to bundle and require
|
|
57
|
+
*/
|
|
58
|
+
filepath: string
|
|
59
|
+
/**
|
|
60
|
+
* The `require` function that is used to load the output file
|
|
61
|
+
* Default to the global `require` function
|
|
62
|
+
* This function can be asynchronous, i.e. returns a Promise
|
|
63
|
+
*/
|
|
64
|
+
require?: RequireFunction
|
|
65
|
+
/**
|
|
66
|
+
* esbuild options
|
|
67
|
+
*
|
|
68
|
+
*/
|
|
69
|
+
rolldownOptions?: {
|
|
70
|
+
input?: InputOptions
|
|
71
|
+
output?: OutputOptions
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Get the path to the output file
|
|
76
|
+
* By default we simply replace the extension with `.bundled_{randomId}.js`
|
|
77
|
+
*/
|
|
78
|
+
getOutputFile?: GetOutputFile
|
|
79
|
+
|
|
80
|
+
/** External packages */
|
|
81
|
+
external?: ExternalOption
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* A custom tsconfig path to read `paths` option
|
|
85
|
+
*
|
|
86
|
+
* Set to `false` to disable tsconfig
|
|
87
|
+
*/
|
|
88
|
+
tsconfig?: string | false
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Preserve compiled temporary file for debugging
|
|
92
|
+
* Default to `process.env.BUNDLE_REQUIRE_PRESERVE`
|
|
93
|
+
*/
|
|
94
|
+
preserveTemporaryFile?: boolean
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Provide bundle format explicitly
|
|
98
|
+
* to skip the default format inference
|
|
99
|
+
*/
|
|
100
|
+
format?: 'cjs' | 'esm'
|
|
101
|
+
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## API
|
|
106
|
+
|
|
107
|
+
https://www.jsdocs.io/package/rolldown-require
|
|
108
|
+
|
|
109
|
+
## 灵感来源
|
|
110
|
+
|
|
111
|
+
- 感谢 [rolldown](https://rolldown.rs/) 提供了优秀的打包体验
|
|
112
|
+
- 感谢 [rolldown-vite](https://github.com/vite/rolldown-vite) 提供了直接加载文件配置的方案
|
|
113
|
+
- 感谢 [bundle-require](https://www.npmjs.com/package/bundle-require) 提供了 API 名称和配置项方面的参考
|
|
114
|
+
|
|
115
|
+
## 参与贡献
|
|
116
|
+
|
|
117
|
+
本库是作为 [weapp-vite](https://github.com/weapp-vite/weapp-vite) 的一部分进行发布的
|
|
118
|
+
|
|
119
|
+
同时本库还在快速的开发中,你遇到什么问题或有任何建议,欢迎在 [GitHub Issues](https://github.com/weapp-vite/weapp-vite/issues) 提出
|
|
120
|
+
|
|
121
|
+
## 基准数据
|
|
122
|
+
|
|
123
|
+
本地(M3,Node 22.21.1),`pnpm --filter rolldown-require-bench benchmark`,冷启动 10 次:
|
|
124
|
+
|
|
125
|
+
```text
|
|
126
|
+
场景:tiny-static(25 modules)
|
|
127
|
+
rolldown-require | avg 60.52ms | median 58.36ms | deps 26 | rssΔ 中位 1.02 MB
|
|
128
|
+
unrun | avg 61.16ms | median 61.32ms | deps 26 | rssΔ 中位 0.64 MB
|
|
129
|
+
|
|
130
|
+
场景:medium-mixed(100 modules,动态每 10 个)
|
|
131
|
+
rolldown-require | avg 49.85ms | median 46.38ms | deps 102 | rssΔ 中位 2.29 MB
|
|
132
|
+
unrun | avg 52.30ms | median 30.49ms | deps 101 | rssΔ 中位 1.44 MB
|
|
133
|
+
|
|
134
|
+
场景:large-static(200 modules)
|
|
135
|
+
rolldown-require | avg 55.47ms | median 45.89ms | deps 201 | rssΔ 中位 2.86 MB
|
|
136
|
+
unrun | avg 64.54ms | median 50.02ms | deps 201 | rssΔ 中位 1.33 MB
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
说明:
|
|
140
|
+
|
|
141
|
+
- 用例是 `benchmarks/rolldown-require-bench/benchmark/index.mjs` 生成的合成 TS 模块图(静态与包含动态 import)。
|
|
142
|
+
- 每轮都会清理 unrun 的 `.unrun` 缓存以保证冷启动,可用 `BENCH_ITERATIONS` 调整迭代次数。
|
|
143
|
+
|
|
144
|
+
### 结论
|
|
145
|
+
|
|
146
|
+
- 在 10 次冷启动平均值下,rolldown-require 在三组场景总体更快(avg/median),unrun 的 RSS 增量略低。
|
|
147
|
+
- 依赖数量一致或接近,表明覆盖的模块图相当。
|
|
148
|
+
- 数据来自 `benchmarks/rolldown-require-bench/benchmark/index.mjs` 的合成样本(M3/Node 22.21.1);正式选型请按真实工作负载复验。
|
|
149
|
+
|
|
150
|
+
### 缓存
|
|
151
|
+
|
|
152
|
+
- 通过 `cache: true` 或 `{ enabled: true, dir?, reset?, onEvent? }` 开启持久缓存(默认目录优先 `node_modules/.rolldown-require-cache`,否则 OS tmp)。会校验入口与依赖的 mtime/size,过期则重建。
|
|
153
|
+
|
|
154
|
+
## License
|
|
155
|
+
|
|
156
|
+
MIT © [sonofmagic](https://github.com/sonofmagic)
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rolldown-require",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.0.
|
|
4
|
+
"version": "2.0.31",
|
|
5
5
|
"description": "bundle and require a file using rolldown!",
|
|
6
6
|
"author": "ice breaker <1324318532@qq.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -57,7 +57,7 @@
|
|
|
57
57
|
"registry": "https://registry.npmjs.org"
|
|
58
58
|
},
|
|
59
59
|
"peerDependencies": {
|
|
60
|
-
"rolldown": "1.2.
|
|
60
|
+
"rolldown": "1.2.9"
|
|
61
61
|
},
|
|
62
62
|
"dependencies": {
|
|
63
63
|
"@rollup/pluginutils": "^5.4.0",
|