Skip to content
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

Tree shaking not working with scss modules #1543

Closed
jordanjlatimer opened this issue Oct 8, 2023 · 1 comment
Closed

Tree shaking not working with scss modules #1543

jordanjlatimer opened this issue Oct 8, 2023 · 1 comment

Comments

@jordanjlatimer
Copy link

jordanjlatimer commented Oct 8, 2023

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

@alexander-akait
Copy link
Member

Sorry, tree shaking for CSS is not supported right now - #506

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants