From c38019e9791e00002bb5434c91b379175abade48 Mon Sep 17 00:00:00 2001 From: Espen Hovlandsdal Date: Thu, 15 Dec 2022 14:54:16 -0800 Subject: [PATCH] feat(css): allow specifying `stopDir` for postcss config resolution --- docs/config/shared-options.md | 16 ++++++++++++++++ packages/vite/src/node/index.ts | 1 + packages/vite/src/node/plugins/css.ts | 15 ++++++++++++++- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/docs/config/shared-options.md b/docs/config/shared-options.md index fe04872a8770e4..ea84bd27d1c38b 100644 --- a/docs/config/shared-options.md +++ b/docs/config/shared-options.md @@ -222,6 +222,22 @@ The search is done using [postcss-load-config](https://github.com/postcss/postcs Note if an inline config is provided, Vite will not search for other PostCSS config sources. +## css.resolveOptions + +- **Type:** `PostCSSConfigResolutionOptions | false` + +Specify options for postcss config resolution. Currently only the [stopDir](https://github.com/davidtheclark/cosmiconfig#stopdir) option for cosmicconfig is supported. + +```js +export default defineConfig({ + css: { + resolveOptions: { + stopDir: process.cwd(), + }, + }, +}) +``` + ## css.preprocessorOptions - **Type:** `Record` diff --git a/packages/vite/src/node/index.ts b/packages/vite/src/node/index.ts index 3ee329d8db6246..5bafc871dbaa25 100644 --- a/packages/vite/src/node/index.ts +++ b/packages/vite/src/node/index.ts @@ -71,6 +71,7 @@ export type { export type { CSSOptions, CSSModulesOptions, + PostCSSConfigResolutionOptions, PreprocessCSSResult, } from './plugins/css' export type { ChunkMetadata } from './plugins/metadata' diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index 9834858bf1a878..5bab1cbf381dfd 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -74,6 +74,7 @@ export interface CSSOptions { * https://github.com/css-modules/postcss-modules */ modules?: CSSModulesOptions | false + resolveOptions?: PostCSSConfigResolutionOptions preprocessorOptions?: Record postcss?: | string @@ -88,6 +89,18 @@ export interface CSSOptions { devSourcemap?: boolean } +/** + * Resolve options for postcss config, loaded by cosmicconfig, through + * https://github.com/postcss/postcss-load-config + */ +export interface PostCSSConfigResolutionOptions { + /** + * Directory where the search for a PostCSS configuration file will stop. + * @default Absolute path to your home directory + */ + stopDir?: string +} + export interface CSSModulesOptions { getJSON?: ( cssFileName: string, @@ -1149,7 +1162,7 @@ async function resolvePostcssConfig( const searchPath = typeof inlineOptions === 'string' ? inlineOptions : config.root try { - result = await postcssrc({}, searchPath) + result = await postcssrc({}, searchPath, config.css?.resolveOptions || {}) } catch (e) { if (!/No PostCSS Config found/.test(e.message)) { if (e instanceof Error) {