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 +21 -0
- package/README.md +126 -0
- package/dist/chunk-Q6XJHTDB.cjs +15 -0
- package/dist/chunk-WUKYLWAZ.mjs +0 -0
- package/dist/false-G5OFYI37.mjs +7 -0
- package/dist/false-SI6OY5LO.cjs +9 -0
- package/dist/index.cjs +1151 -0
- package/dist/index.d.cts +77 -0
- package/dist/index.d.ts +77 -0
- package/dist/index.mjs +1148 -0
- package/misc/false.d.ts +2 -0
- package/misc/false.js +1 -0
- package/misc/true.d.ts +2 -0
- package/misc/true.js +1 -0
- package/package.json +63 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,1148 @@
|
|
|
1
|
+
import "./chunk-WUKYLWAZ.mjs";
|
|
2
|
+
|
|
3
|
+
// src/index.ts
|
|
4
|
+
import fs4 from "fs";
|
|
5
|
+
import fsp2 from "fs/promises";
|
|
6
|
+
import { createRequire as createRequire3 } from "module";
|
|
7
|
+
import path6 from "path";
|
|
8
|
+
import process4 from "process";
|
|
9
|
+
import { pathToFileURL } from "url";
|
|
10
|
+
import { promisify } from "util";
|
|
11
|
+
import { getTsconfig } from "get-tsconfig";
|
|
12
|
+
import { rolldown } from "rolldown";
|
|
13
|
+
|
|
14
|
+
// src/packages.ts
|
|
15
|
+
import fs2 from "fs";
|
|
16
|
+
import { createRequire as createRequire2 } from "module";
|
|
17
|
+
import path3 from "path";
|
|
18
|
+
import process3 from "process";
|
|
19
|
+
|
|
20
|
+
// src/utils.ts
|
|
21
|
+
import { exec } from "child_process";
|
|
22
|
+
import crypto from "crypto";
|
|
23
|
+
import { promises as dns } from "dns";
|
|
24
|
+
import fs from "fs";
|
|
25
|
+
import fsp from "fs/promises";
|
|
26
|
+
import { builtinModules, createRequire } from "module";
|
|
27
|
+
import path2 from "path";
|
|
28
|
+
import process2 from "process";
|
|
29
|
+
import { fileURLToPath as fileURLToPath2, URL as URL2 } from "url";
|
|
30
|
+
import { createFilter as _createFilter } from "@rollup/pluginutils";
|
|
31
|
+
|
|
32
|
+
// src/constants.ts
|
|
33
|
+
import { readFileSync } from "fs";
|
|
34
|
+
import path, { resolve } from "path";
|
|
35
|
+
import { fileURLToPath } from "url";
|
|
36
|
+
var { version } = JSON.parse(
|
|
37
|
+
readFileSync(new URL("../package.json", import.meta.url)).toString()
|
|
38
|
+
);
|
|
39
|
+
var DEFAULT_MAIN_FIELDS = [
|
|
40
|
+
"browser",
|
|
41
|
+
"module",
|
|
42
|
+
"jsnext:main",
|
|
43
|
+
// moment still uses this...
|
|
44
|
+
"jsnext"
|
|
45
|
+
];
|
|
46
|
+
var DEFAULT_CLIENT_MAIN_FIELDS = Object.freeze(DEFAULT_MAIN_FIELDS);
|
|
47
|
+
var DEFAULT_SERVER_MAIN_FIELDS = Object.freeze(
|
|
48
|
+
DEFAULT_MAIN_FIELDS.filter((f) => f !== "browser")
|
|
49
|
+
);
|
|
50
|
+
var DEV_PROD_CONDITION = `development|production`;
|
|
51
|
+
var DEFAULT_CONDITIONS = ["module", "browser", "node", DEV_PROD_CONDITION];
|
|
52
|
+
var DEFAULT_CLIENT_CONDITIONS = Object.freeze(
|
|
53
|
+
DEFAULT_CONDITIONS.filter((c) => c !== "node")
|
|
54
|
+
);
|
|
55
|
+
var DEFAULT_SERVER_CONDITIONS = Object.freeze(
|
|
56
|
+
DEFAULT_CONDITIONS.filter((c) => c !== "browser")
|
|
57
|
+
);
|
|
58
|
+
var OPTIMIZABLE_ENTRY_RE = /\.[cm]?[jt]s$/;
|
|
59
|
+
var SPECIAL_QUERY_RE = /[?&](?:worker|sharedworker|raw|url)\b/;
|
|
60
|
+
var FS_PREFIX = `/@fs/`;
|
|
61
|
+
var CLIENT_PUBLIC_PATH = `/@vite/client`;
|
|
62
|
+
var ENV_PUBLIC_PATH = `/@vite/env`;
|
|
63
|
+
var VITE_PACKAGE_DIR = resolve(
|
|
64
|
+
// import.meta.url is `dist/node/constants.js` after bundle
|
|
65
|
+
fileURLToPath(import.meta.url),
|
|
66
|
+
"../../.."
|
|
67
|
+
);
|
|
68
|
+
var CLIENT_ENTRY = resolve(VITE_PACKAGE_DIR, "dist/client/client.mjs");
|
|
69
|
+
var ENV_ENTRY = resolve(VITE_PACKAGE_DIR, "dist/client/env.mjs");
|
|
70
|
+
var CLIENT_DIR = path.dirname(CLIENT_ENTRY);
|
|
71
|
+
var VALID_ID_PREFIX = `/@id/`;
|
|
72
|
+
var SOURCEMAPPING_URL = "sourceMa";
|
|
73
|
+
SOURCEMAPPING_URL += "ppingURL";
|
|
74
|
+
|
|
75
|
+
// src/sharedUtils.ts
|
|
76
|
+
import process from "process";
|
|
77
|
+
var isWindows = typeof process !== "undefined" && process.platform === "win32";
|
|
78
|
+
var windowsSlashRE = /\\/g;
|
|
79
|
+
function slash(p) {
|
|
80
|
+
return p.replace(windowsSlashRE, "/");
|
|
81
|
+
}
|
|
82
|
+
var postfixRE = /[?#].*$/;
|
|
83
|
+
function cleanUrl(url) {
|
|
84
|
+
return url.replace(postfixRE, "");
|
|
85
|
+
}
|
|
86
|
+
function splitFileAndPostfix(path7) {
|
|
87
|
+
const file = cleanUrl(path7);
|
|
88
|
+
return { file, postfix: path7.slice(file.length) };
|
|
89
|
+
}
|
|
90
|
+
var AsyncFunction = async function() {
|
|
91
|
+
}.constructor;
|
|
92
|
+
|
|
93
|
+
// src/utils.ts
|
|
94
|
+
import { VERSION } from "rolldown";
|
|
95
|
+
import { withFilter } from "rolldown/filter";
|
|
96
|
+
var createFilter = _createFilter;
|
|
97
|
+
var NODE_BUILTIN_NAMESPACE = "node:";
|
|
98
|
+
var NPM_BUILTIN_NAMESPACE = "npm:";
|
|
99
|
+
var BUN_BUILTIN_NAMESPACE = "bun:";
|
|
100
|
+
var nodeBuiltins = builtinModules.filter((id) => !id.includes(":"));
|
|
101
|
+
var isBuiltinCache = /* @__PURE__ */ new WeakMap();
|
|
102
|
+
function isBuiltin(builtins, id) {
|
|
103
|
+
let isBuiltin2 = isBuiltinCache.get(builtins);
|
|
104
|
+
if (!isBuiltin2) {
|
|
105
|
+
isBuiltin2 = createIsBuiltin(builtins);
|
|
106
|
+
isBuiltinCache.set(builtins, isBuiltin2);
|
|
107
|
+
}
|
|
108
|
+
return isBuiltin2(id);
|
|
109
|
+
}
|
|
110
|
+
function createIsBuiltin(builtins) {
|
|
111
|
+
const plainBuiltinsSet = new Set(
|
|
112
|
+
builtins.filter((builtin) => typeof builtin === "string")
|
|
113
|
+
);
|
|
114
|
+
const regexBuiltins = builtins.filter(
|
|
115
|
+
(builtin) => typeof builtin !== "string"
|
|
116
|
+
);
|
|
117
|
+
return (id) => plainBuiltinsSet.has(id) || regexBuiltins.some((regexp) => regexp.test(id));
|
|
118
|
+
}
|
|
119
|
+
var nodeLikeBuiltins = [
|
|
120
|
+
...nodeBuiltins,
|
|
121
|
+
new RegExp(`^${NODE_BUILTIN_NAMESPACE}`),
|
|
122
|
+
new RegExp(`^${NPM_BUILTIN_NAMESPACE}`),
|
|
123
|
+
new RegExp(`^${BUN_BUILTIN_NAMESPACE}`)
|
|
124
|
+
];
|
|
125
|
+
function isNodeLikeBuiltin(id) {
|
|
126
|
+
return isBuiltin(nodeLikeBuiltins, id);
|
|
127
|
+
}
|
|
128
|
+
function isNodeBuiltin(id) {
|
|
129
|
+
if (id.startsWith(NODE_BUILTIN_NAMESPACE)) {
|
|
130
|
+
return true;
|
|
131
|
+
}
|
|
132
|
+
return nodeBuiltins.includes(id);
|
|
133
|
+
}
|
|
134
|
+
function isInNodeModules(id) {
|
|
135
|
+
return id.includes("node_modules");
|
|
136
|
+
}
|
|
137
|
+
function isOptimizable(id, optimizeDeps) {
|
|
138
|
+
const { extensions } = optimizeDeps;
|
|
139
|
+
return OPTIMIZABLE_ENTRY_RE.test(id) || (extensions?.some((ext) => id.endsWith(ext)) ?? false);
|
|
140
|
+
}
|
|
141
|
+
var bareImportRE = /^(?![a-z]:)[\w@](?!.*:\/\/)/i;
|
|
142
|
+
var deepImportRE = /^([^@][^/]*)\/|^(@[^/]+\/[^/]+)\//;
|
|
143
|
+
var _require = createRequire(import.meta.url);
|
|
144
|
+
var _dirname = path2.dirname(fileURLToPath2(import.meta.url));
|
|
145
|
+
var urlCanParse = URL2.canParse ?? ((path7, base) => {
|
|
146
|
+
try {
|
|
147
|
+
new URL2(path7, base);
|
|
148
|
+
return true;
|
|
149
|
+
} catch {
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
function normalizePath(id) {
|
|
154
|
+
return path2.posix.normalize(isWindows ? slash(id) : id);
|
|
155
|
+
}
|
|
156
|
+
var internalPrefixes = [
|
|
157
|
+
FS_PREFIX,
|
|
158
|
+
VALID_ID_PREFIX,
|
|
159
|
+
CLIENT_PUBLIC_PATH,
|
|
160
|
+
ENV_PUBLIC_PATH
|
|
161
|
+
];
|
|
162
|
+
var InternalPrefixRE = new RegExp(`^(?:${internalPrefixes.join("|")})`);
|
|
163
|
+
function injectQuery(url, queryToInject) {
|
|
164
|
+
const { file, postfix } = splitFileAndPostfix(url);
|
|
165
|
+
const normalizedFile = isWindows ? slash(file) : file;
|
|
166
|
+
return `${normalizedFile}?${queryToInject}${postfix[0] === "?" ? `&${postfix.slice(1)}` : (
|
|
167
|
+
/* hash only */
|
|
168
|
+
postfix
|
|
169
|
+
)}`;
|
|
170
|
+
}
|
|
171
|
+
function isObject(value) {
|
|
172
|
+
return Object.prototype.toString.call(value) === "[object Object]";
|
|
173
|
+
}
|
|
174
|
+
function tryStatSync(file) {
|
|
175
|
+
try {
|
|
176
|
+
return fs.statSync(file, { throwIfNoEntry: false });
|
|
177
|
+
} catch {
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
function isFilePathESM(filePath, packageCache) {
|
|
181
|
+
if (/\.m[jt]s$/.test(filePath)) {
|
|
182
|
+
return true;
|
|
183
|
+
} else if (/\.c[jt]s$/.test(filePath)) {
|
|
184
|
+
return false;
|
|
185
|
+
} else {
|
|
186
|
+
try {
|
|
187
|
+
const pkg = findNearestPackageData(path2.dirname(filePath), packageCache);
|
|
188
|
+
return pkg?.data.type === "module";
|
|
189
|
+
} catch {
|
|
190
|
+
return false;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
var safeRealpathSync = isWindows ? windowsSafeRealPathSync : fs.realpathSync.native;
|
|
195
|
+
var windowsNetworkMap = /* @__PURE__ */ new Map();
|
|
196
|
+
function windowsMappedRealpathSync(path7) {
|
|
197
|
+
const realPath = fs.realpathSync.native(path7);
|
|
198
|
+
if (realPath.startsWith("\\\\")) {
|
|
199
|
+
for (const [network, volume] of windowsNetworkMap) {
|
|
200
|
+
if (realPath.startsWith(network)) {
|
|
201
|
+
return realPath.replace(network, volume);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return realPath;
|
|
206
|
+
}
|
|
207
|
+
var parseNetUseRE = /^\w* +(\w:) +([^ ]+)\s/;
|
|
208
|
+
var firstSafeRealPathSyncRun = false;
|
|
209
|
+
function windowsSafeRealPathSync(path7) {
|
|
210
|
+
if (!firstSafeRealPathSyncRun) {
|
|
211
|
+
optimizeSafeRealPathSync();
|
|
212
|
+
firstSafeRealPathSyncRun = true;
|
|
213
|
+
}
|
|
214
|
+
return fs.realpathSync(path7);
|
|
215
|
+
}
|
|
216
|
+
function optimizeSafeRealPathSync() {
|
|
217
|
+
const nodeVersion = process2.versions.node.split(".").map(Number);
|
|
218
|
+
if (nodeVersion[0] < 18 || nodeVersion[0] === 18 && nodeVersion[1] < 10) {
|
|
219
|
+
safeRealpathSync = fs.realpathSync;
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
try {
|
|
223
|
+
fs.realpathSync.native(path2.resolve("./"));
|
|
224
|
+
} catch (error) {
|
|
225
|
+
if (error.message.includes("EISDIR: illegal operation on a directory")) {
|
|
226
|
+
safeRealpathSync = fs.realpathSync;
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
exec("net use", (error, stdout) => {
|
|
231
|
+
if (error) {
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
const lines = stdout.split("\n");
|
|
235
|
+
for (const line of lines) {
|
|
236
|
+
const m = parseNetUseRE.exec(line);
|
|
237
|
+
if (m) {
|
|
238
|
+
windowsNetworkMap.set(m[2], m[1]);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
if (windowsNetworkMap.size === 0) {
|
|
242
|
+
safeRealpathSync = fs.realpathSync.native;
|
|
243
|
+
} else {
|
|
244
|
+
safeRealpathSync = windowsMappedRealpathSync;
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
var hash = crypto.hash ?? ((algorithm, data, outputEncoding) => crypto.createHash(algorithm).update(data).digest(outputEncoding));
|
|
249
|
+
function stripBomTag(content) {
|
|
250
|
+
if (content.charCodeAt(0) === 65279) {
|
|
251
|
+
return content.slice(1);
|
|
252
|
+
}
|
|
253
|
+
return content;
|
|
254
|
+
}
|
|
255
|
+
function getNpmPackageName(importPath) {
|
|
256
|
+
const parts = importPath.split("/");
|
|
257
|
+
if (parts[0][0] === "@") {
|
|
258
|
+
if (!parts[1]) {
|
|
259
|
+
return null;
|
|
260
|
+
}
|
|
261
|
+
return `${parts[0]}/${parts[1]}`;
|
|
262
|
+
} else {
|
|
263
|
+
return parts[0];
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
var dynamicImport = async (id, { format }) => {
|
|
267
|
+
const fn = format === "esm" ? (file) => import(file) : true ? createRequire(import.meta.url) : __require;
|
|
268
|
+
return fn(id);
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
// src/packages.ts
|
|
272
|
+
var pnp;
|
|
273
|
+
if (process3.versions.pnp) {
|
|
274
|
+
try {
|
|
275
|
+
pnp = createRequire2(import.meta.url)("pnpapi");
|
|
276
|
+
} catch {
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
function resolvePackageData(pkgName, basedir, preserveSymlinks = false, packageCache) {
|
|
280
|
+
if (pnp) {
|
|
281
|
+
const cacheKey = getRpdCacheKey(pkgName, basedir, preserveSymlinks);
|
|
282
|
+
if (packageCache?.has(cacheKey)) {
|
|
283
|
+
return packageCache.get(cacheKey);
|
|
284
|
+
}
|
|
285
|
+
try {
|
|
286
|
+
const pkg = pnp.resolveToUnqualified(pkgName, basedir, {
|
|
287
|
+
considerBuiltins: false
|
|
288
|
+
});
|
|
289
|
+
if (!pkg) {
|
|
290
|
+
return null;
|
|
291
|
+
}
|
|
292
|
+
const pkgData = loadPackageData(path3.join(pkg, "package.json"));
|
|
293
|
+
packageCache?.set(cacheKey, pkgData);
|
|
294
|
+
return pkgData;
|
|
295
|
+
} catch {
|
|
296
|
+
return null;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
const originalBasedir = basedir;
|
|
300
|
+
while (basedir) {
|
|
301
|
+
if (packageCache) {
|
|
302
|
+
const cached = getRpdCache(
|
|
303
|
+
packageCache,
|
|
304
|
+
pkgName,
|
|
305
|
+
basedir,
|
|
306
|
+
originalBasedir,
|
|
307
|
+
preserveSymlinks
|
|
308
|
+
);
|
|
309
|
+
if (cached) {
|
|
310
|
+
return cached;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
const pkg = path3.join(basedir, "node_modules", pkgName, "package.json");
|
|
314
|
+
try {
|
|
315
|
+
if (fs2.existsSync(pkg)) {
|
|
316
|
+
const pkgPath = preserveSymlinks ? pkg : safeRealpathSync(pkg);
|
|
317
|
+
const pkgData = loadPackageData(pkgPath);
|
|
318
|
+
if (packageCache) {
|
|
319
|
+
setRpdCache(
|
|
320
|
+
packageCache,
|
|
321
|
+
pkgData,
|
|
322
|
+
pkgName,
|
|
323
|
+
basedir,
|
|
324
|
+
originalBasedir,
|
|
325
|
+
preserveSymlinks
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
return pkgData;
|
|
329
|
+
}
|
|
330
|
+
} catch {
|
|
331
|
+
}
|
|
332
|
+
const nextBasedir = path3.dirname(basedir);
|
|
333
|
+
if (nextBasedir === basedir) {
|
|
334
|
+
break;
|
|
335
|
+
}
|
|
336
|
+
basedir = nextBasedir;
|
|
337
|
+
}
|
|
338
|
+
return null;
|
|
339
|
+
}
|
|
340
|
+
function findNearestPackageData(basedir, packageCache) {
|
|
341
|
+
const originalBasedir = basedir;
|
|
342
|
+
while (basedir) {
|
|
343
|
+
if (packageCache) {
|
|
344
|
+
const cached = getFnpdCache(packageCache, basedir, originalBasedir);
|
|
345
|
+
if (cached) {
|
|
346
|
+
return cached;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
const pkgPath = path3.join(basedir, "package.json");
|
|
350
|
+
if (tryStatSync(pkgPath)?.isFile()) {
|
|
351
|
+
try {
|
|
352
|
+
const pkgData = loadPackageData(pkgPath);
|
|
353
|
+
if (packageCache) {
|
|
354
|
+
setFnpdCache(packageCache, pkgData, basedir, originalBasedir);
|
|
355
|
+
}
|
|
356
|
+
return pkgData;
|
|
357
|
+
} catch {
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
const nextBasedir = path3.dirname(basedir);
|
|
361
|
+
if (nextBasedir === basedir) {
|
|
362
|
+
break;
|
|
363
|
+
}
|
|
364
|
+
basedir = nextBasedir;
|
|
365
|
+
}
|
|
366
|
+
return null;
|
|
367
|
+
}
|
|
368
|
+
function findNearestMainPackageData(basedir, packageCache) {
|
|
369
|
+
const nearestPackage = findNearestPackageData(basedir, packageCache);
|
|
370
|
+
return nearestPackage && (nearestPackage.data.name ? nearestPackage : findNearestMainPackageData(
|
|
371
|
+
path3.dirname(nearestPackage.dir),
|
|
372
|
+
packageCache
|
|
373
|
+
));
|
|
374
|
+
}
|
|
375
|
+
function loadPackageData(pkgPath) {
|
|
376
|
+
const data = JSON.parse(stripBomTag(fs2.readFileSync(pkgPath, "utf-8")));
|
|
377
|
+
const pkgDir = normalizePath(path3.dirname(pkgPath));
|
|
378
|
+
const { sideEffects } = data;
|
|
379
|
+
let hasSideEffects;
|
|
380
|
+
if (typeof sideEffects === "boolean") {
|
|
381
|
+
hasSideEffects = () => sideEffects;
|
|
382
|
+
} else if (Array.isArray(sideEffects)) {
|
|
383
|
+
if (sideEffects.length <= 0) {
|
|
384
|
+
hasSideEffects = () => false;
|
|
385
|
+
} else {
|
|
386
|
+
const finalPackageSideEffects = sideEffects.map((sideEffect) => {
|
|
387
|
+
if (sideEffect.includes("/")) {
|
|
388
|
+
return sideEffect;
|
|
389
|
+
}
|
|
390
|
+
return `**/${sideEffect}`;
|
|
391
|
+
});
|
|
392
|
+
hasSideEffects = createFilter(finalPackageSideEffects, null, {
|
|
393
|
+
resolve: pkgDir
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
} else {
|
|
397
|
+
hasSideEffects = () => null;
|
|
398
|
+
}
|
|
399
|
+
const resolvedCache = {};
|
|
400
|
+
const pkg = {
|
|
401
|
+
dir: pkgDir,
|
|
402
|
+
data,
|
|
403
|
+
hasSideEffects,
|
|
404
|
+
setResolvedCache(key, entry, options) {
|
|
405
|
+
resolvedCache[getResolveCacheKey(key, options)] = entry;
|
|
406
|
+
},
|
|
407
|
+
getResolvedCache(key, options) {
|
|
408
|
+
return resolvedCache[getResolveCacheKey(key, options)];
|
|
409
|
+
}
|
|
410
|
+
};
|
|
411
|
+
return pkg;
|
|
412
|
+
}
|
|
413
|
+
function getResolveCacheKey(key, options) {
|
|
414
|
+
return [
|
|
415
|
+
key,
|
|
416
|
+
options.isRequire ? "1" : "0",
|
|
417
|
+
options.conditions.join("_"),
|
|
418
|
+
options.extensions.join("_"),
|
|
419
|
+
options.mainFields.join("_")
|
|
420
|
+
].join("|");
|
|
421
|
+
}
|
|
422
|
+
function findNearestNodeModules(basedir) {
|
|
423
|
+
while (basedir) {
|
|
424
|
+
const pkgPath = path3.join(basedir, "node_modules");
|
|
425
|
+
if (tryStatSync(pkgPath)?.isDirectory()) {
|
|
426
|
+
return pkgPath;
|
|
427
|
+
}
|
|
428
|
+
const nextBasedir = path3.dirname(basedir);
|
|
429
|
+
if (nextBasedir === basedir) {
|
|
430
|
+
break;
|
|
431
|
+
}
|
|
432
|
+
basedir = nextBasedir;
|
|
433
|
+
}
|
|
434
|
+
return null;
|
|
435
|
+
}
|
|
436
|
+
function getRpdCache(packageCache, pkgName, basedir, originalBasedir, preserveSymlinks) {
|
|
437
|
+
const cacheKey = getRpdCacheKey(pkgName, basedir, preserveSymlinks);
|
|
438
|
+
const pkgData = packageCache.get(cacheKey);
|
|
439
|
+
if (pkgData) {
|
|
440
|
+
traverseBetweenDirs(originalBasedir, basedir, (dir) => {
|
|
441
|
+
packageCache.set(getRpdCacheKey(pkgName, dir, preserveSymlinks), pkgData);
|
|
442
|
+
});
|
|
443
|
+
return pkgData;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
function setRpdCache(packageCache, pkgData, pkgName, basedir, originalBasedir, preserveSymlinks) {
|
|
447
|
+
packageCache.set(getRpdCacheKey(pkgName, basedir, preserveSymlinks), pkgData);
|
|
448
|
+
traverseBetweenDirs(originalBasedir, basedir, (dir) => {
|
|
449
|
+
packageCache.set(getRpdCacheKey(pkgName, dir, preserveSymlinks), pkgData);
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
function getRpdCacheKey(pkgName, basedir, preserveSymlinks) {
|
|
453
|
+
return `rpd_${pkgName}_${basedir}_${preserveSymlinks}`;
|
|
454
|
+
}
|
|
455
|
+
function getFnpdCache(packageCache, basedir, originalBasedir) {
|
|
456
|
+
const cacheKey = getFnpdCacheKey(basedir);
|
|
457
|
+
const pkgData = packageCache.get(cacheKey);
|
|
458
|
+
if (pkgData) {
|
|
459
|
+
traverseBetweenDirs(originalBasedir, basedir, (dir) => {
|
|
460
|
+
packageCache.set(getFnpdCacheKey(dir), pkgData);
|
|
461
|
+
});
|
|
462
|
+
return pkgData;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
function setFnpdCache(packageCache, pkgData, basedir, originalBasedir) {
|
|
466
|
+
packageCache.set(getFnpdCacheKey(basedir), pkgData);
|
|
467
|
+
traverseBetweenDirs(originalBasedir, basedir, (dir) => {
|
|
468
|
+
packageCache.set(getFnpdCacheKey(dir), pkgData);
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
function getFnpdCacheKey(basedir) {
|
|
472
|
+
return `fnpd_${basedir}`;
|
|
473
|
+
}
|
|
474
|
+
function traverseBetweenDirs(longerDir, shorterDir, cb) {
|
|
475
|
+
while (longerDir !== shorterDir) {
|
|
476
|
+
cb(longerDir);
|
|
477
|
+
longerDir = path3.dirname(longerDir);
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
// src/plugins/resolve.ts
|
|
482
|
+
import fs3 from "fs";
|
|
483
|
+
import path5 from "path";
|
|
484
|
+
import { hasESMSyntax } from "mlly";
|
|
485
|
+
import { exports, imports } from "resolve.exports";
|
|
486
|
+
|
|
487
|
+
// src/external.ts
|
|
488
|
+
import path4 from "path";
|
|
489
|
+
function canExternalizeFile(filePath) {
|
|
490
|
+
const ext = path4.extname(filePath);
|
|
491
|
+
return !ext || ext === ".js" || ext === ".mjs" || ext === ".cjs";
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
// src/plugins/resolve.ts
|
|
495
|
+
var ERR_RESOLVE_PACKAGE_ENTRY_FAIL = "ERR_RESOLVE_PACKAGE_ENTRY_FAIL";
|
|
496
|
+
var browserExternalId = "__vite-browser-external";
|
|
497
|
+
var optionalPeerDepId = "__vite-optional-peer-dep";
|
|
498
|
+
function tryFsResolve(fsPath, options, tryIndex = true, skipPackageJson = false) {
|
|
499
|
+
const hashIndex = fsPath.indexOf("#");
|
|
500
|
+
if (hashIndex >= 0 && isInNodeModules(fsPath)) {
|
|
501
|
+
const queryIndex = fsPath.indexOf("?");
|
|
502
|
+
if (queryIndex < 0 || queryIndex > hashIndex) {
|
|
503
|
+
const file2 = queryIndex > hashIndex ? fsPath.slice(0, queryIndex) : fsPath;
|
|
504
|
+
const res2 = tryCleanFsResolve(file2, options, tryIndex, skipPackageJson);
|
|
505
|
+
if (res2) {
|
|
506
|
+
return res2 + fsPath.slice(file2.length);
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
const { file, postfix } = splitFileAndPostfix(fsPath);
|
|
511
|
+
const res = tryCleanFsResolve(file, options, tryIndex, skipPackageJson);
|
|
512
|
+
if (res) {
|
|
513
|
+
return res + postfix;
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
var knownTsOutputRE = /\.(?:js|mjs|cjs|jsx)$/;
|
|
517
|
+
var isPossibleTsOutput = (url) => knownTsOutputRE.test(url);
|
|
518
|
+
function tryCleanFsResolve(file, options, tryIndex = true, skipPackageJson = false) {
|
|
519
|
+
const { tryPrefix, extensions, preserveSymlinks } = options;
|
|
520
|
+
const fileResult = tryResolveRealFileOrType(file, options.preserveSymlinks);
|
|
521
|
+
if (fileResult?.path) {
|
|
522
|
+
return fileResult.path;
|
|
523
|
+
}
|
|
524
|
+
let res;
|
|
525
|
+
const possibleJsToTs = isPossibleTsOutput(file);
|
|
526
|
+
if (possibleJsToTs || options.extensions.length || tryPrefix) {
|
|
527
|
+
const dirPath = path5.dirname(file);
|
|
528
|
+
if (isDirectory(dirPath)) {
|
|
529
|
+
if (possibleJsToTs) {
|
|
530
|
+
const fileExt = path5.extname(file);
|
|
531
|
+
const fileName = file.slice(0, -fileExt.length);
|
|
532
|
+
if (res = tryResolveRealFile(
|
|
533
|
+
fileName + fileExt.replace("js", "ts"),
|
|
534
|
+
preserveSymlinks
|
|
535
|
+
)) {
|
|
536
|
+
return res;
|
|
537
|
+
}
|
|
538
|
+
if (fileExt === ".js" && (res = tryResolveRealFile(`${fileName}.tsx`, preserveSymlinks))) {
|
|
539
|
+
return res;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
if (res = tryResolveRealFileWithExtensions(
|
|
543
|
+
file,
|
|
544
|
+
extensions,
|
|
545
|
+
preserveSymlinks
|
|
546
|
+
)) {
|
|
547
|
+
return res;
|
|
548
|
+
}
|
|
549
|
+
if (tryPrefix) {
|
|
550
|
+
const prefixed = `${dirPath}/${options.tryPrefix}${path5.basename(file)}`;
|
|
551
|
+
if (res = tryResolveRealFile(prefixed, preserveSymlinks)) {
|
|
552
|
+
return res;
|
|
553
|
+
}
|
|
554
|
+
if (res = tryResolveRealFileWithExtensions(
|
|
555
|
+
prefixed,
|
|
556
|
+
extensions,
|
|
557
|
+
preserveSymlinks
|
|
558
|
+
)) {
|
|
559
|
+
return res;
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
if (tryIndex && fileResult?.type === "directory") {
|
|
565
|
+
const dirPath = file;
|
|
566
|
+
if (!skipPackageJson) {
|
|
567
|
+
let pkgPath = `${dirPath}/package.json`;
|
|
568
|
+
try {
|
|
569
|
+
if (fs3.existsSync(pkgPath)) {
|
|
570
|
+
if (!options.preserveSymlinks) {
|
|
571
|
+
pkgPath = safeRealpathSync(pkgPath);
|
|
572
|
+
}
|
|
573
|
+
const pkg = loadPackageData(pkgPath);
|
|
574
|
+
return resolvePackageEntry(dirPath, pkg, options);
|
|
575
|
+
}
|
|
576
|
+
} catch (e) {
|
|
577
|
+
if (e.code !== ERR_RESOLVE_PACKAGE_ENTRY_FAIL && e.code !== "ENOENT") {
|
|
578
|
+
throw e;
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
if (res = tryResolveRealFileWithExtensions(
|
|
583
|
+
`${dirPath}/index`,
|
|
584
|
+
extensions,
|
|
585
|
+
preserveSymlinks
|
|
586
|
+
)) {
|
|
587
|
+
return res;
|
|
588
|
+
}
|
|
589
|
+
if (tryPrefix) {
|
|
590
|
+
if (res = tryResolveRealFileWithExtensions(
|
|
591
|
+
`${dirPath}/${options.tryPrefix}index`,
|
|
592
|
+
extensions,
|
|
593
|
+
preserveSymlinks
|
|
594
|
+
)) {
|
|
595
|
+
return res;
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
function tryNodeResolve(id, importer, options, depsOptimizer, externalize) {
|
|
601
|
+
const { root, dedupe, isBuild, preserveSymlinks, packageCache } = options;
|
|
602
|
+
const deepMatch = deepImportRE.exec(id);
|
|
603
|
+
const pkgId = deepMatch ? deepMatch[1] || deepMatch[2] : cleanUrl(id);
|
|
604
|
+
let basedir;
|
|
605
|
+
if (dedupe.includes(pkgId)) {
|
|
606
|
+
basedir = root;
|
|
607
|
+
} else if (importer && path5.isAbsolute(importer) && (importer.endsWith("*") || fs3.existsSync(cleanUrl(importer)))) {
|
|
608
|
+
basedir = path5.dirname(importer);
|
|
609
|
+
} else {
|
|
610
|
+
basedir = root;
|
|
611
|
+
}
|
|
612
|
+
const isModuleBuiltin = (id2) => isBuiltin(options.builtins, id2);
|
|
613
|
+
let selfPkg = null;
|
|
614
|
+
if (!isModuleBuiltin(id) && !id.includes("\0") && bareImportRE.test(id)) {
|
|
615
|
+
const selfPackageData = findNearestPackageData(basedir, packageCache);
|
|
616
|
+
selfPkg = selfPackageData?.data.exports && selfPackageData.data.name === pkgId ? selfPackageData : null;
|
|
617
|
+
}
|
|
618
|
+
const pkg = selfPkg || resolvePackageData(pkgId, basedir, preserveSymlinks, packageCache);
|
|
619
|
+
if (!pkg) {
|
|
620
|
+
if (basedir !== root && !isModuleBuiltin(id) && !id.includes("\0") && bareImportRE.test(id)) {
|
|
621
|
+
const mainPkg = findNearestMainPackageData(basedir, packageCache)?.data;
|
|
622
|
+
if (mainPkg) {
|
|
623
|
+
const pkgName = getNpmPackageName(id);
|
|
624
|
+
if (pkgName != null && mainPkg.peerDependencies?.[pkgName] && mainPkg.peerDependenciesMeta?.[pkgName]?.optional) {
|
|
625
|
+
return {
|
|
626
|
+
id: `${optionalPeerDepId}:${id}:${mainPkg.name}`
|
|
627
|
+
};
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
return;
|
|
632
|
+
}
|
|
633
|
+
const resolveId = deepMatch ? resolveDeepImport : resolvePackageEntry;
|
|
634
|
+
const unresolvedId = deepMatch ? `.${id.slice(pkgId.length)}` : id;
|
|
635
|
+
let resolved = resolveId(unresolvedId, pkg, options);
|
|
636
|
+
if (!resolved) {
|
|
637
|
+
return;
|
|
638
|
+
}
|
|
639
|
+
const processResult = (resolved2) => {
|
|
640
|
+
if (!externalize) {
|
|
641
|
+
return resolved2;
|
|
642
|
+
}
|
|
643
|
+
if (!canExternalizeFile(resolved2.id)) {
|
|
644
|
+
return resolved2;
|
|
645
|
+
}
|
|
646
|
+
let resolvedId = id;
|
|
647
|
+
if (deepMatch && !pkg.data.exports && path5.extname(id) !== path5.extname(resolved2.id)) {
|
|
648
|
+
const index = resolved2.id.indexOf(id);
|
|
649
|
+
if (index > -1) {
|
|
650
|
+
resolvedId = resolved2.id.slice(index);
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
return { ...resolved2, id: resolvedId, external: true };
|
|
654
|
+
};
|
|
655
|
+
if (!options.idOnly && (!options.scan && isBuild || externalize)) {
|
|
656
|
+
return processResult({
|
|
657
|
+
id: resolved,
|
|
658
|
+
moduleSideEffects: pkg.hasSideEffects(resolved)
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
if (!isInNodeModules(resolved) || !depsOptimizer || options.scan) {
|
|
662
|
+
return { id: resolved };
|
|
663
|
+
}
|
|
664
|
+
const isJsType = isOptimizable(resolved, depsOptimizer.options);
|
|
665
|
+
const exclude = depsOptimizer.options.exclude;
|
|
666
|
+
const skipOptimization = depsOptimizer.options.noDiscovery || !isJsType || importer && isInNodeModules(importer) || exclude?.includes(pkgId) || exclude?.includes(id) || SPECIAL_QUERY_RE.test(resolved);
|
|
667
|
+
if (skipOptimization) {
|
|
668
|
+
const versionHash = depsOptimizer.metadata.browserHash;
|
|
669
|
+
if (versionHash && isJsType) {
|
|
670
|
+
resolved = injectQuery(resolved, `v=${versionHash}`);
|
|
671
|
+
}
|
|
672
|
+
} else {
|
|
673
|
+
const optimizedInfo = depsOptimizer.registerMissingImport(id, resolved);
|
|
674
|
+
resolved = depsOptimizer.getOptimizedDepId(optimizedInfo);
|
|
675
|
+
}
|
|
676
|
+
return { id: resolved };
|
|
677
|
+
}
|
|
678
|
+
function resolvePackageEntry(id, { dir, data, setResolvedCache, getResolvedCache }, options) {
|
|
679
|
+
const { postfix } = splitFileAndPostfix(id);
|
|
680
|
+
const cached = getResolvedCache(".", options);
|
|
681
|
+
if (cached) {
|
|
682
|
+
return cached + postfix;
|
|
683
|
+
}
|
|
684
|
+
try {
|
|
685
|
+
let entryPoint;
|
|
686
|
+
if (data.exports) {
|
|
687
|
+
entryPoint = resolveExportsOrImports(data, ".", options, "exports");
|
|
688
|
+
}
|
|
689
|
+
if (!entryPoint) {
|
|
690
|
+
for (const field of options.mainFields) {
|
|
691
|
+
if (field === "browser") {
|
|
692
|
+
entryPoint = tryResolveBrowserEntry(dir, data, options);
|
|
693
|
+
if (entryPoint) {
|
|
694
|
+
break;
|
|
695
|
+
}
|
|
696
|
+
} else if (typeof data[field] === "string") {
|
|
697
|
+
entryPoint = data[field];
|
|
698
|
+
break;
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
entryPoint ||= data.main;
|
|
703
|
+
const entryPoints = entryPoint ? [entryPoint] : ["index.js", "index.json", "index.node"];
|
|
704
|
+
for (let entry of entryPoints) {
|
|
705
|
+
let skipPackageJson = false;
|
|
706
|
+
if (options.mainFields[0] === "sass" && !options.extensions.includes(path5.extname(entry))) {
|
|
707
|
+
entry = "";
|
|
708
|
+
skipPackageJson = true;
|
|
709
|
+
} else {
|
|
710
|
+
const { browser: browserField } = data;
|
|
711
|
+
if (options.mainFields.includes("browser") && isObject(browserField)) {
|
|
712
|
+
entry = mapWithBrowserField(entry, browserField) || entry;
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
const entryPointPath = path5.join(dir, entry);
|
|
716
|
+
const resolvedEntryPoint = tryFsResolve(
|
|
717
|
+
entryPointPath,
|
|
718
|
+
options,
|
|
719
|
+
true,
|
|
720
|
+
skipPackageJson
|
|
721
|
+
);
|
|
722
|
+
if (resolvedEntryPoint) {
|
|
723
|
+
setResolvedCache(".", resolvedEntryPoint, options);
|
|
724
|
+
return resolvedEntryPoint + postfix;
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
} catch (e) {
|
|
728
|
+
packageEntryFailure(
|
|
729
|
+
id,
|
|
730
|
+
// @ts-ignore
|
|
731
|
+
e.message
|
|
732
|
+
);
|
|
733
|
+
}
|
|
734
|
+
packageEntryFailure(id);
|
|
735
|
+
}
|
|
736
|
+
function packageEntryFailure(id, details) {
|
|
737
|
+
const err = new Error(
|
|
738
|
+
`Failed to resolve entry for package "${id}". The package may have incorrect main/module/exports specified in its package.json${details ? `: ${details}` : "."}`
|
|
739
|
+
);
|
|
740
|
+
err.code = ERR_RESOLVE_PACKAGE_ENTRY_FAIL;
|
|
741
|
+
throw err;
|
|
742
|
+
}
|
|
743
|
+
function getConditions(conditions, isProduction, isRequire) {
|
|
744
|
+
const resolvedConditions = conditions.map((condition) => {
|
|
745
|
+
if (condition === DEV_PROD_CONDITION) {
|
|
746
|
+
return isProduction ? "production" : "development";
|
|
747
|
+
}
|
|
748
|
+
return condition;
|
|
749
|
+
});
|
|
750
|
+
if (isRequire) {
|
|
751
|
+
resolvedConditions.push("require");
|
|
752
|
+
} else {
|
|
753
|
+
resolvedConditions.push("import");
|
|
754
|
+
}
|
|
755
|
+
return resolvedConditions;
|
|
756
|
+
}
|
|
757
|
+
function resolveExportsOrImports(pkg, key, options, type) {
|
|
758
|
+
const conditions = getConditions(
|
|
759
|
+
options.conditions,
|
|
760
|
+
options.isProduction,
|
|
761
|
+
options.isRequire
|
|
762
|
+
);
|
|
763
|
+
const fn = type === "imports" ? imports : exports;
|
|
764
|
+
const result = fn(pkg, key, { conditions, unsafe: true });
|
|
765
|
+
return result ? result[0] : void 0;
|
|
766
|
+
}
|
|
767
|
+
function resolveDeepImport(id, { setResolvedCache, getResolvedCache, dir, data }, options) {
|
|
768
|
+
const cache = getResolvedCache(id, options);
|
|
769
|
+
if (cache) {
|
|
770
|
+
return cache;
|
|
771
|
+
}
|
|
772
|
+
let relativeId = id;
|
|
773
|
+
const { exports: exportsField, browser: browserField } = data;
|
|
774
|
+
if (exportsField) {
|
|
775
|
+
if (isObject(exportsField) && !Array.isArray(exportsField)) {
|
|
776
|
+
const { file, postfix } = splitFileAndPostfix(relativeId);
|
|
777
|
+
const exportsId = resolveExportsOrImports(data, file, options, "exports");
|
|
778
|
+
if (exportsId !== void 0) {
|
|
779
|
+
relativeId = exportsId + postfix;
|
|
780
|
+
} else {
|
|
781
|
+
relativeId = void 0;
|
|
782
|
+
}
|
|
783
|
+
} else {
|
|
784
|
+
relativeId = void 0;
|
|
785
|
+
}
|
|
786
|
+
if (!relativeId) {
|
|
787
|
+
throw new Error(
|
|
788
|
+
`Package subpath '${relativeId}' is not defined by "exports" in ${path5.join(dir, "package.json")}.`
|
|
789
|
+
);
|
|
790
|
+
}
|
|
791
|
+
} else if (options.mainFields.includes("browser") && isObject(browserField)) {
|
|
792
|
+
const { file, postfix } = splitFileAndPostfix(relativeId);
|
|
793
|
+
const mapped = mapWithBrowserField(file, browserField);
|
|
794
|
+
if (mapped) {
|
|
795
|
+
relativeId = mapped + postfix;
|
|
796
|
+
} else if (mapped === false) {
|
|
797
|
+
setResolvedCache(id, browserExternalId, options);
|
|
798
|
+
return browserExternalId;
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
if (relativeId) {
|
|
802
|
+
const resolved = tryFsResolve(
|
|
803
|
+
path5.join(dir, relativeId),
|
|
804
|
+
options,
|
|
805
|
+
!exportsField
|
|
806
|
+
// try index only if no exports field
|
|
807
|
+
);
|
|
808
|
+
if (resolved) {
|
|
809
|
+
setResolvedCache(id, resolved, options);
|
|
810
|
+
return resolved;
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
function tryResolveBrowserEntry(dir, data, options) {
|
|
815
|
+
const browserEntry = typeof data.browser === "string" ? data.browser : isObject(data.browser) && data.browser["."];
|
|
816
|
+
if (browserEntry) {
|
|
817
|
+
if (!options.isRequire && options.mainFields.includes("module") && typeof data.module === "string" && data.module !== browserEntry) {
|
|
818
|
+
const resolvedBrowserEntry = tryFsResolve(
|
|
819
|
+
path5.join(dir, browserEntry),
|
|
820
|
+
options
|
|
821
|
+
);
|
|
822
|
+
if (resolvedBrowserEntry) {
|
|
823
|
+
const content = fs3.readFileSync(resolvedBrowserEntry, "utf-8");
|
|
824
|
+
if (hasESMSyntax(content)) {
|
|
825
|
+
return browserEntry;
|
|
826
|
+
} else {
|
|
827
|
+
return data.module;
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
} else {
|
|
831
|
+
return browserEntry;
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
function mapWithBrowserField(relativePathInPkgDir, map) {
|
|
836
|
+
const normalizedPath = path5.posix.normalize(relativePathInPkgDir);
|
|
837
|
+
for (const key in map) {
|
|
838
|
+
const normalizedKey = path5.posix.normalize(key);
|
|
839
|
+
if (normalizedPath === normalizedKey || equalWithoutSuffix(normalizedPath, normalizedKey, ".js") || equalWithoutSuffix(normalizedPath, normalizedKey, "/index.js")) {
|
|
840
|
+
return map[key];
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
function equalWithoutSuffix(path7, key, suffix) {
|
|
845
|
+
return key.endsWith(suffix) && key.slice(0, -suffix.length) === path7;
|
|
846
|
+
}
|
|
847
|
+
function tryResolveRealFile(file, preserveSymlinks) {
|
|
848
|
+
const stat = tryStatSync(file);
|
|
849
|
+
if (stat?.isFile()) {
|
|
850
|
+
return getRealPath(file, preserveSymlinks);
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
function tryResolveRealFileWithExtensions(filePath, extensions, preserveSymlinks) {
|
|
854
|
+
for (const ext of extensions) {
|
|
855
|
+
const res = tryResolveRealFile(filePath + ext, preserveSymlinks);
|
|
856
|
+
if (res) {
|
|
857
|
+
return res;
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
function tryResolveRealFileOrType(file, preserveSymlinks) {
|
|
862
|
+
const fileStat = tryStatSync(file);
|
|
863
|
+
if (fileStat?.isFile()) {
|
|
864
|
+
return { path: getRealPath(file, preserveSymlinks), type: "file" };
|
|
865
|
+
}
|
|
866
|
+
if (fileStat?.isDirectory()) {
|
|
867
|
+
return { type: "directory" };
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
function getRealPath(resolved, preserveSymlinks) {
|
|
871
|
+
if (!preserveSymlinks) {
|
|
872
|
+
resolved = safeRealpathSync(resolved);
|
|
873
|
+
}
|
|
874
|
+
return normalizePath(resolved);
|
|
875
|
+
}
|
|
876
|
+
function isDirectory(path7) {
|
|
877
|
+
const stat = tryStatSync(path7);
|
|
878
|
+
return stat?.isDirectory() ?? false;
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
// src/index.ts
|
|
882
|
+
var promisifiedRealpath = promisify(fs4.realpath);
|
|
883
|
+
var configDefaults = Object.freeze({
|
|
884
|
+
resolve: {
|
|
885
|
+
extensions: [".mjs", ".js", ".mts", ".ts", ".jsx", ".tsx", ".json"]
|
|
886
|
+
}
|
|
887
|
+
});
|
|
888
|
+
var defaultGetOutputFile = (filepath, _format) => {
|
|
889
|
+
return filepath;
|
|
890
|
+
};
|
|
891
|
+
function collectAllModules(bundle, fileName, allModules, analyzedModules = /* @__PURE__ */ new Set()) {
|
|
892
|
+
if (analyzedModules.has(fileName)) {
|
|
893
|
+
return;
|
|
894
|
+
}
|
|
895
|
+
analyzedModules.add(fileName);
|
|
896
|
+
const chunk = bundle[fileName];
|
|
897
|
+
for (const mod of chunk.moduleIds) {
|
|
898
|
+
allModules.add(mod);
|
|
899
|
+
}
|
|
900
|
+
for (const i of chunk.imports) {
|
|
901
|
+
analyzedModules.add(i);
|
|
902
|
+
collectAllModules(bundle, i, allModules, analyzedModules);
|
|
903
|
+
}
|
|
904
|
+
for (const i of chunk.dynamicImports) {
|
|
905
|
+
analyzedModules.add(i);
|
|
906
|
+
collectAllModules(bundle, i, allModules, analyzedModules);
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
async function bundleFile(fileName, options) {
|
|
910
|
+
const { isESM } = options;
|
|
911
|
+
const isModuleSyncConditionEnabled = (await import(
|
|
912
|
+
// @ts-ignore
|
|
913
|
+
"./false-G5OFYI37.mjs"
|
|
914
|
+
)).default;
|
|
915
|
+
const dirnameVarName = "__vite_injected_original_dirname";
|
|
916
|
+
const filenameVarName = "__vite_injected_original_filename";
|
|
917
|
+
const importMetaUrlVarName = "__vite_injected_original_import_meta_url";
|
|
918
|
+
const rolldownOptions = options?.rolldownOptions || {};
|
|
919
|
+
const bundle = await rolldown({
|
|
920
|
+
...rolldownOptions,
|
|
921
|
+
input: fileName,
|
|
922
|
+
// target: [`node${process.versions.node}`],
|
|
923
|
+
platform: "node",
|
|
924
|
+
resolve: {
|
|
925
|
+
mainFields: ["main"],
|
|
926
|
+
tsconfigFilename: options.tsconfig
|
|
927
|
+
},
|
|
928
|
+
define: {
|
|
929
|
+
"__dirname": dirnameVarName,
|
|
930
|
+
"__filename": filenameVarName,
|
|
931
|
+
"import.meta.url": importMetaUrlVarName,
|
|
932
|
+
"import.meta.dirname": dirnameVarName,
|
|
933
|
+
"import.meta.filename": filenameVarName
|
|
934
|
+
},
|
|
935
|
+
// disable treeshake to include files that is not sideeffectful to `moduleIds`
|
|
936
|
+
treeshake: false,
|
|
937
|
+
plugins: [
|
|
938
|
+
/* @__PURE__ */ (() => {
|
|
939
|
+
const packageCache = /* @__PURE__ */ new Map();
|
|
940
|
+
const resolveByViteResolver = (id, importer, isRequire) => {
|
|
941
|
+
return tryNodeResolve(id, importer, {
|
|
942
|
+
root: path6.dirname(fileName),
|
|
943
|
+
isBuild: true,
|
|
944
|
+
isProduction: true,
|
|
945
|
+
preferRelative: false,
|
|
946
|
+
tryIndex: true,
|
|
947
|
+
mainFields: [],
|
|
948
|
+
conditions: [
|
|
949
|
+
"node",
|
|
950
|
+
...isModuleSyncConditionEnabled ? ["module-sync"] : []
|
|
951
|
+
],
|
|
952
|
+
externalConditions: [],
|
|
953
|
+
external: [],
|
|
954
|
+
noExternal: [],
|
|
955
|
+
dedupe: [],
|
|
956
|
+
extensions: configDefaults.resolve.extensions,
|
|
957
|
+
preserveSymlinks: false,
|
|
958
|
+
packageCache,
|
|
959
|
+
isRequire,
|
|
960
|
+
builtins: nodeLikeBuiltins
|
|
961
|
+
})?.id;
|
|
962
|
+
};
|
|
963
|
+
return {
|
|
964
|
+
name: "externalize-deps",
|
|
965
|
+
resolveId: {
|
|
966
|
+
filter: { id: /^[^.#].*/ },
|
|
967
|
+
async handler(id, importer, { kind }) {
|
|
968
|
+
if (!importer || path6.isAbsolute(id) || isNodeBuiltin(id)) {
|
|
969
|
+
return;
|
|
970
|
+
}
|
|
971
|
+
if (isNodeLikeBuiltin(id)) {
|
|
972
|
+
return { id, external: true };
|
|
973
|
+
}
|
|
974
|
+
const isImport = isESM || kind === "dynamic-import";
|
|
975
|
+
let idFsPath;
|
|
976
|
+
try {
|
|
977
|
+
idFsPath = resolveByViteResolver(id, importer, !isImport);
|
|
978
|
+
} catch (e) {
|
|
979
|
+
if (!isImport) {
|
|
980
|
+
let canResolveWithImport = false;
|
|
981
|
+
try {
|
|
982
|
+
canResolveWithImport = !!resolveByViteResolver(
|
|
983
|
+
id,
|
|
984
|
+
importer,
|
|
985
|
+
false
|
|
986
|
+
);
|
|
987
|
+
} catch {
|
|
988
|
+
}
|
|
989
|
+
if (canResolveWithImport) {
|
|
990
|
+
throw new Error(
|
|
991
|
+
`Failed to resolve ${JSON.stringify(
|
|
992
|
+
id
|
|
993
|
+
)}. This package is ESM only but it was tried to load by \`require\`. See https://vite.dev/guide/troubleshooting.html#this-package-is-esm-only for more details.`
|
|
994
|
+
);
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
throw e;
|
|
998
|
+
}
|
|
999
|
+
if (!idFsPath) {
|
|
1000
|
+
return;
|
|
1001
|
+
}
|
|
1002
|
+
if (idFsPath.endsWith(".json")) {
|
|
1003
|
+
return idFsPath;
|
|
1004
|
+
}
|
|
1005
|
+
if (idFsPath && isImport) {
|
|
1006
|
+
idFsPath = pathToFileURL(idFsPath).href;
|
|
1007
|
+
}
|
|
1008
|
+
return { id: idFsPath, external: true };
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
};
|
|
1012
|
+
})(),
|
|
1013
|
+
{
|
|
1014
|
+
name: "inject-file-scope-variables",
|
|
1015
|
+
transform: {
|
|
1016
|
+
filter: { id: /\.[cm]?[jt]s$/ },
|
|
1017
|
+
async handler(code, id) {
|
|
1018
|
+
const injectValues = `const ${dirnameVarName} = ${JSON.stringify(path6.dirname(id))};const ${filenameVarName} = ${JSON.stringify(id)};const ${importMetaUrlVarName} = ${JSON.stringify(
|
|
1019
|
+
pathToFileURL(id).href
|
|
1020
|
+
)};`;
|
|
1021
|
+
return { code: injectValues + code, map: null };
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
],
|
|
1026
|
+
external: options.external
|
|
1027
|
+
// preserveEntrySignatures: 'exports-only'
|
|
1028
|
+
});
|
|
1029
|
+
const result = await bundle.generate({
|
|
1030
|
+
format: options.format,
|
|
1031
|
+
sourcemap: "inline",
|
|
1032
|
+
sourcemapPathTransform(relative) {
|
|
1033
|
+
return path6.resolve(fileName, relative);
|
|
1034
|
+
},
|
|
1035
|
+
// we want to generate a single chunk like esbuild does with `splitting: false`
|
|
1036
|
+
inlineDynamicImports: true
|
|
1037
|
+
});
|
|
1038
|
+
await bundle.close();
|
|
1039
|
+
const entryChunk = result.output.find(
|
|
1040
|
+
(chunk) => chunk.type === "chunk" && chunk.isEntry
|
|
1041
|
+
);
|
|
1042
|
+
const bundleChunks = Object.fromEntries(
|
|
1043
|
+
result.output.flatMap((c) => c.type === "chunk" ? [[c.fileName, c]] : [])
|
|
1044
|
+
);
|
|
1045
|
+
const allModules = /* @__PURE__ */ new Set();
|
|
1046
|
+
collectAllModules(bundleChunks, entryChunk.fileName, allModules);
|
|
1047
|
+
allModules.delete(fileName);
|
|
1048
|
+
return {
|
|
1049
|
+
code: entryChunk.code,
|
|
1050
|
+
dependencies: [...allModules]
|
|
1051
|
+
};
|
|
1052
|
+
}
|
|
1053
|
+
var _require2 = createRequire3(import.meta.url);
|
|
1054
|
+
async function loadFromBundledFile(fileName, bundledCode, options) {
|
|
1055
|
+
const { isESM } = options;
|
|
1056
|
+
if (isESM) {
|
|
1057
|
+
let nodeModulesDir = typeof process4.versions.deno === "string" ? void 0 : findNearestNodeModules(path6.dirname(fileName));
|
|
1058
|
+
if (nodeModulesDir) {
|
|
1059
|
+
try {
|
|
1060
|
+
await fsp2.mkdir(path6.resolve(nodeModulesDir, ".vite-temp/"), {
|
|
1061
|
+
recursive: true
|
|
1062
|
+
});
|
|
1063
|
+
} catch (e) {
|
|
1064
|
+
if (e.code === "EACCES") {
|
|
1065
|
+
nodeModulesDir = void 0;
|
|
1066
|
+
} else {
|
|
1067
|
+
throw e;
|
|
1068
|
+
}
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
const hash2 = `timestamp-${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
|
1072
|
+
const tempFileName = nodeModulesDir ? path6.resolve(
|
|
1073
|
+
nodeModulesDir,
|
|
1074
|
+
`.vite-temp/${path6.basename(fileName)}.${hash2}.${isESM ? "mjs" : "cjs"}`
|
|
1075
|
+
) : `${fileName}.${hash2}.mjs`;
|
|
1076
|
+
const getOutputFile = options.getOutputFile || defaultGetOutputFile;
|
|
1077
|
+
const outfile = getOutputFile(tempFileName, options.format);
|
|
1078
|
+
await fsp2.writeFile(outfile, bundledCode);
|
|
1079
|
+
let mod;
|
|
1080
|
+
const req = options.require || dynamicImport;
|
|
1081
|
+
try {
|
|
1082
|
+
mod = await req(
|
|
1083
|
+
options.format === "esm" ? pathToFileURL(outfile).href : outfile,
|
|
1084
|
+
{ format: options.format }
|
|
1085
|
+
);
|
|
1086
|
+
return mod;
|
|
1087
|
+
} finally {
|
|
1088
|
+
if (!options?.preserveTemporaryFile) {
|
|
1089
|
+
fs4.unlink(outfile, () => {
|
|
1090
|
+
});
|
|
1091
|
+
}
|
|
1092
|
+
}
|
|
1093
|
+
} else {
|
|
1094
|
+
const extension = path6.extname(fileName);
|
|
1095
|
+
const realFileName = await promisifiedRealpath(fileName);
|
|
1096
|
+
const loaderExt = extension in _require2.extensions ? extension : ".js";
|
|
1097
|
+
const defaultLoader = _require2.extensions[loaderExt];
|
|
1098
|
+
_require2.extensions[loaderExt] = (module, filename) => {
|
|
1099
|
+
if (filename === realFileName) {
|
|
1100
|
+
;
|
|
1101
|
+
module._compile(bundledCode, filename);
|
|
1102
|
+
} else {
|
|
1103
|
+
defaultLoader(module, filename);
|
|
1104
|
+
}
|
|
1105
|
+
};
|
|
1106
|
+
delete _require2.cache[_require2.resolve(fileName)];
|
|
1107
|
+
const raw = _require2(fileName);
|
|
1108
|
+
_require2.extensions[loaderExt] = defaultLoader;
|
|
1109
|
+
return raw.__esModule ? raw.default : raw;
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
1112
|
+
async function bundleRequire(options) {
|
|
1113
|
+
const resolvedPath = path6.isAbsolute(options.filepath) ? options.filepath : path6.resolve(options.cwd || process4.cwd(), options.filepath);
|
|
1114
|
+
const isESM = typeof process4.versions.deno === "string" || isFilePathESM(resolvedPath);
|
|
1115
|
+
if (options.tsconfig !== false) {
|
|
1116
|
+
options.tsconfig = options.tsconfig ?? getTsconfig(options.cwd, "tsconfig.json")?.path ?? void 0;
|
|
1117
|
+
} else {
|
|
1118
|
+
options.tsconfig = void 0;
|
|
1119
|
+
}
|
|
1120
|
+
if (!options.format) {
|
|
1121
|
+
options.format = isESM ? "esm" : "cjs";
|
|
1122
|
+
}
|
|
1123
|
+
const internalOptions = {
|
|
1124
|
+
...options,
|
|
1125
|
+
isESM,
|
|
1126
|
+
format: options.format,
|
|
1127
|
+
tsconfig: options.tsconfig
|
|
1128
|
+
};
|
|
1129
|
+
const bundled = await bundleFile(
|
|
1130
|
+
resolvedPath,
|
|
1131
|
+
internalOptions
|
|
1132
|
+
);
|
|
1133
|
+
const mod = await loadFromBundledFile(
|
|
1134
|
+
resolvedPath,
|
|
1135
|
+
bundled.code,
|
|
1136
|
+
internalOptions
|
|
1137
|
+
);
|
|
1138
|
+
return {
|
|
1139
|
+
mod,
|
|
1140
|
+
dependencies: bundled.dependencies
|
|
1141
|
+
};
|
|
1142
|
+
}
|
|
1143
|
+
export {
|
|
1144
|
+
bundleFile,
|
|
1145
|
+
bundleRequire,
|
|
1146
|
+
configDefaults,
|
|
1147
|
+
loadFromBundledFile
|
|
1148
|
+
};
|