-
-
Notifications
You must be signed in to change notification settings - Fork 304
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
feat(rollup-plugin-html): glob patterns exclusion for external assets #2671
Changes from all commits
49a4007
745b6d7
1420d75
1815649
50c410e
c30651e
5e4af37
6207e41
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@web/rollup-plugin-html': minor | ||
--- | ||
|
||
glob patterns exclusion for external assets |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import { Document, Element } from 'parse5'; | ||
import path from 'path'; | ||
import picomatch from 'picomatch'; | ||
import { findElements, getTagName, getAttribute } from '@web/parse5-utils'; | ||
import { createError } from '../utils.js'; | ||
import { serialize } from 'v8'; | ||
|
@@ -143,3 +144,11 @@ export function getSourcePaths(node: Element) { | |
export function findAssets(document: Document) { | ||
return findElements(document, isAsset); | ||
} | ||
|
||
// picomatch follows glob spec and requires "./" to be removed for the matcher to work | ||
// it is safe, because with or without it resolves to the same file | ||
// read more: https://github.com/micromatch/picomatch/issues/77 | ||
const removeLeadingSlash = (str: string) => (str.startsWith('./') ? str.slice(2) : str); | ||
export function createAssetPicomatchMatcher(glob?: string | string[]) { | ||
return picomatch(glob || [], { format: removeLeadingSlash }); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this was pretty unexpected, but the proposed workaround from the picomatch maintainer seems to be good for us I added different variations in the test, with and without |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#a1 { | ||
background-image: url('image-a.png'); | ||
} | ||
|
||
#a2 { | ||
background-image: url('image-a.svg'); | ||
} | ||
|
||
#d1 { | ||
background-image: url('./image-d.png'); | ||
} | ||
|
||
#d2 { | ||
background-image: url('./image-d.svg'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. some deduplication of images was happening, I think because we try to identify images with same hash and merge them into one, so it's a feature of this plugin the deduplication continues to work correctly, it's just that test would be hard to read if I mix so much different functionality in one place |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can't we just
path.normalize
it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to keep modifications to the original paths used in source files to a minimum
path.normalize
would solve it too, but might make other unexpected modifications which might influence the matcher behaviornot sure we should use that to be honest