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(build-import-analysis): should not append ?used when css request has ?url or ?raw #11910

Merged
merged 2 commits into from
Mar 1, 2023

Conversation

sun0day
Copy link
Member

@sun0day sun0day commented Feb 2, 2023

Description

fix #11893

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 PR Title 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.

@sun0day
Copy link
Member Author

sun0day commented Feb 15, 2023

Can you help review this PR @sapphi-red @bluwy

Thanks.

@sapphi-red
Copy link
Member

sapphi-red commented Feb 28, 2023

I think adding ?url/?raw check here would be better as we will remove ?used in Vite 5.

@sun0day sun0day changed the title fix(asset): hide internal css query param ?used fix(build-import-analysis): should not append ?used when css request has ?url or ?raw Feb 28, 2023
@sun0day
Copy link
Member Author

sun0day commented Feb 28, 2023

I think adding ?url/?raw check here would be better as we will remove ?used in Vite 5.

done

@sapphi-red sapphi-red added p3-minor-bug An edge case that only affects very specific usage (priority) feat: ssr labels Mar 1, 2023
@@ -361,7 +361,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
// because there is no way to check whether the default export is used
(source.slice(expStart, start).includes('from') || isDynamicImport) &&
// already has ?used query (by import.meta.glob)
!specifier.match(/\?used(&|$)/) &&
!specifier.match(/\?(used|raw|url)(&|$)/) &&
Copy link
Member

Choose a reason for hiding this comment

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

I think we can use SPECIAL_QUERY_RE to check the special queries, and leave !specifier.match(/\?used(&|$)/) as is.

PS: I also tried to implement the idea I mentioned in discord last week. It's tricky since dynamic imports of css makes it harder to process it, which reminds me that I think we haven't logged the warnings for dynamic imports of CSS yet.

Copy link
Member Author

Choose a reason for hiding this comment

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

What warnings?

Copy link
Member

Choose a reason for hiding this comment

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

We have some warnings at

colors.yellow(
`Default and named imports from CSS files are deprecated. ` +
`Use the ?inline query instead. ` +
`For example: ${newImport}`,
),
and
'Default import of CSS without `?inline` is deprecated. ' +
"Add the `{ query: '?inline' }` glob option to fix this.\n" +
`For example: \`import.meta.glob(${jsonStringifyInOneline(
globs.length === 1 ? globs[0] : globs,
)}, ${jsonStringifyInOneline({ ...options, query: '?inline' })})\``,
that discourages the use of default import of css, because that needs the ?used hack. I don't think it's related to this PR now though, and we can go in with this quick fix for now.

Copy link
Member Author

@sun0day sun0day Mar 1, 2023

Choose a reason for hiding this comment

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

Alright. Done with SPECIAL_QUERY_RE

Speaking of dynamic import CSS, is there an alternative?

Copy link
Member

Choose a reason for hiding this comment

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

Speaking of dynamic import CSS, is there an alternative?

import('./foo.css').then(mod => mod.default) should now be replaced with import('./foo.css?inline').then(mod => mod.default). Dynamic import without using default import (import('./foo.css')) is fine.

Copy link
Member

@sapphi-red sapphi-red left a comment

Choose a reason for hiding this comment

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

Thanks!

@bluwy bluwy merged commit e3f725f into vitejs:main Mar 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: ssr 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.

?used is appended to imported asset URLs when building
3 participants