Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to include sass file in reactjs

I use browserify to transform my code now getting error while the sass files were getting imported

@import parse error

Can any one help me to sort this issue how to use and integrate

like image 294
NAVEED Avatar asked Dec 10 '22 05:12

NAVEED


2 Answers

With the latest version of create-react-app you don't have to eject your react app to add sass support.

Just add node-sass to your project:

NPM: npm install node-sass --save

YARN yarn add node-sass

And now you can import sass files in your react components. Check the official documentation here

like image 99
Karim Avatar answered Dec 19 '22 03:12

Karim


Recently, the React team and a number of contributors released a fantastic tool for creating configuration-less React applications based on a set of minimal ideas to help beginners dive into building React applications without having to worry about tooling a beginner may find daunting.

There are two ways you can add SASS or SCSS to a create-react-app project.

Install With: NPM Scripts

This method is simpler and can work without ejecting your project. If you prefer not to eject, use this method. Click here to view the project’s official guide on how to install Sass or Scss using NPM Scripts.

Install With: Webpack

This method is not as simple, but more extensible and automated. Unfortunately, this method requires ejection. Ejecting a project, although useful in many advanced cases, means that futures updates to create-react-app will not be easy for you to get.

Before starting, install and save the necessary tools from your packager of choice by running one of the following commands in your project directory.

Yarn: yarn add sass-loader node-sass --dev

NPM: npm install sass-loader node-sass --save-dev

Adding sass or scss to your create-react-app project will first require you to eject, which can be done by executing the following command in your project’s base directory.

npm run eject

Once ejection has completed, locate the config folder which holds, among other things, two webpack configuration files. Each of these configuration files corresponds to a different environment — development or production. Enter the development configuration file webpack.config.dev.js and locate the loaders block within the modules block.

In webpack, loaders allow the developer to “pre-process” files as they are required/loaded. Create react app uses multiple loaders to handle various build tasks such as transpiling with babel, prefixing with PostCSS, or allowing the importing of assets. To add Sass or Scss to this project, you will be adding a loader that can process Sass and Scss files.

The beginning of the loaders block, before any modification, should look something like the following code (Exact code may change in future versions of CRA).

loaders: [
  // Process JS with Babel.
  {
    test: /\.js$/,
    include: paths.appSrc,
    loader: 'babel',
    query: require('./babel.dev')
  },
  ...

After the beginning of the loaders array, you can add your own loader for Sass and Scss. The “sass” loader featured in the loaders array in the code below is able to process both Sass and Scss files.

If you prefer SASS, you can add:

{
  test: /\.sass$/,
  include: paths.appSrc,
  loaders: ["style", "css", "sass"]
},

If you prefer SCSS, you can add:

{
  test: /\.scss$/,
  include: paths.appSrc,
  loaders: ["style", "css", "sass"]
},

Important: In newer versions of create-react-app, if you see an exclude array in your webpack config, you need to add the following lines to

/\.sass$/,
/\.scss$/,

Now that the Sass and Scss loader is in place, you can begin using Sass/Scss files. For example, in the default App.js component that comes with create-react-app, you could now write

import './App.sass'; // or `.scss` if you chose scss

Note, you’d also have to refactor the “App.css” file to Sass and change its filename to “App.sass”.

Reference is taken from this website.

like image 37
Yashu Mittal Avatar answered Dec 19 '22 04:12

Yashu Mittal