reactjs - 如何在我的 React/webpack 项目中使用 sass?

标签 reactjs sass webpack

我之前通过使用

导入它来工作
import sass from '../scss/application.scss';

Is this the wrong type of import above?

现在它给了我以下错误......

ERROR in ./app/components/Landing.js Module not found: Error: Can't resolve 'style' in '/Users/sam/Desktop/talkingwith/talkingwith' BREAKING CHANGE: It's no longer allowed to omit the '-loader' suffix when using loaders. You need to specify 'style-loader' instead of 'style'. @ ./app/components/Landing.js 13:19-54 @ ./app/App.js @ multi (webpack)-dev-server/client?http://localhost:3333 ./app/App.js

现在不知道如何在我的组件中使用 SASS。

编辑:我的 webpack 文件

var HtmlWebpackPlugin = require('html-webpack-plugin');
var path = require('path');


var HTMLWebpackPluginConfig = new HtmlWebpackPlugin({
  template: __dirname + '/app/index.html',
  filename: 'index.html',
  inject: 'body'
});

module.exports = {
  entry: [
    './app/App.js'
  ],
  output: {
    path: __dirname + '/public',
    filename: "bundle.js"
  },
  plugins: [HTMLWebpackPluginConfig],
  devServer: {
       inline:true,
       contentBase: './public',
       port: 3333
     },
  module: {
    loaders: [{ 
         test: /\.js$/, 
         exclude: /node_modules/, 
         loader: "babel-loader"
      },
        {
              test: /\.scss$/,
              loader: 'style!css!sass'
            }]
  }
};

最佳答案

我不是 100% 确定你正在使用什么 sass 库。对我来说真正有效的是使用node-sass库和sass-loader。 https://github.com/jtangelder/sass-loader 将 sass-loader 与 css-loader 和 style-loader 一起使用也是一个很好的做法。 (如我下面的示例)

使用 sass-loader 你的 webpack.config.js 看起来像这样。

var HtmlWebpackPlugin = require('html-webpack-plugin');
var path = require('path');


var HTMLWebpackPluginConfig = new HtmlWebpackPlugin({
  template: __dirname + '/app/index.html',
  filename: 'index.html',
  inject: 'body'
});

module.exports = {
  entry: [
   './app/App.js'
  ],
  output: {
   path: __dirname + '/public',
   filename: "bundle.js"
  },
  plugins: [HTMLWebpackPluginConfig],
  devServer: {
    inline:true,
    contentBase: './public',
    port: 3333
  },
  module: {
    loaders: [
     { 
      test: /\.js$/, 
      exclude: /node_modules/, 
      loader: "babel-loader"
     },
     { 
      test: /\.scss$/, 
      loaders: ["style-loader", "css-loader", "sass-loader"] 
     }
   ]
  }
};

将 webpack.config.js 更新为上述内容后,您需要做的就是在入口点 js 文件中添加 .scss 文件,在您的情况下为 App.js

require('path/to/your/style.scss');

关于reactjs - 如何在我的 React/webpack 项目中使用 sass?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41970217/

相关文章:

node.js - 收到错误 : Cannot find module 'errno' running node script

javascript - HOC 组件 - 数组返回 true 或 false

javascript - 在 google-map-react 中向 Google map 添加标记

javascript - 在父级调用函数并将 `this` 绑定(bind)到子级?

html - 在 HTML 中使用变量作为类

Webpack:插件的顺序重要吗?

javascript - ReactJS 几个css文件

css - 带有自定义调色板的 Angular Material 自定义主题

ruby - 如何在 Sinatra 应用程序中链接 Sass 文件?

javascript - JS 在多个文件上运行。创建类还是使用 Webpack 和 Babel?