Comprehensive guide to progressively migrate legacy CSS to CSS Modules in React with Webpack, ensuring compatibility and minimal UI regression.
/*
Example Webpack configuration to support legacy CSS and CSS Modules
and two React components: one legacy and one modular.
*/
// webpack.config.js
const path = require('path');
module.exports = {
mode: 'development',
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},
module: {
rules: [
{
test: /\.module\.css$/,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
modules: {
localIdentName: '[name]__[local]___[hash:base64:5]',
},
},
},
],
},
{
test: /(?<!\.module)\.css$/,
use: [
'style-loader',
'css-loader',
],
},
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env', '@babel/preset-react'],
},
},
},
],
},
devServer: {
static: './dist',
hot: true,
},
};
/*
Legacy CSS: styles/legacy-button.css
Defines global classes without isolation.
*/
/* styles/legacy-button.css */
.legacy-button {
background-color: #007bff;
color: white;
border: none;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
border-radius: 4px;
transition: background-color 0.3s ease;
}
.legacy-button:hover {
background-color: #0056b3;
}
/*
CSS Module: styles/Button.module.css
Isolates classes with hashed naming.
*/
/* styles/Button.module.css */
.button {
background-color: #28a745;
color: white;
border: none;
padding: 12px 24px;
font-size: 18px;
cursor: pointer;
border-radius: 6px;
transition: background-color 0.3s ease;
}
.button:hover {
background-color: #1e7e34;
}
/*
Legacy React component using legacy CSS
*/
// src/components/ButtonLegacy.js
import React from 'react';
import '../styles/legacy-button.css'; // global import
/**
* Legacy button with global CSS classes
*/
export function ButtonLegacy({ onClick, children }) {
return (
<button className="legacy-button" onClick={onClick}>
{children}
</button>
);
}
/*
Modular React component using CSS Modules
*/
// src/components/ButtonModular.js
import React from 'react';
import styles from '../styles/Button.module.css'; // CSS Module import
/**
* Modular button with isolated CSS classes
*/
export function ButtonModular({ onClick, children }) {
return (
<button className={styles.button} onClick={onClick}>
{children}
</button>
);
}
/*
React entry point
*/
// src/index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { ButtonLegacy } from './components/ButtonLegacy';
import { ButtonModular } from './components/ButtonModular';
function App() {
return (
<div style={{ padding: '20px' }}>
<h1>Legacy CSS to CSS Modules Migration Demo</h1>
<h2>Legacy Component</h2>
<ButtonLegacy onClick={() => alert('Legacy button clicked!')}>Legacy</ButtonLegacy>
<h2>Modular Component</h2>
<ButtonModular onClick={() => alert('Modular button clicked!')}>Modular</ButtonModular>
</div>
);
}
ReactDOM.render(<App />, document.getElementById('root'));
/*
package.json snippet for dependencies and scripts
*/
/*
{
"name": "css-migration-example",
"version": "1.0.0",
"scripts": {
"start": "webpack serve --open",
"build": "webpack --mode production"
},
"dependencies": {
"react": "^18.0.0",
"react-dom": "^18.0.0"
},
"devDependencies": {
"@babel/core": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"@babel/preset-react": "^7.0.0",
"babel-loader": "^8.0.0",
"css-loader": "^6.0.0",
"style-loader": "^3.0.0",
"webpack": "^5.0.0",
"webpack-cli": "^4.0.0",
"webpack-dev-server": "^4.0.0"
}
}
*/React Components: ButtonLegacy, ButtonModular
Legacy CSS: styles/legacy-button.css
CSS Modules: styles/Button.module.css
Webpack Configuration: support for both legacy CSS and CSS Modules
Software Dependencies: react, react-dom, css-loader, style-loader, babel-loader, webpack, webpack-cli
Babel Configuration: JSX and ES6+ support
Arduino Wiring/Pinout: not applicable (frontend software)
Configurations: package.json scripts for build and dev server