Skip to content

Commit

Permalink
Include nested images (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
timriley authored Jan 8, 2024
1 parent f8c8f51 commit ffd89ef
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
10 changes: 7 additions & 3 deletions dist/esbuild-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,23 @@ const hanamiEsbuild = (options = { ...defaults }) => {
};
const processAssetDirectory = (pattern, inputs, options) => {
const dirPath = path.dirname(pattern);
const files = fs.readdirSync(dirPath);
const files = fs.readdirSync(dirPath, { recursive: true });
const assets = [];
files.forEach((file) => {
const srcPath = path.join(dirPath, file);
const srcPath = path.join(dirPath, file.toString());
// Skip if the file is already processed by esbuild
if (inputs.hasOwnProperty(srcPath)) {
return;
}
// Skip directories and any other non-files
if (!fs.statSync(srcPath).isFile()) {
return;
}
const fileHash = calculateHash(fs.readFileSync(srcPath), options.hash);
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 destPath = path.join(options.destDir, path.relative(dirPath, srcPath).replace(path.basename(file.toString()), destFileName));
if (fs.lstatSync(srcPath).isDirectory()) {
assets.push(...processAssetDirectory(destPath, inputs, options));
}
Expand Down
11 changes: 8 additions & 3 deletions src/esbuild-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,25 +121,30 @@ const hanamiEsbuild = (options: PluginOptions = { ...defaults }): Plugin => {
options: PluginOptions,
): string[] => {
const dirPath = path.dirname(pattern);
const files = fs.readdirSync(dirPath);
const files = fs.readdirSync(dirPath, { recursive: true });
const assets: string[] = [];

files.forEach((file) => {
const srcPath = path.join(dirPath, file);
const srcPath = path.join(dirPath, file.toString());

// Skip if the file is already processed by esbuild
if (inputs.hasOwnProperty(srcPath)) {
return;
}

// Skip directories and any other non-files
if (!fs.statSync(srcPath).isFile()) {
return;
}

const fileHash = calculateHash(fs.readFileSync(srcPath), options.hash);
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),
path.relative(dirPath, srcPath).replace(path.basename(file.toString()), destFileName),
);

if (fs.lstatSync(srcPath).isDirectory()) {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions test/hanami-assets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ describe("hanami-assets", () => {
url: "/assets/logo-C1EF77E4.png",
sri: ["sha384-7q5x+ZjZrCoWwyV0BTyc8HUPf1xr+n9l77gwxmwywPWSe0PtopZj1T8NTUPFo0FI"],
},
"nested/image.jpg": {
url: "/assets/nested/image-83509E65.jpg",
sri: ["sha384-M7QyKTUfzyVWNC4FoMYq0ypu7LDifAYWEtXRT5d6M3Prpau9t5wavW1216HhvCJc"],
},
});
});

Expand Down

0 comments on commit ffd89ef

Please sign in to comment.