rolldown-require 2.0.7 → 2.0.9
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 +126 -0
- package/dist/index.mjs +749 -1006
- package/package.json +7 -7
- package/dist/index.d.ts +0 -121
package/dist/index.mjs
CHANGED
|
@@ -1,1102 +1,845 @@
|
|
|
1
|
-
|
|
2
|
-
import
|
|
3
|
-
import { pathToFileURL
|
|
1
|
+
import { builtinModules, createRequire } from "node:module";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
4
4
|
import { rolldown } from "rolldown";
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
import fs, { readFileSync } from "node:fs";
|
|
6
|
+
import "node:child_process";
|
|
7
|
+
import process from "node:process";
|
|
8
|
+
import { createFilter } from "@rollup/pluginutils";
|
|
9
|
+
import crypto from "node:crypto";
|
|
10
|
+
import fsp from "node:fs/promises";
|
|
11
|
+
import os from "node:os";
|
|
12
|
+
import { promisify } from "node:util";
|
|
13
|
+
import { Buffer as Buffer$1 } from "node:buffer";
|
|
14
|
+
import { getTsconfig } from "get-tsconfig";
|
|
15
|
+
//#region \0rolldown/runtime.js
|
|
16
|
+
var __require = /* @__PURE__ */ createRequire(import.meta.url);
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region src/collect.ts
|
|
19
|
+
/**
|
|
20
|
+
* 从 `fileName` 起遍历 chunk 图并收集所有引用的模块 id。
|
|
21
|
+
* Rolldown 在 `inlineDynamicImports` 为 true 时会内联动态导入,
|
|
22
|
+
* 因此只需要遍历已输出的 chunk。
|
|
23
|
+
*/
|
|
7
24
|
function collectReferencedModules(bundle, fileName, allModules, analyzedModules = /* @__PURE__ */ new Set()) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
for (const imported of chunk.dynamicImports) {
|
|
23
|
-
collectReferencedModules(bundle, imported, allModules, analyzedModules);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// src/externalize.ts
|
|
28
|
-
import fs3 from "fs";
|
|
29
|
-
import { createRequire as createRequire3 } from "module";
|
|
30
|
-
import path3 from "path";
|
|
31
|
-
import { fileURLToPath, pathToFileURL } from "url";
|
|
32
|
-
|
|
33
|
-
// src/utils.ts
|
|
34
|
-
import { exec } from "child_process";
|
|
35
|
-
import fs2 from "fs";
|
|
36
|
-
import { builtinModules, createRequire as createRequire2 } from "module";
|
|
37
|
-
import path2 from "path";
|
|
38
|
-
import process3 from "process";
|
|
39
|
-
import { createFilter as _createFilter } from "@rollup/pluginutils";
|
|
40
|
-
|
|
41
|
-
// src/constants.ts
|
|
42
|
-
import { readFileSync } from "fs";
|
|
43
|
-
var { version } = JSON.parse(
|
|
44
|
-
readFileSync(new URL("../package.json", import.meta.url)).toString()
|
|
45
|
-
);
|
|
46
|
-
|
|
47
|
-
// src/packages.ts
|
|
48
|
-
import fs from "fs";
|
|
49
|
-
import { createRequire } from "module";
|
|
50
|
-
import path from "path";
|
|
51
|
-
import process from "process";
|
|
52
|
-
var pnp;
|
|
53
|
-
if (process.versions.pnp) {
|
|
54
|
-
try {
|
|
55
|
-
pnp = createRequire(import.meta.url)("pnpapi");
|
|
56
|
-
} catch {
|
|
57
|
-
}
|
|
58
|
-
}
|
|
25
|
+
if (analyzedModules.has(fileName)) return;
|
|
26
|
+
analyzedModules.add(fileName);
|
|
27
|
+
const chunk = bundle[fileName];
|
|
28
|
+
if (!chunk) return;
|
|
29
|
+
for (const mod of chunk.moduleIds) allModules.add(mod);
|
|
30
|
+
for (const imported of chunk.imports) collectReferencedModules(bundle, imported, allModules, analyzedModules);
|
|
31
|
+
for (const imported of chunk.dynamicImports) collectReferencedModules(bundle, imported, allModules, analyzedModules);
|
|
32
|
+
}
|
|
33
|
+
//#endregion
|
|
34
|
+
//#region src/constants.ts
|
|
35
|
+
const { version } = JSON.parse(readFileSync(new URL("../package.json", import.meta.url)).toString());
|
|
36
|
+
if (process.versions.pnp) try {
|
|
37
|
+
createRequire(import.meta.url)("pnpapi");
|
|
38
|
+
} catch {}
|
|
59
39
|
function findNearestPackageData(basedir, packageCache) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
const nextBasedir = path.dirname(basedir);
|
|
80
|
-
if (nextBasedir === basedir) {
|
|
81
|
-
break;
|
|
82
|
-
}
|
|
83
|
-
basedir = nextBasedir;
|
|
84
|
-
}
|
|
85
|
-
return null;
|
|
40
|
+
const originalBasedir = basedir;
|
|
41
|
+
while (basedir) {
|
|
42
|
+
if (packageCache) {
|
|
43
|
+
const cached = getFnpdCache(packageCache, basedir, originalBasedir);
|
|
44
|
+
if (cached) return cached;
|
|
45
|
+
}
|
|
46
|
+
const pkgPath = path.join(basedir, "package.json");
|
|
47
|
+
if (tryStatSync(pkgPath)?.isFile()) try {
|
|
48
|
+
const pkgData = loadPackageData(pkgPath);
|
|
49
|
+
if (packageCache) setFnpdCache(packageCache, pkgData, basedir, originalBasedir);
|
|
50
|
+
return pkgData;
|
|
51
|
+
} catch {}
|
|
52
|
+
const nextBasedir = path.dirname(basedir);
|
|
53
|
+
if (nextBasedir === basedir) break;
|
|
54
|
+
basedir = nextBasedir;
|
|
55
|
+
}
|
|
56
|
+
return null;
|
|
86
57
|
}
|
|
87
58
|
function loadPackageData(pkgPath) {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
const resolvedCache = {};
|
|
112
|
-
const pkg = {
|
|
113
|
-
dir: pkgDir,
|
|
114
|
-
data,
|
|
115
|
-
hasSideEffects,
|
|
116
|
-
setResolvedCache(key, entry, options) {
|
|
117
|
-
resolvedCache[getResolveCacheKey(key, options)] = entry;
|
|
118
|
-
},
|
|
119
|
-
getResolvedCache(key, options) {
|
|
120
|
-
return resolvedCache[getResolveCacheKey(key, options)];
|
|
121
|
-
}
|
|
122
|
-
};
|
|
123
|
-
return pkg;
|
|
59
|
+
const data = JSON.parse(stripBomTag(fs.readFileSync(pkgPath, "utf-8")));
|
|
60
|
+
const pkgDir = normalizePath(path.dirname(pkgPath));
|
|
61
|
+
const { sideEffects } = data;
|
|
62
|
+
let hasSideEffects;
|
|
63
|
+
if (typeof sideEffects === "boolean") hasSideEffects = () => sideEffects;
|
|
64
|
+
else if (Array.isArray(sideEffects)) if (sideEffects.length <= 0) hasSideEffects = () => false;
|
|
65
|
+
else hasSideEffects = createFilter$1(sideEffects.map((sideEffect) => {
|
|
66
|
+
if (sideEffect.includes("/")) return sideEffect;
|
|
67
|
+
return `**/${sideEffect}`;
|
|
68
|
+
}), null, { resolve: pkgDir });
|
|
69
|
+
else hasSideEffects = () => null;
|
|
70
|
+
const resolvedCache = {};
|
|
71
|
+
return {
|
|
72
|
+
dir: pkgDir,
|
|
73
|
+
data,
|
|
74
|
+
hasSideEffects,
|
|
75
|
+
setResolvedCache(key, entry, options) {
|
|
76
|
+
resolvedCache[getResolveCacheKey(key, options)] = entry;
|
|
77
|
+
},
|
|
78
|
+
getResolvedCache(key, options) {
|
|
79
|
+
return resolvedCache[getResolveCacheKey(key, options)];
|
|
80
|
+
}
|
|
81
|
+
};
|
|
124
82
|
}
|
|
125
83
|
function getResolveCacheKey(key, options) {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
84
|
+
return [
|
|
85
|
+
key,
|
|
86
|
+
options.isRequire ? "1" : "0",
|
|
87
|
+
options.conditions.join("_"),
|
|
88
|
+
options.extensions.join("_"),
|
|
89
|
+
options.mainFields.join("_")
|
|
90
|
+
].join("|");
|
|
133
91
|
}
|
|
134
92
|
function findNearestNodeModules(basedir) {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
93
|
+
while (basedir) {
|
|
94
|
+
const pkgPath = path.join(basedir, "node_modules");
|
|
95
|
+
if (tryStatSync(pkgPath)?.isDirectory()) return pkgPath;
|
|
96
|
+
const nextBasedir = path.dirname(basedir);
|
|
97
|
+
if (nextBasedir === basedir) break;
|
|
98
|
+
basedir = nextBasedir;
|
|
99
|
+
}
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* 根据 `basedir` 获取缓存的 `findNearestPackageData`。
|
|
104
|
+
* 当命中缓存且已遍历 `basedir` 与 `originalBasedir` 之间的路径时,
|
|
105
|
+
* 会把中间目录也写入缓存。
|
|
106
|
+
*
|
|
107
|
+
* 这样共享的 `basedir` 只需要读一次文件系统。
|
|
108
|
+
*/
|
|
148
109
|
function getFnpdCache(packageCache, basedir, originalBasedir) {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
110
|
+
const cacheKey = getFnpdCacheKey(basedir);
|
|
111
|
+
const pkgData = packageCache.get(cacheKey);
|
|
112
|
+
if (pkgData) {
|
|
113
|
+
traverseBetweenDirs(originalBasedir, basedir, (dir) => {
|
|
114
|
+
packageCache.set(getFnpdCacheKey(dir), pkgData);
|
|
115
|
+
});
|
|
116
|
+
return pkgData;
|
|
117
|
+
}
|
|
157
118
|
}
|
|
158
119
|
function setFnpdCache(packageCache, pkgData, basedir, originalBasedir) {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
120
|
+
packageCache.set(getFnpdCacheKey(basedir), pkgData);
|
|
121
|
+
traverseBetweenDirs(originalBasedir, basedir, (dir) => {
|
|
122
|
+
packageCache.set(getFnpdCacheKey(dir), pkgData);
|
|
123
|
+
});
|
|
163
124
|
}
|
|
164
125
|
function getFnpdCacheKey(basedir) {
|
|
165
|
-
|
|
126
|
+
return `fnpd_${basedir}`;
|
|
166
127
|
}
|
|
128
|
+
/**
|
|
129
|
+
* 在 `longerDir`(含)与 `shorterDir`(不含)之间遍历路径并执行回调。
|
|
130
|
+
* @param longerDir 较长路径,如 `/User/foo/bar/baz`
|
|
131
|
+
* @param shorterDir 较短路径,如 `/User/foo`
|
|
132
|
+
*/
|
|
167
133
|
function traverseBetweenDirs(longerDir, shorterDir, cb) {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
var windowsSlashRE = /\\/g;
|
|
134
|
+
while (longerDir !== shorterDir) {
|
|
135
|
+
cb(longerDir);
|
|
136
|
+
longerDir = path.dirname(longerDir);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
//#endregion
|
|
140
|
+
//#region src/sharedUtils.ts
|
|
141
|
+
const isWindows = typeof process !== "undefined" && process.platform === "win32";
|
|
142
|
+
const windowsSlashRE = /\\/g;
|
|
178
143
|
function slash(p) {
|
|
179
|
-
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
144
|
+
return p.replace(windowsSlashRE, "/");
|
|
145
|
+
}
|
|
146
|
+
//#endregion
|
|
147
|
+
//#region src/utils.ts
|
|
148
|
+
const MJS_MTS_EXT_RE = /\.m[jt]s$/;
|
|
149
|
+
const CJS_CTS_EXT_RE = /\.c[jt]s$/;
|
|
150
|
+
const createFilter$1 = createFilter;
|
|
151
|
+
const NODE_BUILTIN_NAMESPACE = "node:";
|
|
152
|
+
const NPM_BUILTIN_NAMESPACE = "npm:";
|
|
153
|
+
const BUN_BUILTIN_NAMESPACE = "bun:";
|
|
154
|
+
const nodeBuiltins = builtinModules.filter((id) => !id.includes(":"));
|
|
155
|
+
const isBuiltinCache = /* @__PURE__ */ new WeakMap();
|
|
191
156
|
function isBuiltin(builtins, id) {
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
157
|
+
let isBuiltin = isBuiltinCache.get(builtins);
|
|
158
|
+
if (!isBuiltin) {
|
|
159
|
+
isBuiltin = createIsBuiltin(builtins);
|
|
160
|
+
isBuiltinCache.set(builtins, isBuiltin);
|
|
161
|
+
}
|
|
162
|
+
return isBuiltin(id);
|
|
198
163
|
}
|
|
199
164
|
function createIsBuiltin(builtins) {
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
...nodeBuiltins,
|
|
210
|
-
new RegExp(`^${NODE_BUILTIN_NAMESPACE}`),
|
|
211
|
-
new RegExp(`^${NPM_BUILTIN_NAMESPACE}`),
|
|
212
|
-
new RegExp(`^${BUN_BUILTIN_NAMESPACE}`)
|
|
165
|
+
const plainBuiltinsSet = new Set(builtins.filter((builtin) => typeof builtin === "string"));
|
|
166
|
+
const regexBuiltins = builtins.filter((builtin) => typeof builtin !== "string");
|
|
167
|
+
return (id) => plainBuiltinsSet.has(id) || regexBuiltins.some((regexp) => regexp.test(id));
|
|
168
|
+
}
|
|
169
|
+
const nodeLikeBuiltins = [
|
|
170
|
+
...nodeBuiltins,
|
|
171
|
+
new RegExp(`^${NODE_BUILTIN_NAMESPACE}`),
|
|
172
|
+
new RegExp(`^${NPM_BUILTIN_NAMESPACE}`),
|
|
173
|
+
new RegExp(`^${BUN_BUILTIN_NAMESPACE}`)
|
|
213
174
|
];
|
|
214
175
|
function isNodeLikeBuiltin(id) {
|
|
215
|
-
|
|
176
|
+
return isBuiltin(nodeLikeBuiltins, id);
|
|
216
177
|
}
|
|
217
178
|
function isNodeBuiltin(id) {
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
}
|
|
221
|
-
return nodeBuiltins.includes(id);
|
|
179
|
+
if (id.startsWith(NODE_BUILTIN_NAMESPACE)) return true;
|
|
180
|
+
return nodeBuiltins.includes(id);
|
|
222
181
|
}
|
|
223
182
|
function normalizePath(id) {
|
|
224
|
-
|
|
183
|
+
return path.posix.normalize(isWindows ? slash(id) : id);
|
|
225
184
|
}
|
|
226
185
|
function tryStatSync(file) {
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
}
|
|
186
|
+
try {
|
|
187
|
+
return fs.statSync(file, { throwIfNoEntry: false });
|
|
188
|
+
} catch {}
|
|
231
189
|
}
|
|
232
190
|
function isFilePathESM(filePath, packageCache) {
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
return false;
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
var currentSafeRealpathSync = isWindows ? windowsSafeRealPathSync : fs2.realpathSync.native;
|
|
247
|
-
var windowsNetworkMap = /* @__PURE__ */ new Map();
|
|
248
|
-
function windowsMappedRealpathSync(path9) {
|
|
249
|
-
const realPath = fs2.realpathSync.native(path9);
|
|
250
|
-
if (realPath.startsWith("\\\\")) {
|
|
251
|
-
for (const [network, volume] of windowsNetworkMap) {
|
|
252
|
-
if (realPath.startsWith(network)) {
|
|
253
|
-
return realPath.replace(network, volume);
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
return realPath;
|
|
258
|
-
}
|
|
259
|
-
var parseNetUseRE = /^\w* +(\w:) +([^ ]+)\s/;
|
|
260
|
-
var firstSafeRealPathSyncRun = false;
|
|
261
|
-
function windowsSafeRealPathSync(path9) {
|
|
262
|
-
if (!firstSafeRealPathSyncRun) {
|
|
263
|
-
optimizeSafeRealPathSync();
|
|
264
|
-
firstSafeRealPathSyncRun = true;
|
|
265
|
-
}
|
|
266
|
-
return fs2.realpathSync(path9);
|
|
267
|
-
}
|
|
268
|
-
function optimizeSafeRealPathSync() {
|
|
269
|
-
const nodeVersion = process3.versions.node.split(".").map(Number);
|
|
270
|
-
if (nodeVersion[0] < 18 || nodeVersion[0] === 18 && nodeVersion[1] < 10) {
|
|
271
|
-
currentSafeRealpathSync = fs2.realpathSync;
|
|
272
|
-
return;
|
|
273
|
-
}
|
|
274
|
-
try {
|
|
275
|
-
fs2.realpathSync.native(path2.resolve("./"));
|
|
276
|
-
} catch (error) {
|
|
277
|
-
if (error.message.includes("EISDIR: illegal operation on a directory")) {
|
|
278
|
-
currentSafeRealpathSync = fs2.realpathSync;
|
|
279
|
-
return;
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
exec("net use", (error, stdout) => {
|
|
283
|
-
if (error) {
|
|
284
|
-
return;
|
|
285
|
-
}
|
|
286
|
-
const lines = stdout.split("\n");
|
|
287
|
-
for (const line of lines) {
|
|
288
|
-
const m = parseNetUseRE.exec(line);
|
|
289
|
-
if (m) {
|
|
290
|
-
windowsNetworkMap.set(m[2], m[1]);
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
currentSafeRealpathSync = windowsNetworkMap.size === 0 ? fs2.realpathSync.native : windowsMappedRealpathSync;
|
|
294
|
-
});
|
|
295
|
-
}
|
|
191
|
+
if (MJS_MTS_EXT_RE.test(filePath)) return true;
|
|
192
|
+
else if (CJS_CTS_EXT_RE.test(filePath)) return false;
|
|
193
|
+
else try {
|
|
194
|
+
return findNearestPackageData(path.dirname(filePath), packageCache)?.data.type === "module";
|
|
195
|
+
} catch {
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
isWindows || fs.realpathSync.native;
|
|
296
200
|
function stripBomTag(content) {
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
}
|
|
300
|
-
return content;
|
|
201
|
+
if (content.charCodeAt(0) === 65279) return content.slice(1);
|
|
202
|
+
return content;
|
|
301
203
|
}
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
return fn(id);
|
|
204
|
+
const dynamicImport = async (id, { format }) => {
|
|
205
|
+
return (format === "esm" ? (file) => import(file) : TSUP_FORMAT === "esm" ? createRequire(import.meta.url) : __require)(id);
|
|
305
206
|
};
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
function createExternalizeDepsPlugin({
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
);
|
|
370
|
-
let idOut = resolvedPath + query;
|
|
371
|
-
if (isImport && shouldExternalize) {
|
|
372
|
-
idOut = pathToFileURL(resolvedPath).href + query;
|
|
373
|
-
}
|
|
374
|
-
const result = { id: idOut, external: shouldExternalize };
|
|
375
|
-
externalizeCache.set(cacheKey, result);
|
|
376
|
-
return result;
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
};
|
|
207
|
+
//#endregion
|
|
208
|
+
//#region src/externalize.ts
|
|
209
|
+
const NON_RELATIVE_NON_HASH_RE = /^[^.#].*/;
|
|
210
|
+
function createExternalizeDepsPlugin({ entryFile, isESM }) {
|
|
211
|
+
const entryDir = path.dirname(entryFile);
|
|
212
|
+
const externalizeCache = /* @__PURE__ */ new Map();
|
|
213
|
+
const entryResolveCache = /* @__PURE__ */ new Map();
|
|
214
|
+
return {
|
|
215
|
+
name: "externalize-deps",
|
|
216
|
+
resolveId: {
|
|
217
|
+
filter: { id: NON_RELATIVE_NON_HASH_RE },
|
|
218
|
+
async handler(id, importer, options) {
|
|
219
|
+
const { kind } = options;
|
|
220
|
+
if (!importer || path.isAbsolute(id) || isNodeBuiltin(id)) return;
|
|
221
|
+
if (isNodeLikeBuiltin(id)) return {
|
|
222
|
+
id,
|
|
223
|
+
external: true
|
|
224
|
+
};
|
|
225
|
+
const cacheKey = `${importer}::${kind}::${id}`;
|
|
226
|
+
const cached = externalizeCache.get(cacheKey);
|
|
227
|
+
if (cached !== void 0) return cached ?? void 0;
|
|
228
|
+
const isImport = isESM || kind === "dynamic-import";
|
|
229
|
+
const resolved = await resolveWithRolldown(this, id, importer, kind);
|
|
230
|
+
if (!resolved?.id) {
|
|
231
|
+
externalizeCache.set(cacheKey, null);
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
if (resolved.external) {
|
|
235
|
+
const external = resolved.external === "absolute" ? "absolute" : true;
|
|
236
|
+
const result = {
|
|
237
|
+
id: resolved.id,
|
|
238
|
+
external
|
|
239
|
+
};
|
|
240
|
+
externalizeCache.set(cacheKey, result);
|
|
241
|
+
return result;
|
|
242
|
+
}
|
|
243
|
+
const { cleanId, query } = splitIdAndQuery(resolved.id);
|
|
244
|
+
const resolvedPath = toFilePath(cleanId);
|
|
245
|
+
if (!resolvedPath) {
|
|
246
|
+
externalizeCache.set(cacheKey, null);
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
if (!path.isAbsolute(resolvedPath)) {
|
|
250
|
+
externalizeCache.set(cacheKey, null);
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
if (resolvedPath.endsWith(".json")) {
|
|
254
|
+
const idWithQuery = resolvedPath + query;
|
|
255
|
+
externalizeCache.set(cacheKey, idWithQuery);
|
|
256
|
+
return idWithQuery;
|
|
257
|
+
}
|
|
258
|
+
const shouldExternalize = shouldExternalizeBareImport(id, importer, entryDir, resolvedPath, entryResolveCache);
|
|
259
|
+
let idOut = resolvedPath + query;
|
|
260
|
+
if (isImport && shouldExternalize) idOut = pathToFileURL(resolvedPath).href + query;
|
|
261
|
+
const result = {
|
|
262
|
+
id: idOut,
|
|
263
|
+
external: shouldExternalize
|
|
264
|
+
};
|
|
265
|
+
externalizeCache.set(cacheKey, result);
|
|
266
|
+
return result;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
};
|
|
380
270
|
}
|
|
381
271
|
function shouldExternalizeBareImport(specifier, importer, entryDir, resolvedPath, canResolveCache) {
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
return false;
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
if (!canResolveFromEntry(specifier, entryDir, canResolveCache)) {
|
|
402
|
-
return false;
|
|
403
|
-
}
|
|
404
|
-
return true;
|
|
272
|
+
if (!specifier || specifier.startsWith(".") || path.isAbsolute(specifier)) return false;
|
|
273
|
+
if (isNodeLikeBuiltin(specifier)) return true;
|
|
274
|
+
const importerPath = normalizeImporterPath(importer, entryDir);
|
|
275
|
+
const resolvedFromImporter = resolvedPath ?? resolveSpecifierFromImporter(specifier, importerPath);
|
|
276
|
+
if (resolvedFromImporter) {
|
|
277
|
+
const containingNodeModules = findContainingNodeModules(resolvedFromImporter);
|
|
278
|
+
if (containingNodeModules) {
|
|
279
|
+
const ownerDir = path.dirname(containingNodeModules);
|
|
280
|
+
const ownerInsideEntry = isPathWithinDirectory(entryDir, ownerDir);
|
|
281
|
+
const entryInsideOwner = isPathWithinDirectory(ownerDir, entryDir);
|
|
282
|
+
if (ownerInsideEntry && !entryInsideOwner) return false;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
if (!canResolveFromEntry(specifier, entryDir, canResolveCache)) return false;
|
|
286
|
+
return true;
|
|
405
287
|
}
|
|
406
288
|
function normalizeImporterPath(importer, fallback) {
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
const [withoutQuery] = importer.split("?");
|
|
411
|
-
return withoutQuery || fallback;
|
|
289
|
+
if (!importer || importer.startsWith("\0")) return fallback;
|
|
290
|
+
const [withoutQuery] = importer.split("?");
|
|
291
|
+
return withoutQuery || fallback;
|
|
412
292
|
}
|
|
413
293
|
function findContainingNodeModules(filePath) {
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
break;
|
|
422
|
-
}
|
|
423
|
-
current = path3.dirname(current);
|
|
424
|
-
}
|
|
294
|
+
let current = path.dirname(filePath);
|
|
295
|
+
const { root } = path.parse(current);
|
|
296
|
+
while (true) {
|
|
297
|
+
if (path.basename(current) === "node_modules") return current;
|
|
298
|
+
if (current === root) break;
|
|
299
|
+
current = path.dirname(current);
|
|
300
|
+
}
|
|
425
301
|
}
|
|
426
302
|
function isPathWithinDirectory(parent, child) {
|
|
427
|
-
|
|
428
|
-
|
|
303
|
+
const relative = path.relative(parent, child);
|
|
304
|
+
return relative === "" || !relative.startsWith("..") && !path.isAbsolute(relative);
|
|
429
305
|
}
|
|
430
306
|
function getPackageName(specifier) {
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
return void 0;
|
|
440
|
-
}
|
|
441
|
-
const [name] = specifier.split("/");
|
|
442
|
-
return name || void 0;
|
|
307
|
+
if (!specifier) return;
|
|
308
|
+
if (specifier.startsWith("@")) {
|
|
309
|
+
const segments = specifier.split("/");
|
|
310
|
+
if (segments.length >= 2) return `${segments[0]}/${segments[1]}`;
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
const [name] = specifier.split("/");
|
|
314
|
+
return name || void 0;
|
|
443
315
|
}
|
|
444
316
|
function canResolveFromEntry(specifier, entryDir, cache) {
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
break;
|
|
461
|
-
}
|
|
462
|
-
currentDir = parentDir;
|
|
463
|
-
}
|
|
464
|
-
cache?.set(packageName, false);
|
|
465
|
-
return false;
|
|
317
|
+
const packageName = getPackageName(specifier);
|
|
318
|
+
if (!packageName) return false;
|
|
319
|
+
if (cache?.has(packageName)) return cache.get(packageName);
|
|
320
|
+
let currentDir = entryDir;
|
|
321
|
+
while (true) {
|
|
322
|
+
if (fs.existsSync(path.join(currentDir, "node_modules", packageName))) {
|
|
323
|
+
cache?.set(packageName, true);
|
|
324
|
+
return true;
|
|
325
|
+
}
|
|
326
|
+
const parentDir = path.dirname(currentDir);
|
|
327
|
+
if (parentDir === currentDir) break;
|
|
328
|
+
currentDir = parentDir;
|
|
329
|
+
}
|
|
330
|
+
cache?.set(packageName, false);
|
|
331
|
+
return false;
|
|
466
332
|
}
|
|
467
333
|
function resolveWithRolldown(ctx, id, importer, kind) {
|
|
468
|
-
|
|
334
|
+
return ctx.resolve(id, importer, {
|
|
335
|
+
kind,
|
|
336
|
+
skipSelf: true
|
|
337
|
+
});
|
|
469
338
|
}
|
|
470
339
|
function splitIdAndQuery(id) {
|
|
471
|
-
|
|
472
|
-
|
|
340
|
+
const [cleanId, rawQuery] = id.split("?");
|
|
341
|
+
return {
|
|
342
|
+
cleanId,
|
|
343
|
+
query: rawQuery ? `?${rawQuery}` : ""
|
|
344
|
+
};
|
|
473
345
|
}
|
|
474
346
|
function toFilePath(id) {
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
if (id.startsWith("file://")) {
|
|
479
|
-
return fileURLToPath(id);
|
|
480
|
-
}
|
|
481
|
-
return id;
|
|
347
|
+
if (!id) return null;
|
|
348
|
+
if (id.startsWith("file://")) return fileURLToPath(id);
|
|
349
|
+
return id;
|
|
482
350
|
}
|
|
483
351
|
function resolveSpecifierFromImporter(specifier, importerPath) {
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
352
|
+
try {
|
|
353
|
+
return createRequire(importerPath).resolve(specifier);
|
|
354
|
+
} catch {
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
//#endregion
|
|
359
|
+
//#region src/module-sync.ts
|
|
360
|
+
let cachedModuleSyncCondition;
|
|
361
|
+
let moduleSyncConditionPromise;
|
|
494
362
|
async function getModuleSyncConditionEnabled() {
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
});
|
|
508
|
-
}
|
|
509
|
-
return moduleSyncConditionPromise;
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
// src/bundler.ts
|
|
513
|
-
var JS_TS_EXT_RE = /\.[cm]?[jt]s$/;
|
|
363
|
+
if (cachedModuleSyncCondition !== void 0) return cachedModuleSyncCondition;
|
|
364
|
+
if (!moduleSyncConditionPromise) moduleSyncConditionPromise = import("#module-sync-enabled").then((mod) => Boolean(mod?.default)).catch(() => false).then((result) => {
|
|
365
|
+
cachedModuleSyncCondition = result;
|
|
366
|
+
return result;
|
|
367
|
+
}).finally(() => {
|
|
368
|
+
moduleSyncConditionPromise = void 0;
|
|
369
|
+
});
|
|
370
|
+
return moduleSyncConditionPromise;
|
|
371
|
+
}
|
|
372
|
+
//#endregion
|
|
373
|
+
//#region src/bundler.ts
|
|
374
|
+
const JS_TS_EXT_RE = /\.[cm]?[jt]s$/;
|
|
514
375
|
async function bundleFile(fileName, options) {
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
const rolldownOutputOptions = options?.rolldownOptions?.output || {};
|
|
590
|
-
const { codeSplitting: _codeSplitting, ...normalizedOutputOptions } = rolldownOutputOptions;
|
|
591
|
-
const result = await bundle.generate({
|
|
592
|
-
...normalizedOutputOptions,
|
|
593
|
-
format: options.format,
|
|
594
|
-
sourcemap: resolveSourcemapOutput(normalizedOutputOptions.sourcemap, options.sourcemap),
|
|
595
|
-
codeSplitting: false
|
|
596
|
-
});
|
|
597
|
-
await bundle.close();
|
|
598
|
-
const entryChunk = result.output.find(
|
|
599
|
-
(chunk) => chunk.type === "chunk" && chunk.isEntry
|
|
600
|
-
);
|
|
601
|
-
const bundleChunks = Object.fromEntries(
|
|
602
|
-
result.output.flatMap((c) => c.type === "chunk" ? [[c.fileName, c]] : [])
|
|
603
|
-
);
|
|
604
|
-
const allModules = /* @__PURE__ */ new Set();
|
|
605
|
-
collectReferencedModules(bundleChunks, entryChunk.fileName, allModules);
|
|
606
|
-
allModules.delete(fileName);
|
|
607
|
-
return {
|
|
608
|
-
code: entryChunk.code,
|
|
609
|
-
dependencies: [...allModules]
|
|
610
|
-
};
|
|
376
|
+
const { isESM } = options;
|
|
377
|
+
const dirnameVarName = "__vite_injected_original_dirname";
|
|
378
|
+
const filenameVarName = "__vite_injected_original_filename";
|
|
379
|
+
const importMetaUrlVarName = "__vite_injected_original_import_meta_url";
|
|
380
|
+
const { transform: userTransform, resolve: userResolve, ...restRolldownInputOptions } = options?.rolldownOptions?.input || {};
|
|
381
|
+
const transformDefine = {
|
|
382
|
+
...userTransform?.define ?? {},
|
|
383
|
+
"__dirname": dirnameVarName,
|
|
384
|
+
"__filename": filenameVarName,
|
|
385
|
+
"import.meta.url": importMetaUrlVarName,
|
|
386
|
+
"import.meta.dirname": dirnameVarName,
|
|
387
|
+
"import.meta.filename": filenameVarName
|
|
388
|
+
};
|
|
389
|
+
const transformOptions = {
|
|
390
|
+
...userTransform ?? {},
|
|
391
|
+
define: transformDefine
|
|
392
|
+
};
|
|
393
|
+
const moduleSyncEnabled = await getModuleSyncConditionEnabled();
|
|
394
|
+
const conditionNames = [...userResolve?.conditionNames ?? []];
|
|
395
|
+
if (moduleSyncEnabled && !conditionNames.includes("module-sync")) conditionNames.push("module-sync");
|
|
396
|
+
const resolveOptions = {
|
|
397
|
+
...userResolve ?? {},
|
|
398
|
+
mainFields: ["main"],
|
|
399
|
+
...typeof options.tsconfig === "string" ? { tsconfigFilename: options.tsconfig } : {},
|
|
400
|
+
...conditionNames.length ? { conditionNames } : {}
|
|
401
|
+
};
|
|
402
|
+
const originalConsoleWarn = console.warn;
|
|
403
|
+
console.warn = (...args) => {
|
|
404
|
+
const message = typeof args[0] === "string" ? args[0] : "";
|
|
405
|
+
if (message.includes("resolve.tsconfigFilename") || message.includes("Invalid input options") || message.includes("top-level \"define\" option is deprecated")) return;
|
|
406
|
+
originalConsoleWarn(...args);
|
|
407
|
+
};
|
|
408
|
+
let bundle;
|
|
409
|
+
try {
|
|
410
|
+
bundle = await rolldown({
|
|
411
|
+
...restRolldownInputOptions,
|
|
412
|
+
input: fileName,
|
|
413
|
+
platform: "node",
|
|
414
|
+
...options.tsconfig !== void 0 ? { tsconfig: options.tsconfig } : {},
|
|
415
|
+
resolve: resolveOptions,
|
|
416
|
+
define: transformDefine,
|
|
417
|
+
transform: transformOptions,
|
|
418
|
+
treeshake: false,
|
|
419
|
+
plugins: [createExternalizeDepsPlugin({
|
|
420
|
+
entryFile: fileName,
|
|
421
|
+
isESM
|
|
422
|
+
}), createFileScopeVariablesPlugin({
|
|
423
|
+
dirnameVarName,
|
|
424
|
+
filenameVarName,
|
|
425
|
+
importMetaUrlVarName
|
|
426
|
+
})],
|
|
427
|
+
external: options.external
|
|
428
|
+
});
|
|
429
|
+
} finally {
|
|
430
|
+
console.warn = originalConsoleWarn;
|
|
431
|
+
}
|
|
432
|
+
if (!bundle) throw new Error("Failed to initialize bundler");
|
|
433
|
+
const { codeSplitting: _codeSplitting, ...normalizedOutputOptions } = options?.rolldownOptions?.output || {};
|
|
434
|
+
const result = await bundle.generate({
|
|
435
|
+
...normalizedOutputOptions,
|
|
436
|
+
format: options.format,
|
|
437
|
+
sourcemap: resolveSourcemapOutput(normalizedOutputOptions.sourcemap, options.sourcemap),
|
|
438
|
+
codeSplitting: false
|
|
439
|
+
});
|
|
440
|
+
await bundle.close();
|
|
441
|
+
const entryChunk = result.output.find((chunk) => chunk.type === "chunk" && chunk.isEntry);
|
|
442
|
+
const bundleChunks = Object.fromEntries(result.output.flatMap((c) => c.type === "chunk" ? [[c.fileName, c]] : []));
|
|
443
|
+
const allModules = /* @__PURE__ */ new Set();
|
|
444
|
+
collectReferencedModules(bundleChunks, entryChunk.fileName, allModules);
|
|
445
|
+
allModules.delete(fileName);
|
|
446
|
+
return {
|
|
447
|
+
code: entryChunk.code,
|
|
448
|
+
dependencies: [...allModules]
|
|
449
|
+
};
|
|
611
450
|
}
|
|
612
451
|
function resolveSourcemapOutput(outputSourcemap, requested) {
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
}
|
|
636
|
-
}
|
|
637
|
-
};
|
|
638
|
-
}
|
|
639
|
-
|
|
640
|
-
// src/cache.ts
|
|
641
|
-
import crypto from "crypto";
|
|
642
|
-
import fs4 from "fs";
|
|
643
|
-
import fsp from "fs/promises";
|
|
644
|
-
import { createRequire as createRequire4 } from "module";
|
|
645
|
-
import os from "os";
|
|
646
|
-
import path5 from "path";
|
|
647
|
-
import process4 from "process";
|
|
648
|
-
import { pathToFileURL as pathToFileURL3 } from "url";
|
|
649
|
-
var _require = createRequire4(import.meta.url);
|
|
650
|
-
var memoryCache = /* @__PURE__ */ new Map();
|
|
452
|
+
if (outputSourcemap !== void 0) return outputSourcemap;
|
|
453
|
+
if (requested === true) return "inline";
|
|
454
|
+
return requested ?? false;
|
|
455
|
+
}
|
|
456
|
+
function createFileScopeVariablesPlugin({ dirnameVarName, filenameVarName, importMetaUrlVarName }) {
|
|
457
|
+
return {
|
|
458
|
+
name: "inject-file-scope-variables",
|
|
459
|
+
transform: {
|
|
460
|
+
filter: { id: JS_TS_EXT_RE },
|
|
461
|
+
async handler(code, id) {
|
|
462
|
+
return {
|
|
463
|
+
code: `const ${dirnameVarName} = ${JSON.stringify(path.dirname(id))};const ${filenameVarName} = ${JSON.stringify(id)};const ${importMetaUrlVarName} = ${JSON.stringify(pathToFileURL(id).href)};` + code,
|
|
464
|
+
map: null
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
//#endregion
|
|
471
|
+
//#region src/cache.ts
|
|
472
|
+
const _require$1 = createRequire(import.meta.url);
|
|
473
|
+
const memoryCache = /* @__PURE__ */ new Map();
|
|
651
474
|
function resolveCacheOptions(fileName, options) {
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
};
|
|
475
|
+
const cacheOpt = options.cache;
|
|
476
|
+
if (!(cacheOpt === true || typeof cacheOpt === "object" && cacheOpt.enabled !== false)) return {
|
|
477
|
+
enabled: false,
|
|
478
|
+
key: "",
|
|
479
|
+
dir: "",
|
|
480
|
+
reset: false,
|
|
481
|
+
entryPath: fileName,
|
|
482
|
+
memory: false,
|
|
483
|
+
onEvent: void 0
|
|
484
|
+
};
|
|
485
|
+
const dir = typeof cacheOpt === "object" && cacheOpt.dir ? cacheOpt.dir : resolveDefaultCacheDir(fileName);
|
|
486
|
+
const reset = typeof cacheOpt === "object" && cacheOpt.reset === true;
|
|
487
|
+
const onEvent = typeof cacheOpt === "object" ? cacheOpt.onEvent : void 0;
|
|
488
|
+
const memory = !(typeof cacheOpt === "object" && cacheOpt.memory === false);
|
|
489
|
+
const stat = tryStatSync(fileName);
|
|
490
|
+
if (!stat) {
|
|
491
|
+
onEvent?.({
|
|
492
|
+
type: "skip-invalid",
|
|
493
|
+
key: "",
|
|
494
|
+
reason: "missing-entry"
|
|
495
|
+
});
|
|
496
|
+
return {
|
|
497
|
+
enabled: false,
|
|
498
|
+
key: "",
|
|
499
|
+
dir,
|
|
500
|
+
reset,
|
|
501
|
+
entryPath: fileName,
|
|
502
|
+
memory,
|
|
503
|
+
onEvent
|
|
504
|
+
};
|
|
505
|
+
}
|
|
506
|
+
const hash = crypto.createHash("sha1");
|
|
507
|
+
hash.update(JSON.stringify({
|
|
508
|
+
entry: path.resolve(fileName),
|
|
509
|
+
mtimeMs: stat.mtimeMs,
|
|
510
|
+
size: stat.size,
|
|
511
|
+
format: options.format,
|
|
512
|
+
isESM: options.isESM,
|
|
513
|
+
tsconfig: options.tsconfig ?? "auto",
|
|
514
|
+
node: process.versions.node,
|
|
515
|
+
rolldown: hashRolldownOptions(options.rolldownOptions)
|
|
516
|
+
}));
|
|
517
|
+
return {
|
|
518
|
+
enabled: true,
|
|
519
|
+
key: hash.digest("hex"),
|
|
520
|
+
dir,
|
|
521
|
+
reset,
|
|
522
|
+
entryPath: path.resolve(fileName),
|
|
523
|
+
memory,
|
|
524
|
+
onEvent
|
|
525
|
+
};
|
|
704
526
|
}
|
|
705
527
|
function resolveDefaultCacheDir(fileName) {
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
}
|
|
712
|
-
return path5.join(os.tmpdir(), "rolldown-require-cache");
|
|
528
|
+
if (typeof process.versions.deno !== "string") {
|
|
529
|
+
const nearest = findNearestNodeModules(path.dirname(fileName));
|
|
530
|
+
if (nearest) return path.resolve(nearest, ".rolldown-require-cache");
|
|
531
|
+
}
|
|
532
|
+
return path.join(os.tmpdir(), "rolldown-require-cache");
|
|
713
533
|
}
|
|
714
534
|
async function maybeReadCache(cache, options) {
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
535
|
+
const metaPath = path.join(cache.dir, `${cache.key}.meta.json`);
|
|
536
|
+
const mem = cache.memory ? memoryCache.get(cache.key) : void 0;
|
|
537
|
+
if (mem?.meta) {
|
|
538
|
+
const valid = validateMeta(mem.meta, options);
|
|
539
|
+
if (valid === true) {
|
|
540
|
+
cache.onEvent?.({
|
|
541
|
+
type: "hit",
|
|
542
|
+
key: cache.key,
|
|
543
|
+
reason: "memory"
|
|
544
|
+
});
|
|
545
|
+
return mem;
|
|
546
|
+
}
|
|
547
|
+
memoryCache.delete(cache.key);
|
|
548
|
+
cache.onEvent?.({
|
|
549
|
+
type: "skip-invalid",
|
|
550
|
+
key: cache.key,
|
|
551
|
+
reason: valid
|
|
552
|
+
});
|
|
553
|
+
}
|
|
554
|
+
const meta = await readCacheMeta(metaPath);
|
|
555
|
+
if (!meta) return;
|
|
556
|
+
const valid = validateMeta(meta, options);
|
|
557
|
+
if (valid !== true) {
|
|
558
|
+
cache.onEvent?.({
|
|
559
|
+
type: "skip-invalid",
|
|
560
|
+
key: cache.key,
|
|
561
|
+
reason: valid
|
|
562
|
+
});
|
|
563
|
+
return;
|
|
564
|
+
}
|
|
565
|
+
if (mem?.mod !== void 0 && cache.memory) {
|
|
566
|
+
const enriched = {
|
|
567
|
+
mod: mem.mod,
|
|
568
|
+
codePath: meta.codePath,
|
|
569
|
+
meta
|
|
570
|
+
};
|
|
571
|
+
memoryCache.set(cache.key, enriched);
|
|
572
|
+
cache.onEvent?.({
|
|
573
|
+
type: "hit",
|
|
574
|
+
key: cache.key,
|
|
575
|
+
reason: "memory"
|
|
576
|
+
});
|
|
577
|
+
return enriched;
|
|
578
|
+
}
|
|
579
|
+
return {
|
|
580
|
+
codePath: meta.codePath,
|
|
581
|
+
meta
|
|
582
|
+
};
|
|
742
583
|
}
|
|
743
584
|
async function importCachedCode(cached, options) {
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
return options.require(target, { format: options.format });
|
|
750
|
-
}
|
|
751
|
-
if (options.format === "esm") {
|
|
752
|
-
return import(target);
|
|
753
|
-
}
|
|
754
|
-
return _require(target);
|
|
585
|
+
if (cached.mod !== void 0) return cached.mod;
|
|
586
|
+
const target = options.format === "esm" ? pathToFileURL(cached.codePath).href : cached.codePath;
|
|
587
|
+
if (options.require) return options.require(target, { format: options.format });
|
|
588
|
+
if (options.format === "esm") return import(target);
|
|
589
|
+
return _require$1(target);
|
|
755
590
|
}
|
|
756
591
|
async function storeCacheOutput(cache, code, options, dependencies) {
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
outfile: options.format === "esm" ? pathToFileURL3(codePath).href : codePath,
|
|
773
|
-
cleanup: async () => {
|
|
774
|
-
},
|
|
775
|
-
cacheMeta: {
|
|
776
|
-
format: options.format,
|
|
777
|
-
codePath,
|
|
778
|
-
files: trackedFiles
|
|
779
|
-
}
|
|
780
|
-
};
|
|
592
|
+
await fsp.mkdir(cache.dir, { recursive: true });
|
|
593
|
+
const ext = options.format === "cjs" ? "cjs" : "mjs";
|
|
594
|
+
const codePath = path.join(cache.dir, `${cache.key}.code.${ext}`);
|
|
595
|
+
if (cache.reset) await Promise.allSettled([fsp.rm(codePath, { force: true }), fsp.rm(path.join(cache.dir, `${cache.key}.meta.json`), { force: true })]);
|
|
596
|
+
await fsp.writeFile(codePath, code);
|
|
597
|
+
const trackedFiles = collectFileStats([cache.entryPath, ...dependencies ?? []]);
|
|
598
|
+
return {
|
|
599
|
+
outfile: options.format === "esm" ? pathToFileURL(codePath).href : codePath,
|
|
600
|
+
cleanup: async () => {},
|
|
601
|
+
cacheMeta: {
|
|
602
|
+
format: options.format,
|
|
603
|
+
codePath,
|
|
604
|
+
files: trackedFiles
|
|
605
|
+
}
|
|
606
|
+
};
|
|
781
607
|
}
|
|
782
608
|
async function writeCacheMeta(cache, meta) {
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
memoryCache.set(cache.key, { codePath: meta.codePath, meta });
|
|
790
|
-
}
|
|
609
|
+
await fsp.mkdir(cache.dir, { recursive: true });
|
|
610
|
+
await fsp.writeFile(path.join(cache.dir, `${cache.key}.meta.json`), JSON.stringify(meta));
|
|
611
|
+
if (cache.memory) memoryCache.set(cache.key, {
|
|
612
|
+
codePath: meta.codePath,
|
|
613
|
+
meta
|
|
614
|
+
});
|
|
791
615
|
}
|
|
792
616
|
function collectFileStats(files) {
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
});
|
|
807
|
-
}
|
|
808
|
-
}
|
|
809
|
-
return stats;
|
|
617
|
+
const seen = /* @__PURE__ */ new Set();
|
|
618
|
+
const stats = [];
|
|
619
|
+
for (const file of files) {
|
|
620
|
+
if (!file || seen.has(file)) continue;
|
|
621
|
+
seen.add(file);
|
|
622
|
+
const stat = tryStatSync(file);
|
|
623
|
+
if (stat?.isFile()) stats.push({
|
|
624
|
+
path: path.resolve(file),
|
|
625
|
+
mtimeMs: stat.mtimeMs,
|
|
626
|
+
size: stat.size
|
|
627
|
+
});
|
|
628
|
+
}
|
|
629
|
+
return stats;
|
|
810
630
|
}
|
|
811
631
|
function writeMemoryCache(cache, mod, meta) {
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
632
|
+
if (!cache.memory) return;
|
|
633
|
+
memoryCache.set(cache.key, {
|
|
634
|
+
mod,
|
|
635
|
+
codePath: meta.codePath,
|
|
636
|
+
meta
|
|
637
|
+
});
|
|
816
638
|
}
|
|
817
639
|
async function readCacheMeta(metaPath) {
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
}
|
|
823
|
-
return void 0;
|
|
640
|
+
try {
|
|
641
|
+
const raw = await fsp.readFile(metaPath, "utf-8");
|
|
642
|
+
return JSON.parse(raw);
|
|
643
|
+
} catch {}
|
|
824
644
|
}
|
|
825
645
|
function validateMeta(meta, options) {
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
const stat = tryStatSync(file.path);
|
|
834
|
-
if (!stat || stat.mtimeMs !== file.mtimeMs || stat.size !== file.size) {
|
|
835
|
-
return "stale-deps";
|
|
836
|
-
}
|
|
837
|
-
}
|
|
838
|
-
return true;
|
|
646
|
+
if (meta.format !== options.format) return "format-mismatch";
|
|
647
|
+
if (!meta.codePath || !fs.existsSync(meta.codePath)) return "missing-code";
|
|
648
|
+
for (const file of meta.files ?? []) {
|
|
649
|
+
const stat = tryStatSync(file.path);
|
|
650
|
+
if (!stat || stat.mtimeMs !== file.mtimeMs || stat.size !== file.size) return "stale-deps";
|
|
651
|
+
}
|
|
652
|
+
return true;
|
|
839
653
|
}
|
|
840
654
|
function hashRolldownOptions(options) {
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
var defaultGetOutputFile = (filepath, _format) => {
|
|
861
|
-
return filepath;
|
|
655
|
+
if (!options) return "none";
|
|
656
|
+
return crypto.createHash("sha1").update(JSON.stringify(options, (_key, value) => {
|
|
657
|
+
if (typeof value === "function" || typeof value === "symbol") return;
|
|
658
|
+
return value;
|
|
659
|
+
})).digest("hex");
|
|
660
|
+
}
|
|
661
|
+
//#endregion
|
|
662
|
+
//#region src/config.ts
|
|
663
|
+
const configDefaults = Object.freeze({ resolve: { extensions: [
|
|
664
|
+
".mjs",
|
|
665
|
+
".js",
|
|
666
|
+
".mts",
|
|
667
|
+
".ts",
|
|
668
|
+
".jsx",
|
|
669
|
+
".tsx",
|
|
670
|
+
".json"
|
|
671
|
+
] } });
|
|
672
|
+
const defaultGetOutputFile = (filepath, _format) => {
|
|
673
|
+
return filepath;
|
|
862
674
|
};
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
import { createRequire as createRequire5 } from "module";
|
|
867
|
-
import path7 from "path";
|
|
868
|
-
import { promisify } from "util";
|
|
869
|
-
|
|
870
|
-
// src/temp-output.ts
|
|
871
|
-
import { Buffer as Buffer2 } from "buffer";
|
|
872
|
-
import fsp2 from "fs/promises";
|
|
873
|
-
import os2 from "os";
|
|
874
|
-
import path6 from "path";
|
|
875
|
-
import process5 from "process";
|
|
876
|
-
import { pathToFileURL as pathToFileURL4 } from "url";
|
|
877
|
-
var NON_WORD_CHAR_RE = /[^\w.-]/g;
|
|
675
|
+
//#endregion
|
|
676
|
+
//#region src/temp-output.ts
|
|
677
|
+
const NON_WORD_CHAR_RE = /[^\w.-]/g;
|
|
878
678
|
function sanitizeFilename(name) {
|
|
879
|
-
|
|
679
|
+
return name.replace(NON_WORD_CHAR_RE, "_");
|
|
880
680
|
}
|
|
881
681
|
async function resolveTempOutputFile(sourceFile, code, options) {
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
const dataUrl = `data:text/javascript;base64,${Buffer2.from(code).toString("base64")}`;
|
|
918
|
-
return {
|
|
919
|
-
outfile: dataUrl,
|
|
920
|
-
cleanup: async () => {
|
|
921
|
-
}
|
|
922
|
-
};
|
|
923
|
-
}
|
|
924
|
-
throw new Error("Failed to create temporary output file for bundled code");
|
|
925
|
-
}
|
|
926
|
-
|
|
927
|
-
// src/loader.ts
|
|
928
|
-
var _require2 = createRequire5(import.meta.url);
|
|
929
|
-
var promisifiedRealpath = promisify(fs5.realpath);
|
|
682
|
+
const getOutputFile = options.getOutputFile || defaultGetOutputFile;
|
|
683
|
+
const fileName = `${sanitizeFilename(path.basename(sourceFile))}.${`timestamp-${Date.now()}-${Math.random().toString(16).slice(2)}`}.${options.format === "cjs" ? "cjs" : "mjs"}`;
|
|
684
|
+
const candidates = [];
|
|
685
|
+
if (typeof process.versions.deno !== "string") {
|
|
686
|
+
const nearest = findNearestNodeModules(path.dirname(sourceFile));
|
|
687
|
+
if (nearest) candidates.push(path.resolve(nearest, ".rolldown-require"));
|
|
688
|
+
}
|
|
689
|
+
candidates.push(path.join(os.tmpdir(), "rolldown-require"));
|
|
690
|
+
for (const base of candidates) {
|
|
691
|
+
const target = getOutputFile(path.join(base, fileName), options.format);
|
|
692
|
+
try {
|
|
693
|
+
await fsp.mkdir(path.dirname(target), { recursive: true });
|
|
694
|
+
await fsp.writeFile(target, code);
|
|
695
|
+
const cleanup = async () => {
|
|
696
|
+
if (options.preserveTemporaryFile) return;
|
|
697
|
+
try {
|
|
698
|
+
await fsp.unlink(target);
|
|
699
|
+
} catch {}
|
|
700
|
+
};
|
|
701
|
+
return {
|
|
702
|
+
outfile: options.format === "esm" ? pathToFileURL(target).href : target,
|
|
703
|
+
cleanup
|
|
704
|
+
};
|
|
705
|
+
} catch {}
|
|
706
|
+
}
|
|
707
|
+
if (options.format === "esm") return {
|
|
708
|
+
outfile: `data:text/javascript;base64,${Buffer$1.from(code).toString("base64")}`,
|
|
709
|
+
cleanup: async () => {}
|
|
710
|
+
};
|
|
711
|
+
throw new Error("Failed to create temporary output file for bundled code");
|
|
712
|
+
}
|
|
713
|
+
//#endregion
|
|
714
|
+
//#region src/loader.ts
|
|
715
|
+
const _require = createRequire(import.meta.url);
|
|
716
|
+
const promisifiedRealpath = promisify(fs.realpath);
|
|
930
717
|
async function loadFromBundledFile(fileName, bundledCode, options, dependencies) {
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
}
|
|
999
|
-
}
|
|
1000
|
-
}
|
|
1001
|
-
|
|
1002
|
-
// src/options.ts
|
|
1003
|
-
import path8 from "path";
|
|
1004
|
-
import process6 from "process";
|
|
1005
|
-
import { getTsconfig } from "get-tsconfig";
|
|
718
|
+
const cacheConfig = resolveCacheOptions(fileName, options);
|
|
719
|
+
if (cacheConfig.enabled) {
|
|
720
|
+
const cached = await maybeReadCache(cacheConfig, options);
|
|
721
|
+
if (cached) {
|
|
722
|
+
if (!cached.mod) cacheConfig.onEvent?.({
|
|
723
|
+
type: "hit",
|
|
724
|
+
key: cacheConfig.key
|
|
725
|
+
});
|
|
726
|
+
return importCachedCode(cached, options);
|
|
727
|
+
}
|
|
728
|
+
cacheConfig.onEvent?.({
|
|
729
|
+
type: "miss",
|
|
730
|
+
key: cacheConfig.key
|
|
731
|
+
});
|
|
732
|
+
}
|
|
733
|
+
const { isESM } = options;
|
|
734
|
+
if (isESM) {
|
|
735
|
+
const tempOutput = cacheConfig.enabled ? await storeCacheOutput(cacheConfig, bundledCode, options, dependencies) : await resolveTempOutputFile(fileName, bundledCode, options);
|
|
736
|
+
const outfile = tempOutput?.outfile;
|
|
737
|
+
const cleanup = tempOutput?.cleanup;
|
|
738
|
+
let mod;
|
|
739
|
+
const req = options.require || dynamicImport;
|
|
740
|
+
try {
|
|
741
|
+
mod = await req(outfile, { format: options.format });
|
|
742
|
+
if (cacheConfig.enabled && tempOutput?.cacheMeta) {
|
|
743
|
+
await writeCacheMeta(cacheConfig, tempOutput.cacheMeta);
|
|
744
|
+
cacheConfig.onEvent?.({
|
|
745
|
+
type: "store",
|
|
746
|
+
key: cacheConfig.key
|
|
747
|
+
});
|
|
748
|
+
writeMemoryCache(cacheConfig, mod, tempOutput.cacheMeta);
|
|
749
|
+
}
|
|
750
|
+
return mod;
|
|
751
|
+
} finally {
|
|
752
|
+
if (!cacheConfig.enabled) await cleanup?.();
|
|
753
|
+
}
|
|
754
|
+
} else {
|
|
755
|
+
const extension = path.extname(fileName);
|
|
756
|
+
const realFileName = await promisifiedRealpath(fileName);
|
|
757
|
+
const loaderExt = extension in _require.extensions ? extension : ".js";
|
|
758
|
+
const defaultLoader = _require.extensions[loaderExt];
|
|
759
|
+
const compileLoader = (module, filename) => {
|
|
760
|
+
if (filename === realFileName) module._compile(bundledCode, filename);
|
|
761
|
+
else defaultLoader(module, filename);
|
|
762
|
+
};
|
|
763
|
+
let raw;
|
|
764
|
+
try {
|
|
765
|
+
_require.extensions[loaderExt] = compileLoader;
|
|
766
|
+
delete _require.cache[_require.resolve(fileName)];
|
|
767
|
+
raw = _require(fileName);
|
|
768
|
+
return raw.__esModule ? raw.default : raw;
|
|
769
|
+
} finally {
|
|
770
|
+
_require.extensions[loaderExt] = defaultLoader;
|
|
771
|
+
if (cacheConfig.enabled && raw !== void 0) {
|
|
772
|
+
const cachedPath = await storeCacheOutput(cacheConfig, bundledCode, options, dependencies);
|
|
773
|
+
await writeCacheMeta(cacheConfig, cachedPath.cacheMeta);
|
|
774
|
+
cacheConfig.onEvent?.({
|
|
775
|
+
type: "store",
|
|
776
|
+
key: cacheConfig.key
|
|
777
|
+
});
|
|
778
|
+
writeMemoryCache(cacheConfig, raw.__esModule ? raw.default : raw, cachedPath.cacheMeta);
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
//#endregion
|
|
784
|
+
//#region src/options.ts
|
|
1006
785
|
function resolveEntryFilepath(options) {
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
const cwd = options.cwd ? path8.resolve(options.cwd) : process6.cwd();
|
|
1011
|
-
return path8.resolve(cwd, options.filepath);
|
|
786
|
+
if (path.isAbsolute(options.filepath)) return options.filepath;
|
|
787
|
+
const cwd = options.cwd ? path.resolve(options.cwd) : process.cwd();
|
|
788
|
+
return path.resolve(cwd, options.filepath);
|
|
1012
789
|
}
|
|
1013
790
|
function detectModuleType(resolvedPath) {
|
|
1014
|
-
|
|
791
|
+
return typeof process.versions.deno === "string" || isFilePathESM(resolvedPath);
|
|
1015
792
|
}
|
|
1016
793
|
function createInternalOptions(userOptions, isESM) {
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
format,
|
|
1029
|
-
tsconfig,
|
|
1030
|
-
sourcemap
|
|
1031
|
-
};
|
|
794
|
+
const { filepath: _filepath, cwd: _cwd, ...rest } = userOptions;
|
|
795
|
+
const tsconfig = userOptions.tsconfig === false ? false : resolveTsconfigPath(userOptions);
|
|
796
|
+
const format = userOptions.format ?? (isESM ? "esm" : "cjs");
|
|
797
|
+
const sourcemap = resolveSourcemapOption(userOptions);
|
|
798
|
+
return {
|
|
799
|
+
...rest,
|
|
800
|
+
isESM,
|
|
801
|
+
format,
|
|
802
|
+
tsconfig,
|
|
803
|
+
sourcemap
|
|
804
|
+
};
|
|
1032
805
|
}
|
|
1033
806
|
function resolveTsconfigPath(options) {
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
if (typeof options.tsconfig === "string") {
|
|
1038
|
-
return options.tsconfig;
|
|
1039
|
-
}
|
|
1040
|
-
return getTsconfig(options.cwd, "tsconfig.json")?.path ?? void 0;
|
|
807
|
+
if (options.tsconfig === false) return;
|
|
808
|
+
if (typeof options.tsconfig === "string") return options.tsconfig;
|
|
809
|
+
return getTsconfig(options.cwd, "tsconfig.json")?.path ?? void 0;
|
|
1041
810
|
}
|
|
1042
811
|
function resolveSourcemapOption(options) {
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
}
|
|
1046
|
-
if (shouldEnableSourcemapFromEnv()) {
|
|
1047
|
-
return "inline";
|
|
1048
|
-
}
|
|
1049
|
-
return void 0;
|
|
812
|
+
if (options.sourcemap !== void 0) return options.sourcemap;
|
|
813
|
+
if (shouldEnableSourcemapFromEnv()) return "inline";
|
|
1050
814
|
}
|
|
1051
815
|
function shouldEnableSourcemapFromEnv() {
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
if (process6.env.VSCODE_INSPECTOR_OPTIONS) {
|
|
1057
|
-
return true;
|
|
1058
|
-
}
|
|
1059
|
-
if (process6.env.DEBUG) {
|
|
1060
|
-
return true;
|
|
1061
|
-
}
|
|
1062
|
-
return false;
|
|
816
|
+
if ((process.env.NODE_OPTIONS ?? "").includes("--inspect")) return true;
|
|
817
|
+
if (process.env.VSCODE_INSPECTOR_OPTIONS) return true;
|
|
818
|
+
if (process.env.DEBUG) return true;
|
|
819
|
+
return false;
|
|
1063
820
|
}
|
|
1064
|
-
|
|
1065
|
-
|
|
821
|
+
//#endregion
|
|
822
|
+
//#region src/index.ts
|
|
1066
823
|
async function bundleRequire(options) {
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
internalOptions,
|
|
1090
|
-
bundled.dependencies
|
|
1091
|
-
);
|
|
1092
|
-
return {
|
|
1093
|
-
mod,
|
|
1094
|
-
dependencies: bundled.dependencies
|
|
1095
|
-
};
|
|
1096
|
-
}
|
|
1097
|
-
export {
|
|
1098
|
-
bundleFile,
|
|
1099
|
-
bundleRequire,
|
|
1100
|
-
configDefaults,
|
|
1101
|
-
loadFromBundledFile
|
|
1102
|
-
};
|
|
824
|
+
const resolvedPath = resolveEntryFilepath(options);
|
|
825
|
+
const internalOptions = createInternalOptions(options, detectModuleType(resolvedPath));
|
|
826
|
+
const cacheConfig = resolveCacheOptions(resolvedPath, internalOptions);
|
|
827
|
+
if (cacheConfig.enabled) {
|
|
828
|
+
const cached = await maybeReadCache(cacheConfig, internalOptions);
|
|
829
|
+
if (cached) {
|
|
830
|
+
const mod = await importCachedCode(cached, internalOptions);
|
|
831
|
+
if (cacheConfig.memory && cached.meta) writeMemoryCache(cacheConfig, mod, cached.meta);
|
|
832
|
+
return {
|
|
833
|
+
mod,
|
|
834
|
+
dependencies: cached.meta?.files?.map((file) => file.path).filter((file) => file !== cacheConfig.entryPath) ?? []
|
|
835
|
+
};
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
const bundled = await bundleFile(resolvedPath, internalOptions);
|
|
839
|
+
return {
|
|
840
|
+
mod: await loadFromBundledFile(resolvedPath, bundled.code, internalOptions, bundled.dependencies),
|
|
841
|
+
dependencies: bundled.dependencies
|
|
842
|
+
};
|
|
843
|
+
}
|
|
844
|
+
//#endregion
|
|
845
|
+
export { bundleFile, bundleRequire, configDefaults, loadFromBundledFile };
|