@nasti-toolchain/nasti 1.6.4 → 1.7.0

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.cjs CHANGED
@@ -57,7 +57,8 @@ var init_defaults = __esm({
57
57
  sourcemap: false,
58
58
  target: "es2022",
59
59
  rolldownOptions: {},
60
- emptyOutDir: true
60
+ emptyOutDir: true,
61
+ css: {}
61
62
  };
62
63
  defaultElectron = {
63
64
  main: "src/electron/main.ts",
@@ -301,20 +302,29 @@ var init_config = __esm({
301
302
  function resolvePlugin(config) {
302
303
  const { alias, extensions } = config.resolve;
303
304
  const require2 = (0, import_node_module.createRequire)(import_node_path2.default.resolve(config.root, "package.json"));
305
+ const aliasEntries = Object.entries(alias).sort(
306
+ ([a], [b]) => b.length - a.length
307
+ );
304
308
  return {
305
309
  name: "nasti:resolve",
306
310
  enforce: "pre",
307
311
  resolveId(source, importer) {
308
- for (const [key, value] of Object.entries(alias)) {
312
+ for (const [key, value] of aliasEntries) {
309
313
  if (source === key || source.startsWith(key + "/")) {
310
- source = source.replace(key, value);
311
- if (!import_node_path2.default.isAbsolute(source)) {
312
- source = import_node_path2.default.resolve(config.root, source);
313
- }
314
+ const aliasBase = resolveAliasTarget(value, config.root);
315
+ const sub = source.slice(key.length).replace(/^\//, "");
316
+ const target = sub ? import_node_path2.default.join(aliasBase, sub) : aliasBase;
317
+ const resolved = tryResolveFile(target, extensions);
318
+ if (resolved) return resolved;
314
319
  break;
315
320
  }
316
321
  }
317
- if (import_node_path2.default.isAbsolute(source)) {
322
+ if (source.startsWith("/") && !source.startsWith("//")) {
323
+ const rootRelative = import_node_path2.default.join(config.root, source.slice(1));
324
+ const resolved = tryResolveFile(rootRelative, extensions);
325
+ if (resolved) return resolved;
326
+ }
327
+ if (import_node_path2.default.isAbsolute(source) && import_node_fs2.default.existsSync(source)) {
318
328
  const resolved = tryResolveFile(source, extensions);
319
329
  if (resolved) return resolved;
320
330
  }
@@ -337,6 +347,7 @@ function resolvePlugin(config) {
337
347
  return null;
338
348
  },
339
349
  load(id) {
350
+ if (id.startsWith("\0")) return null;
340
351
  if (!import_node_fs2.default.existsSync(id)) return null;
341
352
  if (id.endsWith(".json")) {
342
353
  const content = import_node_fs2.default.readFileSync(id, "utf-8");
@@ -346,6 +357,11 @@ function resolvePlugin(config) {
346
357
  }
347
358
  };
348
359
  }
360
+ function resolveAliasTarget(value, root) {
361
+ if (import_node_path2.default.isAbsolute(value) && import_node_fs2.default.existsSync(value)) return value;
362
+ if (value.startsWith("/")) return import_node_path2.default.join(root, value.slice(1));
363
+ return import_node_path2.default.resolve(root, value);
364
+ }
349
365
  function tryResolveFile(file, extensions) {
350
366
  if (import_node_fs2.default.existsSync(file) && import_node_fs2.default.statSync(file).isFile()) {
351
367
  return file;
@@ -445,8 +461,8 @@ function cssPlugin(config) {
445
461
  cssSource = compiled.css;
446
462
  }
447
463
  const rewritten = rewriteCssUrls(cssSource, id, config.root);
464
+ const escaped = JSON.stringify(rewritten);
448
465
  if (config.command === "serve") {
449
- const escaped = JSON.stringify(rewritten);
450
466
  return {
451
467
  code: `
452
468
  const css = ${escaped};
@@ -470,7 +486,42 @@ export default css;
470
486
  `
471
487
  };
472
488
  }
473
- return rewritten !== code ? { code: rewritten } : null;
489
+ const cssConfig = config.build.css || {};
490
+ const nonce = cssConfig.nonce;
491
+ const emitCssFile = cssConfig.emitCssFile;
492
+ if (emitCssFile) {
493
+ const fileName = `assets/${import_node_path4.default.basename(id, ".css")}.css`;
494
+ this.emitFile({
495
+ type: "asset",
496
+ fileName,
497
+ source: rewritten
498
+ });
499
+ return {
500
+ code: `
501
+ const link = document.createElement('link');
502
+ link.rel = 'stylesheet';
503
+ link.href = ${JSON.stringify("/" + fileName)};
504
+ document.head.appendChild(link);
505
+
506
+ export default ${escaped};
507
+ `,
508
+ moduleType: "js"
509
+ };
510
+ }
511
+ const nonceAttr = nonce ? `style.setAttribute('nonce', ${JSON.stringify(nonce)});` : "";
512
+ return {
513
+ code: `
514
+ const css = ${escaped};
515
+ const style = document.createElement('style');
516
+ style.setAttribute('data-nasti-css', ${JSON.stringify(id)});
517
+ ${nonceAttr}
518
+ style.textContent = css;
519
+ document.head.appendChild(style);
520
+
521
+ export default css;
522
+ `,
523
+ moduleType: "js"
524
+ };
474
525
  }
475
526
  };
476
527
  }
@@ -873,7 +924,7 @@ __export(build_exports, {
873
924
  async function build(inlineConfig = {}) {
874
925
  const config = await resolveConfig(inlineConfig, "build");
875
926
  const startTime = performance.now();
876
- console.log(import_picocolors.default.cyan("\n\u{1F528} nasti build") + import_picocolors.default.dim(` v${"1.6.4"}`));
927
+ console.log(import_picocolors.default.cyan("\n\u{1F528} nasti build") + import_picocolors.default.dim(` v${"1.7.0"}`));
877
928
  console.log(import_picocolors.default.dim(` root: ${config.root}`));
878
929
  console.log(import_picocolors.default.dim(` mode: ${config.mode}`));
879
930
  const outDir = import_node_path8.default.resolve(config.root, config.build.outDir);
@@ -927,9 +978,12 @@ async function build(inlineConfig = {}) {
927
978
  };
928
979
  const env = loadEnv(config.mode, config.root, config.envPrefix);
929
980
  const envDefine = buildEnvDefine(env, config.mode);
981
+ const { output: userOutput, transform: userTransform, ...restInputOptions } = config.build.rolldownOptions;
982
+ const mergedDefine = { ...userTransform?.define ?? {}, ...envDefine };
930
983
  const bundle = await (0, import_rolldown.rolldown)({
984
+ ...restInputOptions,
931
985
  input: entryPoints,
932
- define: envDefine,
986
+ transform: { ...userTransform, define: mergedDefine },
933
987
  plugins: [
934
988
  oxcTransformPlugin,
935
989
  // 转换 Nasti 插件为 Rolldown 插件格式
@@ -945,17 +999,19 @@ async function build(inlineConfig = {}) {
945
999
  // manifest/SW writers) rely on for final-stage artifact emission.
946
1000
  closeBundle: p.closeBundle
947
1001
  }))
948
- ],
949
- ...config.build.rolldownOptions
1002
+ ]
950
1003
  });
951
1004
  const { output } = await bundle.write({
952
- dir: outDir,
953
1005
  format: "esm",
954
1006
  sourcemap: !!config.build.sourcemap,
955
1007
  minify: !!config.build.minify,
956
1008
  entryFileNames: "assets/[name].[hash].js",
957
1009
  chunkFileNames: "assets/[name].[hash].js",
958
- assetFileNames: "assets/[name].[hash][extname]"
1010
+ assetFileNames: "assets/[name].[hash][extname]",
1011
+ // 用户可覆盖默认输出:代码拆分(advancedChunks / codeSplitting)、chunk 命名等
1012
+ ...userOutput,
1013
+ // dir 始终由 Nasti 掌管 —— 下方 HTML 改写依赖固定的产物目录,故放在最后强制生效
1014
+ dir: outDir
959
1015
  });
960
1016
  await bundle.close();
961
1017
  await pluginContainer.buildEnd();
@@ -1626,7 +1682,7 @@ function rewriteImports(code, config, filePath) {
1626
1682
  const baseSpec = suffix ? spec.slice(0, -suffix.length) : spec;
1627
1683
  for (const [key, value] of aliasEntries) {
1628
1684
  if (baseSpec === key || baseSpec.startsWith(key + "/")) {
1629
- const aliasBase = resolveAliasTarget(value, root);
1685
+ const aliasBase = resolveAliasTarget2(value, root);
1630
1686
  const sub = baseSpec.slice(key.length).replace(/^\//, "");
1631
1687
  const target = sub ? import_node_path10.default.join(aliasBase, sub) : aliasBase;
1632
1688
  const resolved = tryResolveDiskPath(target);
@@ -1657,7 +1713,7 @@ function rewriteImports(code, config, filePath) {
1657
1713
  (_m, q, s) => `import(${q}${transformSpec(s)}${q})`
1658
1714
  );
1659
1715
  }
1660
- function resolveAliasTarget(value, root) {
1716
+ function resolveAliasTarget2(value, root) {
1661
1717
  if (import_node_path10.default.isAbsolute(value) && import_node_fs8.default.existsSync(value)) return value;
1662
1718
  if (value.startsWith("/")) return import_node_path10.default.join(root, value.slice(1));
1663
1719
  return import_node_path10.default.resolve(root, value);
@@ -2081,7 +2137,7 @@ async function createServer(inlineConfig = {}) {
2081
2137
  const localUrl = `http://localhost:${actualPort}`;
2082
2138
  const networkUrl = host === "0.0.0.0" ? `http://${getNetworkAddress()}:${actualPort}` : null;
2083
2139
  console.log();
2084
- console.log(import_picocolors3.default.cyan(" nasti dev server") + import_picocolors3.default.dim(` v${"1.6.4"}`));
2140
+ console.log(import_picocolors3.default.cyan(" nasti dev server") + import_picocolors3.default.dim(` v${"1.7.0"}`));
2085
2141
  console.log();
2086
2142
  console.log(` ${import_picocolors3.default.green(">")} Local: ${import_picocolors3.default.cyan(localUrl)}`);
2087
2143
  if (networkUrl) {
@@ -2225,7 +2281,7 @@ async function buildElectron(inlineConfig = {}) {
2225
2281
  const config = await resolveConfig({ ...inlineConfig, target: "electron" }, "build");
2226
2282
  const startTime = performance.now();
2227
2283
  assertElectronVersion(config);
2228
- console.log(import_picocolors2.default.cyan("\n\u26A1 nasti build (electron)") + import_picocolors2.default.dim(` v${"1.6.4"}`));
2284
+ console.log(import_picocolors2.default.cyan("\n\u26A1 nasti build (electron)") + import_picocolors2.default.dim(` v${"1.7.0"}`));
2229
2285
  console.log(import_picocolors2.default.dim(` root: ${config.root}`));
2230
2286
  console.log(import_picocolors2.default.dim(` mode: ${config.mode}`));
2231
2287
  console.log(import_picocolors2.default.dim(` target: electron (\u2265 ${config.electron.minVersion})`));
@@ -2303,19 +2359,23 @@ async function bundleNode(config, entry, opts) {
2303
2359
  return { code: result.code, map: result.map ? JSON.parse(result.map) : void 0 };
2304
2360
  }
2305
2361
  };
2362
+ const { output: userOutput, transform: userTransform, ...restInputOptions } = config.build.rolldownOptions;
2363
+ const mergedDefine = { ...userTransform?.define ?? {}, ...envDefine };
2306
2364
  const bundle = await (0, import_rolldown2.rolldown)({
2365
+ ...restInputOptions,
2307
2366
  input: entry,
2308
- define: envDefine,
2309
2367
  platform: "node",
2310
- plugins: [oxcTransformPlugin, electronPlugin(config), resolvePlugin(config)],
2311
- ...config.build.rolldownOptions
2368
+ transform: { ...userTransform, define: mergedDefine },
2369
+ plugins: [oxcTransformPlugin, electronPlugin(config), resolvePlugin(config)]
2312
2370
  });
2313
2371
  import_node_fs7.default.mkdirSync(import_node_path9.default.dirname(opts.outFile), { recursive: true });
2314
2372
  await bundle.write({
2315
- file: opts.outFile,
2316
- format: opts.format === "cjs" ? "cjs" : "esm",
2317
2373
  sourcemap: !!config.build.sourcemap,
2318
2374
  minify: !!config.build.minify,
2375
+ // 允许用户微调 output;但主进程 / preload 的单文件约束由下方键强制保证
2376
+ ...userOutput,
2377
+ file: opts.outFile,
2378
+ format: opts.format === "cjs" ? "cjs" : "esm",
2319
2379
  codeSplitting: false
2320
2380
  });
2321
2381
  await bundle.close();
@@ -2372,7 +2432,7 @@ async function startElectronDev(inlineConfig = {}) {
2372
2432
  const { noSpawn, ...rest } = inlineConfig;
2373
2433
  const config = await resolveConfig({ ...rest, target: "electron" }, "serve");
2374
2434
  warnElectronVersion(config);
2375
- console.log(import_picocolors4.default.cyan("\n\u26A1 nasti electron dev") + import_picocolors4.default.dim(` v${"1.6.4"}`));
2435
+ console.log(import_picocolors4.default.cyan("\n\u26A1 nasti electron dev") + import_picocolors4.default.dim(` v${"1.7.0"}`));
2376
2436
  const { createServer: createServer2 } = await Promise.resolve().then(() => (init_server(), server_exports));
2377
2437
  const server = await createServer2({ ...rest, target: "electron" });
2378
2438
  await server.listen();