I just figured it out. We should be using Sass Modules instead of @import. Also, this video helped me a lot: https://www.youtube.com/watch?v=CR-a8upNjJ0
When I replaced @import with @use it started working.
I just figured it out. We should be using Sass Modules instead of @import. Also, this video helped me a lot: https://www.youtube.com/watch?v=CR-a8upNjJ0
When I replaced @import with @use it started working.
this is my configs object for scss, and it's working with import.
You would need to install autoprefixer and add a css-loader and a style-loader loaders
{
test: /\.scss$/i,
use: [{
// inject CSS to page
loader: 'style-loader'
}, {
// translates CSS into CommonJS modules
loader: 'css-loader'
}, {
// Run postcss actions
loader: 'postcss-loader',
options: {
// `postcssOptions` is needed for postcss 8.x;
// if you use postcss 7.x skip the key
postcssOptions: {
// postcss plugins, can be exported to postcss.config.js
plugins: function () {
return [
require('autoprefixer')
];
}
}
}
}, {
// compiles Sass to CSS
loader: 'sass-loader'
}]
},
Videos
» npm install sass-loader
Edit: Fixed - installed webpack manually and downgraded to webpack 4.44.2. Solution from: https://github.com/facebook/create-react-app/issues/11631
I created a react app project ealrier in the year without sass-loader, I wanted to implement it now but this is the Error I am receiving when I try to implement sass-loader and do npm start. Why is it happening?
> react-scripts start There might be a problem with the project dependency tree. It is likely not a bug in Create React App, but something you need to fix locally. The react-scripts package provided by Create React App requires a dependency: "webpack": "4.44.2" Don't try to install it manually: your package manager does it automatically. However, a different version of webpack was detected higher up in the tree: C:\MyCode\redditProject\node_modules\webpack (version: 5.65.0) Manually installing incompatible versions is known to cause hard-to-debug issues. If you would prefer to ignore this check, add SKIP_PREFLIGHT_CHECK=true to an .env file in your project. That will permanently disable this message but you might encounter other issues. To fix the dependency tree, try following the steps below in the exact order: 1. Delete package-lock.json (not package.json!) and/or yarn.lock in your project folder. 2. Delete node_modules in your project folder. 3. Remove "webpack" from dependencies and/or devDependencies in the package.json file in your project folder. 4. Run npm install or yarn, depending on the package manager you use.
I have followed the steps, but to no avail.
I can run this project if I did not have the dependencies listed below (sass-loader, css-loader, style-loader, file-loader) and if I did not include the webpack.config.js here:
module.exports = {
module: {
rules: [
{
test: /\.s[ac]ss$/i,
use: [
// Creates `style` nodes from JS strings
"style-loader",
// Translates CSS into CommonJS
"css-loader",
// Compiles Sass to CSS
"sass-loader",
],
},
{
test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
use: [
{
loader: "file-loader",
options: {
name: "[name].[ext]",
outputPath: "fonts/",
},
},
],
},
],
},
};Package.json
{
"name": "reddit",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.11.10",
"@testing-library/react": "^11.2.6",
"@testing-library/user-event": "^12.8.3",
"@types/jest": "^26.0.22",
"@types/lodash": "^4.14.168",
"@types/node": "^12.20.7",
"@types/react": "^17.0.37",
"@types/react-dom": "^17.0.3",
"@types/react-router-dom": "^5.1.7",
"css-loader": "^6.5.1",
"embla-carousel": "^4.3.2",
"file-loader": "^6.2.0",
"history": "^4.10.1",
"lodash": "^4.17.21",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-icons": "^4.2.0",
"react-redux": "^7.2.3",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.3",
"react-spring": "^9.0.0",
"react-use": "^17.2.1",
"redux": "^4.0.5",
"redux-devtools-extension": "^2.13.9",
"redux-thunk": "^2.3.0",
"sass-loader": "^10.2.0",
"style-loader": "^3.3.1",
"typescript": "^4.2.3",
"web-vitals": "^1.1.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"node-sass": "^6.0.1"
}
}
» npm install fast-sass-loader
You can use mini-css-extract-plugin.
https://github.com/webpack-contrib/mini-css-extract-plugin
I used the same plugin for extracting SASS to CSS using webpack 4 and it's working like a charm.
webpack 4 is not yet capable of outputting standalone *.css file on its own. To get a separate *.css file, you need to use the extract-text-webpack-plugin to pull out all the CSS into its own entry chunk. This is a good tutorial.