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

feat(rollup-plugin-html): resolves assets in styles #2664

Merged
merged 6 commits into from
Mar 14, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: simplify svg regex
thepassle committed Mar 14, 2024
commit dd95a7872ebacc1e2b68626f4bf687e0c0f8a5df
6 changes: 3 additions & 3 deletions packages/rollup-plugin-html/src/output/emitAssets.ts
Original file line number Diff line number Diff line change
@@ -12,8 +12,7 @@ export interface EmittedAssets {
}

const allowedFileExtensions = [
// https://www.w3.org/TR/html4/types.html#:~:text=ID%20and%20NAME%20tokens%20must,tokens%20defined%20by%20other%20attributes.
/.*\.svg(#[A-Za-z][A-Za-z0-9\-_:.]*)?/,
/.*\.svg/,
/.*\.png/,
/.*\.jpg/,
/.*\.jpeg/,
@@ -93,9 +92,10 @@ export async function emitAssets(
visitor: {
Url: url => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this recursively follow all css files includes (the @import ones)?

if yes, looks like source = Buffer.from(code); would not update anything but the entry point

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code as currently implemented does not bundle css via @import. I looked at both esbuild and lightningcss, and they have a bundle for css, but it only follows @import, and not url, which is what we need. So for now I only added support for assets referenced via url, afaik you can't add .css files in url. In the future we could consider bundling imported css

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clear, makes sense!

// Support foo.svg#bar
// https://www.w3.org/TR/html4/types.html#:~:text=ID%20and%20NAME%20tokens%20must,tokens%20defined%20by%20other%20attributes.
const [filePath, idRef] = url.url.split('#');

if (shouldHandleAsset(url.url)) {
if (shouldHandleAsset(filePath)) {
// Read the font file, get the font from the source location on the FS using asset.filePath
const assetLocation = path.resolve(path.dirname(asset.filePath), filePath);
const assetContent = fs.readFileSync(assetLocation);