Skip to content

Commit a376a94

Browse files
authored
Merge pull request facebook#136 from Jorer/fix-jest-snapshot-css-modules
Fix Jest snapshot CSS Module class names, add CSS Module class name template as a configuration option. Closes facebook#106 and facebook#97.
2 parents dea4a2b + e08ee6b commit a376a94

File tree

6 files changed

+25
-15
lines changed

6 files changed

+25
-15
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Add a `.env.` file with the desired features.
4444
- ```REACT_APP_SASS_MODULES=true``` - enable Sass modules
4545
- ```REACT_APP_LESS_MODULES=true``` - enable Less modules
4646
- ```REACT_APP_STYLUS_MODULES=true``` - enable Stylus modules
47+
- ```REACT_APP_CSS_MODULE_CLASSNAME_TEMPLATE='module-[sha512:hash:base32]-[name]-[local]'``` - add custom CSS Module hash ident name
4748

4849
Note: to use modules the file must be named in the following format: ```$name.module.$preprocessorName```.
4950

packages/react-scripts/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ The generated project comes with every option turned on by default, but you can
3535
- ```REACT_APP_SASS_MODULES``` - enable Sass modules
3636
- ```REACT_APP_LESS_MODULES``` - enable Less modules
3737
- ```REACT_APP_STYLUS_MODULES``` - enable Stylus modules
38+
- ```REACT_APP_CSS_MODULE_CLASSNAME_TEMPLATE='module-[sha512:hash:base32]-[name]-[local]'``` - add custom CSS Module hash ident name
3839

3940
Note: to use modules the file must be named in the following format: ```$name.module.$preprocessorName```.
4041

packages/react-scripts/config/custom-react-scripts/webpack-config/style-loader.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ const postCssOptions = require('../options/postcss-options');
22
const extractTextPluginOptions = require('../options/extract-text-plugin-options');
33
const ExtractTextPlugin = require('extract-text-webpack-plugin');
44
const shouldUseSourceMap = process.env.GENERATE_SOURCEMAP !== 'false';
5+
const localIdentName =
6+
process.env.REACT_APP_CSS_MODULE_CLASSNAME_TEMPLATE ||
7+
'[sha512:hash:base32]-[name]-[local]';
58

69
module.exports = (loader, test, exclude, modules) => isDev => {
710
let loaders = isDev
@@ -20,7 +23,7 @@ module.exports = (loader, test, exclude, modules) => isDev => {
2023
{ importLoaders: 1 },
2124
modules === true
2225
? {
23-
localIdentName: '[sha512:hash:base32]-[name]-[local]',
26+
localIdentName: localIdentName,
2427
modules: true,
2528
}
2629
: {}

packages/react-scripts/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"file-loader": "0.11.2",
4646
"fs-extra": "3.0.1",
4747
"html-webpack-plugin": "2.29.0",
48+
"identity-obj-proxy": "^3.0.0",
4849
"jest": "20.0.4",
4950
"less": "^2.7.2",
5051
"less-loader": "^4.0.5",

packages/react-scripts/scripts/utils/createJestConfig.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ module.exports = (resolve, rootDir, isEjecting) => {
4242
transformIgnorePatterns: ['[/\\\\]node_modules[/\\\\].+\\.(js|jsx)$'],
4343
moduleNameMapper: {
4444
'^react-native$': 'react-native-web',
45+
"\\.(s?css|styl|less)$": "identity-obj-proxy"
4546
},
4647
moduleFileExtensions: ['web.js', 'js', 'json', 'web.jsx', 'jsx', 'node'],
4748
};
@@ -67,20 +68,20 @@ module.exports = (resolve, rootDir, isEjecting) => {
6768
console.error(
6869
chalk.red(
6970
'Out of the box, Create React App only supports overriding ' +
70-
'these Jest options:\n\n' +
71-
supportedKeys.map(key => chalk.bold(' \u2022 ' + key)).join('\n') +
72-
'.\n\n' +
73-
'These options in your package.json Jest configuration ' +
74-
'are not currently supported by Create React App:\n\n' +
75-
unsupportedKeys
76-
.map(key => chalk.bold(' \u2022 ' + key))
77-
.join('\n') +
78-
'\n\nIf you wish to override other Jest options, you need to ' +
79-
'eject from the default setup. You can do so by running ' +
80-
chalk.bold('npm run eject') +
81-
' but remember that this is a one-way operation. ' +
82-
'You may also file an issue with Create React App to discuss ' +
83-
'supporting more options out of the box.\n'
71+
'these Jest options:\n\n' +
72+
supportedKeys.map(key => chalk.bold(' \u2022 ' + key)).join('\n') +
73+
'.\n\n' +
74+
'These options in your package.json Jest configuration ' +
75+
'are not currently supported by Create React App:\n\n' +
76+
unsupportedKeys
77+
.map(key => chalk.bold(' \u2022 ' + key))
78+
.join('\n') +
79+
'\n\nIf you wish to override other Jest options, you need to ' +
80+
'eject from the default setup. You can do so by running ' +
81+
chalk.bold('npm run eject') +
82+
' but remember that this is a one-way operation. ' +
83+
'You may also file an issue with Create React App to discuss ' +
84+
'supporting more options out of the box.\n'
8485
)
8586
);
8687
process.exit(1);

packages/react-scripts/template/.env

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ REACT_APP_STYLUS_MODULES = true;
1111
REACT_APP_LESS_MODULES = true;
1212

1313
REACT_APP_WEBPACK_DASHBOARD = true;
14+
15+
# CSS Module hash
16+
# REACT_APP_CSS_MODULE_CLASSNAME_TEMPLATE = 'module-[sha512:hash:base32]-[name]-[local]';

0 commit comments

Comments
 (0)