-
Notifications
You must be signed in to change notification settings - Fork 300
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
cssModuleHook option is not working #333
Comments
i can do that. import style from '../../styles/base.scss' file: <app>/src/server/index.js require.extensions[".css",".scss"] = () => { return; }; file: <app>/webpack.config.dev.js module.exports = function(composer, options, compose) { const customProfile = { partials: { "_extract-style": { enable: false }, "custom-extract-style": { order: 2101 } } } composer.addPartials( { "custom-extract-style": { config: require('./config/webpack/partial/custom-extract-style')() } }) return composer.compose({}, [].concat(options.profileNames).concat([customProfile])) } file: <app>/config/webpack/partial/custom-extract-style.js "use strict"; const archetype = require("electrode-archetype-react-app/config/archetype"); const Path = require("path"); const webpack = require("webpack"); const glob = require("glob"); const ExtractTextPlugin = require("extract-text-webpack-plugin"); const CSSSplitPlugin = require("css-split-webpack-plugin").default; const atImport = require("postcss-import"); const cssnext = require("postcss-cssnext"); const autoprefixer = require("autoprefixer-stylus"); const cssLoader = require.resolve("css-loader"); const styleLoader = require.resolve("style-loader"); const stylusLoader = require.resolve("stylus-relative-loader"); const postcssLoader = require.resolve("postcss-loader"); const AppMode = archetype.AppMode; /** * [cssModuleSupport By default, this archetype assumes you are using CSS-Modules + CSS-Next] * * Stylus is also supported for which the following cases can occur. * * case 1: *only* *.css exists => CSS-Modules + CSS-Next * case 2: *only* *.styl exists => stylus * case 3: *both* *.css & *.styl exists => CSS-Modules + CSS-Next takes priority * with a warning message * case 4: *none* *.css & *.styl exists => CSS-Modules + CSS-Next takes priority * case 5: *cssModuleStylusSupport* config is true => Use both Stylus and CSS Modules */ const cssNextExists = (glob.sync(Path.resolve(AppMode.src.client, "**", "*.css")).length > 0); const stylusExists = (glob.sync(Path.resolve(AppMode.src.client, "**", "*.styl")).length > 0); // By default, this archetype assumes you are using CSS-Modules + CSS-Next const cssModuleSupport = !stylusExists && cssNextExists; module.exports = function () { const cssModuleStylusSupport = archetype.webpack.cssModuleStylusSupport; const stylusQuery = cssLoader + "?-autoprefixer!" + stylusLoader; const cssLoaderOptions = "?modules&localIdentName=[name]__[local]__[hash:base64:5]&-autoprefixer"; const cssQuery = cssLoader + cssLoaderOptions + "!" + postcssLoader; const cssStylusQuery = cssLoader + cssLoaderOptions + "!" + postcssLoader + "!" + stylusLoader; // By default, this archetype assumes you are using CSS-Modules + CSS-Next const rules = [{ _name: "extract-css", test: /\.css$/, loader: ExtractTextPlugin.extract({ fallback: styleLoader, use: cssQuery, publicPath: "" }) }]; if (cssModuleStylusSupport) { rules.push({ _name: "extract-css-stylus", test: /\.styl$/, use: ExtractTextPlugin.extract({ fallback: styleLoader, use: cssStylusQuery, publicPath: "" }) }); } else if (!cssModuleSupport) { rules.push({ _name: "extract-stylus", test: /\.styl$/, use: ExtractTextPlugin.extract({ fallback: styleLoader, use: stylusQuery, publicPath: "" }) }); } if (cssModuleSupport) { rules.push({ _name: "postcss", test: /\.scss$/, use: ExtractTextPlugin.extract({fallback: styleLoader, use: [{loader: cssQuery}, {loader: "sass-loader"}],publicPath: ""}) }); } return { module: { rules }, plugins: [ new ExtractTextPlugin({ filename: "[name].style.[hash].css" }), /* preserve: default: false. Keep the original unsplit file as well. Sometimes this is desirable if you want to target a specific browser (IE) with the split files and then serve the unsplit ones to everyone else. */ new CSSSplitPlugin({ size: 4000, imports: true, preserve: true }), new webpack.LoaderOptionsPlugin({ options: { context: Path.resolve(process.cwd(), "src"), postcss: () => { return cssModuleSupport ? [atImport, cssnext({ browsers: ["last 2 versions", "ie >= 9", "> 5%"] })] : []; }, stylus: { use: () => { return !cssModuleSupport ? [autoprefixer({ browsers: ["last 2 versions", "ie >= 9", "> 5%"] })] : []; } } } }) ] }; }; |
I did a similar fix but I don't understand why I need the change to /src/server/index.js and I don't love the require.extensions in that file. Here's my complete /webpack.config.dev.js. This works with electrode-archetype-react-app 3.x.
|
@dustinaleksiuk thank you very much. |
@caoanhhao Sorry I wasn't clear. I needed it too, but I didn't exactly understand why. I thought there would be a more elegant way to ignore SCSS on the server side. |
add this line: |
@dustinaleksiuk Your snippet solved the problem for scss files. I have few css libraries imported to the app from node_modules, they are being transformed to this format - |
@sgsvenkatesh I'm not sure, and I'm worried that we're going to have the same problem on our project. What I thought my solution did was add in a SCSS rule to the list of rules and kept the existing ones. If you solve this please report back and I will do the same. |
Yes. I found a temporary fix to this problem. You can include all your It will be good to have a webpack hook to avoid this problem. |
@sgsvenkatesh use @dustinaleksiuk 's code.
[name][local]_[hash:base64:5] => [local] |
The styling support does need a serious overhaul and update. We use stylus internally so there's extensive custom work done to support that https://github.com/walmartlabs/stylus-relative-loader. The original dev is no longer working on these. It detects *.css files and assumes CSS Modules. Obviously the issue is the use of CSS Modules by default and you can't turn it off. So I am thinking a quick update is to allow you to turn that off with a flag. |
@caoanhhao Yes, I did that and that solved for *.scss files. I'm looking for a similar solution for *.css files as well. @jchip Yes, that should suffice. Thanks! |
Can somebody upload a repository with a working example of getting scss to play nice with electrode? I can't seem to figure this out. |
We have it working but it'll take a bit of time for me to put something that I can put in a repo. This pull request might be worth looking at: #483 |
@SimonSomlai Here is something that worked for me - electrode-sass |
@sgsvenkatesh remove sass-loader and *.scss hook for normal .css file |
Has anyone managed to get a sass working with CSS-modules in electrode-app? |
@sergesemashko yes. |
@SimonSomlai can you help me understand the entry point. What drives into archetype/config/webpack? Is the Electrode build looking there automatically or is there a hook somewhere? |
@dan3721 yes, Electrode archetype look into your app's |
@dustinaleksiuk thanks for your code. I use it differently in my code. keep css loader local and hash scss files. when i need to import css from 3rd party libs, it would not apply to DOM. Thanks all the way. |
@caoanhhao can css be splitted? they run into wrong order. for vendor and app |
@yongzhoulu or use css @import url(); |
change what ? |
@caoanhhao order in |
@yongzhoulu when localIdentName=[local] add some thing like this to your html file. Or use css @import |
and then using them like this: AND USING to make node happy:
It "almost" works. That is the client render after hydration has the styles and they are applied as expected. However upon closer inspection the styles are missing from the initial Server Side Render. I suspect it is the require.extensions; just not sure how to solution so the SSR works. It would be incredibly helpful if the Electrode artifact would just include a .scss example. |
Hello, |
cssModuleHook option is not working.
"base.css"
"server/index.js"
=> "main.style.css"
i expect file "main.style.css"
How can i do that? ( test in "samples/universal-react-node" )
Thank you.
The text was updated successfully, but these errors were encountered: