Skip to content

Commit

Permalink
feat(css): allow specifying stopDir for postcss config resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Dec 16, 2022
1 parent af86e5b commit a31a240
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
16 changes: 16 additions & 0 deletions docs/config/shared-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

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<string, object>`
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export type {
export type {
CSSOptions,
CSSModulesOptions,
PostCSSConfigResolutionOptions,
PreprocessCSSResult,
} from './plugins/css'
export type { ChunkMetadata } from './plugins/metadata'
Expand Down
15 changes: 14 additions & 1 deletion packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export interface CSSOptions {
* https://github.com/css-modules/postcss-modules
*/
modules?: CSSModulesOptions | false
resolveOptions?: PostCSSConfigResolutionOptions
preprocessorOptions?: Record<string, any>
postcss?:
| string
Expand All @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit a31a240

Please sign in to comment.