-
Notifications
You must be signed in to change notification settings - Fork 71
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: expose loader context and virtual write to preprocess #144
Conversation
b8b0064
to
517bc5c
Compare
We removed the virtual modules in #151 so I suspect this PR won't work anymore |
Looks like it just moved to a dependency. I can update this to match the new code if it seems like something y |
I don't think it was moved to a dependency but was actually removed. I'm not a webpack expert though. It's hard for me to give you an answer as to whether we'd accept this because I'm not really sure what it'd look like in the new world. This PR is mostly touching |
O I see @non25 you did the |
@jquense We also ran into problems when using subsequent css loaders, but the solution to the problem is to add a link to the existing file at the end of the import. For example, to the same source file.
It's already done in svelte-loader. So there are no problems with subsequent css-loaders. |
It's not like we've tested it in all scenarios where published I've tested published The way to make |
@jquense It also works with |
I'm most concerned about @jquense's suggestion that postcss or sass imports might not work (webpack/webpack#11074 (comment)). Has anyone tested either of those with the code in I'm less worried about |
I'm using sass in my project with
If It will defenitily help when using tailwind through |
I trust ya'll have tested the approach I was more curious how y'all solved the issues I had run into trying this in another project. I guess one other downside now is that it's harder for other files to import the extracted css since it's not a file path anymore. Admittedly that is pretty dang unlikely in svelte's context, I don't see why some other file would want to @non25 last question, when appending the resource |
I think then we would have to hack webpack's compiler like in |
I've just tried with following css pipeline:
I've tried to use following import:
It works on first build, but doesn't work on watch-rebuild.
We should answer the question: "Why do we want to process css files through the webpack css pipeline when we have svelte-preprocess?" For example, if the imported file contains classes, they will not be hashed by the Svelte compiler if the file is processed by the webpack pipeline. It seems that css processing via webpack only has one purpose: it should be merged into one file with the other pieces of css without any additional processing or with minimal additional processing. |
Maybe we should change or make an option to change output extension ? |
Maybe we should look into the vue-loader approach, but it's a much more complex SFC loader. |
The other use case that comes to mind for me is css code splitting, but that's similar in that it's just dividing up how css is put in files and not actually changing the contents |
There are few reasons to prefer webpack's pipeline:
The last one is the space I've been exploring in particular with my own CSS preprocessor. I originally wanted this PR to completely opt out of svelte's css handling by removing the style and emitting the uncompiled style for webpack to handle and optimize via another loader. That's a bit here nor there, but generally is the svelte-preprocess model precludes more interesting cross file compilation optimizations for non-JS languages since you need to compile away those languages before the tool with dep graph knowledge can see them. I'll just say that this is totally not a common use-case, i'm actively experimenting here, so was looking for reasonable escape hatches, not first class support (yet). it does seem tho like since the VM's were removed there isn't much to add here. It may make sense to expose some option to adjust the file extension in the future but i can hack that in and see if it helps for my experiments. |
I feel like if you prefer webpack's pipeline and want to use css modules you would be better off opting out of SFC. I don't see much of a choice for general svelte usage, since As to making consistent configurations for external css and svelte component css... I see two steps for css:
So ideally, for svelte, you would want to make mergable preprocess and postprocess parts. What problems these aren't solving ?
Can you make an example ? What tooling you are having problems with ?
I don't see how this fits svelte's scoping mindset. As to last paragraph I don't understand what you are getting at. What problems you are trying to solve? To conclude, |
To be clear i'm not advocating for using virtual modules, this PR was an attempt to simply add an escape hatch, and since the underlying approach changed its not really relevant any more. I think we're veering into a more general discussion about pain-points of using compiled style languages with svelte, and downsides with svelte's css scoping approach. I can open a separate issue about that with more clearly stated issues. In the meantime I'm fine forking svelte-loader if needed (that's where this came from) especially since i'm not the general use-case. |
👋 Hey folks. Not trying to be presumptuous, but thought this proposal would be best served with a PR.
This allows the
preprocess
option to be a function that is passed the webpack loader context as well as the virtualwriteModule
method. I don't expect this to be commonly used, but allows for more advanced integration into the webpack build pipeline by preprocessors.The impetus for this is trying to add better css-module (and related languages) support for svelte style tags, but this applies to any preprocessor that might need to do further downstream processing of the resulting styles, markup or js. In my case I need to do further coordination with style files after they have been extracted from the svelte file. Basically this requires doing a very similar trick to what svelte-loader does, by removing the style markup, emitting a (non
.css
) file and adding an import to that file to the script portion of the component. This bypasses svelte's styling handling but lets webpack process the file further with additional loaders.This is helpful for few reasons:
.my-css
, and.svelte.css
Alternatives to this involve sharing some manner of stateful compiler instance that is passed to both the specific svelte preprocess function and a loader config.
I realize this use-case is hard to articulate, but if it helps there is some prior art for this sort of thing with
postcss-loader
: https://github.com/webpack-contrib/postcss-loader#function I can also point to my local experiments adding support for my css-module light preprocessor if that might help clarify the need.