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

fix(css): remove ?used hack (fixes #6421, #8245) #8278

Merged
merged 2 commits into from May 22, 2022

Conversation

sapphi-red
Copy link
Member

@sapphi-red sapphi-red commented May 22, 2022

Description

This ?used hack was introduced by 3e3c203.

At that time, esbuild v0.12.25 was used. This version of esbuild had trouble with tree-shaking.
This is the reproduction with esbuild repl. You can see linked variable and imported variable exists in the output.

But now Vite uses esbuild v0.14.38 and tree-shake works. This is the reproduction with esbuild repl. You can see linked is removed from output. imported is still included because of new URL('---', import.meta.url).href.
So here it needs to be marked as pure.
esbuild 0.13.0+ supports /* #__PURE__ */ and it worked if I marked by using it (esbuild repl).
The same code worked successfully with terser too.

This PR removes the ?used hack and changes the css default export to export default /* #__PURE__ */ (() => ${JSON.stringify(content)})() which I showed above.
I also tested with this project (stackblitz).

commit index.js size tree-shake notes
67743a3 872B the parent commit of this PR
a49bd36 13984B because I removed ?used hack
197253e 872B because I added pure comment

fixes #6421
fixes #8245

Additional context


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.
  • 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.

@sapphi-red sapphi-red added feat: css p3-minor-bug An edge case that only affects very specific usage (priority) labels May 22, 2022
Copy link
Member

@patak-dev patak-dev left a comment

Choose a reason for hiding this comment

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

This is too awesome 🔥

@patak-dev patak-dev merged commit 0b25cc1 into vitejs:main May 22, 2022
@sapphi-red sapphi-red deleted the fix/remove-css-used-query branch May 23, 2022 00:07
@sapphi-red
Copy link
Member Author

sapphi-red commented May 23, 2022

This PR makes the output of import content from 'foo.css' to

const content = (() => "body{color:red}")()
console.log(content)

.

But this might hurt runtime perf if import content from 'foo.css' is used many places. https://v8.dev/blog/preparser#pife

const content = (function () { return "body{color:red}"})()
console.log(content)

This might be better for perf though the code is longer.

I'm not making a PR for this as I didn't measure the perf and it is an edge case but still leaving a note here.

sapphi-red added a commit to sapphi-red/vite that referenced this pull request Jun 5, 2022
This was referenced Jun 30, 2022
sapphi-red added a commit to sapphi-red/vite that referenced this pull request Jul 3, 2022
This reverts commit 0b25cc1.
@sapphi-red sapphi-red mentioned this pull request Jul 3, 2022
9 tasks
patak-dev pushed a commit that referenced this pull request Jul 3, 2022
This reverts commit 0b25cc1.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: css p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CSS Modules not included with import statment and resolve alias CSS is missed in transform hook
2 participants