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

Use postcss.config.js when present #4411

Closed
wants to merge 13 commits into from
1 change: 1 addition & 0 deletions lib/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"file-loader": "^2.0.0",
"file-system-cache": "^1.0.5",
"find-cache-dir": "^2.0.0",
"find-up": "^3.0.0",
"global": "^4.3.2",
"html-webpack-plugin": "^4.0.0-beta.2",
"inquirer": "^6.2.0",
Expand Down
42 changes: 42 additions & 0 deletions lib/core/src/server/config/style.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { logger } from '@storybook/node-logger';
import autoprefixer from 'autoprefixer';
import findUp from 'find-up';

export default function getStyleConfig(baseConfig) {
// null if file is not found
const customPostcssConfig = findUp.sync('postcss.config.js', {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will be very nice if you could asyncify this... Need to add some additional awaits in the call chain though. Not critical one, bit since we already support async/await that be great =)

cwd: baseConfig.context,
});

let postcssConfig = {};
if (customPostcssConfig) {
logger.info('=> Using postcss.config.js');
postcssConfig = customPostcssConfig;
} else {
postcssConfig = {
plugins: () => [
require('postcss-flexbugs-fixes'), // eslint-disable-line global-require
autoprefixer({
flexbox: 'no-2009',
}),
],
};
}

return {
test: /\.css$/,
use: [
require.resolve('style-loader'),
{
loader: require.resolve('css-loader'),
options: {
importLoaders: 1,
},
},
{
loader: require.resolve('postcss-loader'),
options: postcssConfig,
},
],
};
}
28 changes: 2 additions & 26 deletions lib/core/src/server/config/webpack.config.default.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import autoprefixer from 'autoprefixer';
import getStyleConfig from './style.config';

export function createDefaultWebpackConfig(storybookBaseConfig) {
return {
Expand All @@ -7,31 +7,7 @@ export function createDefaultWebpackConfig(storybookBaseConfig) {
...storybookBaseConfig.module,
rules: [
...storybookBaseConfig.module.rules,
{
test: /\.css$/,
use: [
require.resolve('style-loader'),
{
loader: require.resolve('css-loader'),
options: {
importLoaders: 1,
},
},
{
loader: require.resolve('postcss-loader'),
options: {
ident: 'postcss', // https://webpack.js.org/guides/migrating/#complex-options
postcss: {},
plugins: () => [
require('postcss-flexbugs-fixes'), // eslint-disable-line
autoprefixer({
flexbox: 'no-2009',
}),
],
},
},
],
},
getStyleConfig(storybookBaseConfig),
{
test: /\.(svg|ico|jpg|jpeg|png|gif|eot|otf|webp|ttf|woff|woff2)(\?.*)?$/,
loader: require.resolve('file-loader'),
Expand Down