From 7594e876e0d4f92cba784e8670a3d0e16fb489b8 Mon Sep 17 00:00:00 2001 From: Chad Fawcett Date: Wed, 10 Oct 2018 22:42:42 -0700 Subject: [PATCH 01/21] Support css-modules --- .../src/server/config/webpack.config.default.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/core/src/server/config/webpack.config.default.js b/lib/core/src/server/config/webpack.config.default.js index 4e1362300e72..16659146a2b7 100644 --- a/lib/core/src/server/config/webpack.config.default.js +++ b/lib/core/src/server/config/webpack.config.default.js @@ -1,5 +1,7 @@ import autoprefixer from 'autoprefixer'; +const cssModuleRegex = /\.module\.css$/; + export function createDefaultWebpackConfig(storybookBaseConfig) { return { ...storybookBaseConfig, @@ -9,6 +11,7 @@ export function createDefaultWebpackConfig(storybookBaseConfig) { ...storybookBaseConfig.module.rules, { test: /\.css$/, + exclude: cssModuleRegex, use: [ require.resolve('style-loader'), { @@ -32,6 +35,20 @@ export function createDefaultWebpackConfig(storybookBaseConfig) { }, ], }, + { + test: cssModuleRegex, + loaders: [ + require.resolve('style-loader'), + { + loader: require.resolve('css-loader'), + options: { + importLoaders: 1, + modules: true, + localIdentName: '[name]__[local]___[hash:base64:5]', + }, + }, + ], + }, { test: /\.(svg|ico|jpg|jpeg|png|gif|eot|otf|webp|ttf|woff|woff2)(\?.*)?$/, loader: require.resolve('file-loader'), From 0441fc5603037b064c1233659d5f4b11e6b5ad11 Mon Sep 17 00:00:00 2001 From: Chad Fawcett Date: Tue, 16 Oct 2018 16:16:42 -0700 Subject: [PATCH 02/21] Add [module.]scss/sass loader support --- lib/core/package.json | 2 +- .../server/config/webpack.config.default.js | 109 ++++++++++++------ 2 files changed, 77 insertions(+), 34 deletions(-) diff --git a/lib/core/package.json b/lib/core/package.json index 1b0d5a761969..1ed917294ab4 100644 --- a/lib/core/package.json +++ b/lib/core/package.json @@ -37,7 +37,6 @@ "@storybook/node-logger": "4.0.0-alpha.24", "@storybook/ui": "4.0.0-alpha.24", "airbnb-js-shims": "^1 || ^2", - "autoprefixer": "^9.1.5", "babel-plugin-macros": "^2.4.2", "babel-preset-minify": "^0.5.0", "case-sensitive-paths-webpack-plugin": "^2.1.2", @@ -61,6 +60,7 @@ "opn": "^5.4.0", "postcss-flexbugs-fixes": "^4.1.0", "postcss-loader": "^3.0.0", + "postcss-preset-env": "^6.1.1", "prop-types": "^15.6.2", "qs": "^6.5.2", "raw-loader": "^0.5.1", diff --git a/lib/core/src/server/config/webpack.config.default.js b/lib/core/src/server/config/webpack.config.default.js index 16659146a2b7..3ef8cd6740ce 100644 --- a/lib/core/src/server/config/webpack.config.default.js +++ b/lib/core/src/server/config/webpack.config.default.js @@ -1,6 +1,45 @@ -import autoprefixer from 'autoprefixer'; +import getCSSModuleLocalIdent from 'react-dev-utils/getCSSModuleLocalIdent'; +// style files regexes +const cssRegex = /\.css$/; const cssModuleRegex = /\.module\.css$/; +const sassRegex = /\.(scss|sass)$/; +const sassModuleRegex = /\.module\.(scss|sass)$/; + +// common function to get style loaders +const getStyleLoaders = (cssOptions, preProcessor) => { + const loaders = [ + require.resolve('style-loader'), + { + loader: require.resolve('css-loader'), + options: cssOptions, + }, + { + // Options for PostCSS as we reference these options twice + // Adds vendor prefixing based on your specified browser support in + // package.json + loader: require.resolve('postcss-loader'), + options: { + // Necessary for external CSS imports to work + // https://github.com/facebook/create-react-app/issues/2677 + ident: 'postcss', + plugins: () => [ + require('postcss-flexbugs-fixes'), // eslint-disable-line + require('postcss-preset-env')({ // eslint-disable-line + autoprefixer: { + flexbox: 'no-2009', + }, + stage: 3, + }), + ], + }, + }, + ]; + if (preProcessor) { + loaders.push(require.resolve(preProcessor)); + } + return loaders; +}; export function createDefaultWebpackConfig(storybookBaseConfig) { return { @@ -9,45 +48,49 @@ export function createDefaultWebpackConfig(storybookBaseConfig) { ...storybookBaseConfig.module, rules: [ ...storybookBaseConfig.module.rules, + // "postcss" loader applies autoprefixer to our CSS. + // "css" loader resolves paths in CSS and adds assets as dependencies. + // "style" loader turns CSS into JS modules that inject