-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(tools): #21 Remove origional images from the bundel after deploy
Our bundle was over 1.3gb due to all the high rez images being included in the build. They were not being used however. Due to some use cases Astro includes them by default, however Github's max deploy for pages is 1GB (with some flexibility). This promises to drasitcally reduce our bundle.
- Loading branch information
Showing
2 changed files
with
31 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import fs from "node:fs/promises"; | ||
import path from "node:path"; | ||
import type { AstroIntegration } from "astro"; | ||
|
||
// In Astro 4, unused images are removed from the build, but some original images may still remain in the _astro folder. | ||
const removeOriginalImages: AstroIntegration = { | ||
name: 'remove-original-images', | ||
hooks: { | ||
'astro:build:done': async ({ dir }) => { | ||
const astroDir = path.join(dir.pathname, `_astro/`); | ||
const files = await fs.readdir(astroDir); | ||
for (const file of files) { | ||
const { name, ext } = path.parse(file); | ||
const { ext: hashStr } = path.parse(name); | ||
if (!ext) continue; | ||
if (!hashStr) continue; | ||
if (![`.jpg`, `.jpeg`, `.png`, `.webp`].includes(ext)) continue; | ||
if (hashStr.includes(`_`)) continue; | ||
|
||
console.log(`Removing original image: ${file}`); | ||
await fs.unlink(path.join(astroDir, file)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
export default removeOriginalImages; | ||
|