-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
feat: export helpers to allow 3rd parties to emit assets #9693
Conversation
* converts the source filepath of the asset to the output filename based on the assetFileNames option. \ | ||
* this function imitates the behavior of rollup.js. \ | ||
* converts the source filepath of the asset to the output filename based on the assetFileNames option. | ||
* this function imitates the behavior of rollup.js. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Linter was complaining about the trailing backslash
* @param assetFileNames filename pattern. e.g. `'assets/[name].[hash][extname]'` | ||
* @param file filepath of the asset | ||
* @param contentHash hash of the asset. used for `'[hash]'` placeholder | ||
* @param content content of the asset. passed to `assetFileNames` if `assetFileNames` is a function | ||
* @param assetFileNames - filename pattern. e.g. `'assets/[name].[hash][extname]'` | ||
* @param file - filepath of the asset | ||
* @param contentHash - hash of the asset. used for `'[hash]'` placeholder | ||
* @param content - content of the asset. passed to `assetFileNames` if `assetFileNames` is a function |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Linter was complaining about the missing dash
hey @ElMassimo, have you come up with an elegant way of handling this in Ruby land that I might have missed? |
@timacdonald Vite Ruby provides an
A little history: Until
but they were closed as
That's no longer true, as
As Vite became more used in backend integrations, the manifest became more important than initially anticipated, so it looks like we are circling back to #1765. From my point of view, it would be better if Vite processed any file provided in |
Thank you so much for this insight. I agree that a first party solution would be lovely (although I understand we are both a little bias for making things work with Bbck-end projects as well as possible hehe 😇) From what I can see, you have had to replicate all of the functionality that I'm proposing Vite export. If an end-user was to change the naming convention used in rollup.output.assetFileNames, your plugin would not currently respect it - or you'd have to re-implement it yourself. So I can see a benefit for Vite ruby in also having the proposed functions exported as well (again, assuming that there isn't a first party solution). Would you agree? |
I think the backend integrations ecosystem in Vite has changed enough in the past year that there is merit in having a rediscussion, more now that the manifest includes non-js assets (that was one of the reasons Evan wasn't onboard with the idea #1765 (comment)). Exposing all the needed helpers may be more problematic than providing a first-class solution. @ElMassimo @timacdonald we're going to set up a vitejs/rfcs repo to formalize the RFC process. I think an RFC could be a good way to get the different parties involved and propose this Vite feature. |
Agreed, exposing the helpers in the public API would make any internal change a potential breaking change.
Happy to chime in if @timacdonald likes the idea of opening an RFC for this, it would benefit all backend integrations. |
Hey folks, If that is how we wanna move forward - I'm more than happy to whip something up. Should I open a PR against https://github.com/vitejs/rfcs and are there any guidelines or anything? Should I just take a look at the Vue RFC stuff and try to replicate that in the Vite repo? |
Hey @timacdonald, Anthony has been working today to set up the vitejs/rfcs repo. We are going to use something similar to Vue, although with some simplifications. There are already docs there in the Readme that you can follow. I think what we are missing is some automation to auto-create a discussion after a PR when needed, but you can do it by hand for now. |
@timacdonald The RFC repo is ready, and you can follow the instruction on the repo readme. Looking forward to seeing your RFC! |
Closing as #10909 removed @timacdonald When {
/* ... */
async buildStart() {
resolveAssetsFromConfig().forEach(asset => {
const content = await fsp.readFile(
path.resolve(resolveConfig.root, asset)
)
this.emitFile({
type: 'asset',
source: content,
name: asset, // maybe this should be `path.basename(assets)` instead
})
}),
},
} |
Description
When using Vite as the build tool for server side language templated views, such as Laravel Blade (or PHP, Ruby, etc), it can be useful to inject assets into the build that are not referenced in JS or CSS "entry points".
Imagine the following contrived Blade template in an application that does not include any built JS or CSS:
Vite does not compile or know about this template, so Vite has no way of knowing about the two logos that the developer wants to bundle.
To allow applications that do not include build entry points to also utilise Vite as a build tool, I would like to propose that Vite export the suggested functions. Exporting these functions allows plugins (namely, the official Laravel plugin) to inject assets that are to be included in the build.
As an example of what this unlocks: in the Laravel plugin we could offer a new key for paths to include in the build:
The Laravel plugin may then internally use this
emitFile
function to add these to the build. Having access to these internal function allows us to ensure we stay inline with Vite naming conventions and also handle the naming as per the default or overridden config for filename ([hash], [name], etc) patterns.Why not just use the static copy plugin?
As shown in an earlier example, including these in the actual build allows vite plugins such as
viteImagemin
to "just work". Images included in the Laravel plugin then benefit from the same side-effects as if the assets were simply referenced in JS files.Why not just import into your JS
Our current solution is that we recommend people still have an
app.js
and import images with globing:However we feel this is really weird workaround for apps that literally don't have any JS in their app, thus we would love to be able to improve this experience for our plugin users.
Additional context
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).