Skip to content

Commit e6c8965

Browse files
pd4d10tomayacbluwy
authored
fix: support stylesheets with link tag and media/disable prop (#6751)
Co-authored-by: Thomas Steiner <[email protected]> Co-authored-by: bluwy <[email protected]>
1 parent 2fcb027 commit e6c8965

File tree

6 files changed

+27
-2
lines changed

6 files changed

+27
-2
lines changed

packages/vite/src/node/plugins/html.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,14 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
354354
// assetsUrl may be encodeURI
355355
const url = decodeURI(p.value.content)
356356
if (!isExcludedUrl(url)) {
357-
if (node.tag === 'link' && isCSSRequest(url)) {
357+
if (
358+
node.tag === 'link' &&
359+
isCSSRequest(url) &&
360+
// should not be converted if following attributes are present (#6748)
361+
!node.props.some(
362+
(p) => p.name === 'media' || p.name === 'disabled'
363+
)
364+
) {
358365
// CSS references, convert to import
359366
const importExpression = `\nimport ${JSON.stringify(url)}`
360367
styleUrls.push({

playground/html/__tests__/html.spec.ts

+7
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,13 @@ describe('Unicode path', () => {
212212
})
213213
})
214214

215+
describe('link with props', () => {
216+
test('separate links with different media props', async () => {
217+
await page.goto(viteTestUrl + '/link-props/index.html')
218+
expect(await getColor('h1')).toBe('red')
219+
})
220+
})
221+
215222
describe.runIf(isServe)('invalid', () => {
216223
test('should be 500 with overlay', async () => {
217224
const response = await page.goto(viteTestUrl + '/invalid.html')

playground/html/link-props/index.html

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<link rel="stylesheet" href="./screen.css" media="screen" />
2+
<link rel="stylesheet" href="./print.css" media="print" />
3+
4+
<h1 id="link-props">test color</h1>

playground/html/link-props/print.css

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#link-props {
2+
color: green;
3+
}

playground/html/link-props/screen.css

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#link-props {
2+
color: red;
3+
}

playground/html/vite.config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ module.exports = {
2525
unicodePath: resolve(
2626
__dirname,
2727
'unicode-path/中文-にほんご-한글-🌕🌖🌗/index.html'
28-
)
28+
),
29+
linkProps: resolve(__dirname, 'link-props/index.html')
2930
}
3031
}
3132
},

0 commit comments

Comments
 (0)