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 examples/cra-kitchen-sink/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"enzyme-adapter-react-16": "^1.6.0",
"enzyme-to-json": "^3.3.4",
"jest": "^23.6.0",
"postcss-color-rebeccapurple": "^4.0.1",
"react-scripts": "^2.0.5"
}
}
3 changes: 3 additions & 0 deletions examples/cra-kitchen-sink/postcss.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
'postcss-color-rebeccapurple': {},
};
12 changes: 8 additions & 4 deletions examples/cra-kitchen-sink/src/App.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@import "./base.css";
@import './base.css';

.App {
text-align: center;
Expand All @@ -13,14 +13,18 @@
background-color: #222;
height: 150px;
padding: 20px;
color: white;
color: rebeccapurple;
}

.App-intro {
font-size: large;
}

@keyframes App-logo-spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
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
47 changes: 47 additions & 0 deletions lib/core/src/server/config/style.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { logger } from '@storybook/node-logger';
import autoprefixer from 'autoprefixer';
import findUp from 'find-up';

export default async function getStyleConfig(baseConfig) {
// null if file is not found
const customPostcssConfig = await 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.

it should be just findUp(...) without the sync

Copy link
Member Author

Choose a reason for hiding this comment

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

🤦‍♂️ fixed

cwd: baseConfig.context,
});

let postcssConfig = {};
if (customPostcssConfig) {
logger.info('=> Using postcss.config.js');
postcssConfig = {
config: {
path: customPostcssConfig,
},
};
} else {
postcssConfig = {
plugins: () => [
// eslint-disable-next-line global-require
require('postcss-flexbugs-fixes'),
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,
},
],
};
}
30 changes: 3 additions & 27 deletions lib/core/src/server/config/webpack.config.default.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,13 @@
import autoprefixer from 'autoprefixer';
import getStyleConfig from './style.config';

export function createDefaultWebpackConfig(storybookBaseConfig) {
export async function createDefaultWebpackConfig(storybookBaseConfig) {
return {
...storybookBaseConfig,
module: {
...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',
}),
],
},
},
],
},
await getStyleConfig(storybookBaseConfig),
{
test: /\.(svg|ico|jpg|jpeg|png|gif|eot|otf|webp|ttf|woff|woff2)(\?.*)?$/,
loader: require.resolve('file-loader'),
Expand Down
2 changes: 1 addition & 1 deletion lib/core/src/server/core-preset-webpack-custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function wrapPresets(presets) {
}

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

Expand Down