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
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/importAnalysisBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.

// edge case for package names ending with .css (e.g normalize.css)
!(bareImportRE.test(specifier) && !specifier.includes('/'))
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
viteConfig,
} from '~utils'

const getBase = () => (viteConfig ? viteConfig?.testConfig?.baseRoute : '')

const absoluteAssetMatch = isBuild
? /http.*\/other-assets\/asset-\w{8}\.png/
: '/nested/asset.png'
Expand Down Expand Up @@ -138,7 +140,7 @@ describe('css url() references', () => {
describe.runIf(isBuild)('index.css URLs', () => {
let css: string
beforeAll(() => {
const base = viteConfig ? viteConfig?.testConfig?.baseRoute : ''
const base = getBase()
css = findAssetFile(/index.*\.css$/, base, 'other-assets')
})

Expand Down Expand Up @@ -200,6 +202,10 @@ test('?url import on css', async () => {
expect(txt).toMatch(
isBuild ? /http.*\/other-assets\/icons-\w{8}\.css/ : '/css/icons.css',
)
isBuild &&
expect(findAssetFile(/index.*\.js$/, getBase(), 'entries')).toMatch(
/icons-.+\.css(?!\?used)/,
)
})

test('new URL(..., import.meta.url)', async () => {
Expand Down