feat(vite): prevent outputting empty js files#3129
feat(vite): prevent outputting empty js files#3129DylanPiercey wants to merge 1 commit intovitejs:mainfrom
Conversation
|
Nice! Looks good to me. @dominikg @GrygrFlzr could you check this is ok regarding SvelteKit? @IndexXuan this could also affect vite-plugin-mpa, maybe you could help testing this branch? |
| }) | ||
| } else { | ||
| // entry js chunk was effectively empty, prevent rollup outputting it | ||
| delete bundle[chunk.fileName] |
There was a problem hiding this comment.
It feels weird to be mutating the bundle in the HTML plugin - maybe this should be done somewhere else (a separate plugin?) so that the HTML plugin's logic remains intact.
There was a problem hiding this comment.
I could be wrong but I think that would make the implementation a bit more awkward, but probably doable. I think I'd need to extract https://github.com/vitejs/vite/pull/3129/files#diff-89bae1df62862bb7f4a03d82a1e9cbf4ac6d0c042f21fbbacb0a2238bd050042R350-R356 because it would have to be called in two places. Once here in the plugin, since the plugin still needs to know if it should output the script, and once in this other plugin.
The problem is we can't exactly delete the chunk from the bundle until after the html plugin has looked at it, since it still needs to analyze preload/stylesheet links for that chunk even if there is no js.
Description
This PR implements the suggestion from #3127 which is to prevent outputting empty js files in some cases.
It does this by analyzing the entry js chunk to determine if it contains any imports, any modules that have content (ignoring css files and vite/dynamic-import-polyfill). If there are no modules that have content, then the chunk is no longer written to disk, and the
<script>for the entry file is no longer inlined.Additional context
This is probably not in a mergeable state since I am having issues running the test suite on my development vm. It does however work when linked into the marko/vite project.I will work on getting the tests running properly.I was able to get the tests working, and seems all existing tests pass, also the CI is happy. However I'm not sure where would make the most sense in this codebase to add a test for this. Would appreciate some pointers 😄
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123).