{"id":19857702,"url":"https://github.com/markmur/webpack-css-tree-shaking-example","last_synced_at":"2025-05-02T02:30:39.770Z","repository":{"id":44034268,"uuid":"226080226","full_name":"markmur/webpack-css-tree-shaking-example","owner":"markmur","description":"Working example of tree shaking CSS modules","archived":false,"fork":false,"pushed_at":"2023-01-05T02:22:34.000Z","size":353,"stargazers_count":8,"open_issues_count":9,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-06T21:05:56.037Z","etag":null,"topics":["css","css-modules","modules","tree-shaking","webpack"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/markmur.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-12-05T10:53:38.000Z","updated_at":"2024-02-01T11:18:00.000Z","dependencies_parsed_at":"2023-02-03T06:01:35.257Z","dependency_job_id":null,"html_url":"https://github.com/markmur/webpack-css-tree-shaking-example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/markmur%2Fwebpack-css-tree-shaking-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/markmur%2Fwebpack-css-tree-shaking-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/markmur%2Fwebpack-css-tree-shaking-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/markmur%2Fwebpack-css-tree-shaking-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/markmur","download_url":"https://codeload.github.com/markmur/webpack-css-tree-shaking-example/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251972418,"owners_count":21673600,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["css","css-modules","modules","tree-shaking","webpack"],"created_at":"2024-11-12T14:19:30.461Z","updated_at":"2025-05-02T02:30:38.440Z","avatar_url":"https://github.com/markmur.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CSS Modules with Tree Shaking\n\nThis repo shows how to use css-modules (with `css-loader`) and tree shake any used imports from _both_ the outputted CSS file as well as the js bundle.\n\n## Quick Reference\n\nYou will need to have the following dependencies installed:\n\n```sh\nyarn add @fullhuman/postcss-purgecss \\\n         css-loader \\\n         cssnano \\\n         mini-css-extract-plugin \\\n         node-sass \\\n         postcss-loader \\\n         postcss-scss \\\n         sass-loader \\\n         style-loader\n```\n\nHere is the webpack config for quick reference:\n\n```js\n/* eslint-env node */\n\nconst path = require('path')\nconst glob = require('glob')\nconst webpack = require('webpack')\n\nconst MiniCssExtractPlugin = require('mini-css-extract-plugin')\n\nconst entry = filename =\u003e [path.resolve(__dirname, filename)]\n\nmodule.exports = {\n  mode: 'production',\n\n  entry: entry('src/index.js'),\n\n  output: {\n    filename: '[name].js',\n    path: path.resolve('dist'),\n  },\n\n  resolve: {\n    extensions: ['.js'],\n  },\n\n  module: {\n    rules: [\n      {\n        test: /\\.jsx?$/,\n        loader: 'babel-loader',\n        exclude: /node_modules/,\n      },\n      {\n        test: /\\.s?css$/,\n        exclude: /node_modules/,\n        use: [\n          // Extract the css to file\n          MiniCssExtractPlugin.loader,\n          { \n            loader: require.resolve('css-loader'), \n            options: { \n              // Enable CSS modules\n              modules: {\n                // Specify format of class names\n                localIdentName: '[local]_[hash:base64:5]'\n              },\n            } \n          },\n          {\n            loader: require.resolve('postcss-loader'),\n            options: {\n              indent: 'postcss',\n              syntax: 'postcss-scss',\n              plugins: () =\u003e [\n                // Purge unused CSS\n                require('@fullhuman/postcss-purgecss')({\n                  content: glob.sync('src/**/*.{js,jsx}', { nodir: true }),\n                  extractors: [\n                    {\n                      extractor: class {\n                        static extract(content) {\n                          // NOTE: this regex needs work. At the moment it simply matches any keyword that matches a class name. If you were to have a class name \"alert\" and create a react component with a text body that has the word \"alert\" in it, it would result in the alert className being unnecessarily imported.\n                          return content.match(/\\w+/g) || [];\n                        }\n                      },\n                      extensions: ['js', 'jsx' ]\n                    }\n                  ]\n                }),\n                require('cssnano')\n              ]\n            }\n          },\n          require.resolve('sass-loader')\n        ],\n      },\n    ],\n  },\n  plugins: [\n    // Extract all css into a separate file\n    new MiniCssExtractPlugin({\n      filename: '[name].css',\n    }),\n  ],\n}\n\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarkmur%2Fwebpack-css-tree-shaking-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarkmur%2Fwebpack-css-tree-shaking-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarkmur%2Fwebpack-css-tree-shaking-example/lists"}