Skip to content

feat: Configuration for obfuscate/minify css module's local ident names #9223

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docusaurus/docs/advanced-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ You can adjust various development and production settings by setting environmen
| REACT_EDITOR | ✅ Used | 🚫 Ignored | When an app crashes in development, you will see an error overlay with clickable stack trace. When you click on it, Create React App will try to determine the editor you are using based on currently running processes, and open the relevant source file. You can [send a pull request to detect your editor of choice](https://github.com/facebook/create-react-app/issues/2636). Setting this environment variable overrides the automatic detection. If you do it, make sure your systems [PATH](<https://en.wikipedia.org/wiki/PATH_(variable)>) environment variable points to your editor’s bin folder. You can also set it to `none` to disable it completely. |
| CHOKIDAR_USEPOLLING | ✅ Used | 🚫 Ignored | When set to `true`, the watcher runs in polling mode, as necessary inside a VM. Use this option if `npm start` isn't detecting changes. |
| GENERATE_SOURCEMAP | 🚫 Ignored | ✅ Used | When set to `false`, source maps are not generated for a production build. This solves out of memory (OOM) issues on some smaller machines. |
| CSS_LOCAL_IDENT_NAME | ✅ Used | ✅ Used | When set to `false`, css modules don't generate full path to component in ident names (e.g. class names). Ident names obfuscated/minified, not readable. |
| INLINE_RUNTIME_CHUNK | 🚫 Ignored | ✅ Used | By default, Create React App will embed the runtime script into `index.html` during the production build. When set to `false`, the script will not be embedded and will be imported as usual. This is normally required when dealing with CSP. |
| IMAGE_INLINE_SIZE_LIMIT | ✅ Used | ✅ Used | By default, images smaller than 10,000 bytes are encoded as a data URI in base64 and inlined in the CSS or JS build artifact. Set this to control the size limit in bytes. Setting it to `0` will disable the inlining of images. |
| FAST_REFRESH | ✅ Used | 🚫 Ignored | When set to `false`, disables experimental support for Fast Refresh to allow you to tweak your components in real time without reloading the page. |
Expand Down
17 changes: 9 additions & 8 deletions packages/react-scripts/config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ const appPackageJson = require(paths.appPackageJson);
// Source maps are resource heavy and can cause out of memory issue for large source files.
const shouldUseSourceMap = process.env.GENERATE_SOURCEMAP !== 'false';

const shouldUseLocalIdentName = process.env.CSS_LOCAL_IDENT_NAME !== 'false';

const webpackDevClientEntry = require.resolve(
'react-dev-utils/webpackHotDevClient'
);
Expand Down Expand Up @@ -170,6 +172,11 @@ module.exports = function (webpackEnv) {
return loaders;
};

const getCssModulesIdentNameConfig = () =>
shouldUseLocalIdentName
? { compileType: 'module', getLocalIdent: getCSSModuleLocalIdent }
: { compileType: 'module', localIdentName: '[hash:base64]' };

return {
mode: isEnvProduction ? 'production' : isEnvDevelopment && 'development',
// Stop compilation early in production
Expand Down Expand Up @@ -537,10 +544,7 @@ module.exports = function (webpackEnv) {
sourceMap: isEnvProduction
? shouldUseSourceMap
: isEnvDevelopment,
modules: {
compileType: 'module',
getLocalIdent: getCSSModuleLocalIdent,
},
modules: getCssModulesIdentNameConfig(),
}),
},
// Opt-in support for SASS (using .scss or .sass extensions).
Expand Down Expand Up @@ -577,10 +581,7 @@ module.exports = function (webpackEnv) {
sourceMap: isEnvProduction
? shouldUseSourceMap
: isEnvDevelopment,
modules: {
compileType: 'module',
getLocalIdent: getCSSModuleLocalIdent,
},
modules: getCssModulesIdentNameConfig(),
},
'sass-loader'
),
Expand Down