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

Conversation

DylanPiercey
Copy link
Contributor

@DylanPiercey DylanPiercey commented Apr 24, 2021

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?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • [ x Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@patak-dev
Copy link
Member

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?

@antfu antfu added the p2-nice-to-have Not breaking anything but nice to have (priority) label Apr 27, 2021
})
} 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants