简体   繁体   English

反应:sass-loader 没有将 SCSS 转换为 CSS

[英]React: sass-loader not transpiling SCSS into CSS

I have a react app with CSS files working fine.我有一个react应用程序,其中CSS文件工作正常。
I need to start using SCSS so the following changes:我需要开始使用SCSS进行以下更改:

  1. Install node-sass and sass-loader安装node-sasssass-loader
  2. Change the imports from .css to .scss将导入从.css更改为.scss
  3. Change file extension from .css to .scss将文件扩展名从.css更改为.scss

But my pages are not loading properly:但是我的页面没有正确加载:

Current behavior:当前行为:在此处输入图像描述

Expected behavior:预期行为:在此处输入图像描述

My code:我的代码:

package.json

{
  "name": "frontend",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@coreui/coreui": "^3.0.0",
    "@stardust-ui/docs-components": "^0.40.0",
    "axios": "^0.19.0",
    "bootstrap": "^4.4.1",
    "compass": "^0.1.1",
    "css-loader": "^3.4.2",
    "mdbreact": "^4.25.4",
    "prettier": "^1.6.1",
    "prop-types": "^15.7.2",
    "react": "^16.10.2",
    "react-bootstrap": "^1.0.0-beta.16",
    "react-bootstrap-sidebar": "0.0.1",
    "react-dom": "^16.10.2",
    "react-fixed-bottom": "^1.0.2",
    "react-modal": "^3.11.1",
    "react-rangeslider": "^2.2.0",
    "react-router-dom": "^5.1.2",
    "react-scripts": "^1.1.5",
    "reconnecting-websocket": "^4.2.0",
    "reconnectingwebsocket": "^1.0.0",
    "semantic-ui-react": "^0.88.2",
    "style-loader": "^1.1.3",
    "styled-components": "^4.4.1"
  },
  "scripts": {
    "start": "PORT=3000 react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  },
  "devDependencies": {
    "node-sass": "^4.13.1",
    "sass-loader": "^8.0.2"
  }
}

I injected the code for the loaders in node_modules/react-scripts/config/webpack.config.dev.js like this:我在 node_modules/react-scripts/config/webpack.config.dev.js 中注入了加载程序的代码,如下所示:

// Process JS with Babel.
          {
            test: /\.(js|jsx|mjs)$/,
            include: paths.appSrc,
            loader: require.resolve('babel-loader'),
            options: {
              // @remove-on-eject-begin
              babelrc: false,
              presets: [require.resolve('babel-preset-react-app')],
              // @remove-on-eject-end
              // This is a feature of `babel-loader` for webpack (not Babel itself).
              // It enables caching results in ./node_modules/.cache/babel-loader/
              // directory for faster rebuilds.
              cacheDirectory: true,
            },
          },
          { 
            test: /\.scss$/, 
            loader: [
              "css-loader",
              "sass-loader"
            ]
          },

Here is my component:这是我的组件:

import React, { Component } from "react";
import '../../../general.scss';
import { Warning } from '../../../images/images_svg';

class Terminos extends Component{
  state = { error: false }

  saveAndContinue = () => {
    if(this.props.values.terminos) {
       this.props.nextStep()      
    }
    else {
       console.log(this.props.values.terminos_error)
       this.props.mandar_error('terminos_error')
    }     
  }

  render() { 
    return (
      <div className="formulario">
        <div className="formulario-seccion-titulo">
          <div className="formulario-seccion-barrita"/>
            <div className="formulario-seccion-content">
               Presentacion
            </div>
          </div>      
          <div className={this.props.values.terminos_error ? "formulario-seccion" : "formulario-seccion-red"}>
             La actual pandemia que afecta al mundo trae consecuencias a nivel sanitario, económico y social. Lo que está ocurriendo con el COVID impacta de lleno, no sólo en la salud física de las personas, sino que también afecta de forma clara y contundente su salud mental.<br/><br/>
             Ud. puede decidir si contestar o no la encuesta que sigue pero de hacerlo,acepta que los resultados de la misma sean compartidos con su empleador con el único fin de brindarle contención y asistencia. La encuesta debe completarse en las próximas 10 horas.<br/><br/>
             <input type="checkbox" 
               id="terminos" 
               className="formulario-checkbox"
               onChange={() => this.props.handleTerminos()}
               checked={this.props.values.terminos}
             />  
             <label className="formulario-checkbox-label" htmlFor="terminos"> 
               Acepto terminos y condiciones.
             </label>
             {this.props.values.terminos_error===false &&
                <div className="formulario-seccion-error">
                  <div className="formulario-seccion-error-logo" >
                    <Warning color="#d93025"/>
                  </div>
                    Debes aceptar los terminos y condiciones para continuar
                </div>
              }
           </div>
           <div className="formulario-footer">
             <button className="formulario-botones" onClick={this.saveAndContinue}>Siguiente</button>
           </div>
         </div>
       )    
    }
}

export default Terminos;

If I change the import and the file extension from .scss to .css the page load properly.如果我将导入和文件扩展名从.scss更改为.css页面加载正确。
I´m using npm start to develop and testing the code.我正在使用npm start开发和测试代码。

Have you created project with Create-React-App?您是否使用 Create-React-App 创建了项目? In this case, you don't need sass-loader , you just need node-sass , and import your CSS style into your root file with.scss/sass extension, for example, import '../example.scss' , then need to configure webpack.config.js at all.在这种情况下,您不需要sass-loader ,您只需要node-sass ,并将您的 CSS 样式导入您的根文件,扩展名为 .scss/sass ,例如import '../example.scss' ,然后完全需要配置 webpack.config.js 。

Please provide more information.请提供更多信息。

Have a nice day.祝你今天过得愉快。

Webpack has seen tremendous changes up until version 4 Webpackversion 4之前已经看到了巨大的变化

When we use create-react-app , it installs a compatible version of Webpack that react-scripts utilizes to bundle your files and serves them using Webpack-server as the dev server when you run npm start .当我们使用create-react-app时,它会安装Webpack的兼容版本,当您运行npm start时, react-scripts会利用该版本来捆绑您的文件并使用Webpack-server作为开发服务器提供它们。

  • Remove "webpack": "^3.11.0" from your package.json since it's already installed by react-scripts .package.json中删除"webpack": "^3.11.0" ,因为它已经由react-scripts安装。

  • Also react 16+ often works with react-scripts 3+ so if you manually edited some of this package.json file, i recommend you leave the defaults installed by create-react-app and just add your other dependencies . react 16+通常与react-scripts 3+一起使用,因此如果您手动编辑了一些package.json文件,我建议您保留由create-react-app安装的默认值,然后添加其他dependencies项。

Otherwise, here's What Webpack says about loaders .否则,这就是 Webpack 关于 loaders的说法。

Ok, I solved it deleting the node-modules folder, the package-lock.json and all my dependencies in the package.json.好的,我解决了它删除节点模块文件夹,package-lock.json 以及我在 package.json 中的所有依赖项。 Then I reinstalled all my dependencies with npm install... .然后我用npm install...重新安装了所有依赖项。 I think the problem was the react-scripts module, i upgraded from version 1.1.5 to 3.4.1.我认为问题出在 react-scripts 模块,我从版本 1.1.5 升级到了 3.4.1。 Thank you all for your answers.谢谢大家的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM