rolldown-require 2.0.35 → 2.1.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/dist/index.d.mts CHANGED
@@ -1,5 +1,4 @@
1
1
  import { ExternalOption, InputOptions, OutputOptions } from "rolldown";
2
- //#region src/types.d.ts
3
2
  type RequireFunction = (outfile: string, ctx: {
4
3
  format: 'cjs' | 'esm';
5
4
  }) => any;
@@ -103,26 +102,18 @@ interface InternalOptions extends Omit<Options, 'cwd' | 'filepath'> {
103
102
  tsconfig?: string | false;
104
103
  sourcemap?: boolean | 'inline';
105
104
  }
106
- //#endregion
107
- //#region src/config.d.ts
108
105
  export declare const configDefaults: Readonly<{
109
106
  resolve: {
110
107
  extensions: string[];
111
108
  };
112
109
  }>;
113
- //#endregion
114
- //#region src/bundler.d.ts
115
110
  export declare function bundleFile(fileName: string, options: InternalOptions): Promise<{
116
111
  code: string;
117
112
  dependencies: string[];
118
113
  }>;
119
- //#endregion
120
- //#region src/loader.d.ts
121
114
  export declare function loadFromBundledFile(fileName: string, bundledCode: string, options: InternalOptions, dependencies?: string[]): Promise<any>;
122
- //#endregion
123
- //#region src/index.d.ts
124
115
  export declare function bundleRequire<T = any>(options: Options): Promise<{
125
116
  mod: T;
126
117
  dependencies: string[];
127
118
  }>;
128
- //#endregion
119
+ export {};
package/dist/index.mjs CHANGED
@@ -12,10 +12,9 @@ import os from "node:os";
12
12
  import { promisify } from "node:util";
13
13
  import { Buffer as Buffer$1 } from "node:buffer";
14
14
  import { getTsconfig } from "get-tsconfig";
15
- //#region \0rolldown/runtime.js
15
+ // \0rolldown/runtime.js
16
16
  var __require = /* #__PURE__ */ (() => createRequire(import.meta.url))();
17
- //#endregion
18
- //#region src/collect.ts
17
+ // src/collect.ts
19
18
  /**
20
19
  * 从 `fileName` 起遍历 chunk 图并收集所有引用的模块 id。
21
20
  * Rolldown 在 `inlineDynamicImports` 为 true 时会内联动态导入,
@@ -30,15 +29,13 @@ function collectReferencedModules(bundle, fileName, allModules, analyzedModules
30
29
  for (const imported of chunk.imports) collectReferencedModules(bundle, imported, allModules, analyzedModules);
31
30
  for (const imported of chunk.dynamicImports) collectReferencedModules(bundle, imported, allModules, analyzedModules);
32
31
  }
33
- //#endregion
34
- //#region src/sharedUtils.ts
32
+ // src/sharedUtils.ts
35
33
  const isWindows = typeof process !== "undefined" && process.platform === "win32";
36
34
  const windowsSlashRE = /\\/g;
37
35
  function slash(p) {
38
36
  return p.replace(windowsSlashRE, "/");
39
37
  }
40
- //#endregion
41
- //#region src/constants.ts
38
+ // src/constants.ts
42
39
  const BOM_RE = /^\uFEFF/;
43
40
  function stripBom(value) {
44
41
  return value.replace(BOM_RE, "");
@@ -151,8 +148,7 @@ function traverseBetweenDirs(longerDir, shorterDir, cb) {
151
148
  longerDir = path.dirname(longerDir);
152
149
  }
153
150
  }
154
- //#endregion
155
- //#region src/utils.ts
151
+ // src/utils.ts
156
152
  const MJS_MTS_EXT_RE = /\.m[jt]s$/;
157
153
  const CJS_CTS_EXT_RE = /\.c[jt]s$/;
158
154
  const createFilter$1 = createFilter;
@@ -256,8 +252,7 @@ function stripBomTag(content) {
256
252
  const dynamicImport = async (id, { format }) => {
257
253
  return (format === "esm" ? (file) => import(file) : TSUP_FORMAT === "esm" ? createRequire(import.meta.url) : __require)(id);
258
254
  };
259
- //#endregion
260
- //#region src/externalize.ts
255
+ // src/externalize.ts
261
256
  const NON_RELATIVE_NON_HASH_RE = /^[^.#].*/;
262
257
  function createExternalizeDepsPlugin({ entryFile, isESM }) {
263
258
  const entryDir = path.dirname(entryFile);
@@ -408,8 +403,7 @@ function resolveSpecifierFromImporter(specifier, importerPath) {
408
403
  return;
409
404
  }
410
405
  }
411
- //#endregion
412
- //#region src/module-sync.ts
406
+ // src/module-sync.ts
413
407
  let cachedModuleSyncCondition;
414
408
  let moduleSyncConditionPromise;
415
409
  async function getModuleSyncConditionEnabled() {
@@ -422,8 +416,7 @@ async function getModuleSyncConditionEnabled() {
422
416
  });
423
417
  return moduleSyncConditionPromise;
424
418
  }
