Repository navigation
Add relativeUrls option for sprite URLs in library builds - #25
Open
NullVoxPopuli wants to merge 1 commit into
Open
NullVoxPopuli wants to merge 1 commit into
NullVoxPopuli wants to merge 1 commit into
Conversation
With relativeUrls, renderChunk writes each sprite reference as new URL('./assets/sprite-<hash>.svg', import.meta.url).href + '#<id>'.
The path is relative to the chunk that holds it.
The app that consumes the library then resolves the sprite with its own bundler, under its own base.
Without the option, output is unchanged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds a
relativeUrlsoption.With it, library builds point at their sprite relative to the chunk, so the consuming app serves it under the app's own base.
Why
A library built with tsdown/Rolldown bakes
baseinto its sprite URLs at build time.It can't know the base of each app that consumes it.
An app served at
/pr-previews/pr-1234/or/__demo__/foo/then requests/assets/sprite-<hash>.svgfrom the domain root, and the icons don't render.With
new URL(..., import.meta.url), the app's bundler does the work.Vite rewrites the URL under its
basein dev, and copies the sprite into the app's build with a hash.The library no longer needs the app to serve
dist/assetsat a fixed path.Notes
false, so existing output is unchanged.chunk.fileName, so chunks in subdirectories get../assets/....#<id>fragment is appended after.href, so the bundler sees a plain file URL.?fileimports and refs inside sprite sheets still usebase.I kept them out of scope, and the option docs say so.
Testing
../from a nested chunk, and the sprite is still emitted.resolveOptionsunit tests for the default.The dev server serves the sprite under a non-root base.
vite buildemits one hashed sprite and writes<base>assets/sprite-<hash>-<vitehash>.svginto the code.