02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Importing SCSS Files with Webpack

In this tutorial, we will learn how to import SCSS files dynamically using Webpack and explore SASS mix-ins. This approach helps modularize styles, reuse them across components, and leverage Bootstrap’s features for seamless UI development.


1. Introduction to SCSS in Webpack

SCSS (SASS with CSS syntax) offers powerful features like variables, mix-ins, and imports. Combining SCSS with Webpack allows us to dynamically include styles into our React projects.

Why Modular SCSS?

  • Prevents style duplication.
  • Makes components more maintainable and reusable.
  • Leverages tools like mix-ins and variables for dynamic styling.

2. Setting Up Modular Imports

Before diving into SCSS, you need a working Webpack setup. Here's what you’ll do:

  1. Ensure SCSS Support in Webpack:
    Confirm your webpack.config.js includes loaders for SCSS. For instance:

    {
        test: /\.(scss|css)$/,
        use: ['style-loader', 'css-loader', 'sass-loader'],
    }
    
  2. Organize Your SCSS Files:
    Create modular SCSS files for each component. For example:

    /src
        /styles
            _variables.scss
            _mixins.scss
            core.scss
            button.scss
            jumbotron.scss
    
  3. Import Core Styles:
    Add a core.scss file to consolidate shared styles like variables and mix-ins.

    @import './variables';
    @import './mixins';
    

3. Understanding Mix-Ins

Mix-ins allow you to define reusable blocks of styles. Here's how:

  1. Create a Mix-In:
    Add the following to _mixins.scss:

    @mixin background-it($image-path, $text-color: #fff) {
        background-image: url($image-path);
        color: $text-color;
        text-align: center;
        background-size: cover;
    }
    
  2. Use the Mix-In in Components:
    In jumbotron.scss, include the mix-in:

    @import './core';
    .jumbotron {
        @include background-it('assets/bg.jpg');
    }
    

4. Modular Component Styling

Jumbotron Component Example:

  1. Remove inline styles or direct SCSS imports from components:
    import './styles/jumbotron.scss';
    
  2. Ensure the styles reference the core.scss file for variables and mix-ins.

5. Avoiding Style Duplication

When combining multiple SCSS files (e.g., button.scss, jumbotron.scss), duplication can occur. Tools like Webpack’s optimization plugins can clean up the final CSS bundle, ensuring only unique styles are retained.


6. Importing SCSS Dynamically in Webpack

Now, your SCSS imports are modular. When you include the button.scss or jumbotron.scss files in their respective components, Webpack processes them dynamically. Example:

import './styles/button.scss';
import './styles/jumbotron.scss';

7. Testing Your Setup

  1. Run your development server:
    npm start
    
  2. Ensure styles are applied correctly to all components.
  3. Open the browser developer tools to verify the compiled CSS includes only the required styles.

8. Bootstrap Integration

To integrate Bootstrap, you can:

  1. Use SCSS imports to include only necessary Bootstrap features:
    @import '~bootstrap/scss/bootstrap';
    
  2. Customize Bootstrap variables by overriding them in your _variables.scss.

Key Takeaways

  • SCSS imports improve maintainability and component reusability.
  • Mix-ins streamline repetitive styles, ensuring cleaner code.
  • Modular styles enhance the efficiency of large-scale applications.

By following these steps, you can create a scalable, maintainable SCSS structure integrated seamlessly into React projects using Webpack.

In the next section, we’ll dive into animations using SCSS and continue enhancing our user interface skills.

Video Transcript

