加载Sass时,Webpack ExtractTextPlugin会引发错误

时间:2016-03-28 01:59:26

标签: sass webpack sass-loader

尝试将sass-loader添加到我的webpack配置中并遇到错误:

70% 1/1 build modules/Users/a557789/Documents/f/Portal/node_modules/webpack/node_modules/webpack-core/lib/LoadersList.js:81
        r.forEach(function(r) {
          ^
TypeError: undefined is not a function
    at /Users/a557789/Documents/f/Portal/node_modules/webpack/node_modules/webpack-core/lib/LoadersList.js:81:5
    at Array.reduce (native)
    at LoadersList.match (/Users/a557789/Documents/f/Portal/node_modules/webpack/node_modules/webpack-core/lib/LoadersList.js:80:27)

webpack.config:

var webpack = require("webpack");
var baseDir = "dist";

var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require("extract-text-webpack-plugin");

var path = require("path");

module.exports = {
    context: __dirname + "/app",
    entry: {
        app: "./main"
    },
    resolve: {
        extensions: ['', '.js', '.ts', '.css', '.scss']
    },
    output: {
        path: __dirname + "/dist",
        sourceMapFilename: "[name].map",
        filename: "[name].js"
    },
    module: {
        loaders: [
            //https://www.npmjs.com/package/webpack-typescript
            {
                test: /\.ts$/,
                loader: "ts-loader"
            },
            {
                test: /\.scss$/,
                loaders: ExtractTextPlugin.extract("style", "css!sass")
                //loaders: ExtractTextPlugin.extract("style!css!sass")
            }
        ],
        noParse: [ /angular2\/bundles\/.+/ ]
    },
    plugins: [
        new webpack.optimize.OccurenceOrderPlugin(true),
        new ExtractTextPlugin("style.css"),
        new HtmlWebpackPlugin({
            template: "../index.html",
            inject: "body"
        })
    ],
    devtool: "source-map"
};

我已经为extract()调用的参数尝试了一些不同的选项,但没有运气。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:4)

而不是使用

loaders: ExtractTextPlugin.extract("style", "css!sass")

你应该使用

loader: ExtractTextPlugin.extract("style", "css!sass")

代替。

在这种情况下,错误并不是特别具有描述性。

答案 1 :(得分:0)

在我的情况下,解决方案是使用MiniCssExtractPlugin代替ExtractTextPlugin。该视频中的更多详细信息https://www.youtube.com/watch?v=JlBDfj75T3Y&list=PLblA84xge2_zwxh3XJqy6UVxS60YdusY8&index=10 所以,我的配置看起来像

const { CleanWebpackPlugin } = require("clean-webpack-plugin");    
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const path = require("path");
module.exports = {
    entry: "./src/index.js",
    output: {
        filename: "[name].[contenthash].bundle.js",
        path: path.resolve(__dirname, "dist"),
    },
    module: {
        rules: [
            //babel
            {
                test: /\.m?js$/,
                exclude: /(node_modules|bower_components)/,
                use: {
                    loader: "babel-loader",
                    options: {
                        presets: ["@babel/preset-env"],
                    },
                },
            },
            //css
            {
                test: /\.less$/,
                use: [MiniCssExtractPlugin.loader, "css-loader", "less-loader"],
            },
        ],
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: "index.html",
            title: "My app",
        }),
        new MiniCssExtractPlugin(),
        new CleanWebpackPlugin(),
    ],
};