fix: css url public path#17170
Conversation
|
For maintainers only:
|
lib/css/CssModulesPlugin.js
Outdated
|
|
||
| const moduleSourceCode = moduleSource.source().toString(); | ||
| const publicPathRegex = new RegExp( | ||
| CssUrlDependency.PUBLIC_PATH_PLACEHOLDER, |
There was a problem hiding this comment.
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.
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.
Is there any link about the source maps issues, would like to learn more.
There was a problem hiding this comment.
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.
Good to know, thanks
alexander-akait
left a comment
There was a problem hiding this comment.
Sorry, wrong solution
|
@ahabhgk I tried different solution and I think I found. We have two problems here:
Time to finish CSS feature |
| (match.index += match[0].length - 1), | ||
| publicPath | ||
| ); | ||
| } |
There was a problem hiding this comment.
Ideally we need to use ReplaceSource here for caching, just an idea
There was a problem hiding this comment.
Did you mean CachedSource?
There was a problem hiding this comment.
There was a problem hiding this comment.
Oh I see, add cache for css renderModule result just like _moduleFactoryCache added in js renderModule
There was a problem hiding this comment.
Yeah, feel free to try it, should work without perf and source maps problems
alexander-akait
left a comment
There was a problem hiding this comment.
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.
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.
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.
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
alexander-akait
left a comment
There was a problem hiding this comment.
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? |
|
@ahabhgk Can you rebase, thank you ⭐ |
5be76c5 to
9ed5255
Compare
|
Thank you, will look soon and we will make a release ⭐ |
|
Thank you, release will be tomorrow with a lot of fixes and features |
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
ReplaceSourceclass andgetUndoPathfunction to enable replacing public path placeholder in CSS module source (link, link)filenameandinfoproperties of CSS asset by callingcompilation.getPathWithInfoand computepublicPathproperty usinggetUndoPathorcompilation.getAssetPath(link)filenameTemplateandpathOptionsproperties of CSS asset withfilename,info,publicPath, andmodulesproperties (link)publicPathparameter torenderContentAssetmethod ofCssModulesPluginclass and wrapmoduleSourceinReplaceSourceinstance to replace public path placeholder (link, link, link)PUBLIC_PATH_PLACEHOLDERconstant as a special string to mark public path in CSS url dependencies and use it inruntimeTemplate.assetUrlcalls inCssUrlDependencyTemplateclass (link, link, link, link)test/configCases/css/urls-css-filename/index.jsto check styles indiv.css(link)nested.cssfile totest/configCases/css/urls-css-filename/to test nested import and url handling (link)webpack.config.jsfile totest/configCases/css/urls-css-filename/to set up output options, CSS experiments, and split chunks optimization for test case (link)