在Webpack中编译Scss

时间:2017-12-04 23:02:38

标签: javascript css webpack

所以我试图让我的webpack配置文件创建一个已编译的css文件,以便稍后当我添加webpack-dev-server时,我将能够编译我的js和css并将其放入dist文件夹。我认为这很简单所以我到目前为止在我的webpack.config.js文件

中已经有了这个
var ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
  entry: ['./js/app.js', './scss/main.scss'],
  output: {
    filename: 'dist/bundle.js'
  },
  module: {

    rules: [
      /*
      your other rules for JavaScript transpiling go in here
      */
      { // regular css files
        test: /\.css$/,
        loader: ExtractTextPlugin.extract({
          loader: 'css-loader?importLoaders=',
        }),
      },
      { // sass / scss loader for webpack
        test: /\.(sass|scss)$/,
        loader: ExtractTextPlugin.extract(['css-loader', 'sass-loader'])
      }
    ]
  },
  plugins: [
    new ExtractTextPlugin({ // define where to save the file
      filename: 'dist/[name].bundle.css',
      allChunks: true,
    }),
  ],
};

但是当运行webpack命令时,我正在

throw new _ValidationError2.default(ajv.errors, name);

我不知道为什么,但我认为这可能是因为我使用的是过时的东西所以我去搜索并且我找不到任何有用的东西。

哦,我也在Windows电脑上,这是我的package.json

 {
  "name": "portfolio_site",
  "version": "1.0.0",
  "description": "",
  "main": "webpack.config.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack",
    "start": "webpack-dev-server"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "css-loader": "^0.28.7",
    "extract-text-webpack-plugin": "^3.0.2",
    "node-sass": "^4.7.2",
    "sass-loader": "^6.0.6",
    "style-loader": "^0.19.0",
    "webpack": "^3.9.1",
    "webpack-dev-server": "^2.9.5"
  },
  "dependencies": {}
}

先谢谢,我只是想知道它为什么不起作用。

1 个答案:

答案 0 :(得分:0)

在你的webpack配置中:

{ // regular css files
        test: /\.css$/,
        loader: ExtractTextPlugin.extract({
          loader: 'css-loader?importLoaders=',
        }),
      },
      { // sass / scss loader for webpack
        test: /\.(sass|scss)$/,
        loader: ExtractTextPlugin.extract(['css-loader', 'sass-loader'])
      }

相反它应该是:

{ // regular css files
        test: /\.css$/,
        use: ExtractTextPlugin.extract({
          use: 'css-loader?importLoaders=',
        }),
      },
      { // sass / scss loader for webpack
        test: /\.(sass|scss)$/,
        use: ExtractTextPlugin.extract(['css-loader', 'sass-loader'])
      }

"装载"已被弃用。

参考:https://github.com/webpack-contrib/extract-text-webpack-plugin/issues/569