Skip to content
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(vite): prevent outputting empty js files #3129

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
return {
code: modulesCode || `export default ${JSON.stringify(css)}`,
map: { mappings: '' },
meta: { css: true },
// avoid the css module from being tree-shaken so that we can retrieve
// it in renderChunk()
moduleSideEffects: 'no-treeshake'
Expand Down
28 changes: 23 additions & 5 deletions packages/vite/src/node/plugins/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,22 +344,40 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {

// inject chunk asset links
if (chunk) {
const assetTags = [
const assetTags = []

if (
chunk.imports.length ||
Object.entries(chunk.modules).some(
([id, mod]) =>
id !== 'vite/dynamic-import-polyfill' &&
mod.renderedLength &&
!this.getModuleInfo(id)?.meta.css
)
) {
// js entry chunk for this page
{
assetTags.push({
tag: 'script',
attrs: {
type: 'module',
crossorigin: true,
src: toPublicPath(chunk.fileName, config)
}
},
})
} else {
// entry js chunk was effectively empty, prevent rollup outputting it
delete bundle[chunk.fileName]
Copy link
Member

Choose a reason for hiding this comment

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

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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.

}

assetTags.push(
// preload for imports
...getPreloadLinksForChunk(chunk),
...getCssTagsForChunk(chunk)
]
)

result = injectToHead(result, assetTags)
if (assetTags.length) {
result = injectToHead(result, assetTags)
}
}

// inject css link when cssCodeSplit is false
Expand Down