Skip to content

Commit

Permalink
Ensure static assets coming from slices have the slice name prefix in…
Browse files Browse the repository at this point in the history
… dest URL
  • Loading branch information
jodosha committed Nov 14, 2023
1 parent f8c8f51 commit fc88e1f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
16 changes: 15 additions & 1 deletion dist/esbuild-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ const hanamiEsbuild = (options = { ...defaults }) => {
build.onEnd(async (result) => {
const outputs = result.metafile?.outputs;
const assetsManifest = {};
const extractSliceName = (dirPath) => {
const regex = /^slices\/([^\/]+)/;
const match = dirPath.match(regex);
return match ? match[1] : null;
};
const calulateSourceUrl = (str) => {
return normalizeUrl(str)
.replace(/\/assets\//, "")
Expand All @@ -45,6 +50,9 @@ const hanamiEsbuild = (options = { ...defaults }) => {
const result = crypto.createHash("sha256").update(hashBytes).digest("hex");
return result.slice(0, 8).toUpperCase();
};
const compactArray = (arr) => {
return arr.filter((token) => token !== null);
};
function extractEsbuildInputs(inputData) {
const inputs = {};
for (const key in inputData) {
Expand Down Expand Up @@ -87,7 +95,13 @@ const hanamiEsbuild = (options = { ...defaults }) => {
const fileExtension = path.extname(srcPath);
const baseName = path.basename(srcPath, fileExtension);
const destFileName = [baseName, fileHash].filter((item) => item !== null).join("-") + fileExtension;
const destPath = path.join(options.destDir, path.relative(dirPath, srcPath).replace(file, destFileName));
const sliceName = extractSliceName(dirPath);
const pathTokens = compactArray([
options.destDir,
sliceName,
path.relative(dirPath, srcPath).replace(file, destFileName),
]);
const destPath = path.join(...pathTokens);
if (fs.lstatSync(srcPath).isDirectory()) {
assets.push(...processAssetDirectory(destPath, inputs, options));
}
Expand Down
17 changes: 15 additions & 2 deletions src/esbuild-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ const hanamiEsbuild = (options: PluginOptions = { ...defaults }): Plugin => {
const outputs = result.metafile?.outputs;
const assetsManifest: Record<string, Asset> = {};

const extractSliceName = (dirPath: string): string | null => {
const regex = /^slices\/([^\/]+)/;
const match = dirPath.match(regex);
return match ? match[1] : null;
}

const calulateSourceUrl = (str: string): string => {
return normalizeUrl(str)
.replace(/\/assets\//, "")
Expand Down Expand Up @@ -78,6 +84,10 @@ const hanamiEsbuild = (options: PluginOptions = { ...defaults }): Plugin => {
return result.slice(0, 8).toUpperCase();
};

const compactArray = (arr: Array<string | null>): Array<string> => {
return arr.filter((token): token is string => token !== null);
}

function extractEsbuildInputs(inputData: Record<string, any>): Record<string, boolean> {
const inputs: Record<string, boolean> = {};

Expand Down Expand Up @@ -137,10 +147,13 @@ const hanamiEsbuild = (options: PluginOptions = { ...defaults }): Plugin => {
const baseName = path.basename(srcPath, fileExtension);
const destFileName =
[baseName, fileHash].filter((item) => item !== null).join("-") + fileExtension;
const destPath = path.join(
const sliceName = extractSliceName(dirPath);
const pathTokens = compactArray([
options.destDir,
sliceName,
path.relative(dirPath, srcPath).replace(file, destFileName),
);
]);
const destPath = path.join(...pathTokens);

if (fs.lstatSync(srcPath).isDirectory()) {
assets.push(...processAssetDirectory(destPath, inputs, options));
Expand Down
Binary file added test/fixtures/todo/app/assets/images/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 10 additions & 6 deletions test/hanami-assets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ describe("hanami-assets", () => {
url: "/assets/background-UU2XY655.jpg",
sri: ["sha384-M7QyKTUfzyVWNC4FoMYq0ypu7LDifAYWEtXRT5d6M3Prpau9t5wavW1216HhvCJc"],
},
"font.otf": {
url: "/assets/font-E1A70B27.otf",
sri: ["sha384-Lpm/oUsCQkOg41WyENyyB1zjaX/FB522VWlU44JKakwzwBxvu11le0ILkiPsR73K"],
},
"logo.png": {
"url": "/assets/logo-C1EF77E4.png",
"sri": ["sha384-7q5x+ZjZrCoWwyV0BTyc8HUPf1xr+n9l77gwxmwywPWSe0PtopZj1T8NTUPFo0FI"],
},
"app.css": {
url: "/assets/app-4HPGUYGF.css",
sri: ["sha384-KsEObWWMvw+PouA5LgKpXohYpsOO4h9dL9pv7LwznkIg83/n1gkJo+S/oU/9Qb8Q"],
Expand All @@ -138,12 +146,8 @@ describe("hanami-assets", () => {
url: "/assets/admin/app-H646WNEB.js",
sri: ["sha384-noZH9am6sCla+CnG7l+IGxBlTqo68Wz891fhqfIF1U2kgafUrRzZewAt0yA6jl15"],
},
"font.otf": {
url: "/assets/font-E1A70B27.otf",
sri: ["sha384-Lpm/oUsCQkOg41WyENyyB1zjaX/FB522VWlU44JKakwzwBxvu11le0ILkiPsR73K"],
},
"logo.png": {
url: "/assets/logo-C1EF77E4.png",
"admin/logo.png": {
url: "/assets/admin/logo-C1EF77E4.png",
sri: ["sha384-7q5x+ZjZrCoWwyV0BTyc8HUPf1xr+n9l77gwxmwywPWSe0PtopZj1T8NTUPFo0FI"],
},
});
Expand Down

0 comments on commit fc88e1f

Please sign in to comment.