Skip to content

Commit

Permalink
feat(css): stop injecting ?used
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Aug 17, 2023
1 parent d87a047 commit ded43c9
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 29 deletions.
25 changes: 1 addition & 24 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
Expand Up @@ -7,7 +7,6 @@ import colors from 'picocolors'
import type { RawSourceMap } from '@ampproject/remapping'
import convertSourceMap from 'convert-source-map'
import {
bareImportRE,
cleanUrl,
combineSourcemaps,
generateCodeFrame,
Expand All @@ -23,8 +22,7 @@ import type { ResolvedConfig } from '../config'
import { toOutputFilePathInJS } from '../build'
import { genSourceMapUrl } from '../server/sourcemap'
import { getDepsOptimizer, optimizedDepNeedsInterop } from '../optimizer'
import { SPECIAL_QUERY_RE } from '../constants'
import { isCSSRequest, removedPureCssFilesCache } from './css'
import { removedPureCssFilesCache } from './css'
import { interopNamedImports } from './importAnalysis'

/**
Expand Down Expand Up @@ -379,27 +377,6 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
}
}
}

// Differentiate CSS imports that use the default export from those that
// do not by injecting a ?used query - this allows us to avoid including
// the CSS string when unnecessary (esbuild has trouble tree-shaking
// them)
if (
specifier &&
isCSSRequest(specifier) &&
// always inject ?used query when it is a dynamic import
// 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(&|$)/) &&
// don't append ?used when SPECIAL_QUERY_RE exists
!specifier.match(SPECIAL_QUERY_RE) &&
// edge case for package names ending with .css (e.g normalize.css)
!(bareImportRE.test(specifier) && !specifier.includes('/'))
) {
const url = specifier.replace(/\?|$/, (m) => `?used${m ? '&' : ''}`)
str().update(start, end, isDynamicImport ? `'${url}'` : url)
}
}

if (
Expand Down
6 changes: 1 addition & 5 deletions playground/css/__tests__/css.spec.ts
Expand Up @@ -452,11 +452,7 @@ test("relative path rewritten in Less's data-uri", async () => {
test('PostCSS source.input.from includes query', async () => {
const code = await page.textContent('.postcss-source-input')
// should resolve assets
expect(code).toContain(
isBuild
? '/postcss-source-input.css?used&inline&query=foo'
: '/postcss-source-input.css?inline&query=foo',
)
expect(code).toContain('/postcss-source-input.css?inline&query=foo')
})

test('aliased css has content', async () => {
Expand Down

0 comments on commit ded43c9

Please sign in to comment.