-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
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
fix: css url public path #17170
base: main
Are you sure you want to change the base?
fix: css url public path #17170
Conversation
For maintainers only:
|
lib/css/CssModulesPlugin.js
Outdated
@@ -501,6 +519,21 @@ class CssModulesPlugin { | |||
codeGenResult.sources.get("css") || | |||
codeGenResult.sources.get("css-import"); | |||
|
|||
const moduleSourceCode = moduleSource.source().toString(); | |||
const publicPathRegex = new RegExp( | |||
CssUrlDependency.PUBLIC_PATH_PLACEHOLDER, |
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.
It breaks source maps, we should not do it, we should rewrite assets modules and generate the valid URL there
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.
Yes, we use the same logic in mini-css-extract-plugin, but it was wrong and yeah our source maps are broken in some cases, there are issues about 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.
Is there any link about the source maps issues, would like to learn more.
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.
Just make an URL with a newline and put somethings CSS on the same line.
Also if you look your bundled JS file, it will contain asset module, this is wrong, so the solution is refactor assets module and make it compatibility with CSS
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.
Good to know, thanks
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.
Sorry, wrong solution
@ahabhgk I tried different solution and I think I found. We have two problems here:
Time to finish CSS feature |
lib/css/CssModulesPlugin.js
Outdated
(match.index += match[0].length - 1), | ||
publicPath | ||
); | ||
} |
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.
Ideally we need to use ReplaceSource
here for caching, just an idea
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.
Did you mean CachedSource
?
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.
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.
Oh I see, add cache for css renderModule result just like _moduleFactoryCache
added in js renderModule
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.
Yeah, feel free to try it, should work without perf and source maps problems
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.
And we need more test:
- When asset above CSS file
- When asset on the same level
- When asset in nested directory
Each test should be for public path auto public path with string value
Also we have https://webpack.js.org/configuration/module/#rulegeneratoroutputpath, so we need to add a test case too
A lot of tests... If you have time you can add them to your solution to ensure the idea is working |
And feel free to rebase |
33215ce
to
c0c1ac8
Compare
@ahabhgk Thanks for your update. I labeled the Pull Request so reviewers will review it again. @alexander-akait Please review the new changes. |
1af0127
to
b64d1a5
Compare
lib/css/CssModulesPlugin.js
Outdated
) { | ||
source = cacheEntry.source; | ||
} else { | ||
const moduleSourceCode = moduleSourceContent.source().toString(); |
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.
Do we need to use .toString()
? It is very bad for perf
hash: compilation.hash | ||
} | ||
); | ||
assetPathForCss = path + filename; |
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.
Maybe we can generate this only when asset module in CSS dependecy to avoid extra work, because we still need to improve it, have
runtimeRequirements.add(RuntimeGlobals.publicPath);
, and so we generate extra JS code if you have an asset in your CSS (but not in JS), and in future we will need to fix it too (I described it above)
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.
Yes, both need the asset module to aware whether it is only referenced by a css module or a js module, since it's not very related to this PR, I can give a try in the future PR
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.
Thank you look good, I want to make some investigation about perf and more test cases (I will do it), so I will finish and merge it for the next release ❤️
I want to make a release on this week with this fix, so don't worry, it is not abadoned, will review in the next few days |
/cc @vankop Can you look at this too? |
Summary
fixes #16969
🤖 Generated by Copilot at 33215ce
This pull request adds a new feature to the CSS modules plugin that allows using a placeholder for the public path in the CSS source and dependencies. This enables the public path to be resolved at runtime instead of being hardcoded in the generated CSS assets. It also adds a test case to verify the functionality and refactors some code for clarity and consistency.
Details
🤖 Generated by Copilot at 33215ce
ReplaceSource
class andgetUndoPath
function to enable replacing public path placeholder in CSS module source (link, link)filename
andinfo
properties of CSS asset by callingcompilation.getPathWithInfo
and computepublicPath
property usinggetUndoPath
orcompilation.getAssetPath
(link)filenameTemplate
andpathOptions
properties of CSS asset withfilename
,info
,publicPath
, andmodules
properties (link)publicPath
parameter torenderContentAsset
method ofCssModulesPlugin
class and wrapmoduleSource
inReplaceSource
instance to replace public path placeholder (link, link, link)PUBLIC_PATH_PLACEHOLDER
constant as a special string to mark public path in CSS url dependencies and use it inruntimeTemplate.assetUrl
calls inCssUrlDependencyTemplate
class (link, link, link, link)test/configCases/css/urls-css-filename/index.js
to check styles indiv.css
(link)nested.css
file totest/configCases/css/urls-css-filename/
to test nested import and url handling (link)webpack.config.js
file totest/configCases/css/urls-css-filename/
to set up output options, CSS experiments, and split chunks optimization for test case (link)