From ded43c971eb94e2a597cbf19b67461a3f936c380 Mon Sep 17 00:00:00 2001 From: sapphi-red Date: Wed, 16 Aug 2023 21:27:21 +0900 Subject: [PATCH] feat(css): stop injecting `?used` --- .../src/node/plugins/importAnalysisBuild.ts | 25 +------------------ playground/css/__tests__/css.spec.ts | 6 +---- 2 files changed, 2 insertions(+), 29 deletions(-) diff --git a/packages/vite/src/node/plugins/importAnalysisBuild.ts b/packages/vite/src/node/plugins/importAnalysisBuild.ts index 1d046eb49f581e..b602de8c4c760d 100644 --- a/packages/vite/src/node/plugins/importAnalysisBuild.ts +++ b/packages/vite/src/node/plugins/importAnalysisBuild.ts @@ -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, @@ -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' /** @@ -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 ( diff --git a/playground/css/__tests__/css.spec.ts b/playground/css/__tests__/css.spec.ts index a08f50763d88f5..a33377295faf29 100644 --- a/playground/css/__tests__/css.spec.ts +++ b/playground/css/__tests__/css.spec.ts @@ -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 () => {