fix: less @plugin imports of javascript files treated as CSS and rebased (fix #19268) #19269
+35
−1
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.
Description
Fixes #19268:
Less files can contain
@plugin "plugin.js";
style imports, which reference JavaScript files. When Vite runs on a.less
file that contains a@plugin
import themakeLessWorker
function callsviteLessResolve
with theplugin.js
file (in this example), which then callsrebaseUrls
, which is expecting to have received a CSS file and runs a bunch of regex including rebasingurl(...)
s, which is breaking my JavaScript file which contains that string!This PR resolves the issue by using the
mime
value that comes from less and indicates that it's loading a JavaScript file. For.less
files thismime
value isundefined
, so checking explicitly forapplication/javascript
seems like a good option. There is no doubt another option for detecting this situation (when we shouldn't treat the file as CSS and rebase it).I have added a test case based on the example...
Example
I have setup a basic example of this bug at https://github.com/karlvr/vite-less-plugin-bug:
src/main.less
file imports thesrc/plugins/test.js
plugin and then uses it in thebackground-image
attribute.src/plugins/test.js
includes the stringurl(
to demonstrate the bug, asrebaseUrls
mistakenly transforms it, thinking the file is CSSdist/assets/index-CWZCrLGA.css
demonstrates the output; theurl(...)
, which should just contain the data uri, is prefixed with the base of thesrc/plugins/test.js
file:url(plugins/data:image/png;base64...