@nasti-toolchain/nasti 1.3.2 → 1.3.4

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.cjs CHANGED
@@ -725,6 +725,13 @@ async function transformRequest(url, ctx) {
725
725
  if (!filePath || !import_node_fs4.default.existsSync(filePath)) return null;
726
726
  const mod = await moduleGraph.ensureEntryFromUrl(url);
727
727
  moduleGraph.registerModule(mod, filePath);
728
+ const cleanReqUrl = url.split("?")[0];
729
+ if (cleanReqUrl.startsWith("/@modules/")) {
730
+ const code2 = await bundlePackageAsEsm(filePath);
731
+ const transformResult2 = { code: code2 };
732
+ mod.transformResult = transformResult2;
733
+ return transformResult2;
734
+ }
728
735
  let code = import_node_fs4.default.readFileSync(filePath, "utf-8");
729
736
  const pluginResult = await pluginContainer.transform(code, filePath);
730
737
  if (pluginResult) {
@@ -749,6 +756,63 @@ async function transformRequest(url, ctx) {
749
756
  mod.transformResult = transformResult;
750
757
  return transformResult;
751
758
  }
759
+ async function bundlePackageAsEsm(entryFile) {
760
+ if (!esmBundleCache.has(entryFile)) {
761
+ esmBundleCache.set(entryFile, doBundlePackage(entryFile));
762
+ }
763
+ return esmBundleCache.get(entryFile);
764
+ }
765
+ async function doBundlePackage(entryFile) {
766
+ const { rolldown: rolldown2 } = await import("rolldown");
767
+ const bundle = await rolldown2({
768
+ input: entryFile,
769
+ // 仅将其他 npm 包外部化;相对路径(包内部文件)全部内联打包
770
+ external: (id) => {
771
+ if (id.startsWith(".") || id.startsWith("/") || /^[A-Za-z]:\\/.test(id)) return false;
772
+ return true;
773
+ }
774
+ });
775
+ const result = await bundle.generate({ format: "esm", exports: "named" });
776
+ await bundle.close();
777
+ let code = result.output[0].code;
778
+ code = code.replace(/process\.env\.NODE_ENV/g, '"development"');
779
+ code = code.replace(
780
+ /^(import\b[^;'"]*?\bfrom\s+)(['"])([^'"./][^'"]*)(\2)/gm,
781
+ (_, prefix, q, spec) => `${prefix}${q}/@modules/${spec}${q}`
782
+ ).replace(
783
+ /^(export\b[^;'"]*?\bfrom\s+)(['"])([^'"./][^'"]*)(\2)/gm,
784
+ (_, prefix, q, spec) => `${prefix}${q}/@modules/${spec}${q}`
785
+ ).replace(
786
+ /^(import\s+)(['"])([^'"./][^'"]*)(\2)/gm,
787
+ (_, prefix, q, spec) => `${prefix}${q}/@modules/${spec}${q}`
788
+ );
789
+ if (code.includes("__commonJSMin")) {
790
+ code = await injectCjsNamedExports(code, entryFile);
791
+ }
792
+ return code;
793
+ }
794
+ async function injectCjsNamedExports(code, entryFile) {
795
+ try {
796
+ const { createRequire: createRequire2 } = await import("module");
797
+ const req = createRequire2(entryFile);
798
+ const cjsExports = req(entryFile);
799
+ if (!cjsExports || typeof cjsExports !== "object" || Array.isArray(cjsExports)) return code;
800
+ const namedKeys = Object.keys(cjsExports).filter(
801
+ (k) => k !== "__esModule" && k !== "default" && VALID_IDENT.test(k)
802
+ );
803
+ if (namedKeys.length === 0) return code;
804
+ return code.replace(
805
+ /^export default (\w+\(\));?\s*$/m,
806
+ (_, call) => [
807
+ `const __cjsMod = ${call};`,
808
+ `export default __cjsMod;`,
809
+ ...namedKeys.map((k) => `export const ${k} = __cjsMod[${JSON.stringify(k)}];`)
810
+ ].join("\n")
811
+ );
812
+ } catch {
813
+ return code;
814
+ }
815
+ }
752
816
  function rewriteImports(code, _config) {
753
817
  return code.replace(
754
818
  /\bfrom\s+(['"])([^'"./][^'"]*)\1/g,
@@ -975,7 +1039,7 @@ if (!window.__nasti_hot_map) window.__nasti_hot_map = new Map();
975
1039
  window.__NASTI_HMR__ = { createHotContext };
976
1040
  `;
977
1041
  }
978
- var import_node_path4, import_node_fs4, RESOLVE_EXTENSIONS, ESM_CONDITIONS;
1042
+ var import_node_path4, import_node_fs4, esmBundleCache, VALID_IDENT, RESOLVE_EXTENSIONS, ESM_CONDITIONS;
979
1043
  var init_middleware = __esm({
980
1044
  "src/server/middleware.ts"() {
981
1045
  "use strict";
@@ -984,6 +1048,8 @@ var init_middleware = __esm({
984
1048
  init_transformer();
985
1049
  init_html();
986
1050
  init_env();
1051
+ esmBundleCache = /* @__PURE__ */ new Map();
1052
+ VALID_IDENT = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
987
1053
  RESOLVE_EXTENSIONS = [".tsx", ".ts", ".jsx", ".js", ".mjs", ".json", ".vue"];
988
1054
  ESM_CONDITIONS = ["import", "browser", "module", "default"];
989
1055
  }
@@ -1322,7 +1388,7 @@ async function createServer(inlineConfig = {}) {
1322
1388
  const localUrl = `http://localhost:${actualPort}`;
1323
1389
  const networkUrl = host === "0.0.0.0" ? `http://${getNetworkAddress()}:${actualPort}` : null;
1324
1390
  console.log();
1325
- console.log(import_picocolors.default.cyan(" nasti dev server") + import_picocolors.default.dim(` v${"1.3.2"}`));
1391
+ console.log(import_picocolors.default.cyan(" nasti dev server") + import_picocolors.default.dim(` v${"1.3.4"}`));
1326
1392
  console.log();
1327
1393
  console.log(` ${import_picocolors.default.green(">")} Local: ${import_picocolors.default.cyan(localUrl)}`);
1328
1394
  if (networkUrl) {
@@ -1400,7 +1466,7 @@ __export(build_exports, {
1400
1466
  async function build(inlineConfig = {}) {
1401
1467
  const config = await resolveConfig(inlineConfig, "build");
1402
1468
  const startTime = performance.now();
1403
- console.log(import_picocolors2.default.cyan("\n\u{1F528} nasti build") + import_picocolors2.default.dim(` v${"1.3.2"}`));
1469
+ console.log(import_picocolors2.default.cyan("\n\u{1F528} nasti build") + import_picocolors2.default.dim(` v${"1.3.4"}`));
1404
1470
  console.log(import_picocolors2.default.dim(` root: ${config.root}`));
1405
1471
  console.log(import_picocolors2.default.dim(` mode: ${config.mode}`));
1406
1472
  const outDir = import_node_path10.default.resolve(config.root, config.build.outDir);
@@ -1625,6 +1691,6 @@ cli.command("preview [root]", "Preview production build").option("--port <port>"
1625
1691
  }
1626
1692
  });
1627
1693
  cli.help();
1628
- cli.version("1.3.2");
1694
+ cli.version("1.3.4");
1629
1695
  cli.parse();
1630
1696
  //# sourceMappingURL=cli.cjs.map