Skip to content

Commit 8156684

Browse files
authored
feat(css): minify style tag (#23183)
1 parent b78e2f1 commit 8156684

5 files changed

Lines changed: 55 additions & 1 deletion

File tree

‎packages/vite/src/node/plugins/css.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ const commonjsProxyRE = /[?&]commonjs-proxy/
248248
const inlineRE = /[?&]inline\b/
249249
const inlineCSSRE = /[?&]inline-css\b/
250250
const styleAttrRE = /[?&]style-attr\b/
251+
const styleTagCloseRE = /<\/style(?=[\t\n\f\r />])/gi
251252
const functionCallRE = /^[A-Z_][.\w-]*\(/i
252253
const transformOnlyRE = /[?&]transform-only\b/
253254
const nonEscapedDoubleQuoteRe = /(?<!\\)"/g
@@ -563,6 +564,11 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
563564
if (inlineCSS && isHTMLProxy) {
564565
if (styleAttrRE.test(id)) {
565566
css = css.replace(/"/g, '&quot;')
567+
} else {
568+
if (config.command !== 'serve' && config.build.cssMinify) {
569+
css = await minifyCSS(css, config, true, id)
570+
}
571+
css = css.replace(styleTagCloseRE, '<\\/style')
566572
}
567573
const index = htmlProxyIndexRE.exec(id)?.[1]
568574
if (index == null) {
@@ -3392,7 +3398,7 @@ async function compileLightningCSS(
33923398
return id
33933399
},
33943400
},
3395-
minify: config.isProduction && !!config.build.cssMinify,
3401+
minify: false,
33963402
sourceMap:
33973403
config.command === 'build'
33983404
? !!config.build.sourcemap

‎playground/css-lightningcss/__tests__/css-lightningcss.spec.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,18 @@ test.runIf(isBuild)('minify css', async () => {
8080
expect(cssFile).not.toMatch('#ffff00b3')
8181
})
8282

83+
test.runIf(isBuild)('minify inline style', () => {
84+
expect(readFile('dist/index.html')).toContain(
85+
'<style>.inline-style{color:red}</style>',
86+
)
87+
})
88+
89+
test.runIf(isBuild)('escapes closing style tags in inline CSS', () => {
90+
expect(readFile('dist/index.html')).toContain(
91+
'<style>.escaped-closing-style{content:"<\\/style>"}</style>',
92+
)
93+
})
94+
8395
test.runIf(isBuild)('does not run the visitor again during minify', () => {
8496
expect(readFile('dist/media-query-visits.txt')).toBe('2')
8597
})

‎playground/css-lightningcss/index.html‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
<link rel="stylesheet" href="./linked.css" />
22

3+
<style>
4+
.inline-style {
5+
color: red;
6+
}
7+
</style>
8+
9+
<style>
10+
.escaped-closing-style {
11+
content: '<\/style>';
12+
}
13+
</style>
14+
315
<div class="wrapper">
416
<h1>Lightning CSS</h1>
517

‎playground/minify/__tests__/minify.spec.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,15 @@ test.runIf(isBuild)('no minifySyntax', () => {
1919
expect(cssContent).toContain('color:#ff0000')
2020
expect(cssContent).not.toContain('/*! explicit comment */')
2121
})
22+
23+
test.runIf(isBuild)('minifies inline style with esbuild', () => {
24+
expect(readFile('dist/index.html')).toContain(
25+
'<style>.inline-style{color:#ff0000}</style>',
26+
)
27+
})
28+
29+
test.runIf(isBuild)('escapes closing style tags with esbuild', () => {
30+
expect(readFile('dist/index.html')).toContain(
31+
'<style>.escaped-closing-style{content:"<\\/style>"}</style>',
32+
)
33+
})

‎playground/minify/index.html‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
11
<h1>Minify</h1>
22

3+
<style>
4+
.inline-style {
5+
color: #ff0000;
6+
}
7+
</style>
8+
9+
<style>
10+
.escaped-closing-style {
11+
content: '<\/style>';
12+
}
13+
</style>
14+
315
<script type="module" src="./main.js"></script>

0 commit comments

Comments
 (0)