@windwalker-io/fusion-next 0.1.4 → 0.1.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.ts CHANGED
@@ -72,6 +72,7 @@ export declare class ConfigBuilder {
72
72
  merge(override: UserConfig | ((config: UserConfig) => UserConfig)): this;
73
73
  private getDefaultOutput;
74
74
  private getChunkDir;
75
+ getAssetDefaultSubdir(assetInfo: PreRenderedAsset): string;
75
76
  private getChunkNameFromTask;
76
77
  ensurePath(path: string, def?: any): this;
77
78
  get(path: string): any;
@@ -295,6 +296,7 @@ declare type RunnerCliOptions = {
295
296
  v?: number;
296
297
  verbose?: number;
297
298
  serverFile?: string;
299
+ pidFile?: string;
298
300
  s?: string;
299
301
  hmr?: boolean;
300
302
  };
package/dist/index.js CHANGED
@@ -5,7 +5,7 @@ import { basename, parse, resolve, normalize, dirname, relative, isAbsolute } fr
5
5
  import chalk from 'chalk';
6
6
  import require$$0 from 'util';
7
7
  import require$$0$1 from 'path';
8
- import fs$1, { writeFileSync, existsSync } from 'node:fs';
8
+ import fs$1, { writeFileSync, existsSync, readFileSync, unlinkSync } from 'node:fs';
9
9
  import { mergeConfig } from 'vite';
10
10
  import { inspect } from 'node:util';
11
11
  import yargs from 'yargs';
@@ -6207,18 +6207,20 @@ class ConfigBuilder {
6207
6207
  }
6208
6208
  const chunkDir = this.getChunkDir();
6209
6209
  if (this.env.mode === "production" && this.fusionOptions.chunkNameObfuscation) {
6210
- return `${chunkDir}${serial}.js`;
6210
+ return `${chunkDir}${serial}-[hash].js`;
6211
6211
  }
6212
6212
  return `${chunkDir}[name]-[hash].js`;
6213
6213
  },
6214
6214
  assetFileNames: (assetInfo) => {
6215
+ const chunkDir = this.getChunkDir();
6215
6216
  for (const assetFileNamesCallback of this.assetFileNamesCallbacks) {
6216
6217
  const name = assetFileNamesCallback(assetInfo);
6217
6218
  if (name) {
6218
6219
  return name;
6219
6220
  }
6220
6221
  }
6221
- return "[name].[ext]";
6222
+ const dir = this.getAssetDefaultSubdir(assetInfo);
6223
+ return `${chunkDir}${dir}[name]-[hash].[ext]`;
6222
6224
  }
6223
6225
  };
6224
6226
  }
@@ -6233,6 +6235,19 @@ class ConfigBuilder {
6233
6235
  }
6234
6236
  return chunkDir;
6235
6237
  }
