Tree shaking not working with scss modules
Closed this issue · 1 comments
Bug report
Tree shaking is not working with SCSS modules.
I have an application with ComponentA and ComponentB using classA, but not classB.
// ComponentA
import React from "react";
import styles from "./styles.module.scss";
export function ComponentA() {
return <div className={styles.classA}>ComponentA</div>;
}// ComponentB
import React from "react";
import styles from "./styles.module.scss";
export function ComponentB() {
return <div className={styles.classA}>ComponentB</div>;
}// styles.module.scss
.classA {
color: green;
}
.classB {
color: purple;
}I have sideEffects: true in my package.json and the following webpack configuration:
/* eslint-env node */
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
module.exports = {
mode: "production",
entry: {
index: path.resolve(__dirname, "./src/index.tsx"),
},
output: {
path: path.resolve(__dirname, "dist"),
filename: "[name].bundle.js",
},
resolve: {
extensions: [".ts", ".tsx", ".js", ".jsx", ".scss", ".css"],
},
module: {
rules: [
{
test: /\.m?js/u,
resolve: {
fullySpecified: false,
},
},
{
test: /\.tsx?$/u,
use: "ts-loader",
},
{
test: /\.s[ac]ss$/u,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
esModule: true,
},
},
{
loader: "css-loader",
options: {
esModule: true,
modules: {
namedExport: true,
},
},
},
"sass-loader",
],
},
{
test: /\.css$/u,
use: [MiniCssExtractPlugin.loader, "css-loader"],
},
],
},
plugins: [
new HtmlWebpackPlugin({
template: path.resolve(__dirname, "src/index.html"),
}),
new MiniCssExtractPlugin(),
],
};Actual Behavior
After building, css for classB exists in bundled CSS:
// dist/index.css
.lbEYKkO5_LPzVhlhmgOW{color:green}.HrcP_c2cJ59oMgw7mdme{color:purple}Expected Behavior
After building, css for classB would not exist in bundled CSS:
// dist/index.css
.lbEYKkO5_LPzVhlhmgOW{color:green}How Do We Reproduce?
https://github.com/jordanjlatimer/css-modules-css-loader-tree-shaking
Please paste the results of npx webpack-cli info here, and mention other relevant information
System:
OS: macOS 12.6.8
CPU: (4) x64 Intel(R) Core(TM) i5-5350U CPU @ 1.80GHz
Memory: 285.48 MB / 8.00 GB
Binaries:
Node: 16.6.2 - ~/.nvm/versions/node/v16.6.2/bin/node
npm: 7.20.3 - ~/.nvm/versions/node/v16.6.2/bin/npm
Browsers:
Chrome: 117.0.5938.149
Safari: 16.6
Packages:
css-loader: ^6.8.1 => 6.8.1
html-webpack-plugin: ^5.5.3 => 5.5.3
sass-loader: ^13.3.2 => 13.3.2
ts-loader: ^9.5.0 => 9.5.0
webpack: ^5.88.2 => 5.88.2
webpack-cli: ^5.1.4 => 5.1.4
Sorry, tree shaking for CSS is not supported right now - #506