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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support stylesheets with link tag and media/disable prop #6751

Merged
merged 5 commits into from Jul 25, 2022
Merged
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
9 changes: 8 additions & 1 deletion packages/vite/src/node/plugins/html.ts
Expand Up @@ -354,7 +354,14 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
// assetsUrl may be encodeURI
const url = decodeURI(p.value.content)
if (!isExcludedUrl(url)) {
if (node.tag === 'link' && isCSSRequest(url)) {
if (
node.tag === 'link' &&
isCSSRequest(url) &&
// should not be converted if following attributes are present (#6748)
!node.props.some(
(p) => p.name === 'media' || p.name === 'disabled'
)
) {
// CSS references, convert to import
const importExpression = `\nimport ${JSON.stringify(url)}`
styleUrls.push({
Expand Down
7 changes: 7 additions & 0 deletions playground/html/__tests__/html.spec.ts
Expand Up @@ -212,6 +212,13 @@ describe('Unicode path', () => {
})
})

describe('link with props', () => {
test('separate links with different media props', async () => {
await page.goto(viteTestUrl + '/link-props/index.html')
expect(await getColor('h1')).toBe('red')
})
})

describe.runIf(isServe)('invalid', () => {
test('should be 500 with overlay', async () => {
const response = await page.goto(viteTestUrl + '/invalid.html')
Expand Down
4 changes: 4 additions & 0 deletions playground/html/link-props/index.html
@@ -0,0 +1,4 @@
<link rel="stylesheet" href="./screen.css" media="screen" />
<link rel="stylesheet" href="./print.css" media="print" />

<h1 id="link-props">test color</h1>
3 changes: 3 additions & 0 deletions playground/html/link-props/print.css
@@ -0,0 +1,3 @@
#link-props {
color: green;
}
3 changes: 3 additions & 0 deletions playground/html/link-props/screen.css
@@ -0,0 +1,3 @@
#link-props {
color: red;
}
3 changes: 2 additions & 1 deletion playground/html/vite.config.js
Expand Up @@ -25,7 +25,8 @@ module.exports = {
unicodePath: resolve(
__dirname,
'unicode-path/中文-にほんご-한글-🌕🌖🌗/index.html'
)
),
linkProps: resolve(__dirname, 'link-props/index.html')
}
}
},
Expand Down