diff --git a/dist/esbuild-plugin.js b/dist/esbuild-plugin.js index 44209e3..e28de4b 100644 --- a/dist/esbuild-plugin.js +++ b/dist/esbuild-plugin.js @@ -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)); } diff --git a/src/esbuild-plugin.ts b/src/esbuild-plugin.ts index a59a427..0875313 100644 --- a/src/esbuild-plugin.ts +++ b/src/esbuild-plugin.ts @@ -121,17 +121,22 @@ 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); @@ -139,7 +144,7 @@ const hanamiEsbuild = (options: PluginOptions = { ...defaults }): Plugin => { [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()) { diff --git a/test/fixtures/todo/slices/admin/assets/images/nested/image.jpg b/test/fixtures/todo/slices/admin/assets/images/nested/image.jpg new file mode 100644 index 0000000..9340688 Binary files /dev/null and b/test/fixtures/todo/slices/admin/assets/images/nested/image.jpg differ diff --git a/test/hanami-assets.test.ts b/test/hanami-assets.test.ts index ce53203..b0ad447 100644 --- a/test/hanami-assets.test.ts +++ b/test/hanami-assets.test.ts @@ -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"], + }, }); });