Skip to content

Commit

Permalink
35aba35 feat: manage assetsDir (#6588)
Browse files Browse the repository at this point in the history
  • Loading branch information
gioboa committed Jun 25, 2024
1 parent 8effe19 commit 925fb84
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 43 deletions.
7 changes: 4 additions & 3 deletions adapters/cloudflare-pages/vite/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,15 @@ function cloudflarePagesAdapter(opts = {}) {
publicDir: false
};
},
async generate({ clientOutDir, serverOutDir, basePathname }) {
async generate({ clientOutDir, serverOutDir, basePathname, assetsDir }) {
const routesJsonPath = (0, import_node_path.join)(clientOutDir, "_routes.json");
const hasRoutesJson = import_node_fs.default.existsSync(routesJsonPath);
if (!hasRoutesJson && opts.functionRoutes !== false) {
const pathName = assetsDir ? (0, import_node_path.join)(basePathname, assetsDir) : basePathname;
const routesJson = {
version: 1,
include: [basePathname + "*"],
exclude: [basePathname + "build/*", basePathname + "assets/*"]
include: [pathName + "*"],
exclude: [pathName + "build/*", pathName + "assets/*"]
};
await import_node_fs.default.promises.writeFile(routesJsonPath, JSON.stringify(routesJson, void 0, 2));
}
Expand Down
7 changes: 4 additions & 3 deletions adapters/cloudflare-pages/vite/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,15 @@ function cloudflarePagesAdapter(opts = {}) {
publicDir: false
};
},
async generate({ clientOutDir, serverOutDir, basePathname }) {
async generate({ clientOutDir, serverOutDir, basePathname, assetsDir }) {
const routesJsonPath = join(clientOutDir, "_routes.json");
const hasRoutesJson = fs.existsSync(routesJsonPath);
if (!hasRoutesJson && opts.functionRoutes !== false) {
const pathName = assetsDir ? join(basePathname, assetsDir) : basePathname;
const routesJson = {
version: 1,
include: [basePathname + "*"],
exclude: [basePathname + "build/*", basePathname + "assets/*"]
include: [pathName + "*"],
exclude: [pathName + "build/*", pathName + "assets/*"]
};
await fs.promises.writeFile(routesJsonPath, JSON.stringify(routesJson, void 0, 2));
}
Expand Down
17 changes: 11 additions & 6 deletions adapters/shared/vite/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ var import_node_path2 = require("node:path");
var import_node_fs = __toESM(require("node:fs"), 1);
var import_node_path = require("node:path");
var import_request_handler = require("../../../middleware/request-handler/index.cjs");
async function postBuild(clientOutDir, basePathname, userStaticPaths, format, cleanStatic) {
const ignorePathnames = /* @__PURE__ */ new Set([basePathname + "build/", basePathname + "assets/"]);
async function postBuild(clientOutDir, pathName, userStaticPaths, format, cleanStatic) {
if (pathName && !pathName.endsWith("/")) {
pathName += "/";
}
const ignorePathnames = /* @__PURE__ */ new Set([pathName + "build/", pathName + "assets/"]);
const staticPaths = new Set(userStaticPaths.map(normalizeTrailingSlash));
const notFounds = [];
const loadItem = async (fsDir, fsName, pathname) => {
Expand Down Expand Up @@ -78,10 +81,10 @@ async function postBuild(clientOutDir, basePathname, userStaticPaths, format, cl
await Promise.all(itemNames.map((i) => loadItem(fsDir, i, pathname)));
};
if (import_node_fs.default.existsSync(clientOutDir)) {
await loadDir(clientOutDir, basePathname);
await loadDir(clientOutDir, pathName);
}
const notFoundPathsCode = createNotFoundPathsModule(basePathname, notFounds, format);
const staticPathsCode = createStaticPathsModule(basePathname, staticPaths, format);
const notFoundPathsCode = createNotFoundPathsModule(pathName, notFounds, format);
const staticPathsCode = createStaticPathsModule(pathName, staticPaths, format);
return {
notFoundPathsCode,
staticPathsCode
Expand Down Expand Up @@ -264,6 +267,7 @@ function viteAdapter(opts) {
const basePathname = qwikCityPlugin.api.getBasePathname();
const clientOutDir = qwikVitePlugin.api.getClientOutDir();
const clientPublicOutDir = qwikVitePlugin.api.getClientPublicOutDir();
const assetsDir = qwikVitePlugin.api.getAssetsDir();
const rootDir = qwikVitePlugin.api.getRootDir() ?? void 0;
if (renderModulePath && qwikCityPlanModulePath && clientOutDir && clientPublicOutDir) {
let ssgOrigin = ((_a = opts.ssg) == null ? void 0 : _a.origin) ?? opts.origin;
Expand Down Expand Up @@ -306,7 +310,7 @@ function viteAdapter(opts) {
staticPaths.push(...staticGenerateResult.staticPaths);
const { staticPathsCode, notFoundPathsCode } = await postBuild(
clientPublicOutDir,
basePathname,
assetsDir ? (0, import_node_path2.join)(basePathname, assetsDir) : basePathname,
staticPaths,
format,
!!opts.cleanStaticGenerated
Expand All @@ -326,6 +330,7 @@ function viteAdapter(opts) {
clientPublicOutDir,
basePathname,
routes,
assetsDir,
warn: (message) => this.warn(message),
error: (message) => this.error(message)
});
Expand Down
1 change: 1 addition & 0 deletions adapters/shared/vite/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ declare interface ViteAdapterPluginOptions {
serverOutDir: string;
basePathname: string;
routes: BuildRoute[];
assetsDir?: string;
warn: (message: string) => void;
error: (message: string) => void;
}) => Promise<void>;
Expand Down
17 changes: 11 additions & 6 deletions adapters/shared/vite/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import { basename, dirname, join as join2, resolve } from "node:path";
import fs from "node:fs";
import { join } from "node:path";
import { getErrorHtml } from "../../../middleware/request-handler/index.mjs";
async function postBuild(clientOutDir, basePathname, userStaticPaths, format, cleanStatic) {
const ignorePathnames = /* @__PURE__ */ new Set([basePathname + "build/", basePathname + "assets/"]);
async function postBuild(clientOutDir, pathName, userStaticPaths, format, cleanStatic) {
if (pathName && !pathName.endsWith("/")) {
pathName += "/";
}
const ignorePathnames = /* @__PURE__ */ new Set([pathName + "build/", pathName + "assets/"]);
const staticPaths = new Set(userStaticPaths.map(normalizeTrailingSlash));
const notFounds = [];
const loadItem = async (fsDir, fsName, pathname) => {
Expand Down Expand Up @@ -39,10 +42,10 @@ async function postBuild(clientOutDir, basePathname, userStaticPaths, format, cl
await Promise.all(itemNames.map((i) => loadItem(fsDir, i, pathname)));
};
if (fs.existsSync(clientOutDir)) {
await loadDir(clientOutDir, basePathname);
await loadDir(clientOutDir, pathName);
}
const notFoundPathsCode = createNotFoundPathsModule(basePathname, notFounds, format);
const staticPathsCode = createStaticPathsModule(basePathname, staticPaths, format);
const notFoundPathsCode = createNotFoundPathsModule(pathName, notFounds, format);
const staticPathsCode = createStaticPathsModule(pathName, staticPaths, format);
return {
notFoundPathsCode,
staticPathsCode
Expand Down Expand Up @@ -225,6 +228,7 @@ function viteAdapter(opts) {
const basePathname = qwikCityPlugin.api.getBasePathname();
const clientOutDir = qwikVitePlugin.api.getClientOutDir();
const clientPublicOutDir = qwikVitePlugin.api.getClientPublicOutDir();
const assetsDir = qwikVitePlugin.api.getAssetsDir();
const rootDir = qwikVitePlugin.api.getRootDir() ?? void 0;
if (renderModulePath && qwikCityPlanModulePath && clientOutDir && clientPublicOutDir) {
let ssgOrigin = ((_a = opts.ssg) == null ? void 0 : _a.origin) ?? opts.origin;
Expand Down Expand Up @@ -267,7 +271,7 @@ function viteAdapter(opts) {
staticPaths.push(...staticGenerateResult.staticPaths);
const { staticPathsCode, notFoundPathsCode } = await postBuild(
clientPublicOutDir,
basePathname,
assetsDir ? join2(basePathname, assetsDir) : basePathname,
staticPaths,
format,
!!opts.cleanStaticGenerated
Expand All @@ -287,6 +291,7 @@ function viteAdapter(opts) {
clientPublicOutDir,
basePathname,
routes,
assetsDir,
warn: (message) => this.warn(message),
error: (message) => this.error(message)
});
Expand Down
17 changes: 11 additions & 6 deletions adapters/static/vite/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ var import_node_path2 = require("node:path");
var import_node_fs = __toESM(require("node:fs"), 1);
var import_node_path = require("node:path");
var import_request_handler = require("@builder.io/qwik-city/middleware/request-handler");
async function postBuild(clientOutDir, basePathname, userStaticPaths, format, cleanStatic) {
const ignorePathnames = /* @__PURE__ */ new Set([basePathname + "build/", basePathname + "assets/"]);
async function postBuild(clientOutDir, pathName, userStaticPaths, format, cleanStatic) {
if (pathName && !pathName.endsWith("/")) {
pathName += "/";
}
const ignorePathnames = /* @__PURE__ */ new Set([pathName + "build/", pathName + "assets/"]);
const staticPaths = new Set(userStaticPaths.map(normalizeTrailingSlash));
const notFounds = [];
const loadItem = async (fsDir, fsName, pathname) => {
Expand Down Expand Up @@ -75,10 +78,10 @@ async function postBuild(clientOutDir, basePathname, userStaticPaths, format, cl
await Promise.all(itemNames.map((i) => loadItem(fsDir, i, pathname)));
};
if (import_node_fs.default.existsSync(clientOutDir)) {
await loadDir(clientOutDir, basePathname);
await loadDir(clientOutDir, pathName);
}
const notFoundPathsCode = createNotFoundPathsModule(basePathname, notFounds, format);
const staticPathsCode = createStaticPathsModule(basePathname, staticPaths, format);
const notFoundPathsCode = createNotFoundPathsModule(pathName, notFounds, format);
const staticPathsCode = createStaticPathsModule(pathName, staticPaths, format);
return {
notFoundPathsCode,
staticPathsCode
Expand Down Expand Up @@ -261,6 +264,7 @@ function viteAdapter(opts) {
const basePathname = qwikCityPlugin.api.getBasePathname();
const clientOutDir = qwikVitePlugin.api.getClientOutDir();
const clientPublicOutDir = qwikVitePlugin.api.getClientPublicOutDir();
const assetsDir = qwikVitePlugin.api.getAssetsDir();
const rootDir = qwikVitePlugin.api.getRootDir() ?? void 0;
if (renderModulePath && qwikCityPlanModulePath && clientOutDir && clientPublicOutDir) {
let ssgOrigin = ((_a = opts.ssg) == null ? void 0 : _a.origin) ?? opts.origin;
Expand Down Expand Up @@ -303,7 +307,7 @@ function viteAdapter(opts) {
staticPaths.push(...staticGenerateResult.staticPaths);
const { staticPathsCode, notFoundPathsCode } = await postBuild(
clientPublicOutDir,
basePathname,
assetsDir ? (0, import_node_path2.join)(basePathname, assetsDir) : basePathname,
staticPaths,
format,
!!opts.cleanStaticGenerated
Expand All @@ -323,6 +327,7 @@ function viteAdapter(opts) {
clientPublicOutDir,
basePathname,
routes,
assetsDir,
warn: (message) => this.warn(message),
error: (message) => this.error(message)
});
Expand Down
17 changes: 11 additions & 6 deletions adapters/static/vite/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import { basename, dirname, join as join2, resolve } from "node:path";
import fs from "node:fs";
import { join } from "node:path";
import { getErrorHtml } from "@builder.io/qwik-city/middleware/request-handler";
async function postBuild(clientOutDir, basePathname, userStaticPaths, format, cleanStatic) {
const ignorePathnames = /* @__PURE__ */ new Set([basePathname + "build/", basePathname + "assets/"]);
async function postBuild(clientOutDir, pathName, userStaticPaths, format, cleanStatic) {
if (pathName && !pathName.endsWith("/")) {
pathName += "/";
}
const ignorePathnames = /* @__PURE__ */ new Set([pathName + "build/", pathName + "assets/"]);
const staticPaths = new Set(userStaticPaths.map(normalizeTrailingSlash));
const notFounds = [];
const loadItem = async (fsDir, fsName, pathname) => {
Expand Down Expand Up @@ -39,10 +42,10 @@ async function postBuild(clientOutDir, basePathname, userStaticPaths, format, cl
await Promise.all(itemNames.map((i) => loadItem(fsDir, i, pathname)));
};
if (fs.existsSync(clientOutDir)) {
await loadDir(clientOutDir, basePathname);
await loadDir(clientOutDir, pathName);
}
const notFoundPathsCode = createNotFoundPathsModule(basePathname, notFounds, format);
const staticPathsCode = createStaticPathsModule(basePathname, staticPaths, format);
const notFoundPathsCode = createNotFoundPathsModule(pathName, notFounds, format);
const staticPathsCode = createStaticPathsModule(pathName, staticPaths, format);
return {
notFoundPathsCode,
staticPathsCode
Expand Down Expand Up @@ -225,6 +228,7 @@ function viteAdapter(opts) {
const basePathname = qwikCityPlugin.api.getBasePathname();
const clientOutDir = qwikVitePlugin.api.getClientOutDir();
const clientPublicOutDir = qwikVitePlugin.api.getClientPublicOutDir();
const assetsDir = qwikVitePlugin.api.getAssetsDir();
const rootDir = qwikVitePlugin.api.getRootDir() ?? void 0;
if (renderModulePath && qwikCityPlanModulePath && clientOutDir && clientPublicOutDir) {
let ssgOrigin = ((_a = opts.ssg) == null ? void 0 : _a.origin) ?? opts.origin;
Expand Down Expand Up @@ -267,7 +271,7 @@ function viteAdapter(opts) {
staticPaths.push(...staticGenerateResult.staticPaths);
const { staticPathsCode, notFoundPathsCode } = await postBuild(
clientPublicOutDir,
basePathname,
assetsDir ? join2(basePathname, assetsDir) : basePathname,
staticPaths,
format,
!!opts.cleanStaticGenerated
Expand All @@ -287,6 +291,7 @@ function viteAdapter(opts) {
clientPublicOutDir,
basePathname,
routes,
assetsDir,
warn: (message) => this.warn(message),
error: (message) => this.error(message)
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@builder.io/qwik-city",
"description": "The meta-framework for Qwik.",
"version": "1.5.7-dev20240622232712",
"version": "1.5.7-dev20240625202021",
"bugs": "https://github.com/QwikDev/qwik/issues",
"dependencies": {
"@mdx-js/mdx": "^3.0.1",
Expand Down
16 changes: 10 additions & 6 deletions vite/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -26578,8 +26578,11 @@ navigator.serviceWorker?.getRegistrations().then((regs) => {
// packages/qwik-city/adapters/shared/vite/post-build.ts
var import_node_fs7 = __toESM(require("node:fs"), 1);
var import_node_path9 = require("node:path");
async function postBuild(clientOutDir, basePathname, userStaticPaths, format, cleanStatic) {
const ignorePathnames = /* @__PURE__ */ new Set([basePathname + "build/", basePathname + "assets/"]);
async function postBuild(clientOutDir, pathName, userStaticPaths, format, cleanStatic) {
if (pathName && !pathName.endsWith("/")) {
pathName += "/";
}
const ignorePathnames = /* @__PURE__ */ new Set([pathName + "build/", pathName + "assets/"]);
const staticPaths = new Set(userStaticPaths.map(normalizeTrailingSlash));
const notFounds = [];
const loadItem = async (fsDir, fsName, pathname) => {
Expand Down Expand Up @@ -26611,10 +26614,10 @@ async function postBuild(clientOutDir, basePathname, userStaticPaths, format, cl
await Promise.all(itemNames.map((i) => loadItem(fsDir, i, pathname)));
};
if (import_node_fs7.default.existsSync(clientOutDir)) {
await loadDir(clientOutDir, basePathname);
await loadDir(clientOutDir, pathName);
}
const notFoundPathsCode = createNotFoundPathsModule(basePathname, notFounds, format);
const staticPathsCode = createStaticPathsModule(basePathname, staticPaths, format);
const notFoundPathsCode = createNotFoundPathsModule(pathName, notFounds, format);
const staticPathsCode = createStaticPathsModule(pathName, staticPaths, format);
return {
notFoundPathsCode,
staticPathsCode
Expand Down Expand Up @@ -27111,9 +27114,10 @@ function qwikCityPlugin(userOpts) {
}
}
if (outDir && clientOutDir) {
const assetsDir = qwikPlugin.api.getAssetsDir();
const { staticPathsCode, notFoundPathsCode } = await postBuild(
clientOutDir,
api.getBasePathname(),
assetsDir ? (0, import_node_path11.join)(api.getBasePathname(), assetsDir) : api.getBasePathname(),
[],
ssrFormat,
false
Expand Down
16 changes: 10 additions & 6 deletions vite/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26573,8 +26573,11 @@ navigator.serviceWorker?.getRegistrations().then((regs) => {
// packages/qwik-city/adapters/shared/vite/post-build.ts
import fs6 from "node:fs";
import { join as join5 } from "node:path";
async function postBuild(clientOutDir, basePathname, userStaticPaths, format, cleanStatic) {
const ignorePathnames = /* @__PURE__ */ new Set([basePathname + "build/", basePathname + "assets/"]);
async function postBuild(clientOutDir, pathName, userStaticPaths, format, cleanStatic) {
if (pathName && !pathName.endsWith("/")) {
pathName += "/";
}
const ignorePathnames = /* @__PURE__ */ new Set([pathName + "build/", pathName + "assets/"]);
const staticPaths = new Set(userStaticPaths.map(normalizeTrailingSlash));
const notFounds = [];
const loadItem = async (fsDir, fsName, pathname) => {
Expand Down Expand Up @@ -26606,10 +26609,10 @@ async function postBuild(clientOutDir, basePathname, userStaticPaths, format, cl
await Promise.all(itemNames.map((i) => loadItem(fsDir, i, pathname)));
};
if (fs6.existsSync(clientOutDir)) {
await loadDir(clientOutDir, basePathname);
await loadDir(clientOutDir, pathName);
}
const notFoundPathsCode = createNotFoundPathsModule(basePathname, notFounds, format);
const staticPathsCode = createStaticPathsModule(basePathname, staticPaths, format);
const notFoundPathsCode = createNotFoundPathsModule(pathName, notFounds, format);
const staticPathsCode = createStaticPathsModule(pathName, staticPaths, format);
return {
notFoundPathsCode,
staticPathsCode
Expand Down Expand Up @@ -27106,9 +27109,10 @@ function qwikCityPlugin(userOpts) {
}
}
if (outDir && clientOutDir) {
const assetsDir = qwikPlugin.api.getAssetsDir();
const { staticPathsCode, notFoundPathsCode } = await postBuild(
clientOutDir,
api.getBasePathname(),
assetsDir ? join6(api.getBasePathname(), assetsDir) : api.getBasePathname(),
[],
ssrFormat,
false
Expand Down

0 comments on commit 925fb84

Please sign in to comment.