Open Sydney-o9 opened 6 years ago
I am interested too for a such twig extension.
I will work on it and share it before the end of the year.
Hello! We use symfony + react + webpack and I want to share our solutions with you. About css modules in react components and twig:
For example you have some css module file, some-widget.css:
.container {
// some styles
}
.content {
// some styles
}
Add to webpack.config.js post css plugin – css-modules to css loader – https://github.com/css-modules/postcss-modules like this:
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
...
{ test: /.css$/, use: [ { loader: MiniCssExtractPlugin.loader }, { loader: 'css-loader' }, { loader: 'postcss-loader', options: { plugins: () => [ require('postcss-modules')({ generateScopedName: isProdMode ? '[hash:base64:5]' : '[name]__[local]___[hash:base64:5]' }) ] } } ] }
3. After webpack build it will compile some-widget.css.json file like this:
{ "container": "3wMhw", "content": "3PzWu" }
4. In react components you can use local styles like this:
import React from 'react'; import cssClasses from './some-widget.css.json'; ... render() { return
5. And for using in twig templates you should white twig extension for reading json file with caching. Usage will be like this:
{% set cssClasses = getCssClasses('./some-widget.css.json') %}
@mars-abd Since Encore already provides a rule that handles CSS files, you probably shouldn't add a new one but rather:
Encore.enablePostCssLoader(options => { ... })
Encore.configureLoaderRule('css', rule => { ... })
Yes, of course, thanks But I want to talk about twig extension more
Hey @mars-abd - do you have example code for the extension or a guide as to how we can do this with the Encore existing rules?
@mars-abd I'm interested in seeing the code for the "twig extension for reading json file with caching". Can you post it or a link to it?
There simply is no glue between CSS components and Twig. It's completely missing. Everything related to Twig is built around the old-fashioned paradigm of pages instead of components.
CSS Modules - question
Is there any way to have CSS Modules working with Symfony Encore using Twig?
I would assume this is pretty straight forward to setup with React frontend, but if people use twig, would there be a way to use CSS Modules?
I am not sure how to or if this is even possible, but just raising the question because I think frontend becomes a lot more maintainable with CSS modules..
I personally tend to use CSS Modules in all isomorphic js projects but would love to use it with Symfony Encore & Twig. I think it would be amazing.
For example, if we were to:
1 Import our css
2 Reference our classes with mycss
Twig could generate a unique hash per file imported and Webpack with Symfony Encore would then use the same unique hash.
This is just an idea, again.. just to see what you smart people think.