简体   繁体   English

Sass与webpack无法正常工作

[英]Sass with webpack not working

I'm building an app using webpack, react, babel, and Sass. 我正在使用webpack,react,babel和Sass构建应用程序。 I've been trying to get Sass working, but it's not playing ball. 我一直试图让萨斯工作,但它不是在打球。 It's not throwing any errors, it just doesn't seem to be compiling properly, and when I examine the element in Dev Tools, it simply says "invalid property value" where I've referenced a variable. 它没有抛出任何错误,它似乎没有正确编译,当我检查Dev Tools中的元素时,它只是说“无效的属性值”,我引用了一个变量。 Is there something glaringly obvious I'm doing wrong? 有什么明显我做错了吗?

我的文件结构

My app.scss: 我的app.scss:

@import '../../../node_modules/normalize.css/normalize.css';
@import '../variables.scss';

/*
 * Base styles
 * ========================================================================== */

*,
*:after,
*:before {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
}

body {
    text-align: center;
    margin: 0;
}

p {
    color: $blue;
}

My variables.scss: 我的变量.scss:

/*
 * Colors
 * ========================================================================== */

$blue: #334c6a;
$yellow: #f6d463;
$white: #ffffff;
$black: #000000;

/*
 * Typography
 * ========================================================================== */

$font-family-base:      'Segoe UI', 'HelveticaNeue-Light', sans-serif;

/*
 * Layout
 * ========================================================================== */

$container-margin: 55px;

Webpack.config: Webpack.config:

var path = require("path");
var webpack = require("webpack");
var autoprefixer = require('autoprefixer');
var precss = require('precss');

module.exports = {
  entry: [
    'webpack-dev-server/client?http://localhost:8080',
    'webpack/hot/only-dev-server',
    './src/index'
  ],
  output: {
    path: path.resolve(__dirname, 'build'),
    publicPath: '/build/',
    filename: "bundle.js"
  },
  resolve: {
    extensions: ['*', '.js', '.jsx', '.png', '.json']
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoErrorsPlugin(),
    new webpack.LoaderOptionsPlugin({
      options: {
        context: __dirname,
        postcss: [
          autoprefixer
        ]
      }
    })
  ],
  module: {
      rules: [
     {
       test: /\.jsx?$/,
       exclude: /node_modules/,
       loaders: ['react-hot-loader', 'babel-loader']
     },
     {
       test: /\.scss$/,
       loaders: ['style-loader', 'css-loader', 'postcss-loader']
     },
     {
       test: /\.(png|jpg|jpeg|gif|svg|woff|woff2)$/,
       loader: 'url-loader?limit=10000',
     },
     {
       test: /\.css$/,
       use: [ 'style-loader', 'css-loader' ]
     }
   ]
  }
};

Package.json: 的package.json:

{
  "name": "test",
  "version": "1.0.0",
  "description": "test",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "webpack-dev-server"
  },
  "author": "test",
  "license": "ISC",
  "devDependencies": {
    "autoprefixer": "^6.7.7",
    "babel-core": "^6.24.1",
    "babel-loader": "^6.4.1",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "babel-preset-stage-0": "^6.24.1",
    "css-loader": "^0.28.0",
    "file-loader": "^0.11.1",
    "node-sass": "^4.5.2",
    "postcss": "^5.2.17",
    "postcss-loader": "^1.3.3",
    "precss": "^1.4.0",
    "react-hot-loader": "^1.3.1",
    "style-loader": "^0.16.1",
    "webpack": "^2.3.3",
    "webpack-dev-server": "^2.4.2"
  },
  "dependencies": {
    "normalize.css": "^6.0.0",
    "react": "^15.5.4",
    "react-dom": "^15.5.4"
  }
}

This config is working with webpack 2 and sass. 此配置正在使用webpack 2和sass。

    {
      test: /\.s?css$/,
      use: ['style-loader', 'css-loader', 'sass-loader']          
    },

And then in the App.js file 然后在App.js文件中

import './app.scss'

The complete webpack.config 完整的webpack.config

var path = require("path");
var webpack = require("webpack");
var autoprefixer = require('autoprefixer');
var precss = require('precss');

module.exports = {
  context: path.resolve(__dirname, 'src'),
  entry: [
    'webpack-dev-server/client?http://localhost:8080',
    'webpack/hot/only-dev-server',
   './index.js'
  ],
  output: {
    path: path.resolve(__dirname, 'build'),
    publicPath: '/build',
    filename: "bundle.js"
  },

  devServer: {
    hot: true,
    contentBase: path.resolve(__dirname, 'build'),
    publicPath: '/',
  },

  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    // new webpack.NoErrorsPlugin(), not needed any more
    new webpack.LoaderOptionsPlugin({
      options: {
        context: __dirname,
        postcss: [
          autoprefixer
        ]
      }
    })
  ],
  module: {
    rules: [{
        test: /\.js?$/,
        exclude: /node_modules/,
        loaders: ['babel-loader']
      },
      {
        test: /\.s?css$/,
        use: ['style-loader', 'css-loader', 'sass-loader' , 'postcss-loader']
      },
      {
        test: /\.(png|jpg|jpeg|gif|svg|woff|woff2)$/,
        loader: 'url-loader?limit=10000',
      }
    ]
  }
};

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

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