-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4405 from chadfawcett/css-modules
Support css-modules
- Loading branch information
Showing
7 changed files
with
132 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import semver from 'semver'; | ||
import { normalizeCondition } from 'webpack/lib/RuleSet'; | ||
|
||
export function isReactScriptsInstalled() { | ||
try { | ||
// eslint-disable-next-line global-require, import/no-extraneous-dependencies | ||
const reactScriptsJson = require('react-scripts/package.json'); | ||
if (semver.lt(reactScriptsJson.version, '2.0.0')) return false; | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
|
||
export function getStyleRules(rules) { | ||
// Extensions of style rules we're interested in | ||
const extensions = ['.css', '.scss', '.sass', '.module.css', '.module.scss', '.module.sass']; | ||
|
||
return rules.reduce((styleRules, rule) => { | ||
// If at least one style extension satisfies the rule test, the rule is one | ||
// we want to extract | ||
if (rule.test && extensions.some(normalizeCondition(rule.test))) { | ||
// If the base test is for styles, return early | ||
return styleRules.concat(rule); | ||
} | ||
|
||
// Get any style rules contained in rule.oneOf | ||
if (!rule.test && rule.oneOf) { | ||
styleRules.push(...getStyleRules(rule.oneOf)); | ||
} | ||
|
||
// Get any style rules contained in rule.rules | ||
if (!rule.test && rule.rules) { | ||
styleRules.push(...getStyleRules(rule.rules)); | ||
} | ||
|
||
return styleRules; | ||
}, []); | ||
} | ||
|
||
export function getCraWebpackConfig(mode) { | ||
if (mode === 'production') { | ||
// eslint-disable-next-line global-require, import/no-extraneous-dependencies | ||
return require('react-scripts/config/webpack.config.prod'); | ||
} | ||
|
||
// eslint-disable-next-line global-require, import/no-extraneous-dependencies | ||
return require('react-scripts/config/webpack.config.dev'); | ||
} | ||
|
||
export function applyCRAWebpackConfig(baseConfig) { | ||
// Remove any rules from baseConfig that test true for any one of the extensions | ||
const baseRulesExcludingStyles = baseConfig.module.rules.filter( | ||
rule => !rule.test || !['.css', '.scss', '.sass'].some(normalizeCondition(rule.test)) | ||
); | ||
|
||
// Load create-react-app config | ||
const craWebpackConfig = getCraWebpackConfig(baseConfig.mode); | ||
|
||
const craStyleRules = getStyleRules(craWebpackConfig.module.rules); | ||
|
||
// Add css minification for production | ||
const plugins = [...baseConfig.plugins]; | ||
if (baseConfig.mode === 'production') { | ||
// eslint-disable-next-line global-require, import/no-extraneous-dependencies | ||
const MiniCssExtractPlugin = require('mini-css-extract-plugin'); | ||
plugins.push( | ||
new MiniCssExtractPlugin({ | ||
// Options similar to the same options in webpackOptions.output | ||
// both options are optional | ||
filename: 'static/css/[name].[contenthash:8].css', | ||
chunkFilename: 'static/css/[name].[contenthash:8].chunk.css', | ||
}) | ||
); | ||
} | ||
|
||
return { | ||
...baseConfig, | ||
module: { | ||
...baseConfig.module, | ||
rules: [...baseRulesExcludingStyles, ...craStyleRules], | ||
}, | ||
plugins, | ||
}; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { logger } from '@storybook/node-logger'; | ||
import { applyCRAWebpackConfig, isReactScriptsInstalled } from './cra_config'; | ||
|
||
export function webpackFinal(config) { | ||
if (!isReactScriptsInstalled()) { | ||
logger.info('=> Using base config because react-scripts is not installed.'); | ||
return config; | ||
} | ||
|
||
logger.info('=> Loading create-react-app config.'); | ||
|
||
return applyCRAWebpackConfig(config); | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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