@nasti-toolchain/nasti 2.0.0 → 2.0.2

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
@@ -1350,6 +1350,28 @@ async function transformRequest(url, ctx) {
1350
1350
  if (cleanReqUrl === "/@react-refresh") {
1351
1351
  return { code: getReactRefreshRuntimeEsm() };
1352
1352
  }
1353
+ if (cleanReqUrl.startsWith("/@modules/") && url.includes("?")) {
1354
+ const idParam = new URLSearchParams(url.slice(url.indexOf("?") + 1)).get("id");
1355
+ let realId = null;
1356
+ let realIdValid = false;
1357
+ try {
1358
+ if (idParam) {
1359
+ realId = fs4.realpathSync(idParam);
1360
+ realIdValid = fs4.statSync(realId).isFile() && (realId.includes(`${path4.sep}node_modules${path4.sep}`) || isUnderRoot(realId, config.root));
1361
+ }
1362
+ } catch {
1363
+ realId = null;
1364
+ realIdValid = false;
1365
+ }
1366
+ if (realId && realIdValid) {
1367
+ const mod2 = await moduleGraph.ensureEntryFromUrl(url);
1368
+ moduleGraph.registerModule(mod2, realId);
1369
+ const code2 = await bundlePackageAsEsm(realId, config.root);
1370
+ const transformResult2 = { code: code2 };
1371
+ mod2.transformResult = transformResult2;
1372
+ return transformResult2;
1373
+ }
1374
+ }
1353
1375
  if (cleanReqUrl.startsWith("/@modules/")) {
1354
1376
  const spec = cleanReqUrl.slice("/@modules/".length);
1355
1377
  const virtual = await loadVirtualModule(spec, ctx);
@@ -1387,7 +1409,7 @@ async function transformRequest(url, ctx) {
1387
1409
  const mod = await moduleGraph.ensureEntryFromUrl(url);
1388
1410
  moduleGraph.registerModule(mod, filePath);
1389
1411
  if (cleanReqUrl.startsWith("/@modules/")) {
1390
- const code2 = await bundlePackageAsEsm(filePath);
1412
+ const code2 = await bundlePackageAsEsm(filePath, config.root);
1391
1413
  const transformResult2 = { code: code2 };
1392
1414
  mod.transformResult = transformResult2;
1393
1415
  return transformResult2;
@@ -1450,14 +1472,14 @@ async function loadVirtualModule(spec, ctx) {
1450
1472
  code = rewriteImports(code, config, anchor);
1451
1473
  return { id: resolvedId, result: { code } };
1452
1474
  }
1453
- async function bundlePackageAsEsm(entryFile) {
1475
+ async function bundlePackageAsEsm(entryFile, root) {
1454
1476
  if (!esmBundleCache.has(entryFile)) {
1455
- esmBundleCache.set(entryFile, doBundlePackage(entryFile));
1477
+ esmBundleCache.set(entryFile, doBundlePackage(entryFile, root));
1456
1478
  }
1457
1479
  return esmBundleCache.get(entryFile);
1458
1480
  }
1459
- async function doBundlePackage(entryFile) {
1460
- const shim = await tryGenerateSubpathShim(entryFile);
1481
+ async function doBundlePackage(entryFile, root) {
1482
+ const shim = await tryGenerateSubpathShim(entryFile, root);
1461
1483
  if (shim != null) return shim;
1462
1484
  const { rolldown: rolldown4 } = await import("rolldown");
1463
1485
  const bundle2 = await rolldown4({
@@ -1472,23 +1494,24 @@ async function doBundlePackage(entryFile) {
1472
1494
  await bundle2.close();
1473
1495
  let code = result.output[0].code;
1474
1496
  code = code.replace(/process\.env\.NODE_ENV/g, '"development"');
1497
+ const externalBaseDir = path4.dirname(entryFile);
1475
1498
  code = code.replace(
1476
1499
  /^(import\b[^;'"]*?\bfrom\s+)(['"])([^'"./][^'"]*)(\2)/gm,
1477
- (_, prefix, q, spec) => `${prefix}${q}/@modules/${spec}${q}`
1500
+ (_, prefix, q, spec) => `${prefix}${q}${externalSpecToModuleUrl(spec, externalBaseDir, root)}${q}`
1478
1501
  ).replace(
1479
1502
  /^(export\b[^;'"]*?\bfrom\s+)(['"])([^'"./][^'"]*)(\2)/gm,
1480
- (_, prefix, q, spec) => `${prefix}${q}/@modules/${spec}${q}`
1503
+ (_, prefix, q, spec) => `${prefix}${q}${externalSpecToModuleUrl(spec, externalBaseDir, root)}${q}`
1481
1504
  ).replace(
1482
1505
  /^(import\s+)(['"])([^'"./][^'"]*)(\2)/gm,
1483
- (_, prefix, q, spec) => `${prefix}${q}/@modules/${spec}${q}`
1506
+ (_, prefix, q, spec) => `${prefix}${q}${externalSpecToModuleUrl(spec, externalBaseDir, root)}${q}`
1484
1507
  );
1485
- code = rewriteExternalRequires(code);
1508
+ code = rewriteExternalRequires(code, externalBaseDir, root);
1486
1509
  if (code.includes("__commonJSMin")) {
1487
1510
  code = await injectCjsNamedExports(code, entryFile);
1488
1511
  }
1489
1512
  return code;
1490
1513
  }
1491
- async function tryGenerateSubpathShim(entryFile) {
1514
+ async function tryGenerateSubpathShim(entryFile, root) {
1492
1515
  const NM = `${path4.sep}node_modules${path4.sep}`;
1493
1516
  if (!entryFile.includes(NM)) return null;
1494
1517
  let pkgDir = null;
@@ -1539,9 +1562,11 @@ async function tryGenerateSubpathShim(entryFile) {
1539
1562
  if (!("default" in mainNs)) return null;
1540
1563
  if (mainNs["default"] !== subNs["default"]) return null;
1541
1564
  }
1565
+ const rootMain = resolveNodeModule(root, pkgName);
1566
+ const mainEntryUrl = rootMain && rootMain.startsWith(pkgDir + path4.sep) ? `/@modules/${pkgName}` : `/@modules/${pkgName}?id=${encodeURIComponent(mainEntry)}`;
1542
1567
  const lines = [
1543
1568
  `// Nasti subpath shim \u2192 ${pkgName} (avoid duplicate bundling)`,
1544
- `import * as __pkg from "/@modules/${pkgName}";`
1569
+ `import * as __pkg from "${mainEntryUrl}";`
1545
1570
  ];
1546
1571
  for (const k of subKeys) {
1547
1572
  lines.push(`export const ${k} = __pkg[${JSON.stringify(k)}];`);
@@ -1585,7 +1610,7 @@ function pickMainEntryByExtension(pkgDir, preferredExt) {
1585
1610
  }
1586
1611
  return null;
1587
1612
  }
1588
- function rewriteExternalRequires(code) {
1613
+ function rewriteExternalRequires(code, baseDir, root) {
1589
1614
  const pkgs = /* @__PURE__ */ new Set();
1590
1615
  const re = /__require\(["']([^"']+)["']\)/g;
1591
1616
  let m;
@@ -1597,7 +1622,7 @@ function rewriteExternalRequires(code) {
1597
1622
  const imports = [];
1598
1623
  for (const pkg of pkgs) {
1599
1624
  const safe = pkg.replace(/[^a-zA-Z0-9_$]/g, "_");
1600
- imports.push(`import * as __ns_${safe} from "/@modules/${pkg}";`);
1625
+ imports.push(`import * as __ns_${safe} from "${externalSpecToModuleUrl(pkg, baseDir, root)}";`);
1601
1626
  imports.push(`var __req_${safe} = "default" in __ns_${safe} ? __ns_${safe}["default"] : __ns_${safe};`);
1602
1627
  result = result.replaceAll(`__require("${pkg}")`, `__req_${safe}`);
1603
1628
  result = result.replaceAll(`__require('${pkg}')`, `__req_${safe}`);
@@ -1694,7 +1719,23 @@ function isUnderRoot(abs, root) {
1694
1719
  const rel = path4.relative(root, abs);
1695
1720
  return !!rel && !rel.startsWith("..") && !path4.isAbsolute(rel);
1696
1721
  }
1697
- function resolveNodeModule(root, moduleName) {
1722
+ function externalSpecToModuleUrl(spec, baseDir, root) {
1723
+ const resolved = resolveNodeModule(baseDir, spec);
1724
+ if (!resolved) return `/@modules/${spec}`;
1725
+ const rootResolved = resolveNodeModule(root, spec);
1726
+ if (rootResolved && rootResolved === resolved) return `/@modules/${spec}`;
1727
+ return `/@modules/${spec}?id=${encodeURIComponent(resolved)}`;
1728
+ }
1729
+ function resolveNodeModule(baseDir, moduleName) {
1730
+ const resolved = resolveNodeModuleEntry(baseDir, moduleName);
1731
+ if (!resolved) return null;
1732
+ try {
1733
+ return fs4.realpathSync(resolved);
1734
+ } catch {
1735
+ return resolved;
1736
+ }
1737
+ }
1738
+ function resolveNodeModuleEntry(root, moduleName) {
1698
1739
  let pkgName;
1699
1740
  let subpath;
1700
1741
  if (moduleName.startsWith("@")) {
@@ -1764,7 +1805,12 @@ function resolvePackageExports(exports, key, pkgDir) {
1764
1805
  return key === "." ? path4.join(pkgDir, exports) : null;
1765
1806
  }
1766
1807
  const entry = exports[key];
1767
- if (entry === void 0) return null;
1808
+ if (entry === void 0) {
1809
+ if (key === "." && typeof exports === "object" && !Object.keys(exports).some((k) => k.startsWith("."))) {
1810
+ return resolveExportValue(exports, pkgDir);
1811
+ }
1812
+ return null;
1813
+ }
1768
1814
  return resolveExportValue(entry, pkgDir);
1769
1815
  }
1770
1816
  function resolveExportValue(value, pkgDir) {
@@ -3994,7 +4040,7 @@ async function build(inlineConfig = {}) {
3994
4040
  const startTime = performance.now();
3995
4041
  logger.info(
3996
4042
  pc6.cyan(`
3997
- nasti v${"2.0.0"} `) + pc6.green(`building for ${config.mode}...`)
4043
+ nasti v${"2.0.2"} `) + pc6.green(`building for ${config.mode}...`)
3998
4044
  );
3999
4045
  debug5?.(`root: ${config.root}`);
4000
4046
  const buildableNames = Object.keys(config.environments).filter(
@@ -4715,7 +4761,7 @@ async function createServer(inlineConfig = {}) {
4715
4761
  const readyIn = Math.ceil(performance.now() - startTime);
4716
4762
  logger.info(
4717
4763
  `
4718
- ${pc8.cyan(pc8.bold("NASTI"))} ${pc8.cyan(`v${"2.0.0"}`)} ${pc8.dim("ready in")} ${pc8.bold(readyIn)} ${pc8.dim("ms")}
4764
+ ${pc8.cyan(pc8.bold("NASTI"))} ${pc8.cyan(`v${"2.0.2"}`)} ${pc8.dim("ready in")} ${pc8.bold(readyIn)} ${pc8.dim("ms")}
4719
4765
  `
4720
4766
  );
4721
4767
  printServerUrls(
@@ -4845,7 +4891,7 @@ async function buildElectron(inlineConfig = {}) {
4845
4891
  const config = await resolveConfig({ ...inlineConfig, target: "electron" }, "build");
4846
4892
  const startTime = performance.now();
4847
4893
  assertElectronVersion(config);
4848
- console.log(pc9.cyan("\n\u26A1 nasti build (electron)") + pc9.dim(` v${"2.0.0"}`));
4894
+ console.log(pc9.cyan("\n\u26A1 nasti build (electron)") + pc9.dim(` v${"2.0.2"}`));
4849
4895
  console.log(pc9.dim(` root: ${config.root}`));
4850
4896
  console.log(pc9.dim(` mode: ${config.mode}`));
4851
4897
  console.log(pc9.dim(` target: electron (\u2265 ${config.electron.minVersion})`));
@@ -5003,7 +5049,7 @@ async function startElectronDev(inlineConfig = {}) {
5003
5049
  const { noSpawn, ...rest } = inlineConfig;
5004
5050
  const config = await resolveConfig({ ...rest, target: "electron" }, "serve");
5005
5051
  warnElectronVersion(config);
5006
- console.log(pc10.cyan("\n\u26A1 nasti electron dev") + pc10.dim(` v${"2.0.0"}`));
5052
+ console.log(pc10.cyan("\n\u26A1 nasti electron dev") + pc10.dim(` v${"2.0.2"}`));
5007
5053
  const { createServer: createServer2 } = await Promise.resolve().then(() => (init_server(), server_exports));
5008
5054
  const server = await createServer2({ ...rest, target: "electron" });
5009
5055
  await server.listen();
@@ -5348,7 +5394,7 @@ cli.command("preview [root]", "Preview production build").option("--port <port>"
5348
5394
  const host = options.host === true ? "0.0.0.0" : options.host ?? "localhost";
5349
5395
  http2.createServer(app).listen(port, host, () => {
5350
5396
  logger.info(`
5351
- ${pc11.cyan(pc11.bold("NASTI"))} ${pc11.cyan(`v${"2.0.0"}`)} ${pc11.dim("preview")}
5397
+ ${pc11.cyan(pc11.bold("NASTI"))} ${pc11.cyan(`v${"2.0.2"}`)} ${pc11.dim("preview")}
5352
5398
  `);
5353
5399
  printServerUrls2(
5354
5400
  {
@@ -5365,6 +5411,6 @@ cli.command("preview [root]", "Preview production build").option("--port <port>"
5365
5411
  }
5366
5412
  });
5367
5413
  cli.help();
5368
- cli.version("2.0.0");
5414
+ cli.version("2.0.2");
5369
5415
  cli.parse();
5370
5416
  //# sourceMappingURL=cli.js.map