Last active
September 7, 2021 12:11
-
-
Save ademilter/5f56fe9e56c5eb8725292274c68001c5 to your computer and use it in GitHub Desktop.
postcss config for storybook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- webpack.config.js | |
- postcss.config.js | |
- config.js | |
- addons.js |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { configure } from '@storybook/react' | |
import '../src/styles/main.css' | |
const req = require.context('../stories', true, /.stories.js$/) | |
function loadStories() { | |
req.keys().forEach(filename => req(filename)) | |
} | |
configure(loadStories, module) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
plugins: { | |
// PostCSS Preset Env includes autoprefixer and browsers option will be passed to it automatically. | |
'postcss-preset-env': { | |
stage: 0, | |
browsers: 'last 2 versions' | |
// importFrom: 'path/to/file.css' | |
}, | |
'postcss-import': {}, | |
'postcss-nested': {} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
module: { | |
rules: [ | |
{ | |
test: /\.css$/, | |
use: [ | |
{ | |
loader: 'style-loader', | |
options: { sourceMap: true } | |
}, | |
{ | |
loader: 'css-loader', | |
options: { sourceMap: true } | |
}, | |
{ | |
loader: 'postcss-loader', | |
options: { | |
sourceMap: true, | |
config: { | |
path: './.storybook/' | |
} | |
} | |
} | |
] | |
} | |
] | |
} | |
} |
Notes for anyone who stumbles on this gist and fails to get it working like me.
I ended up getting it to work, after tweaking the webpack.config.js
file to look something like this:
const path = require('path');
module.exports = {
module: {
rules: [
{
test: /\.css$/,
use: [
// Loader for webpack to process CSS with PostCSS
{
loader: 'postcss-loader',
options: {
/*
Enable Source Maps
*/
sourceMap: true,
/*
Set postcss.config.js config path && ctx
*/
config: {
path: './.storybook/',
},
},
},
],
include: path.resolve(__dirname, '../'),
},
],
},
};
I had to delete the first two loaders "style-loader" and "css-loader" and only extend to add "postcss-loader".
Reference from storybook documentation on extending webpack
PS: I'm using storybook version 5.
Thanks @Abramovick .
thanks @Abramovick π
Thanks @Abramovick π
Thanks @Abramovick :)
Thanks @Abramovick!
This was the only working postcss example for storybook5.
Has anyone tried configuring postcss with scss files?
Anyone has configuration postcss for Angular?
@Abramovick you saved my life π
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tried this but it's not working...
I get