6238
+ getAssetDefaultSubdir(assetInfo) {
6239
+ let dir = "";
6240
+ if (assetInfo.name && assetInfo.name.endsWith(".css")) {
6241
+ dir = "css/";
6242
+ }
6243
+ if (assetInfo.name && /\.(woff2?|ttf|otf|eot)$/.test(assetInfo.name)) {
6244
+ dir = "fonts/";
6245
+ }
6246
+ if (assetInfo.name && /\.(png|jpe?g|gif|svg|webp)$/.test(assetInfo.name)) {
6247
+ dir = "images/";
6248
+ }
6249
+ return dir;
6250
+ }
6236
6251
  getChunkNameFromTask(chunkInfo) {
6237
6252
  if (this.tasks.has(chunkInfo.name)) {
6238
6253
  const output = this.tasks.get(chunkInfo.name)?.output;
@@ -8837,6 +8852,7 @@ async function resolveTaskAsFlat(name, task, cache) {
8837
8852
 
8838
8853
  let params = parseArgv(getArgsAfterDoubleDashes(process.argv));
8839
8854
  prepareParams(params);
8855
+ let serverRunning = false;
8840
8856
  let builder;
8841
8857
  const originalTasks = params._;
8842
8858
  const extraVitePlugins = [];
@@ -8928,21 +8944,54 @@ function useFusion(fusionOptions = {}, tasks) {
8928
8944
  server.httpServer?.once("listening", async () => {
8929
8945
  const scheme = server.config.server.https ? "https" : "http";
8930
8946
  const address = server.httpServer?.address();
8931
- const host = address && typeof address !== "string" ? address.address : "localhost";
8947
+ let host = address && typeof address !== "string" ? address.address : "localhost";
8932
8948
  const port = address && typeof address !== "string" ? address.port : 80;
8949
+ if (host === "::1") {
8950
+ host = `[${host}]`;
8951
+ }
8933
8952
  const url = `${scheme}://${host}:${port}/`;
8934
8953
  const serverFile = resolve(
8935
8954
  server.config.root,
8936
8955
  resolvedOptions.cliParams?.serverFile ?? "tmp/vite-server"
8937
8956
  );
8938
8957
  const serverFileFull = resolve(server.config.root, serverFile);
8939
- if (existsSync(serverFileFull)) {
8958
+ const pidFile = resolve(
8959
+ server.config.root,
8960
+ resolvedOptions.cliParams?.pidFile ?? "tmp/vite-pid"
8961
+ );
8962
+ const anotherServerRunning = existsSync(serverFileFull) || existsSync(pidFile);
8963
+ await sleep(500);
8964
+ if (!serverRunning && anotherServerRunning) {
8940
8965
  console.log(chalk.yellow(`There may be a dev server running!`));
8941
- console.log(`The server host file exists: ${chalk.cyan(serverFile)}`);
8942
- console.log(`If you want to start a new server, you need to remove this file first.`);
8943
- process.exit(1);
8966
+ console.log(`The server host file: ${chalk.cyan(serverFile)} or PID file: ${chalk.cyan(pidFile)} exists.`);
8967
+ console.log("Do you want to kill other process and start a new server? [N/y]");
8968
+ const answer = await new Promise((resolve2) => {
8969
+ process.stdin.once("data", (data) => {
8970
+ resolve2(data.toString().trim());
8971
+ });
8972
+ });
8973
+ if (answer.toLowerCase() === "y") {
8974
+ if (existsSync(pidFile)) {
8975
+ const pid = readFileSync(pidFile, "utf-8");
8976
+ try {
8977
+ process.kill(parseInt(pid), "SIGTERM");
8978
+ console.log(`Killed process with PID: ${chalk.yellow(pid)}`);
8979
+ } catch (err) {
8980
+ console.log(`Failed to kill process with PID: ${chalk.yellow(pid)}. It may have already exited.`);
8981
+ }
8982
+ }
8983
+ unlinkSync(serverFileFull);
8984
+ unlinkSync(pidFile);
8985
+ console.log(`Start running new server on: ${chalk.green(url)} and PID: ${chalk.green(process.pid)}`);
8986
+ } else {
8987
+ console.log(chalk.yellow("Aborting server start."));
8988
+ process.exit(0);
8989
+ }
8944
8990
  }
8991
+ serverRunning = true;
8992
+ await sleep(300);
8945
8993
  writeFileSync(serverFileFull, url);
8994
+ writeFileSync(pidFile, process.pid.toString());
8946
8995
  if (!exitHandlersBound) {
8947
8996
  process.on("exit", () => {
8948
8997
  for (const callback of builder.serverStopCallbacks) {
@@ -8951,6 +9000,9 @@ function useFusion(fusionOptions = {}, tasks) {
8951
9000
  if (fs$1.existsSync(serverFile)) {
8952
9001
  fs$1.rmSync(serverFile);
8953
9002
  }
9003
+ if (fs$1.existsSync(pidFile)) {
9004
+ fs$1.rmSync(pidFile);
9005
+ }
8954
9006
  });
8955
9007
  process.on("SIGINT", () => process.exit());
8956
9008
  process.on("SIGTERM", () => process.exit());
@@ -9165,6 +9217,9 @@ function fullReloads(...paths) {
9165
9217
  builder.watches.push(...paths);
9166
9218
  builder.watches = uniq(builder.watches);
9167
9219
  }
9220
+ function sleep(ms) {
9221
+ return new Promise((resolve2) => setTimeout(() => resolve2(), ms));
9222
+ }
9168
9223
  const index = {
9169
9224
  ...fusion,
9170
9225
  useFusion,