简体   繁体   English

SASS未在React / Webpack项目中呈现

[英]SASS not rendering in React / Webpack project

I added SASS to my project because I like using SASS for my CSS. 我将SASS添加到我的项目中是因为我喜欢将SASS用于CSS。 In my app folder I made CSS folder and a SCSS folder. 在我的app文件夹中,我创建了CSS文件夹和一个SCSS文件夹。 I run sass --watch scss:css to map over my scss changes to the css folder. 我运行sass --watch scss:css将我的scss更改映射到css文件夹。 This is working great, I think. 我认为这很好。 In my webpack I added a loader to handle SCSS and SASS and added the files need to my package.json. 在我的Webpack中,我添加了一个加载器来处理SCSS和SASS,并将需要的文件添加到了package.json中。 In order to start using the SASS styles do I need to include them in my index.html file or do I need to import them into each component I want to use them with the new es6 import x from 'xpath' thing? 为了开始使用SASS样式,我需要将它们包括在index.html文件中,还是需要将它们导入每个组件中?我想将它们与新的es6 import x from'xpath'一起使用吗?

Here are my package.json and webpack.config.js files 这是我的package.json和webpack.config.js文件

{
  "name": "newaccount",
  "version": "1.0.0",
  "description": "a form submission for new accounts",
  "main": "App.js",
  "scripts": {
    "start": "webpack-dev-server"
  },
  "author": "---",
  "license": "ISC",
  "dependencies": {
    "react": "^15.4.1",
    "react-dom": "^15.4.1",
    "react-hot-loader": "^1.3.1",
    "react-router": "^3.0.0"
  },
  "devDependencies": {
    "babel-core": "^6.18.2",
    "babel-loader": "^6.2.8",
    "babel-preset-es2015": "^6.18.0",
    "babel-preset-react": "^6.16.0",
    "css-loader": "^0.26.1",
    "html-webpack-plugin": "^2.24.1",
    "node-sass": "^3.13.0",
    "sass": "^0.5.0",
    "sass-loader": "^4.0.2",
    "style-loader": "^0.13.1",
    "webpack": "^1.13.3",
    "webpack-dev-server": "^1.16.2"
  }
}

webpack.config.js 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: "index_bundle.js"
  },
  plugins: [HTMLWebpackPluginConfig],
  devServer: {
       inline:true,
       contentBase: './public',
       port: 3333
     },
  module: {
    loaders: [{ 
         test: /\.js$/, 
         exclude: /node_modules/, 
         loader: "babel-loader"
      }, {
          test: /\.scss$/,
          include: path.join(__dirname, "app"),
          loaders: ["style", "css", "sass"]
        },
        {
              test: /\.scss$/,
              loader: 'style!css!sass'
            }]
  }
};

My file structure is basically an app folder and a public folder. 我的文件结构基本上是一个应用程序文件夹和一个公用文件夹。 Inside app you have components / containers / css / scss / utils and inside public is jus the bundle.js and index.html files. 在应用程序内部,您具有组件/容器/ css / scss / utils,而在public内部则是bundle.js和index.html文件。

You should not be using an external Sass command with Webpack. 您不应该在Webpack中使用外部Sass命令。 You need to put the Sass content in Webpack's dependency graph. 您需要将Sass内容放入Webpack的依赖关系图中。 To do this, tell Webpack you are using the files, which is done with require() . 为此,请告诉Webpack您正在使用文件,这是通过require() At the top of your Javascript file for the page needing the style, add 在需要样式的页面的Javascript文件顶部,添加

require('./path/to/sass/scss')

In development mode, this will turn your Sass code into Javascript that will inject the built CSS rules into the page. 在开发模式下,这会将Sass代码转换为Javascript,并将内置的CSS规则注入页面。 It's strange I know, but it gives you hot reloading of styles. 我知道这很奇怪,但是它可以让您重新加载样式。 You should also have a separate production build Webpack config using the ExtractTextPlugin to pull the built CSS out into a separate file, and load that file in your production HTML. 您还应该具有一个单独的生产构建Webpack配置,该配置使用ExtractTextPlugin将构建的CSS拉出到单独的文件中,并将该文件加载到生产HTML中。

Further reading: Webpack: When to Use and Why . 进一步阅读: Webpack:何时使用以及为什么

My webpack.config.js file was loading scss and sass twice, changed it to 我的webpack.config.js文件加载了scss和sass两次,将其更改为

{
     test: /\.scss$/,
     loader: 'style!css!sass'
}]

by removing 通过删除

{
     test: /\.scss$/,
     include: path.join(__dirname, "app"),
     loaders: ["style", "css", "sass"]
 },

Now I can include my SASS files in my react components. 现在,我可以将我的SASS文件包含在我的react组件中。 Now I need to figure out how to access my images inside of my SASS files - seems to toss mass errors =( 现在,我需要弄清楚如何在我的SASS文件中访问我的图像-似乎丢了质量错误=(

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

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