@qwik.dev/router 2.0.0-rc.4 → 2.0.0-rc.6

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.
@@ -1,6 +1,6 @@
1
+ import { y as normalizePathSlash } from "../../../chunks/fs.mjs";
1
2
  import { t as ensureSlash } from "../../../chunks/pathname.mjs";
2
3
  import { viteAdapter } from "../../shared/vite/index.mjs";
3
- import { y as normalizePathSlash } from "../../../chunks/fs.mjs";
4
4
  import fs from "node:fs";
5
5
  import { join, relative } from "node:path";
6
6
  /** @public */
@@ -1,5 +1,5 @@
1
+ import { i as LOADER_REGEX } from "../../../chunks/request-path.mjs";
1
2
  import { t as ensureSlash } from "../../../chunks/pathname.mjs";
2
- import { r as LOADER_REGEX } from "../../../chunks/request-path.mjs";
3
3
  import fs from "node:fs";
4
4
  import { basename, dirname, join, resolve } from "node:path";
5
5
  import { spawn } from "node:child_process";
@@ -78,13 +78,16 @@ var QWIK_SSG_ENTRY_ID = "@qwik-ssg-entry";
78
78
  var QWIK_SSG_ENTRY_RESOLVED = "\0@qwik-ssg-entry";
79
79
  var QWIK_SSG_WORKER_ENTRY_ID = "@qwik-ssg-worker-entry";
80
80
  var QWIK_SSG_WORKER_ENTRY_RESOLVED = "\0@qwik-ssg-worker-entry";
81
+ var appBuilds = /* @__PURE__ */ new Map();
82
+ var appWatchEnvironments = /* @__PURE__ */ new WeakSet();
83
+ function getAppBuildKey(root, outDir) {
84
+ return JSON.stringify([resolve(root), resolve(root, outDir)]);
85
+ }
81
86
  /** @public */
