@nasti-toolchain/nasti 1.6.4 → 1.6.5

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.d.cts CHANGED
@@ -89,6 +89,13 @@ interface BuildConfig {
89
89
  target?: string | string[];
90
90
  rolldownOptions?: Record<string, unknown>;
91
91
  emptyOutDir?: boolean;
92
+ css?: CssConfig;
93
+ }
94
+ interface CssConfig {
95
+ /** CSP nonce to add to inline <style> tags */
96
+ nonce?: string;
97
+ /** Emit CSS as separate files instead of inline injection (CSP-friendly) */
98
+ emitCssFile?: boolean;
92
99
  }
93
100
  interface NastiPlugin {
94
101
  name: string;
@@ -143,6 +150,7 @@ type LoadResult = string | null | undefined | {
143
150
  type TransformResult = string | null | undefined | {
144
151
  code: string;
145
152
  map?: unknown;
153
+ moduleType?: string;
146
154
  };
147
155
  interface EmittedFile {
148
156
  type: 'asset' | 'chunk';
package/dist/index.d.ts CHANGED
@@ -89,6 +89,13 @@ interface BuildConfig {
89
89
  target?: string | string[];
90
90
  rolldownOptions?: Record<string, unknown>;
91
91
  emptyOutDir?: boolean;
92
+ css?: CssConfig;
93
+ }
94
+ interface CssConfig {
95
+ /** CSP nonce to add to inline <style> tags */
96
+ nonce?: string;
97
+ /** Emit CSS as separate files instead of inline injection (CSP-friendly) */
98
+ emitCssFile?: boolean;
92
99
  }
93
100
  interface NastiPlugin {
94
101
  name: string;
@@ -143,6 +150,7 @@ type LoadResult = string | null | undefined | {
143
150
  type TransformResult = string | null | undefined | {
144
151
  code: string;
145
152
  map?: unknown;
153
+ moduleType?: string;
146
154
  };
147
155
  interface EmittedFile {
148
156
  type: 'asset' | 'chunk';
package/dist/index.js CHANGED
@@ -35,7 +35,8 @@ var init_defaults = __esm({
35
35
  sourcemap: false,
36
36
  target: "es2022",
37
37
  rolldownOptions: {},
38
- emptyOutDir: true
38
+ emptyOutDir: true,
39
+ css: {}
39
40
  };
40
41
  defaultElectron = {
41
42
  main: "src/electron/main.ts",
@@ -282,20 +283,29 @@ import { createRequire } from "module";
282
283
  function resolvePlugin(config) {
283
284
  const { alias, extensions } = config.resolve;
284
285
  const require2 = createRequire(path2.resolve(config.root, "package.json"));
286
+ const aliasEntries = Object.entries(alias).sort(
287
+ ([a], [b]) => b.length - a.length
288
+ );
285
289
  return {
286
290
  name: "nasti:resolve",
287
291
  enforce: "pre",
288
292
  resolveId(source, importer) {
289
- for (const [key, value] of Object.entries(alias)) {
293
+ for (const [key, value] of aliasEntries) {
290
294
  if (source === key || source.startsWith(key + "/")) {
291
- source = source.replace(key, value);
292
- if (!path2.isAbsolute(source)) {
293
- source = path2.resolve(config.root, source);
294
- }
295
+ const aliasBase = resolveAliasTarget(value, config.root);
296
+ const sub = source.slice(key.length).replace(/^\//, "");
297
+ const target = sub ? path2.join(aliasBase, sub) : aliasBase;
298
+ const resolved = tryResolveFile(target, extensions);
299
+ if (resolved) return resolved;
295
300
  break;
296
301
  }
297
302
  }
298
- if (path2.isAbsolute(source)) {
303
+ if (source.startsWith("/") && !source.startsWith("//")) {
304
+ const rootRelative = path2.join(config.root, source.slice(1));
305
+ const resolved = tryResolveFile(rootRelative, extensions);
306
+ if (resolved) return resolved;
307
+ }
308
+ if (path2.isAbsolute(source) && fs2.existsSync(source)) {
299
309
  const resolved = tryResolveFile(source, extensions);
300
310
  if (resolved) return resolved;
301
311
  }
@@ -318,6 +328,7 @@ function resolvePlugin(config) {
318
328
  return null;
319
329
  },
320
330
  load(id) {
331
+ if (id.startsWith("\0")) return null;
321
332
  if (!fs2.existsSync(id)) return null;
322
333
  if (id.endsWith(".json")) {
323
334
  const content = fs2.readFileSync(id, "utf-8");
@@ -327,6 +338,11 @@ function resolvePlugin(config) {
327
338
  }
328
339
  };
329
340
  }
341
+ function resolveAliasTarget(value, root) {
342
+ if (path2.isAbsolute(value) && fs2.existsSync(value)) return value;
343
+ if (value.startsWith("/")) return path2.join(root, value.slice(1));
344
+ return path2.resolve(root, value);
345
+ }
330
346
  function tryResolveFile(file, extensions) {
331
347
  if (fs2.existsSync(file) && fs2.statSync(file).isFile()) {
332
348
  return file;
@@ -423,8 +439,8 @@ function cssPlugin(config) {
423
439
  cssSource = compiled.css;
424
440
  }
425
441
  const rewritten = rewriteCssUrls(cssSource, id, config.root);
442
+ const escaped = JSON.stringify(rewritten);
426
443
  if (config.command === "serve") {
427
- const escaped = JSON.stringify(rewritten);
428
444
  return {
429
445
  code: `
430
446
  const css = ${escaped};
@@ -448,7 +464,42 @@ export default css;
448
464
  `
449
465
  };
450
466
  }
451
- return rewritten !== code ? { code: rewritten } : null;
467
+ const cssConfig = config.build.css || {};
468
+ const nonce = cssConfig.nonce;
469
+ const emitCssFile = cssConfig.emitCssFile;
470
+ if (emitCssFile) {
471
+ const fileName = `assets/${path4.basename(id, ".css")}.css`;
472
+ this.emitFile({
473
+ type: "asset",
474
+ fileName,
475
+ source: rewritten
476
+ });
477
+ return {
478
+ code: `
479
+ const link = document.createElement('link');
480
+ link.rel = 'stylesheet';
481
+ link.href = ${JSON.stringify("/" + fileName)};
482
+ document.head.appendChild(link);
483
+
484
+ export default ${escaped};
485
+ `,
486
+ moduleType: "js"
487
+ };
488
+ }
489
+ const nonceAttr = nonce ? `style.setAttribute('nonce', ${JSON.stringify(nonce)});` : "";
490
+ return {
491
+ code: `
492
+ const css = ${escaped};
493
+ const style = document.createElement('style');
494
+ style.setAttribute('data-nasti-css', ${JSON.stringify(id)});
495
+ ${nonceAttr}
496
+ style.textContent = css;
497
+ document.head.appendChild(style);
498
+
499
+ export default css;
500
+ `,
501
+ moduleType: "js"
502
+ };
452
503
  }
453
504
  };
454
505
  }
@@ -852,7 +903,7 @@ import pc from "picocolors";
852
903
  async function build(inlineConfig = {}) {
853
904
  const config = await resolveConfig(inlineConfig, "build");
854
905
  const startTime = performance.now();
855
- console.log(pc.cyan("\n\u{1F528} nasti build") + pc.dim(` v${"1.6.4"}`));
906
+ console.log(pc.cyan("\n\u{1F528} nasti build") + pc.dim(` v${"1.6.5"}`));
856
907
  console.log(pc.dim(` root: ${config.root}`));
857
908
  console.log(pc.dim(` mode: ${config.mode}`));
858
909
  const outDir = path8.resolve(config.root, config.build.outDir);
@@ -906,9 +957,11 @@ async function build(inlineConfig = {}) {
906
957
  };
907
958
  const env = loadEnv(config.mode, config.root, config.envPrefix);
908
959
  const envDefine = buildEnvDefine(env, config.mode);
960
+ const existingTransform = config.build.rolldownOptions?.transform;
961
+ const mergedDefine = { ...existingTransform?.define ?? {}, ...envDefine };
909
962
  const bundle = await rolldown({
910
963
  input: entryPoints,
911
- define: envDefine,
964
+ transform: { ...existingTransform, define: mergedDefine },
912
965
  plugins: [
913
966
  oxcTransformPlugin,
914
967
  // 转换 Nasti 插件为 Rolldown 插件格式
@@ -1603,7 +1656,7 @@ function rewriteImports(code, config, filePath) {
1603
1656
  const baseSpec = suffix ? spec.slice(0, -suffix.length) : spec;
1604
1657
  for (const [key, value] of aliasEntries) {
1605
1658
  if (baseSpec === key || baseSpec.startsWith(key + "/")) {
1606
- const aliasBase = resolveAliasTarget(value, root);
1659
+ const aliasBase = resolveAliasTarget2(value, root);
1607
1660
  const sub = baseSpec.slice(key.length).replace(/^\//, "");
1608
1661
  const target = sub ? path10.join(aliasBase, sub) : aliasBase;
1609
1662
  const resolved = tryResolveDiskPath(target);
@@ -1634,7 +1687,7 @@ function rewriteImports(code, config, filePath) {
1634
1687
  (_m, q, s) => `import(${q}${transformSpec(s)}${q})`
1635
1688
  );
1636
1689
  }
1637
- function resolveAliasTarget(value, root) {
1690
+ function resolveAliasTarget2(value, root) {
1638
1691
  if (path10.isAbsolute(value) && fs8.existsSync(value)) return value;
1639
1692
  if (value.startsWith("/")) return path10.join(root, value.slice(1));
1640
1693
  return path10.resolve(root, value);
@@ -2059,7 +2112,7 @@ async function createServer(inlineConfig = {}) {
2059
2112
  const localUrl = `http://localhost:${actualPort}`;
2060
2113
  const networkUrl = host === "0.0.0.0" ? `http://${getNetworkAddress()}:${actualPort}` : null;
2061
2114
  console.log();
2062
- console.log(pc3.cyan(" nasti dev server") + pc3.dim(` v${"1.6.4"}`));
2115
+ console.log(pc3.cyan(" nasti dev server") + pc3.dim(` v${"1.6.5"}`));
2063
2116
  console.log();
2064
2117
  console.log(` ${pc3.green(">")} Local: ${pc3.cyan(localUrl)}`);
2065
2118
  if (networkUrl) {
@@ -2183,7 +2236,7 @@ async function buildElectron(inlineConfig = {}) {
2183
2236
  const config = await resolveConfig({ ...inlineConfig, target: "electron" }, "build");
2184
2237
  const startTime = performance.now();
2185
2238
  assertElectronVersion(config);
2186
- console.log(pc2.cyan("\n\u26A1 nasti build (electron)") + pc2.dim(` v${"1.6.4"}`));
2239
+ console.log(pc2.cyan("\n\u26A1 nasti build (electron)") + pc2.dim(` v${"1.6.5"}`));
2187
2240
  console.log(pc2.dim(` root: ${config.root}`));
2188
2241
  console.log(pc2.dim(` mode: ${config.mode}`));
2189
2242
  console.log(pc2.dim(` target: electron (\u2265 ${config.electron.minVersion})`));
@@ -2261,9 +2314,11 @@ async function bundleNode(config, entry, opts) {
2261
2314
  return { code: result.code, map: result.map ? JSON.parse(result.map) : void 0 };
2262
2315
  }
2263
2316
  };
2317
+ const existingTransform = config.build.rolldownOptions?.transform;
2318
+ const mergedDefine = { ...existingTransform?.define ?? {}, ...envDefine };
2264
2319
  const bundle = await rolldown2({
2265
2320
  input: entry,
2266
- define: envDefine,
2321
+ transform: { ...existingTransform, define: mergedDefine },
2267
2322
  platform: "node",
2268
2323
  plugins: [oxcTransformPlugin, electronPlugin(config), resolvePlugin(config)],
2269
2324
  ...config.build.rolldownOptions
@@ -2330,7 +2385,7 @@ async function startElectronDev(inlineConfig = {}) {
2330
2385
  const { noSpawn, ...rest } = inlineConfig;
2331
2386
  const config = await resolveConfig({ ...rest, target: "electron" }, "serve");
2332
2387
  warnElectronVersion(config);
2333
- console.log(pc4.cyan("\n\u26A1 nasti electron dev") + pc4.dim(` v${"1.6.4"}`));
2388
+ console.log(pc4.cyan("\n\u26A1 nasti electron dev") + pc4.dim(` v${"1.6.5"}`));
2334
2389
  const { createServer: createServer2 } = await Promise.resolve().then(() => (init_server(), server_exports));
2335
2390
  const server = await createServer2({ ...rest, target: "electron" });
2336
2391
  await server.listen();