Skip to content

[WIP] feat: support css modules for files with *.module.* in Webpack #637

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion config/webpack.dev-stage.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ module.exports = merge(commonConfig, {
],
// Silences compiler deprecation warnings. They mostly come from bootstrap and/or paragon.
quietDeps: true,
silenceDeprecations: ['abs-percent', 'color-functions', 'import', 'mixed-decls', 'global-builtin'],
silenceDeprecations: ['abs-percent', 'color-functions', 'import', 'mixed-decls', 'global-builtin', 'legacy-js-api'],
},
},
},
Expand Down
25 changes: 17 additions & 8 deletions config/webpack.dev.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,18 @@ resolvePrivateEnvConfig('.env.private');
const aliases = getLocalAliases();
const PUBLIC_PATH = process.env.PUBLIC_PATH || '/';

function getStyleUseConfig() {
function getStyleUseConfig({ isModule = false } = {}) {
const cssLoaderOptions = {
sourceMap: true,
...(isModule
? { modules: true }
: { modules: { compileType: 'icss' } }
),
};
return [
{
loader: 'css-loader', // translates CSS into CommonJS
options: {
sourceMap: true,
modules: {
compileType: 'icss',
},
},
options: cssLoaderOptions,
},
{
loader: 'postcss-loader',
Expand All @@ -66,7 +68,7 @@ function getStyleUseConfig() {
],
// Silences compiler deprecation warnings. They mostly come from bootstrap and/or paragon.
quietDeps: true,
silenceDeprecations: ['abs-percent', 'color-functions', 'import', 'mixed-decls', 'global-builtin'],
silenceDeprecations: ['abs-percent', 'color-functions', 'import', 'mixed-decls', 'global-builtin', 'legacy-js-api'],
},
},
},
Expand Down Expand Up @@ -118,6 +120,13 @@ module.exports = merge(commonConfig, {
...getStyleUseConfig(),
],
},
{
resource: /\.module\.(scss|css)$/,
use: [
'style-loader', // creates style nodes from JS strings
...getStyleUseConfig({ isModule: true }),
],
},
{
use: [
'style-loader', // creates style nodes from JS strings
Expand Down
2 changes: 1 addition & 1 deletion config/webpack.prod.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ module.exports = merge(commonConfig, {
],
// Silences compiler deprecation warnings. They mostly come from bootstrap and/or paragon.
quietDeps: true,
silenceDeprecations: ['abs-percent', 'color-functions', 'import', 'mixed-decls', 'global-builtin'],
silenceDeprecations: ['abs-percent', 'color-functions', 'import', 'mixed-decls', 'global-builtin', 'legacy-js-api'],
},
},
},
Expand Down
31 changes: 30 additions & 1 deletion example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,40 @@ import appleUrl, { ReactComponent as Apple } from './apple.svg';
// @ts-ignore
import appleImg from './apple.jpg';

import './style.scss';
import ParagonPreview from './ParagonPreview';

// Without CSS modules support
// @ts-ignore
import defaultStyles from './style.scss';

// With CSS modules support
// @ts-ignore
import moduleStyles from './example.module.scss';

// @ts-ignore
import exampleStyles from './example.scss';

console.log('defaultStyles', defaultStyles);
console.log('moduleStyles', moduleStyles);
console.log('exampleStyles', exampleStyles);

const App = () => {
const newEnglandApples = ['macintosh', 'granny smith'];
const allApples = [...newEnglandApples, 'fuji', 'golden delicious'];

const stylesConfig = {
defaultStyles,
moduleStyles,
exampleStyles,
};

return (
<div>
<h1>Test page</h1>
<h2>SCSS parsing tests</h2>
<pre>
{JSON.stringify(stylesConfig, null, 2)}
</pre>
<h3>The Apples</h3> (&quot;The Apples&quot; should be red)
<h2>ES6 parsing tests</h2>
<ul>
Expand All @@ -41,6 +65,11 @@ const App = () => {
<p>env.config.js integer test: {Number.isInteger(config.INTEGER_VALUE) ? 'It was an integer. Great.' : 'It was not an integer! Why not? '}</p>
<h2>Right-to-left language handling tests</h2>
<p className="text-align-right">I&apos;m aligned right, but left in RTL.</p>
{/* <p className="text-align-right">I&apos;m aligned right, but left in RTL.</p> */}
{/* <div className="style-modified">
<p className="text-align-right">I&apos;m aligned right, but left in RTL.</p>
</div> */}
{/* <p className={moduleStyles['text-align-right']}>I&apos;m aligned right, but left in RTL.</p> */}
<ParagonPreview />
</div>
);
Expand Down
14 changes: 14 additions & 0 deletions example/src/example.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
$h3-color: red;

h3 {
color: $h3-color;
}

.text-align-right {
text-align: right;
color: blue;
}

:export {
h3Color: $h3-color;
}
11 changes: 11 additions & 0 deletions example/src/example.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.text-align-right {
text-align: right;
color: blue;
}

// .style-modified {
// .text-align-right {
// text-align: right;
// color: blue;
// }
// }
6 changes: 4 additions & 2 deletions example/src/index.jsx → example/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';

Check failure on line 2 in example/src/index.tsx

View workflow job for this annotation

GitHub Actions / tests

Unable to resolve path to module 'react-dom/client'
import App from './App';

// This line is to emulate what frontend-platform does when i18n initializes.
Expand All @@ -9,5 +9,7 @@
global.document.getElementsByTagName('html')[0].setAttribute('dir', 'ltr');

const rootContainer = document.getElementById('root');
const root = createRoot(rootContainer);
root.render(<StrictMode><App /></StrictMode>);
if (rootContainer) {
const root = createRoot(rootContainer);
root.render(<StrictMode><App /></StrictMode>);
}
4 changes: 4 additions & 0 deletions example/src/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ h3 {
.text-align-right {
text-align: right;
}

:export {
h3Color: $h3-color;
}
Loading