82
87
  function viteAdapter(opts) {
83
88
  let qwikRouterPlugin = null;
84
89
  let qwikVitePlugin = null;
85
90
  let viteCommand;
86
- let buildAppRan = false;
87
- const outputEntries = [];
88
91
  const ssgOutDirFor = (root) => join(resolve(root), "node_modules", ".cache", `qwik-router-ssg-${process.pid}`);
89
92
  return [{
90
93
  name: `vite-plugin-qwik-router-ssg-${opts.name}`,
@@ -206,72 +209,86 @@ function viteAdapter(opts) {
206
209
  },
207
210
  generateBundle(_, bundles) {
208
211
  if (this.environment.name === "ssr") {
209
- outputEntries.length = 0;
210
- for (const fileName in bundles) {
211
- const chunk = bundles[fileName];
212
- if (chunk.type === "chunk" && chunk.isEntry) outputEntries.push(fileName);
212
+ const { root, build } = this.environment.config;
213
+ const appBuild = appBuilds.get(getAppBuildKey(root, build.outDir));
214
+ if (appBuild) {
215
+ appBuild.outputEntries.length = 0;
216
+ for (const fileName in bundles) {
217
+ const chunk = bundles[fileName];
218
+ if (chunk.type === "chunk" && chunk.isEntry) appBuild.outputEntries.push(fileName);
219
+ }
213
220
  }
214
- if (opts.ssg !== null && viteCommand === "build" && !buildAppRan) this.warn("Qwik Router SSG was skipped: the `ssr` environment was built directly instead of through the Vite app builder, so no pages were prerendered. Build with the Qwik CLI or `createBuilder(config).buildApp()` (a plain `vite build` does this automatically).");
221
+ const isAppWatch = !!build.watch && appWatchEnvironments.has(this.environment);
222
+ if (opts.ssg !== null && viteCommand === "build" && !appBuild && !isAppWatch) this.warn("Qwik Router SSG was skipped: the `ssr` environment was built directly instead of through the Vite app builder, so no pages were prerendered. Build with the Qwik CLI or `createBuilder(config).buildApp()` (a plain `vite build` does this automatically).");
215
223
  }
216
224
  },
217
225
  async buildApp(builder) {
218
- buildAppRan = true;
219
226
  const ssr = builder.environments.ssr;
220
- if (ssr) await builder.build(ssr);
221
- const serverOutDir = ssr?.config.build.outDir;
222
- if (!serverOutDir || !qwikRouterPlugin?.api || !qwikVitePlugin?.api) return;
223
- const log = builder.config.logger;
224
- const staticPaths = opts.staticPaths || [];
225
- const routes = qwikRouterPlugin.api.getRoutes();
226
- const basePathname = qwikRouterPlugin.api.getBasePathname();
227
- const clientOutDir = qwikVitePlugin.api.getClientOutDir();
228
- const clientPublicOutDir = qwikVitePlugin.api.getClientPublicOutDir();
229
- const noTrailingSlash = builder.config.define?.["globalThis.__NO_TRAILING_SLASH__"] === "true";
230
- const ssgEnv = builder.environments.ssg;
231
- const ssgOutDir = ssgOutDirFor(builder.config.root);
232
- if (ssgEnv && clientOutDir && clientPublicOutDir) {
233
- await builder.build(ssgEnv);
234
- const runSsgPath = join(ssgOutDir, "run-ssg.js");
235
- if (await new Promise((res, reject) => {
236
- const child = spawn(process.execPath, [runSsgPath], {
237
- stdio: [
238
- "ignore",
239
- "inherit",
240
- "inherit"
241
- ],
242
- env: {
243
- ...process.env,
244
- NODE_ENV: process.env.NODE_ENV || "production"
245
- }
227
+ if (!ssr) return;
228
+ const buildKey = getAppBuildKey(ssr.config.root, ssr.config.build.outDir);
229
+ if (appBuilds.has(buildKey)) throw new Error(`An app build is already running for ${buildKey}`);
230
+ const appBuild = { outputEntries: [] };
231
+ appBuilds.set(buildKey, appBuild);
232
+ try {
233
+ await builder.build(ssr);
234
+ if (ssr.config.build.watch) appWatchEnvironments.add(ssr);
235
+ const serverOutDir = ssr.config.build.outDir;
236
+ if (!serverOutDir || !qwikRouterPlugin?.api || !qwikVitePlugin?.api) return;
237
+ const log = builder.config.logger;
238
+ const staticPaths = opts.staticPaths || [];
239
+ const routes = qwikRouterPlugin.api.getRoutes();
240
+ const basePathname = qwikRouterPlugin.api.getBasePathname();
241
+ const clientOutDir = qwikVitePlugin.api.getClientOutDir();
242
+ const clientPublicOutDir = qwikVitePlugin.api.getClientPublicOutDir();
243
+ const noTrailingSlash = builder.config.define?.["globalThis.__NO_TRAILING_SLASH__"] === "true";
244
+ const ssgEnv = builder.environments.ssg;
245
+ const ssgOutDir = ssgOutDirFor(builder.config.root);
246
+ if (ssgEnv && clientOutDir && clientPublicOutDir) {
247
+ await builder.build(ssgEnv);
248
+ const runSsgPath = join(ssgOutDir, "run-ssg.js");
249
+ if (await new Promise((res, reject) => {
250
+ const child = spawn(process.execPath, [runSsgPath], {
251
+ stdio: [
252
+ "ignore",
253
+ "inherit",
254
+ "inherit"
255
+ ],
256
+ env: {
257
+ ...process.env,
258
+ NODE_ENV: process.env.NODE_ENV || "production"
259
+ }
260
+ });
261
+ child.on("close", res);
262
+ child.on("error", reject);
263
+ }) !== 0) throw new Error(`Error while running SSG from "${opts.name}" adapter. At least one path failed to render.`);
264
+ const staticPathsFile = join(clientPublicOutDir, "_static-paths.json");
265
+ try {
266
+ staticPaths.push(...JSON.parse(await readFile(staticPathsFile, "utf-8")));
267
+ await unlink(staticPathsFile);
268
+ } catch {}
269
+ if (process.env.QWIK_ROUTER_KEEP_SSG_BUILD) log.warn(`SSG build kept at ${ssgOutDir}`);
270
+ else await rm(ssgOutDir, {
271
+ recursive: true,
272
+ force: true
246
273
  });
247
- child.on("close", res);
248
- child.on("error", reject);
249
- }) !== 0) throw new Error(`Error while running SSG from "${opts.name}" adapter. At least one path failed to render.`);
250
- const staticPathsFile = join(clientPublicOutDir, "_static-paths.json");
251
- try {
252
- staticPaths.push(...JSON.parse(await readFile(staticPathsFile, "utf-8")));
253
- await unlink(staticPathsFile);
254
- } catch {}
255
- if (process.env.QWIK_ROUTER_KEEP_SSG_BUILD) log.warn(`SSG build kept at ${ssgOutDir}`);
256
- else await rm(ssgOutDir, {
257
- recursive: true,
258
- force: true
274
+ }
275
+ await postBuild(clientPublicOutDir, serverOutDir, basePathname, staticPaths, !!opts.cleanStaticGenerated, noTrailingSlash);
276
+ if (typeof opts.generate === "function") await opts.generate({
277
+ outputEntries: appBuild.outputEntries,
278
+ serverOutDir,
279
+ clientOutDir,
280
+ clientPublicOutDir,
281
+ basePathname,
282
+ routes,
283
+ warn: (message) => log.warn(message),
284
+ error: (message) => {
285
+ throw new Error(message);
286
+ }
259
287
  });
288
+ log.warn("\n==============================================\nNote: Make sure that you are serving the built files with proper cache headers.\nSee https://qwik.dev/docs/deployments/#cache-headers for more information.\n==============================================");
289
+ } finally {
290
+ appBuilds.delete(buildKey);
260
291
  }
261
- await postBuild(clientPublicOutDir, serverOutDir, basePathname, staticPaths, !!opts.cleanStaticGenerated, noTrailingSlash);
262
- if (typeof opts.generate === "function") await opts.generate({
263
- outputEntries,
264
- serverOutDir,
265
- clientOutDir,
266
- clientPublicOutDir,
267
- basePathname,
268
- routes,
269
- warn: (message) => log.warn(message),
270
- error: (message) => {
271
- throw new Error(message);
272
- }
273
- });
274
- log.warn("\n==============================================\nNote: Make sure that you are serving the built files with proper cache headers.\nSee https://qwik.dev/docs/deployments/#cache-headers for more information.\n==============================================");
275
292
  }
276
293
  }];
277
294
  }
package/lib/chunks/fs.mjs CHANGED
@@ -1,5 +1,5 @@
1
- import { i as normalizePathname, t as ensureSlash } from "./pathname.mjs";
2
1
  import { i as toTitleCase } from "./format.mjs";
2
+ import { i as normalizePathname, t as ensureSlash } from "./pathname.mjs";
3
3
  import { basename, dirname, normalize, relative } from "node:path";
4
4
  function parseRouteIndexName(extlessName) {
5
5
  let layoutName = "";