@nasti-toolchain/nasti 1.6.1 → 1.6.3

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/cli.js CHANGED
@@ -443,6 +443,19 @@ var init_module_graph = __esm({
443
443
  }
444
444
  mods.add(mod);
445
445
  }
446
+ /**
447
+ * Reindex a module under a plugin-provided canonical id (e.g. a `\0virtual:foo`
448
+ * id returned from `resolveId`). Plugins look up their own virtual modules via
449
+ * `getModuleById(RESOLVED_ID)` to invalidate them on watcher events; without
450
+ * this remap they'd never find the node because `ensureEntryFromUrl` keys by
451
+ * the public URL only.
452
+ */
453
+ setModuleId(mod, id) {
454
+ if (mod.id === id) return;
455
+ this.idToModuleMap.delete(mod.id);
456
+ mod.id = id;
457
+ this.idToModuleMap.set(id, mod);
458
+ }
446
459
  /** 更新模块依赖关系 */
447
460
  updateModuleImports(mod, importedIds) {
448
461
  for (const imported of mod.importedModules) {
@@ -739,7 +752,7 @@ __export(middleware_exports, {
739
752
  import path4 from "path";
740
753
  import fs4 from "fs";
741
754
  import { createRequire } from "module";
742
- import { fileURLToPath } from "url";
755
+ import { fileURLToPath, pathToFileURL as pathToFileURL2 } from "url";
743
756
  function getReactRefreshRuntimeEsm() {
744
757
  if (__refreshRuntimeCache) return __refreshRuntimeCache;
745
758
  let cjsPath;
@@ -894,6 +907,16 @@ async function transformRequest(url, ctx) {
894
907
  if (cleanReqUrl === "/@react-refresh") {
895
908
  return { code: getReactRefreshRuntimeEsm() };
896
909
  }
910
+ if (cleanReqUrl.startsWith("/@modules/")) {
911
+ const spec = cleanReqUrl.slice("/@modules/".length);
912
+ const virtual = await loadVirtualModule(spec, ctx);
913
+ if (virtual) {
914
+ const mod2 = await moduleGraph.ensureEntryFromUrl(url);
915
+ moduleGraph.setModuleId(mod2, virtual.id);
916
+ mod2.transformResult = virtual.result;
917
+ return virtual.result;
918
+ }
919
+ }
897
920
  const filePath = resolveUrlToFile(url, config.root);
898
921
  if (!filePath || !fs4.existsSync(filePath)) return null;
899
922
  const mod = await moduleGraph.ensureEntryFromUrl(url);
@@ -940,6 +963,28 @@ async function transformRequest(url, ctx) {
940
963
  mod.transformResult = transformResult;
941
964
  return transformResult;
942
965
  }
966
+ async function loadVirtualModule(spec, ctx) {
967
+ const { config, pluginContainer } = ctx;
968
+ const resolved = await pluginContainer.resolveId(spec);
969
+ if (resolved == null) return null;
970
+ const resolvedId = typeof resolved === "string" ? resolved : resolved.id;
971
+ const looksVirtual = resolvedId.startsWith("\0") || !fs4.existsSync(resolvedId);
972
+ if (!looksVirtual) return null;
973
+ const loadResult = await pluginContainer.load(resolvedId);
974
+ if (loadResult == null) return null;
975
+ let code = typeof loadResult === "string" ? loadResult : loadResult.code;
976
+ const transformed = await pluginContainer.transform(code, resolvedId);
977
+ if (transformed != null) {
978
+ code = typeof transformed === "string" ? transformed : transformed.code;
979
+ }
980
+ code = replaceEnvInCode(code, ctx.envDefine ?? buildEnvDefine(
981
+ loadEnv(config.mode, config.root, config.envPrefix),
982
+ config.mode
983
+ ));
984
+ const anchor = path4.join(config.root, "__nasti_virtual__.ts");
985
+ code = rewriteImports(code, config, anchor);
986
+ return { id: resolvedId, result: { code } };
987
+ }
943
988
  async function bundlePackageAsEsm(entryFile) {
944
989
  if (!esmBundleCache.has(entryFile)) {
945
990
  esmBundleCache.set(entryFile, doBundlePackage(entryFile));
@@ -947,6 +992,8 @@ async function bundlePackageAsEsm(entryFile) {
947
992
  return esmBundleCache.get(entryFile);
948
993
  }
949
994
  async function doBundlePackage(entryFile) {
995
+ const shim = await tryGenerateSubpathShim(entryFile);
996
+ if (shim != null) return shim;
950
997
  const { rolldown: rolldown4 } = await import("rolldown");
951
998
  const bundle = await rolldown4({
952
999
  input: entryFile,
@@ -976,6 +1023,103 @@ async function doBundlePackage(entryFile) {
976
1023
  }
977
1024
  return code;
978
1025
  }
1026
+ async function tryGenerateSubpathShim(entryFile) {
1027
+ const NM = `${path4.sep}node_modules${path4.sep}`;
1028
+ if (!entryFile.includes(NM)) return null;
1029
+ let pkgDir = null;
1030
+ let pkgName = null;
1031
+ let dir = path4.dirname(entryFile);
1032
+ while (true) {
1033
+ const pkgJsonPath = path4.join(dir, "package.json");
1034
+ if (fs4.existsSync(pkgJsonPath)) {
1035
+ try {
1036
+ const pkg = JSON.parse(fs4.readFileSync(pkgJsonPath, "utf-8"));
1037
+ if (typeof pkg?.name === "string" && pkg.name) {
1038
+ pkgDir = dir;
1039
+ pkgName = pkg.name;
1040
+ break;
1041
+ }
1042
+ } catch {
1043
+ }
1044
+ }
1045
+ const parent = path4.dirname(dir);
1046
+ if (parent === dir) return null;
1047
+ dir = parent;
1048
+ if (!dir.includes(NM)) return null;
1049
+ }
1050
+ if (!pkgDir || !pkgName) return null;
1051
+ const entryExt = path4.extname(entryFile);
1052
+ const mainEntry = pickMainEntryByExtension(pkgDir, entryExt);
1053
+ if (!mainEntry) return null;
1054
+ if (path4.resolve(mainEntry) === path4.resolve(entryFile)) return null;
1055
+ let mainNs;
1056
+ let subNs;
1057
+ try {
1058
+ mainNs = await import(pathToFileURL2(mainEntry).href);
1059
+ subNs = await import(pathToFileURL2(entryFile).href);
1060
+ } catch {
1061
+ return null;
1062
+ }
1063
+ if (!mainNs || typeof mainNs !== "object") return null;
1064
+ if (!subNs || typeof subNs !== "object") return null;
1065
+ const subKeys = Object.keys(subNs).filter(
1066
+ (k) => k !== "__esModule" && k !== "default" && VALID_IDENT.test(k)
1067
+ );
1068
+ if (subKeys.length === 0) return null;
1069
+ for (const k of subKeys) {
1070
+ if (!(k in mainNs)) return null;
1071
+ if (mainNs[k] !== subNs[k]) return null;
1072
+ }
1073
+ if ("default" in subNs) {
1074
+ if (!("default" in mainNs)) return null;
1075
+ if (mainNs["default"] !== subNs["default"]) return null;
1076
+ }
1077
+ const lines = [
1078
+ `// Nasti subpath shim \u2192 ${pkgName} (avoid duplicate bundling)`,
1079
+ `import * as __pkg from "/@modules/${pkgName}";`
1080
+ ];
1081
+ for (const k of subKeys) {
1082
+ lines.push(`export const ${k} = __pkg[${JSON.stringify(k)}];`);
1083
+ }
1084
+ if ("default" in subNs) {
1085
+ lines.push(`export default ("default" in __pkg ? __pkg["default"] : __pkg);`);
1086
+ }
1087
+ return lines.join("\n") + "\n";
1088
+ }
1089
+ function pickMainEntryByExtension(pkgDir, preferredExt) {
1090
+ const pkgJsonPath = path4.join(pkgDir, "package.json");
1091
+ let pkg;
1092
+ try {
1093
+ pkg = JSON.parse(fs4.readFileSync(pkgJsonPath, "utf-8"));
1094
+ } catch {
1095
+ return null;
1096
+ }
1097
+ const candidates = [];
1098
+ const collectFromExportObject = (obj) => {
1099
+ if (!obj || typeof obj !== "object") return;
1100
+ for (const cond of ["import", "module", "default", "require", "node"]) {
1101
+ const v = obj[cond];
1102
+ if (typeof v === "string") candidates.push(v);
1103
+ else if (v && typeof v === "object") collectFromExportObject(v);
1104
+ }
1105
+ };
1106
+ const dot = pkg?.exports?.["."];
1107
+ if (typeof dot === "string") candidates.push(dot);
1108
+ else if (dot && typeof dot === "object") collectFromExportObject(dot);
1109
+ if (typeof pkg.module === "string") candidates.push(pkg.module);
1110
+ if (typeof pkg.main === "string") candidates.push(pkg.main);
1111
+ for (const cand of candidates) {
1112
+ if (path4.extname(cand) === preferredExt) {
1113
+ const full = path4.resolve(pkgDir, cand);
1114
+ if (fs4.existsSync(full)) return full;
1115
+ }
1116
+ }
1117
+ for (const cand of candidates) {
1118
+ const full = path4.resolve(pkgDir, cand);
1119
+ if (fs4.existsSync(full)) return full;
1120
+ }
1121
+ return null;
1122
+ }
979
1123
  function rewriteExternalRequires(code) {
980
1124
  const pkgs = /* @__PURE__ */ new Set();
981
1125
  const re = /__require\(["']([^"']+)["']\)/g;
@@ -1691,7 +1835,7 @@ async function createServer(inlineConfig = {}) {
1691
1835
  const localUrl = `http://localhost:${actualPort}`;
1692
1836
  const networkUrl = host === "0.0.0.0" ? `http://${getNetworkAddress()}:${actualPort}` : null;
1693
1837
  console.log();
1694
- console.log(pc.cyan(" nasti dev server") + pc.dim(` v${"1.6.1"}`));
1838
+ console.log(pc.cyan(" nasti dev server") + pc.dim(` v${"1.6.3"}`));
1695
1839
  console.log();
1696
1840
  console.log(` ${pc.green(">")} Local: ${pc.cyan(localUrl)}`);
1697
1841
  if (networkUrl) {
@@ -1814,7 +1958,7 @@ import pc2 from "picocolors";
1814
1958
  async function build(inlineConfig = {}) {
1815
1959
  const config = await resolveConfig(inlineConfig, "build");
1816
1960
  const startTime = performance.now();
1817
- console.log(pc2.cyan("\n\u{1F528} nasti build") + pc2.dim(` v${"1.6.1"}`));
1961
+ console.log(pc2.cyan("\n\u{1F528} nasti build") + pc2.dim(` v${"1.6.3"}`));
1818
1962
  console.log(pc2.dim(` root: ${config.root}`));
1819
1963
  console.log(pc2.dim(` mode: ${config.mode}`));
1820
1964
  const outDir = path10.resolve(config.root, config.build.outDir);
@@ -1880,7 +2024,11 @@ async function build(inlineConfig = {}) {
1880
2024
  load: p.load,
1881
2025
  transform: p.transform,
1882
2026
  buildStart: p.buildStart,
1883
- buildEnd: p.buildEnd
2027
+ buildEnd: p.buildEnd,
2028
+ // Forward `closeBundle` to Rolldown — it invokes the hook during
2029
+ // `bundle.close()` below. This is the hook Vite plugins (e.g. PWA
2030
+ // manifest/SW writers) rely on for final-stage artifact emission.
2031
+ closeBundle: p.closeBundle
1884
2032
  }))
1885
2033
  ],
1886
2034
  ...config.build.rolldownOptions
@@ -1974,7 +2122,7 @@ async function buildElectron(inlineConfig = {}) {
1974
2122
  const config = await resolveConfig({ ...inlineConfig, target: "electron" }, "build");
1975
2123
  const startTime = performance.now();
1976
2124
  assertElectronVersion(config);
1977
- console.log(pc3.cyan("\n\u26A1 nasti build (electron)") + pc3.dim(` v${"1.6.1"}`));
2125
+ console.log(pc3.cyan("\n\u26A1 nasti build (electron)") + pc3.dim(` v${"1.6.3"}`));
1978
2126
  console.log(pc3.dim(` root: ${config.root}`));
1979
2127
  console.log(pc3.dim(` mode: ${config.mode}`));
1980
2128
  console.log(pc3.dim(` target: electron (\u2265 ${config.electron.minVersion})`));
@@ -2128,7 +2276,7 @@ async function startElectronDev(inlineConfig = {}) {
2128
2276
  const { noSpawn, ...rest } = inlineConfig;
2129
2277
  const config = await resolveConfig({ ...rest, target: "electron" }, "serve");
2130
2278
  warnElectronVersion(config);
2131
- console.log(pc4.cyan("\n\u26A1 nasti electron dev") + pc4.dim(` v${"1.6.1"}`));
2279
+ console.log(pc4.cyan("\n\u26A1 nasti electron dev") + pc4.dim(` v${"1.6.3"}`));
2132
2280
  const { createServer: createServer2 } = await Promise.resolve().then(() => (init_server(), server_exports));
2133
2281
  const server = await createServer2({ ...rest, target: "electron" });
2134
2282
  await server.listen();
@@ -2455,6 +2603,6 @@ cli.command("preview [root]", "Preview production build").option("--port <port>"
2455
2603
  }
2456
2604
  });
2457
2605
  cli.help();
2458
- cli.version("1.6.1");
2606
+ cli.version("1.6.3");
2459
2607
  cli.parse();
2460
2608
  //# sourceMappingURL=cli.js.map