425
- //#endregion
426
- //#region src/bundler.ts
419
+ // src/bundler.ts
427
420
  const JS_TS_EXT_RE = /\.[cm]?[jt]s$/;
428
421
  async function bundleFile(fileName, options) {
429
422
  const { isESM } = options;
@@ -549,8 +542,7 @@ function createFileScopeVariablesPlugin({ dirnameVarName, filenameVarName, impor
549
542
  }
550
543
  };
551
544
  }
552
- //#endregion
553
- //#region src/cache.ts
545
+ // src/cache.ts
554
546
  const _require$1 = createRequire(import.meta.url);
555
547
  const memoryCache = /* @__PURE__ */ new Map();
556
548
  function resolveCacheOptions(fileName, options) {
@@ -745,8 +737,7 @@ function hashEntrySource(source) {
745
737
  if (source === void 0) return "none";
746
738
  return crypto.createHash("sha1").update(source).digest("hex");
747
739
  }
748
- //#endregion
749
- //#region src/config.ts
740
+ // src/config.ts
750
741
  const configDefaults = Object.freeze({ resolve: { extensions: [
751
742
  ".mjs",
752
743
  ".js",
@@ -759,8 +750,7 @@ const configDefaults = Object.freeze({ resolve: { extensions: [
759
750
  const defaultGetOutputFile = (filepath, _format) => {
760
751
  return filepath;
761
752
  };
762
- //#endregion
763
- //#region src/temp-output.ts
753
+ // src/temp-output.ts
764
754
  const NON_WORD_CHAR_RE = /[^\w.-]/g;
765
755
  function sanitizeFilename(name) {
766
756
  return name.replace(NON_WORD_CHAR_RE, "_");
@@ -797,8 +787,7 @@ async function resolveTempOutputFile(sourceFile, code, options) {
797
787
  };
798
788
  throw new Error("Failed to create temporary output file for bundled code");
799
789
  }
800
- //#endregion
801
- //#region src/loader.ts
790
+ // src/loader.ts
802
791
  const _require = createRequire(import.meta.url);
803
792
  const promisifiedRealpath = promisify(fs.realpath);
804
793
  async function loadFromBundledFile(fileName, bundledCode, options, dependencies) {
@@ -868,8 +857,7 @@ async function loadFromBundledFile(fileName, bundledCode, options, dependencies)
868
857
  }
869
858
  }
870
859
  }
871
- //#endregion
872
- //#region src/options.ts
860
+ // src/options.ts
873
861
  function resolveEntryFilepath(options) {
874
862
  if (path.isAbsolute(options.filepath)) return options.filepath;
875
863
  const cwd = options.cwd ? path.resolve(options.cwd) : process.cwd();
@@ -906,8 +894,7 @@ function shouldEnableSourcemapFromEnv() {
906
894
  if (process.env.DEBUG) return true;
907
895
  return false;
908
896
  }
909
- //#endregion
910
- //#region src/index.ts
897
+ // src/index.ts
911
898
  async function bundleRequire(options) {
912
899
  const resolvedPath = resolveEntryFilepath(options);
913
900
  const internalOptions = createInternalOptions(options, detectModuleType(resolvedPath));
@@ -929,5 +916,4 @@ async function bundleRequire(options) {
929
916
  dependencies: bundled.dependencies
930
917
  };
931
918
  }
932
- //#endregion
933
919
  export { bundleFile, bundleRequire, configDefaults, loadFromBundledFile };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "rolldown-require",
3
3
  "type": "module",
4
- "version": "2.0.35",
4
+ "version": "2.1.0",
5
5
  "description": "bundle and require a file using rolldown!",
6
6
  "author": "ice breaker <1324318532@qq.com>",
7
7
  "license": "MIT",
@@ -50,24 +50,24 @@
50
50
  "misc"
51
51
  ],
52
52
  "engines": {
53
- "node": "^20.19.0 || >=22.12.0"
53
+ "node": "^22.23.4 || ^24.22.1 || >=26.12.1"
54
54
  },
55
55
  "publishConfig": {
56
56
  "access": "public",
57
57
  "registry": "https://registry.npmjs.org"
58
58
  },
59
59
  "peerDependencies": {
60
- "rolldown": "1.2.13"
60
+ "rolldown": "1.2.15"
61
61
  },
62
62
  "dependencies": {
63
- "@rollup/pluginutils": "^5.4.0",
63
+ "@rollup/pluginutils": "^5.5.1",
64
64
  "get-tsconfig": "^4.14.3"
65
65
  },
66
66
  "devDependencies": {
67
67
  "@types/pnpapi": "^0.0.5"
68
68
  },
69
69
  "scripts": {
70
- "dev": "unbuild --stub",
70
+ "dev": "tsdown -w --sourcemap",
71
71
  "build:watch": "tsdown -w --sourcemap",
72
72
  "build": "tsdown",
73
73
  "test": "vitest run",
package/README-cn.md DELETED
@@ -1,156 +0,0 @@
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
- ## [主页](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 &copy; [sonofmagic](https://github.com/sonofmagic)