Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core: Allow custom postcss config #8498

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions docs/src/pages/configurations/custom-postcss-config/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
id: 'custom-postcss-config'
title: 'Custom Postcss Config'
---

Storybook can load your custom postcss configuration.

It searches for the first postcss config file in the parents directories of your loaded file :
- `.postcssrc`
- `.postcssrc.json`
- `.postcssrc.yml`
- `.postcssrc.js`
- `postcss.config.js`

If no postcss config file is found, it applies a default configuration which is :

```js
{
ident: 'postcss',
postcss: {},
plugins: () => [
require('postcss-flexbugs-fixes'),
autoprefixer({
flexbox: 'no-2009',
}),
],
}
```

> Currently we do **not** support loading the Babel config from the `package.json` neither in the Storybook config directory (by default, it's `.storybook`).
2 changes: 2 additions & 0 deletions examples/html-kitchen-sink/.postcssrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
plugins:
postcss-color-rebeccapurple: {}
3 changes: 2 additions & 1 deletion examples/html-kitchen-sink/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@storybook/source-loader": "5.3.0-alpha.23",
"eventemitter3": "^4.0.0",
"format-json": "^1.0.3",
"global": "^4.3.2"
"global": "^4.3.2",
"postcss-color-rebeccapurple": "^4.0.1"
}
}
4 changes: 4 additions & 0 deletions examples/html-kitchen-sink/stories/welcome.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
background-color: #ffffff;
}

h1 {
color: rebeccapurple;
}

.logo {
width: 256px;
margin: 15px;
Expand Down
1 change: 1 addition & 0 deletions lib/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"file-loader": "^4.2.0",
"file-system-cache": "^1.0.5",
"find-cache-dir": "^3.0.0",
"find-up": "^3.0.0",
"fs-extra": "^8.0.1",
"global": "^4.3.2",
"html-webpack-plugin": "^4.0.0-beta.2",
Expand Down
45 changes: 34 additions & 11 deletions lib/core/src/server/preview/base-webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,38 @@
import autoprefixer from 'autoprefixer';
import findUp from 'find-up';
import path from 'path';

export async function createDefaultWebpackConfig(storybookBaseConfig) {
const postcssConfigFiles = [
'.postcssrc',
'.postcssrc.json',
'.postcssrc.yml',
'.postcssrc.js',
'postcss.config.js',
];
const customPostcssConfig = await findUp(postcssConfigFiles);

let postcssOptions = {};
if (customPostcssConfig) {
console.log(`=> Using custom ${path.basename(customPostcssConfig)}`);
postcssOptions = {
config: {
path: path.dirname(customPostcssConfig),
},
};
} else {
postcssOptions = {
ident: 'postcss',
postcss: {},
plugins: () => [
require('postcss-flexbugs-fixes'), // eslint-disable-line global-require
autoprefixer({
flexbox: 'no-2009',
}),
],
};
}

export function createDefaultWebpackConfig(storybookBaseConfig) {
return {
...storybookBaseConfig,
module: {
Expand All @@ -20,16 +52,7 @@ export function createDefaultWebpackConfig(storybookBaseConfig) {
},
{
loader: require.resolve('postcss-loader'),
options: {
ident: 'postcss',
postcss: {},
plugins: () => [
require('postcss-flexbugs-fixes'), // eslint-disable-line global-require
autoprefixer({
flexbox: 'no-2009',
}),
],
},
options: postcssOptions,
},
],
},
Expand Down
2 changes: 1 addition & 1 deletion lib/core/src/server/preview/custom-webpack-preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import mergeConfigs from '../utils/merge-webpack-config';
import { createDefaultWebpackConfig } from './base-webpack.config';

async function createFinalDefaultConfig(presets, config, options) {
const defaultConfig = createDefaultWebpackConfig(config);
const defaultConfig = await createDefaultWebpackConfig(config);
return presets.apply('webpackFinal', defaultConfig, options);
}

Expand Down