Skip to content

feat(recipe): add customization of build file names recipe #11678

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

casungo
Copy link
Contributor

@casungo casungo commented May 14, 2025

Description (required)

This pull request introduces a new recipe page to the Astro documentation titled "Customizing Output File Names."

This recipe addresses the need for users to have more control over the naming and directory structure of their built assets (JavaScript, CSS, images, fonts, etc.). It explains how to leverage Vite's Rollup output options within the astro.config.mjs file to customize entryFileNames, chunkFileNames, and assetFileNames.

This content was previously part of the v4 documentation but was removed from the main configuration page as it's considered a more advanced use case. Creating it as a recipe makes this valuable information accessible to users who specifically need this customization, as suggested in issue #10348.

Related issues & labels (optional)

Copy link

netlify bot commented May 14, 2025

Deploy Preview for astro-docs-2 ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit c1e9bcc
🔍 Latest deploy log https://app.netlify.com/projects/astro-docs-2/deploys/682504f53ea6680008045ff5
😎 Deploy Preview https://deploy-preview-11678--astro-docs-2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@astrobot-houston
Copy link
Contributor

Lunaria Status Overview

🌕 This pull request will trigger status changes.

Learn more

By default, every PR changing files present in the Lunaria configuration's files property will be considered and trigger status changes accordingly.

You can change this by adding one of the keywords present in the ignoreKeywords property in your Lunaria configuration file in the PR's title (ignoring all files) or by including a tracker directive in the merged commit's description.

Tracked Files

File Note
en/recipes/customizing-output-filenames.mdx Localization added, will be marked as complete.
Warnings reference
Icon Description
🔄️ The source for this localization has been updated since the creation of this pull request, make sure all changes in the source have been applied.

Comment on lines +74 to +114
:::tip[Advanced Asset Naming]
The `assetFileNames` option can also accept a function if you need more granular control, for example, to place different asset types into different subdirectories within `static/`:

```javascript title="astro.config.mjs" ins={11-29}
// ...
vite: {
build: {
rollupOptions: {
output: {
// For JavaScript entry files - This was likely fine
entryFileNames: "js/[name]-[hash].js",
// For JavaScript code-split chunks - This was likely fine
chunkFileNames: "js/chunks/[name]-[hash].js",
// For other assets like CSS, images, fonts
assetFileNames: (assetInfo) => {
const sourceFileName =
assetInfo.originalFileNames?.[0] || assetInfo.names?.[0];
if (sourceFileName) {
const lowerSourceFileName = sourceFileName.toLowerCase();
// Check for common image types
if (
/\.(png|jpe?g|gif|svg|webp|avif|ico)$/.test(lowerSourceFileName)
) {
return `img/[name]-[hash][extname]`;
}
// Check for font files
if (/\.(woff2?|eot|ttf|otf)$/.test(lowerSourceFileName)) {
return `fonts/[name]-[hash][extname]`;
}
}
// Fallback for any other asset types, or if sourceFileName couldn't be determined
return `static_assets/[name]-[hash][extname]`;
},
},
},
},
},
// ...
```
This example configuration will output images to `img/`, fonts to `fonts/` and any other assets to `static_assets/`.
:::
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed this great new recipe (thanks for the contribution 🙌) today during Talking and Doc'ing, and wanted to share one small point:

I personally feel like we are already in an advanced use-case recipe and the aside covers an even more advanced use-case. I think the link just above this aside should cover us for now when it comes to the advanced use-cases:

For a full list of available placeholders and advanced pattern options, refer to

I think ending this step with that link could be sufficient for now and people can explore the Rollup documentation if they have a need for it.

Does anybody have any thoughts on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Changing the output file names is missing in English
3 participants