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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: minimize css when css experiment is enabled #17309

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/config/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -1351,6 +1351,12 @@ const applyOptimizationDefaults = (
}
}
}).apply(compiler);

// if css experiment is enabled, use css-minimizer-webpack-plugin
if (css) {
const CSSMinimizerWebpackPlugin = require("css-minimizer-webpack-plugin");
new CSSMinimizerWebpackPlugin().apply(compiler);
}
}
}
]);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"acorn-import-assertions": "^1.9.0",
"browserslist": "^4.14.5",
"chrome-trace-event": "^1.0.2",
"css-minimizer-webpack-plugin": "^5.0.0",
"enhanced-resolve": "^5.14.1",
"es-module-lexer": "^1.2.1",
"eslint-scope": "5.1.1",
Expand Down
5 changes: 4 additions & 1 deletion test/ConfigTestCases.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ const describeCases = config => {
options.optimization.minimizer = [
new (require("terser-webpack-plugin"))({
parallel: false
})
}),
...(options.experiments && options.experiments.css
? [new (require("css-minimizer-webpack-plugin"))()]
: [])
];
}
if (!options.entry) options.entry = "./index.js";
Expand Down
10 changes: 10 additions & 0 deletions test/configCases/optimization/css-minimize/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import "./style.css";

it("should compile", done => {
import("./style.css").then(x => {
expect(x).toEqual(nsObj({}));
const style = getComputedStyle(document.body);
expect(style.getPropertyValue("background")).toBe("red");
done();
}, done);
});
3 changes: 3 additions & 0 deletions test/configCases/optimization/css-minimize/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background: red;
}
8 changes: 8 additions & 0 deletions test/configCases/optimization/css-minimize/test.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
moduleScope(scope) {
const link = scope.window.document.createElement("link");
link.rel = "stylesheet";
link.href = "bundle0.css";
scope.window.document.head.appendChild(link);
}
};
35 changes: 35 additions & 0 deletions test/configCases/optimization/css-minimize/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const Compilation = require("../../../../").Compilation;
const Source = require("webpack-sources").Source;

/** @type {import("../../../../").Configuration} */
module.exports = {
target: "web",
experiments: {
css: true
},
optimization: {
minimize: true
},
plugins: [
compiler => {
const files = {};
compiler.hooks.assetEmitted.tap(
"Test",
(file, { content, source, outputPath, compilation, targetPath }) => {
expect(Buffer.isBuffer(content)).toBe(true);
expect(source).toBeInstanceOf(Source);
expect(typeof outputPath).toBe("string");
expect(typeof targetPath).toBe("string");
expect(compilation).toBeInstanceOf(Compilation);
files[file] = content.toString("utf-8");
}
);
compiler.hooks.afterEmit.tap("Test", () => {
// css should be minimized
expect(files["bundle0.css"]).toMatchInlineSnapshot(
`"body{background:red}head{--webpack-179:_258}"`
);
});
}
]
};