00:00Now Bootstrap uses not only variables,
00:03but it uses OTO mix-ins.
00:04So before we actually get our our components
00:07to fully use Bootstrap, I want you to know what Mix-ins are.
00:10And we're gonna take a look at what they are
00:12as we completely integrate our components.
00:15So they'll have that full support
00:16of Bootstrap the way we want them to have.
00:19And by the end of the selection lecture,
00:22and it will be the end of this section
00:23because we're gonna have our component ready.
00:26We're not done, we won't be done with with SaaS
00:28because we're gonna see it more throughout the
00:29next section as well.
00:31Let's set us our component up.
00:34Now, to be able to do that, I'm gonna go ahead
00:36and just go into my jumbotron and delete
00:39or comment out our SCSS file that we're using
00:43and in our button do the exact same thing.
00:46My goal is that our application will look something like
00:49this once it goes back on the screen.
00:52So basically it's going
00:53to be without the design builds at all.
00:56Now the next thing that I want to do is I want
00:58to go into our source app, uh, app source dot
01:03s css, and I'm gonna show you how to create a mix in.
01:06Now, a mix in is just literally you define mix in
01:10and then you set a name.
01:12And really what it is, is like a function
01:14or a wrapper that duplicates something that you do often.
01:17So for example, let's say that we want
01:20to set a background image very often.
01:22So I'm gonna say background it
01:24and maybe what's happening is that we send in a path,
01:27but we want to do the exact same steps on a few different
01:29things such as centering it and so on and so forth.
01:32In this te in this example, let's just go ahead
01:36and set a background,
01:38background image.
01:42And I'm gonna just set the path to be that image.
01:46And let's go ahead and also add here another something.
01:48How about we'll also add a color.
01:50By default, our color will be whatever our text
01:53color is as well.
01:55Or you know what, it's an override.
01:58Whenever we do this custom build, then
02:00whenever there's, we set a background, we set our color
02:02to be white just for the sake of showcasing.
02:05And if I go ahead and delete basically this
02:08and I come around here
02:10and instead of actually doing something here,
02:12I'm gonna go ahead and ask
02:14to basically import this logic.
02:18Now the way I do that, I type in include,
02:21and I just said the name of that method, background it,
02:26and basically call it just like I would call a method.
02:29And what will happen is it's gonna include whatever logic,
02:32whatever lines we have inside of our background it.
02:34Now, if I go ahead and save this,
02:36what's actually gonna happen is our background should remain
02:40the same, but our copy should become white now
02:42because we've mixed in this mix in.
02:44Now that's really the basic basics of what mix-ins are.
02:47And they're normally used for really areas
02:50where you have a lot of things that repeat themselves
02:52and you don't wanna need to type them yourself,
02:54which makes a lot of sense.
02:57Now let's go back into our sample
02:59And really into our bootstrap.
03:02'cause Bootstrap generally,
03:03and I'm just gonna open up that file up
03:05so we take a peek at it.
03:06Bootstrap generally has a few variables.
03:09Well, it has a lot of variables
03:10and has mix-ins that it uses all the time.
03:13Now there's a lot, there's some core dependencies
03:15that are really needed for almost everything.
03:18And just because of that we're just assuming anytime someone
03:21uses one component, they really need all
03:24of these core elements.
03:26So to be able to do that,
03:27I'm gonna go into my bootstrap folder again
03:29and I'm just gonna create a new file
03:31and I'm gonna call it Core Do SS.
03:37And I'm gonna just go ahead
03:38and paste inside all those core elements.
03:41Now variables, we have a local version of it,
03:43which is great mix-ins.
03:45We're actually not gonna create a local variable for it,
03:48so lemme just close that up.
03:50We're not gonna create a local variable for that.
03:52So I'm just gonna go ahead
03:53and literally just already found
03:55that path a little bit earlier as I was playing around.
03:58I'm just gonna go into my core that we've created
04:01and I'm gonna import that mix-ins.
04:04Now I'm gonna go ahead
04:05and literally just add the file path
04:08to all of the items there.
04:10'cause I wanna make sure that they're all always loaded in.
04:13That's the default. I always want all these stuff inside.
04:17I'm gonna go ahead and save that.
04:18Now that I know that I'm loading all of these stuff inside,
04:22I'm going to make sure that I always load up this core
04:25before I actually call the different methods
04:27and buttons and whatnot.
04:28So for example, for our button, uh, we don't need
04:31that anymore for our button.
04:33I'm not going to use the mix-ins anymore.
04:35What I am gonna do though is I'm gonna go
04:37and I'm gonna fetch our core.
04:39So I don't need variables either anymore
04:41'cause that's already defined inside of our uh, core.
04:44So I'm just gonna go ahead here dot equals
04:47and I'm gonna set there the core.
04:49Now let me just go ahead and copy this
04:52and I'm gonna copy that also in our, instead
04:54of our variables load in the core.
04:56So again, for our jumbotron, we're getting the core
04:59and we're also getting the jumbotron perfect for our button.
05:02We're doing the same, we're getting the core,
05:03which includes all of the goodies
05:05that we've just explored a moment ago.
05:07And on top of that we're also adding buttons.
05:10Now if I go into my button
05:12and let's see, I'm just gonna add the button
05:13for a second here and lemme just go ahead and save it.
05:16If I go back into my application,
05:18the core should be loaded up and so should my button.
05:21So although we're not gonna see the jumbotron working,
05:24our core buttons are working moreover than that.
05:27If I take a look at this,
05:28and let me just set here also to view our source
05:31of this page, you'll see that it's a massive file
05:34that includes everything in the core itself.
05:37Now if I go ahead
05:38and I also include our jumbotron logic inside of here
05:43and I save it and I go back into my application, first
05:46of all, our jumbotron,
05:47which should now reappear now and actually work.
05:50And secondly, our CSS will grow even beyond its massive
05:55growth that it was
05:56In already before adding in Otto,
05:59the jumbotron into it, a low jumbotron.
06:02Now, one thing that you might wanna note,
06:04and by the way we see here the button as well
06:06and the jumbotron, is you don't have
06:08to worry too much about duplications
06:10because as you become more proficient
06:13and working in the style, there's plugins
06:15that will actually clean up duplications
06:17and make sure that you don't have duplicates, your CSS
06:20as clean as possible.
06:22But we're not gonna cover that in this title.
06:24'cause really this one is about getting you really familiar
06:26and understanding how to build UIs, not about how
06:29to optimize them to the core, but it's definitely possible
06:33and I urge you to explore that
06:35and look into how do you optimize making sure
06:38that if you load a file in twice, for example,
06:41in our scenario where we're loading jumbotron and button,
06:45but we're also loading the core many times to make sure
06:48that our core is not gonna load in things in a duplication.
06:51So yeah, you could double check it and make sure,
06:53but also in production itself when you're minimizing,
06:56then you'd probably want to care about that as well.
06:58But our main focus right now is the visuals.
07:02So there you go. That's how you basically add in modularly,
07:06see SCSS.
07:08And basically as you build that,
07:10components in the components are actually used.
07:12Only the CSS
07:13that is needed would be added in if you're gonna use a
07:16bootstrap, if you're gonna create your original SCSS files
07:20or even CSS files that you're gonna work with.
07:22We covered all those stuff in this section already
07:25in this lecture we were introduced to Mixings as well,
07:28and that gets us a step closer to completing this title.
07:31But really we're done with the section.
07:33We're done with the main topic as SaaS.
07:35In the next sec, next section, we'll move into animation.
07:38We're not gonna walk away from SaaS.
07:40We'll learn a few more sassy things,
07:42but in the process we'll also learn about animation,
07:44which is an important aspect of user interfaces.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!