Skip to content
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

Less files in components #86

Closed
ilyadoroshin opened this issue Apr 1, 2015 · 20 comments
Closed

Less files in components #86

ilyadoroshin opened this issue Apr 1, 2015 · 20 comments
Labels

Comments

@ilyadoroshin
Copy link

Hey, guys.

I must've msised something, but why do we add less-files into components? What's the purpose on that and how does it work (link on docs will do, google couldn't help me).

Thanks in advance!

@koistya
Copy link
Member

koistya commented Apr 1, 2015

@ilyadoroshin you can google for component-based UI development, take a look here for example:

The purpose of this is to have independent from each other components, instead of sharing styles and other assets between components you use composition. This allows to avoid possible bugs, when editing a shared resource for one component may break another one which depends on the same shared resource (CSS, image etc.)

@ilyadoroshin
Copy link
Author

@koistya thanks, i know a lot about bem, use it everywhere, i just cannot get what does react do with that less file inside of component?
sorry for stupid question)

@koistya
Copy link
Member

koistya commented Apr 1, 2015

@ilyadoroshin as opposed to have shared folders for styles, images? If you have all the resources referenced by the component in a single place (inside component's folder), you will be able to easily find them, you will be confident that editing these files won't affect other components. You can also remove component's folder and be sure that you have not left any assets which are no longer in use.

@koistya
Copy link
Member

koistya commented Apr 1, 2015

@ilyadoroshin or, do you mean, why LESS and not SASS or Stylus or plain CSS?

@ilyadoroshin
Copy link
Author

@koistya oh, so you're saying, that under the hood, less files are combined together from all components into one file, right?
What module does that?

@koistya
Copy link
Member

koistya commented Apr 1, 2015

Regarding how does it work.. during the application bundling process, those CSS/LESS files are aggregated and embedded into the bundle itself (alternatively an external style.css file can be created, depending on your build config).

Check this out:

@ilyadoroshin
Copy link
Author

@koistya sick 👍
Thank you so much for helping understanding that!
The whole approach is awesome!

@koistya
Copy link
Member

koistya commented Apr 1, 2015

For example, you may have a Navigation React component with the following files:

./src/components/Navigation/Navigation.js
./src/components/Navigation/Navigation.css
./src/components/Navigation/close.svg

Inside your CSS or React code you can reference that image using relative path:

.Navigation-close {
  background-image: url('./close.svg');
}

And when you build the project, this close.svg will be copied into ./build/img/close-a3b783c.svg and URL to that image is going to be set to a new one background-image: url(/img/close-a3b783c.svg) (inside the app bundle's CSS).

@ilyadoroshin
Copy link
Author

@koistya jee, man, that's awesome!
Do you know, is there any implementation like that on rails?)

@ilyadoroshin
Copy link
Author

@koistya Actually, I've got only one question left. I'm used to sass, and I moved everything to sass, but build fails because import of component's sass-file fails.
Is there anything I need to tune up for using sass?

@koistya
Copy link
Member

koistya commented Apr 1, 2015

@ilyadoroshin try to replace less-loader with sass-loader in webpack.config.js

{
test: /\.less$/,
loader: 'style-loader!css-loader!' + AUTOPREFIXER_LOADER +
'!less-loader'
},

@ilyadoroshin
Copy link
Author

@koistya tried this one, I get
Uncaught Error: Cannot find module "./App.sass" everytime..

@koistya
Copy link
Member

koistya commented Apr 1, 2015

      {
        test: /\.(sass|scss)$/,
        loader: 'style-loader!css-loader!' + AUTOPREFIXER_LOADER + '!sass-loader'
      }

@ilyadoroshin
Copy link
Author

@koistya nope.. still not loading.
Please tell me, is it performed on server side (server.js)?

@koistya
Copy link
Member

koistya commented Apr 1, 2015

Are you sure you have test: /\.sass$/ part set correctly? These import './App.sass' lines are stripped off during bundling by Webpack (see what's inside ./build/app.js and ./build/server.js after the build). You may want to run gulp build --verbose to see webpack's log.

@ilyadoroshin
Copy link
Author

@koistya I've done all exactly as you said:

// App.js
import './App.sass';
// webpack.config.js
      {
        test: /\.sass$/,
        loader: 'style-loader!css-loader!' + AUTOPREFIXER_LOADER + '!sass-loader'
      },
// $ gulp build --verbose
 ERROR in ./src/components/App/App.js
    Module not found: Error: Cannot resolve 'file' or 'directory' ./App.sass 

@koistya
Copy link
Member

koistya commented Apr 1, 2015

It seems like sass-loader is not used and webpack thinks that it should load ./App.sass as a regular node/npm module.

Have you installed sass-loader? npm install sass-loader --save-dev

@ilyadoroshin
Copy link
Author

@koistya that works! But for proper sass (not scss) support need to add ?indentedStyle to sass-loader

Thanks a bunch!

@cmnstmntmn
Copy link

@ilyadoroshin how did you solve the import of normalize.css after you switched from less to scss?

ERROR in ./src/components/App/App.scss Module parse failed: /Users/ctin/repos/goxgames/src/components/App/App.scss Line 1: Unexpected token ILLEGAL You may need an appropriate loader to handle this file type. | @import (less) '../../../node_modules/normalize.css/normalize.css';

@koistya
Copy link
Member

koistya commented Jun 2, 2015

@cmnstmntmn Try to remove (less) part from that line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants