Skip to content

Instantly share code, notes, and snippets.

@jdb8
Last active March 14, 2020 01:18
Show Gist options
  • Save jdb8/94bcd9892349fd2865c3c6fb2ef40848 to your computer and use it in GitHub Desktop.
Save jdb8/94bcd9892349fd2865c3c6fb2ef40848 to your computer and use it in GitHub Desktop.
The default mini-css-extract-plugin + css-loader, when run with webpack --production, seem to tree-shake css module classname mappings and inline the referenced names directly as strings.
import styles from "./styles.css";
const baz = styles.baz;
console.log("classname for baz", baz);
// output of webpack's main.js with boilerplate removed.
// No css mappings appear here, and only the value of our referenced classname ('baz')
// shows up in the bundle at all
function(e, t, r) {
r.r(t);
console.log("classname for baz", "_1Kxdli63PQyf_5b_GTMl04");
}
// output from webpack, note that unused classnames do not appear
!function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=0)}([function(e,t,r){"use strict";r.r(t);console.log("classname for baz","_1Kxdli63PQyf_5b_GTMl04")}]);
.foo-bar {
color: red;
}
.baz {
color: green;
}
.delete {
background-color: hotpink;
}
.unused {
color: purple;
}
.another-unused {
background: white;
}
const MiniCSSExtractPlugin = require("mini-css-extract-plugin");
module.exports = {
devtool: false,
module: {
rules: [
{
test: /\.css$/,
use: [
{
loader: MiniCSSExtractPlugin.loader,
options: {
esModule: true
}
},
{
loader: "css-loader",
options: {
modules: true,
esModule: true
}
}
]
}
]
},
plugins: [new MiniCSSExtractPlugin({})]
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment