From b9b925eb3f911ab63972124dc8ab0455449b925d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Mon, 28 Oct 2024 14:59:44 +0900 Subject: [PATCH] feat(css): add more stricter typing of lightningcss (#18460) --- eslint.config.js | 1 + packages/vite/src/node/index.ts | 2 +- packages/vite/src/node/plugins/css.ts | 6 ++--- packages/vite/src/types/lightningcss.d.ts | 23 ------------------- .../types/internal/lightningcssOptions.d.ts | 18 +++++++++++++++ 5 files changed, 22 insertions(+), 28 deletions(-) delete mode 100644 packages/vite/src/types/lightningcss.d.ts create mode 100644 packages/vite/types/internal/lightningcssOptions.d.ts diff --git a/eslint.config.js b/eslint.config.js index 69fffe169..968a7a89b 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -90,6 +90,7 @@ export default tseslint.config( 'less', 'sass', 'sass-embedded', + 'lightningcss', 'vitest', 'unbuild', ], diff --git a/packages/vite/src/node/index.ts b/packages/vite/src/node/index.ts index 0e391a5d9..481ea0952 100644 --- a/packages/vite/src/node/index.ts +++ b/packages/vite/src/node/index.ts @@ -219,7 +219,7 @@ export type { Terser } from 'dep-types/terser' export type { RollupCommonJSOptions } from 'dep-types/commonjs' export type { RollupDynamicImportVarsOptions } from 'dep-types/dynamicImportVars' export type { Matcher, AnymatchPattern, AnymatchFn } from 'dep-types/anymatch' -export type { LightningCSSOptions } from 'dep-types/lightningcss' +export type { LightningCSSOptions } from 'types/internal/lightningcssOptions' // Backward compatibility export type { ModuleGraph, ModuleNode } from './server/mixedModuleGraph' diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index c371a867b..ef59cf26a 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -21,7 +21,7 @@ import type Sass from 'sass' import type Stylus from 'stylus' import type Less from 'less' import type { Alias } from 'dep-types/alias' -import type { LightningCSSOptions } from 'dep-types/lightningcss' +import type { LightningCSSOptions } from 'types/internal/lightningcssOptions' import type { TransformOptions } from 'esbuild' import { formatMessages, transform } from 'esbuild' import type { RawSourceMap } from '@ampproject/remapping' @@ -181,9 +181,7 @@ export interface CSSModulesOptions { } export type ResolvedCSSOptions = Omit & { - lightningcss?: LightningCSSOptions & { - targets: LightningCSSOptions['targets'] - } + lightningcss?: LightningCSSOptions } export function resolveCSSOptions( diff --git a/packages/vite/src/types/lightningcss.d.ts b/packages/vite/src/types/lightningcss.d.ts deleted file mode 100644 index e34ac4fbb..000000000 --- a/packages/vite/src/types/lightningcss.d.ts +++ /dev/null @@ -1,23 +0,0 @@ -import type { - CSSModulesConfig, - Drafts, - NonStandard, - PseudoClasses, - Targets, -} from 'lightningcss' - -/** - * Options are spread, so you can also use options that are not typed here like - * visitor (not exposed because it would impact too much the bundle size) - */ -export type LightningCSSOptions = { - targets?: Targets - include?: number - exclude?: number - drafts?: Drafts - nonStandard?: NonStandard - pseudoClasses?: PseudoClasses - unusedSymbols?: string[] - cssModules?: CSSModulesConfig - errorRecovery?: boolean -} diff --git a/packages/vite/types/internal/lightningcssOptions.d.ts b/packages/vite/types/internal/lightningcssOptions.d.ts new file mode 100644 index 000000000..57e6820f3 --- /dev/null +++ b/packages/vite/types/internal/lightningcssOptions.d.ts @@ -0,0 +1,18 @@ +/* eslint-disable @typescript-eslint/ban-ts-comment */ + +// @ts-ignore `sass` may not be installed +import type Lightningcss from 'lightningcss' + +/* eslint-enable @typescript-eslint/ban-ts-comment */ + +export type LightningCSSOptions = Omit< + Lightningcss.BundleAsyncOptions, + | 'filename' + | 'resolver' + | 'minify' + | 'sourceMap' + | 'analyzeDependencies' + // properties not overridden by Vite, but does not make sense to set by end users + | 'inputSourceMap' + | 'projectRoot' +>