-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
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(plugin-vue): make cssm code tree shakeable #6353
Conversation
could you add a test for this? |
Sure! Should I add it under Ideally this should be a unit test and not a e2e test involving browsers, but |
there's a small guide for adding tests https://github.com/vitejs/vite/blob/main/CONTRIBUTING.md |
please move the test to the playground directory and rollback the dependency downgrade |
Thanks! I found the test to not be appropriate under I also downgraded These two versions are basically just the same except for the export syntax, so I think it shouldn't be an issue.
Oops, I replied too late after the commit. Please see my reasoning above. |
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.
Ideally this should be a unit test and not a e2e test involving browsers, but
packages/plugin-vue
doesn't have its own__tests__
..
IMO, ideally, we need an E2E test, that checks :
- the styles that are not meant to be tree-shaked should still be applied;
if (isBuild)
, check the output CSS file, to see if the unused styles are still there.
Because what this PR really fixes is the tree-shakability of the output, not demanding a specific shape of the generated code. Snapshot tests are easy to write but hard to maintain.
You can refer to
vite/packages/playground/assets/__tests__/assets.spec.ts
Lines 187 to 196 in 65cd44d
test('from js import', async () => { | |
const src = readFile('テスト-測試-white space.js') | |
expect(await page.textContent('.unicode-url')).toMatch( | |
isBuild | |
? `data:application/javascript;base64,${Buffer.from(src).toString( | |
'base64' | |
)}` | |
: `/foo/テスト-測試-white space.js` | |
) | |
}) |
And we can avoid downgrading slash
if we use E2E tests instead.
Sorry if I didn't make the context clear enough. This intended for library mode only. Scenario: For a component library exporting
This is already ensured by all existing e2e tests (if they were tree-shaked, none of the styles could have been applied and thus tests fail)
This is probably not applicable to library mode. This fix is trying to tree shake unused components's
I will try to add a test for building a component library and then consuming that library, and check that the resulting bundle doesn't contain unused css modules code from the component library. This should be an e2e tests in the sense that the other end is developers consuming the package, not headless browsers. Some unrelated rant I really want to get off my chest. My sincere apologies.
For I totally understand and support the package author's push for ES module adoption, by changing the current package to use esm export. This way people installing the package will find that it doesn't work by default, do some googling and find out how to set up ESM for their projects. Awareness for ESM is raised. Great! Goal achieved - I now want to use ESM everywhere. But the fact is some critical infrastucture like The bottom line is we should not let one package affect how we write tests. If some tests can be written as unit tests, they just don't have use headless browsers. For Some error excerpts (from a fresh clone + fresh
I guess they fail because some race condition, but I don't want to waste a whole day trying to find out. For simple tests like testing a function output, or the bundling result, we should probably just stick to plain old unit tests that doesn't rely on playwright. For the port issue, I'm not sure why these some tests are using the same port. Maybe we migrate to get-port instead (the 5.1.1 version with cjs export, of course🤣)? |
Sorry for having misunderstood this PR. In that case, I understand that the E2E test might be not easy to write so I'm fine without a test.
I totally agree.
Sounds good! |
Description
Currently
plugin-vue
compiles sfc css modules to assignment expressions, which makes them unable to be tree shaken for a component library.Before:
After this PR:
Additional context
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).