rolldown-require 2.0.16 → 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 CHANGED
@@ -40,6 +40,10 @@ interface Options {
40
40
  * 需要打包并 require 的文件路径。
41
41
  */
42
42
  filepath: string;
43
+ /**
44
+ * 覆盖入口文件源码,同时保留入口文件路径用于相对路径解析。
45
+ */
46
+ source?: string;
43
47
  /**
44
48
  * 用于加载输出文件的 `require` 方法。
45
49
  * 默认为全局 `require`。
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 || fs.realpathSync.native;
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: [createExternalizeDepsPlugin({
424
- entryFile: fileName,
425
- isESM
426
- }), createFileScopeVariablesPlugin({
427
- dirnameVarName,
428
- filenameVarName,
429
- importMetaUrlVarName
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 loaderExt = extension in _require.extensions ? extension : ".js";
762
- const defaultLoader = _require.extensions[loaderExt];
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 defaultLoader(module, filename);
843
+ else fallbackLoader(module, filename);
766
844
  };
767
845
  let raw;
768
846
  try {
769
- _require.extensions[loaderExt] = compileLoader;
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[loaderExt] = defaultLoader;
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);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "rolldown-require",
3
3
  "type": "module",
4
- "version": "2.0.16",
4
+ "version": "2.0.18",
5
5
  "description": "bundle and require a file using rolldown!",
6
6
  "author": "ice breaker <1324318532@qq.com>",
7
7
  "license": "MIT",
@@ -48,7 +48,7 @@
48
48
  "node": "^20.19.0 || >=22.12.0"
49
49
  },
50
50
  "peerDependencies": {
51
- "rolldown": "1.0.1"
51
+ "rolldown": "1.0.2"
52
52
  },
53
53
  "dependencies": {
54
54
  "@rollup/pluginutils": "^5.3.0",