Skip to content

Commit 93478af

Browse files
committed
fix(beasties): strip query/hash when resolving css filenames
1 parent e0caef0 commit 93478af

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

packages/beasties/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ export default class Beasties {
197197

198198
// CHECK - the output path
199199
// path on disk (with output.publicPath removed)
200-
let normalizedPath = href.replace(/^\//, '')
200+
let normalizedPath = href.replace(/^\/|[?#].*$/g, '')
201201
const pathPrefix = `${(publicPath || '').replace(/(^\/|\/$)/g, '')}/`
202202
if (normalizedPath.startsWith(pathPrefix)) {
203203
normalizedPath = normalizedPath

packages/beasties/test/beasties.test.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,81 @@ describe('beasties', () => {
513513
fs.rmSync(tmpDir, { recursive: true })
514514
})
515515

516+
it('handles stylesheets with query strings', async () => {
517+
const beasties = new Beasties({
518+
reduceInlineStyles: false,
519+
path: '/',
520+
})
521+
const assets: Record<string, string> = {
522+
'/style.css': 'h1 { color: blue; } h2.unused { color: red; }',
523+
}
524+
beasties.readFile = filename => assets[filename.replace(/^\w:/, '').replace(/\\/g, '/')]!
525+
526+
const result = await beasties.process(trim`
527+
<html>
528+
<head>
529+
<link rel="stylesheet" href="/style.css?v=123">
530+
</head>
531+
<body>
532+
<h1>Hello World!</h1>
533+
</body>
534+
</html>
535+
`)
536+
537+
expect(result).toContain('<style>h1{color:blue}</style>')
538+
expect(result).toContain('/style.css?v=123')
539+
})
540+
541+
it('handles stylesheets with hash fragments', async () => {
542+
const beasties = new Beasties({
543+
reduceInlineStyles: false,
544+
path: '/',
545+
})
546+
const assets: Record<string, string> = {
547+
'/style.css': 'h1 { color: green; } h2.unused { color: red; }',
548+
}
549+
beasties.readFile = filename => assets[filename.replace(/^\w:/, '').replace(/\\/g, '/')]!
550+
551+
const result = await beasties.process(trim`
552+
<html>
553+
<head>
554+
<link rel="stylesheet" href="/style.css#section">
555+
</head>
556+
<body>
557+
<h1>Hello World!</h1>
558+
</body>
559+
</html>
560+
`)
561+
562+
expect(result).toContain('<style>h1{color:green}</style>')
563+
expect(result).toContain('/style.css#section')
564+
})
565+
566+
it('handles stylesheets with both query strings and hash fragments', async () => {
567+
const beasties = new Beasties({
568+
reduceInlineStyles: false,
569+
path: '/',
570+
})
571+
const assets: Record<string, string> = {
572+
'/style.css': 'h1 { color: purple; } h2.unused { color: red; }',
573+
}
574+
beasties.readFile = filename => assets[filename.replace(/^\w:/, '').replace(/\\/g, '/')]!
575+
576+
const result = await beasties.process(trim`
577+
<html>
578+
<head>
579+
<link rel="stylesheet" href="/style.css?v=456#section">
580+
</head>
581+
<body>
582+
<h1>Hello World!</h1>
583+
</body>
584+
</html>
585+
`)
586+
587+
expect(result).toContain('<style>h1{color:purple}</style>')
588+
expect(result).toContain('/style.css?v=456#section')
589+
})
590+
516591
it('ignores remote stylesheets by default', async () => {
517592
const beasties = new Beasties({
518593
reduceInlineStyles: false,

0 commit comments

Comments
 (0)