rolldown-require 2.0.17 → 2.0.18
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 +4 -0
- package/dist/index.mjs +94 -15
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
package/dist/index.mjs
CHANGED
|
@@ -3,7 +3,7 @@ import path from "node:path";
|
|
|
3
3
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
4
4
|
import { rolldown } from "rolldown";
|
|
5
5
|
import fs, { readFileSync } from "node:fs";
|
|
6
|
-
import "node:child_process";
|
|
6
|
+
import { exec } from "node:child_process";
|
|
7
7
|
import process from "node:process";
|
|
8
8
|
import { createFilter } from "@rollup/pluginutils";
|
|
9
9
|
import crypto from "node:crypto";
|
|
@@ -200,7 +200,51 @@ function isFilePathESM(filePath, packageCache) {
|
|
|
200
200
|
return false;
|
|
201
201
|
}
|
|
202
202
|
}
|
|
203
|
-
isWindows
|
|
203
|
+
let currentSafeRealpathSync = isWindows ? windowsSafeRealPathSync : fs.realpathSync.native;
|
|
204
|
+
function safeRealpathSync(filePath) {
|
|
205
|
+
return currentSafeRealpathSync(filePath);
|
|
206
|
+
}
|
|
207
|
+
const windowsNetworkMap = /* @__PURE__ */ new Map();
|
|
208
|
+
function windowsMappedRealpathSync(path) {
|
|
209
|
+
const realPath = fs.realpathSync.native(path);
|
|
210
|
+
if (realPath.startsWith("\\\\")) {
|
|
211
|
+
for (const [network, volume] of windowsNetworkMap) if (realPath.startsWith(network)) return realPath.replace(network, volume);
|
|
212
|
+
}
|
|
213
|
+
return realPath;
|
|
214
|
+
}
|
|
215
|
+
const parseNetUseRE = /^\w* +(\w:) +([^ ]+)\s/;
|
|
216
|
+
let firstSafeRealPathSyncRun = false;
|
|
217
|
+
function windowsSafeRealPathSync(path) {
|
|
218
|
+
if (!firstSafeRealPathSyncRun) {
|
|
219
|
+
optimizeSafeRealPathSync();
|
|
220
|
+
firstSafeRealPathSyncRun = true;
|
|
221
|
+
}
|
|
222
|
+
return fs.realpathSync(path);
|
|
223
|
+
}
|
|
224
|
+
function optimizeSafeRealPathSync() {
|
|
225
|
+
const nodeVersion = process.versions.node.split(".").map(Number);
|
|
226
|
+
if (nodeVersion[0] < 18 || nodeVersion[0] === 18 && nodeVersion[1] < 10) {
|
|
227
|
+
currentSafeRealpathSync = fs.realpathSync;
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
try {
|
|
231
|
+
fs.realpathSync.native(path.resolve("./"));
|
|
232
|
+
} catch (error) {
|
|
233
|
+
if (error.message.includes("EISDIR: illegal operation on a directory")) {
|
|
234
|
+
currentSafeRealpathSync = fs.realpathSync;
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
exec("net use", (error, stdout) => {
|
|
239
|
+
if (error) return;
|
|
240
|
+
const lines = stdout.split("\n");
|
|
241
|
+
for (const line of lines) {
|
|
242
|
+
const m = parseNetUseRE.exec(line);
|
|
243
|
+
if (m) windowsNetworkMap.set(m[2], m[1]);
|
|
244
|
+
}
|
|
245
|
+
currentSafeRealpathSync = windowsNetworkMap.size === 0 ? fs.realpathSync.native : windowsMappedRealpathSync;
|
|
246
|
+
});
|
|
247
|
+
}
|
|
204
248
|
function stripBomTag(content) {
|
|
205
249
|
if (content.charCodeAt(0) === 65279) return content.slice(1);
|
|
206
250
|
return content;
|
|
@@ -420,14 +464,21 @@ async function bundleFile(fileName, options) {
|
|
|
420
464
|
define: transformDefine,
|
|
421
465
|
transform: transformOptions,
|
|
422
466
|
treeshake: false,
|
|
423
|
-
plugins: [
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
467
|
+
plugins: [
|
|
468
|
+
createEntrySourcePlugin({
|
|
469
|
+
fileName,
|
|
470
|
+
source: options.source
|
|
471
|
+
}),
|
|
472
|
+
createExternalizeDepsPlugin({
|
|
473
|
+
entryFile: fileName,
|
|
474
|
+
isESM
|
|
475
|
+
}),
|
|
476
|
+
createFileScopeVariablesPlugin({
|
|
477
|
+
dirnameVarName,
|
|
478
|
+
filenameVarName,
|
|
479
|
+
importMetaUrlVarName
|
|
480
|
+
})
|
|
481
|
+
],
|
|
431
482
|
external: options.external
|
|
432
483
|
});
|
|
433
484
|
} finally {
|
|
@@ -457,6 +508,28 @@ function resolveSourcemapOutput(outputSourcemap, requested) {
|
|
|
457
508
|
if (requested === true) return "inline";
|
|
458
509
|
return requested ?? false;
|
|
459
510
|
}
|
|
511
|
+
function createEntrySourcePlugin({ fileName, source }) {
|
|
512
|
+
const entryFileNames = collectEntryFileNames(fileName);
|
|
513
|
+
return {
|
|
514
|
+
name: "entry-source",
|
|
515
|
+
transform: source === void 0 ? void 0 : {
|
|
516
|
+
filter: { id: JS_TS_EXT_RE },
|
|
517
|
+
async handler(_code, id) {
|
|
518
|
+
return entryFileNames.has(path.resolve(id)) ? {
|
|
519
|
+
code: source,
|
|
520
|
+
map: null
|
|
521
|
+
} : null;
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
};
|
|
525
|
+
}
|
|
526
|
+
function collectEntryFileNames(fileName) {
|
|
527
|
+
const fileNames = new Set([path.resolve(fileName)]);
|
|
528
|
+
try {
|
|
529
|
+
fileNames.add(safeRealpathSync(fileName));
|
|
530
|
+
} catch {}
|
|
531
|
+
return fileNames;
|
|
532
|
+
}
|
|
460
533
|
function createFileScopeVariablesPlugin({ dirnameVarName, filenameVarName, importMetaUrlVarName }) {
|
|
461
534
|
return {
|
|
462
535
|
name: "inject-file-scope-variables",
|
|
@@ -515,6 +588,7 @@ function resolveCacheOptions(fileName, options) {
|
|
|
515
588
|
format: options.format,
|
|
516
589
|
isESM: options.isESM,
|
|
517
590
|
tsconfig: options.tsconfig ?? "auto",
|
|
591
|
+
source: hashEntrySource(options.source),
|
|
518
592
|
node: process.versions.node,
|
|
519
593
|
rolldown: hashRolldownOptions(options.rolldownOptions)
|
|
520
594
|
}));
|
|
@@ -662,6 +736,10 @@ function hashRolldownOptions(options) {
|
|
|
662
736
|
return value;
|
|
663
737
|
})).digest("hex");
|
|
664
738
|
}
|
|
739
|
+
function hashEntrySource(source) {
|
|
740
|
+
if (source === void 0) return "none";
|
|
741
|
+
return crypto.createHash("sha1").update(source).digest("hex");
|
|
742
|
+
}
|
|
665
743
|
//#endregion
|
|
666
744
|
//#region src/config.ts
|
|
667
745
|
const configDefaults = Object.freeze({ resolve: { extensions: [
|
|
@@ -758,20 +836,21 @@ async function loadFromBundledFile(fileName, bundledCode, options, dependencies)
|
|
|
758
836
|
} else {
|
|
759
837
|
const extension = path.extname(fileName);
|
|
760
838
|
const realFileName = await promisifiedRealpath(fileName);
|
|
761
|
-
const
|
|
762
|
-
const
|
|
839
|
+
const originalLoader = _require.extensions[extension];
|
|
840
|
+
const fallbackLoader = originalLoader ?? _require.extensions[".js"];
|
|
763
841
|
const compileLoader = (module, filename) => {
|
|
764
842
|
if (filename === realFileName) module._compile(bundledCode, filename);
|
|
765
|
-
else
|
|
843
|
+
else fallbackLoader(module, filename);
|
|
766
844
|
};
|
|
767
845
|
let raw;
|
|
768
846
|
try {
|
|
769
|
-
_require.extensions[
|
|
847
|
+
_require.extensions[extension] = compileLoader;
|
|
770
848
|
delete _require.cache[_require.resolve(fileName)];
|
|
771
849
|
raw = _require(fileName);
|
|
772
850
|
return raw.__esModule ? raw.default : raw;
|
|
773
851
|
} finally {
|
|
774
|
-
_require.extensions[
|
|
852
|
+
if (originalLoader) _require.extensions[extension] = originalLoader;
|
|
853
|
+
else delete _require.extensions[extension];
|
|
775
854
|
if (cacheConfig.enabled && raw !== void 0) {
|
|
776
855
|
const cachedPath = await storeCacheOutput(cacheConfig, bundledCode, options, dependencies);
|
|
777
856
|
await writeCacheMeta(cacheConfig, cachedPath.cacheMeta);
|