@nasti-toolchain/nasti 1.3.2 → 1.3.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.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,67 @@ 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
+ define: {
775
+ // CJS 包(如 react)通过 process.env.NODE_ENV 判断环境,需在打包时替换
776
+ "process.env.NODE_ENV": '"development"'
777
+ }
778
+ });
779
+ const result = await bundle.generate({ format: "esm", exports: "named" });
780
+ await bundle.close();
781
+ let code = result.output[0].code;
782
+ code = code.replace(/process\.env\.NODE_ENV/g, '"development"');
783
+ code = code.replace(
784
+ /^(import\b[^;'"]*?\bfrom\s+)(['"])([^'"./][^'"]*)(\2)/gm,
785
+ (_, prefix, q, spec) => `${prefix}${q}/@modules/${spec}${q}`
786
+ ).replace(
787
+ /^(export\b[^;'"]*?\bfrom\s+)(['"])([^'"./][^'"]*)(\2)/gm,
788
+ (_, prefix, q, spec) => `${prefix}${q}/@modules/${spec}${q}`
789
+ ).replace(
790
+ /^(import\s+)(['"])([^'"./][^'"]*)(\2)/gm,
791
+ (_, prefix, q, spec) => `${prefix}${q}/@modules/${spec}${q}`
792
+ );
793
+ if (code.includes("__commonJSMin")) {
794
+ code = await injectCjsNamedExports(code, entryFile);
795
+ }
796
+ return code;
797
+ }
798
+ async function injectCjsNamedExports(code, entryFile) {
799
+ try {
800
+ const { createRequire: createRequire2 } = await import("module");
801
+ const req = createRequire2(entryFile);
802
+ const cjsExports = req(entryFile);
803
+ if (!cjsExports || typeof cjsExports !== "object" || Array.isArray(cjsExports)) return code;
804
+ const namedKeys = Object.keys(cjsExports).filter(
805
+ (k) => k !== "__esModule" && k !== "default" && VALID_IDENT.test(k)
806
+ );
807
+ if (namedKeys.length === 0) return code;
808
+ return code.replace(
809
+ /^export default (\w+\(\));?\s*$/m,
810
+ (_, call) => [
811
+ `const __cjsMod = ${call};`,
812
+ `export default __cjsMod;`,
813
+ ...namedKeys.map((k) => `export const ${k} = __cjsMod[${JSON.stringify(k)}];`)
814
+ ].join("\n")
815
+ );
816
+ } catch {
817
+ return code;
818
+ }
819
+ }
752
820
  function rewriteImports(code, _config) {
753
821
  return code.replace(
754
822
  /\bfrom\s+(['"])([^'"./][^'"]*)\1/g,
@@ -975,7 +1043,7 @@ if (!window.__nasti_hot_map) window.__nasti_hot_map = new Map();
975
1043
  window.__NASTI_HMR__ = { createHotContext };
976
1044
  `;
977
1045
  }
978
- var import_node_path4, import_node_fs4, RESOLVE_EXTENSIONS, ESM_CONDITIONS;
1046
+ var import_node_path4, import_node_fs4, esmBundleCache, VALID_IDENT, RESOLVE_EXTENSIONS, ESM_CONDITIONS;
979
1047
  var init_middleware = __esm({
980
1048
  "src/server/middleware.ts"() {
981
1049
  "use strict";
@@ -984,6 +1052,8 @@ var init_middleware = __esm({
984
1052
  init_transformer();
985
1053
  init_html();
986
1054
  init_env();
1055
+ esmBundleCache = /* @__PURE__ */ new Map();
1056
+ VALID_IDENT = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
987
1057
  RESOLVE_EXTENSIONS = [".tsx", ".ts", ".jsx", ".js", ".mjs", ".json", ".vue"];
988
1058
  ESM_CONDITIONS = ["import", "browser", "module", "default"];
989
1059
  }
@@ -1322,7 +1392,7 @@ async function createServer(inlineConfig = {}) {
1322
1392
  const localUrl = `http://localhost:${actualPort}`;
1323
1393
  const networkUrl = host === "0.0.0.0" ? `http://${getNetworkAddress()}:${actualPort}` : null;
1324
1394
  console.log();
1325
- console.log(import_picocolors.default.cyan(" nasti dev server") + import_picocolors.default.dim(` v${"1.3.2"}`));
1395
+ console.log(import_picocolors.default.cyan(" nasti dev server") + import_picocolors.default.dim(` v${"1.3.3"}`));
1326
1396
  console.log();
1327
1397
  console.log(` ${import_picocolors.default.green(">")} Local: ${import_picocolors.default.cyan(localUrl)}`);
1328
1398
  if (networkUrl) {
@@ -1400,7 +1470,7 @@ __export(build_exports, {
1400
1470
  async function build(inlineConfig = {}) {
1401
1471
  const config = await resolveConfig(inlineConfig, "build");
1402
1472
  const startTime = performance.now();
1403
- console.log(import_picocolors2.default.cyan("\n\u{1F528} nasti build") + import_picocolors2.default.dim(` v${"1.3.2"}`));
1473
+ console.log(import_picocolors2.default.cyan("\n\u{1F528} nasti build") + import_picocolors2.default.dim(` v${"1.3.3"}`));
1404
1474
  console.log(import_picocolors2.default.dim(` root: ${config.root}`));
1405
1475
  console.log(import_picocolors2.default.dim(` mode: ${config.mode}`));
1406
1476
  const outDir = import_node_path10.default.resolve(config.root, config.build.outDir);
@@ -1625,6 +1695,6 @@ cli.command("preview [root]", "Preview production build").option("--port <port>"
1625
1695
  }
1626
1696
  });
1627
1697
  cli.help();
1628
- cli.version("1.3.2");
1698
+ cli.version("1.3.3");
1629
1699
  cli.parse();
1630
1700
  //# sourceMappingURL=cli.cjs.map