Skip to content

Commit d832662

Browse files
authored
fix(turbopack): Call .minify() of lightningcss StyleSheet (#77313)
### What? Call `minify()` of `StyleSheet`. ### Why? We should call it regardless, as it's actually `transform()` with a bad name. See parcel-bundler/lightningcss#935 (comment) ### How - Closes #75526
1 parent 91c56d6 commit d832662

9 files changed

+27
-17
lines changed

test/integration/critical-css/test/index.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ function runTests() {
3939
expect(html).toMatch(
4040
/<link rel="stylesheet" href="\/_next\/static\/.*\.css" .*>/
4141
)
42-
expect(html).toMatch(/body{font-family:SF Pro Text/)
42+
expect(html).toMatch(/body{/)
4343
})
4444

4545
it('should inline critical CSS (dynamic)', async () => {
4646
const html = await renderViaHTTP(appPort, '/another')
4747
expect(html).toMatch(
4848
/<link rel="stylesheet" href="\/_next\/static\/.*\.css" .*>/
4949
)
50-
expect(html).toMatch(/body{font-family:SF Pro Text/)
50+
expect(html).toMatch(/body{/)
5151
})
5252

5353
it('should not inline non-critical css', async () => {

test/integration/css-minify/test/index.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ function runTests() {
2222
const css = await renderViaHTTP(appPort, href)
2323
if (process.env.TURBOPACK) {
2424
expect(css).toMatchInlineSnapshot(`
25-
"/* [project]/test/integration/css-minify/styles/global.css [client] (css) */
26-
.a{--var-1:0;--var-2:0;--var-1:-50%;--var-2:-50%}.b{--var-1:0;--var-2:0;--var-2:-50%}
25+
"/* [project]/test/integration/css-minify/styles/global.css [client] (css) */
26+
.a{--var-1:-50%;--var-2:-50%}.b{--var-1:0;--var-2:-50%}
2727
28-
/*# sourceMappingURL=test_integration_css-minify_styles_global_411632c3.css.map*/
29-
"
28+
/*# sourceMappingURL=test_integration_css-minify_styles_global_411632c3.css.map*/
29+
"
3030
`)
3131
} else {
3232
expect(css).toMatchInlineSnapshot(

test/integration/css-modules/test/index.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ describe('CSS Module Composes Usage (Basic)', () => {
522522
expect(
523523
cssContent.replace(/\/\*.*?\*\//g, '').trim()
524524
).toMatchInlineSnapshot(
525-
`".index-module__QppuLW__className{background:red;color:#ff0}.index-module__QppuLW__subClass{background:#00f;}"`
525+
`".index-module__QppuLW__className{color:#ff0;background:red}.index-module__QppuLW__subClass{background:#00f;}"`
526526
)
527527
} else {
528528
expect(

test/integration/css/test/basic-global-support.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ module.exports = {
534534
535535
536536
537-
.blue-text{color:orange;font-weight:bolder;background-image:url(../media/light.180573e4.svg)}
537+
.blue-text{color:orange;background-image:url(../media/light.180573e4.svg);font-weight:bolder}
538538
539539
540540
.blue-text{color:#00f}"
@@ -547,7 +547,7 @@ module.exports = {
547547
548548
549549
550-
.blue-text{color:orange;font-weight:bolder;background-image:url(../media/light.180573e4.svg)}
550+
.blue-text{color:orange;background-image:url(../media/light.180573e4.svg);font-weight:bolder}
551551
552552
553553
.blue-text{color:#00f}"
@@ -611,7 +611,7 @@ describe('CSS URL via `file-loader` and asset prefix (1)', () => {
611611
".red-text{color:red;background-image:url(../media/dark.993bedd3.svg) url(../media/dark2.993bedd3.svg)}
612612
613613
614-
.blue-text{color:orange;font-weight:bolder;background-image:url(../media/light.180573e4.svg)}
614+
.blue-text{color:orange;background-image:url(../media/light.180573e4.svg);font-weight:bolder}
615615
616616
.blue-text{color:#00f}"
617617
`)
@@ -670,7 +670,7 @@ describe('CSS URL via `file-loader` and asset prefix (2)', () => {
670670
671671
672672
673-
.blue-text{color:orange;font-weight:bolder;background-image:url(../media/light.180573e4.svg)}
673+
.blue-text{color:orange;background-image:url(../media/light.180573e4.svg);font-weight:bolder}
674674
675675
676676
.blue-text{color:#00f}"

turbopack/crates/turbopack-css/src/process.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::sync::{Arc, RwLock};
22

3-
use anyhow::{bail, Result};
3+
use anyhow::{bail, Context, Result};
44
use lightningcss::{
55
css_modules::{CssModuleExport, CssModuleExports, Pattern, Segment},
66
stylesheet::{ParserOptions, PrinterOptions, StyleSheet, ToCssResult},
@@ -191,6 +191,8 @@ pub async fn process_css_with_placeholder(
191191
_ => bail!("this case should be filtered out while parsing"),
192192
};
193193

194+
// We use NoMinify because this is not a final css. We need to replace url references,
195+
// and we do final codegen with proper minification.
194196
let (result, _) = stylesheet.to_css(&code, MinifyType::NoMinify, false, false)?;
195197

196198
let exports = result.exports.map(|exports| {
@@ -435,6 +437,14 @@ async fn process_content(
435437
}
436438
}
437439

440+
// minify() is actually transform, and it performs operations like CSS modules
441+
// handling.
442+
//
443+
//
444+
// See: https://github.com/parcel-bundler/lightningcss/issues/935#issuecomment-2739325537
445+
ss.minify(Default::default())
446+
.context("failed to transform css")?;
447+
438448
stylesheet_into_static(&ss, without_warnings(config.clone()))
439449
}
440450
Err(e) => {

turbopack/crates/turbopack-tests/tests/snapshot/css/embed-url/output/4e721_crates_turbopack-tests_tests_snapshot_css_embed-url_input_style_css_b7298ed4._.single.css

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

turbopack/crates/turbopack-tests/tests/snapshot/css/embed-url/output/b1abf_turbopack-tests_tests_snapshot_css_embed-url_input_style_module_css_b7298ed4._.single.css

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

turbopack/crates/turbopack-tests/tests/snapshot/css/embed-url/output/turbopack_crates_turbopack-tests_tests_snapshot_css_embed-url_input_14a7ded7._.css

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

turbopack/crates/turbopack-tests/tests/snapshot/cssmodules/composes/output/b1abf_turbopack-tests_tests_snapshot_cssmodules_composes_input_index_module_22660277.css

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)