webpack错误:模块解析失败您可能需要适当的加载程序来处理此文件类型

时间:2017-02-08 14:54:57

标签: javascript angularjs webpack

我想用extract-text-webpack-plugin从sass文件中提取和编译css代码,但webpack给我这个错误:

ERROR in ./style.scss
Module parse failed: H:\projects\new\app\style.scss Unexpected token (1:4)
You may need an appropriate loader to handle this file type.
| body{
|   background-color: #d9534f;
| }
 @ ./index.js 2:0-22

我的webpack版本和extract-text-webpack-plugin:

"extract-text-webpack-plugin": "^2.0.0-beta.4",
"webpack": "^2.1.0-beta.22"

我的webpack.config.js

var path = require('path');
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
let ExtractTextPlugin = require('extract-text-webpack-plugin');
let extractCSS = new ExtractTextPlugin('test.css');

module.exports = {
    context:path.resolve(__dirname, 'app'),
    entry: './index.js',
    output: {
        path: path.resolve(__dirname, 'app'),
        filename: 'bundle.js'
    },
    module: {
        loader: [
            {
                test: /\.js$/,
                loader: 'babel-loader',
                query: {
                    presets: ['es2015']
                }
            }
            , {
                test: /\.s?css$/,
                loader: extractCSS.extract({ fallbackLoader: 'style-loader', loader: 'css-loader!sass-loader' })
    }
            , {
                test: /\.html$/,
                loader: 'raw-loader'
            },
            {
                test: /\.(jpe?g|png|gif)$/,
                exclude: /(node_modules)/,
                loader: 'url-loader?limit=10000'
            },
            {
                test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                loader: 'url-loader?limit=10000&minetype=application/font-woff'
}, {
        test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                loader: 'url-loader'
}
        ]
    },
    plugins: [
        extractCSS,
    new HtmlWebpackPlugin({
        template: path.resolve(__dirname, 'app')+'/index.html',
        inject: 'body'
    })
]
};

和我简单的.scss文件:

body{
  background-color: #d9534f;
}

3 个答案:

答案 0 :(得分:0)

这样对我有用。

在顶部添加const ExtractTextPlugin = require("extract-text-webpack-plugin");

并将其添加到加载器:

        test: /\.s?css$/,
        loader: ExtractTextPlugin.extract({ fallback: 'style-loader', use: 'css-loader!sass-loader'}),
    },

答案 1 :(得分:0)

我解决它:

 module: {
        loaders: [
            {

而不是

 module: {
        loader: [
            {

答案 2 :(得分:0)

这里你去了好友

{ test: /\.sass$/, use: extractText.extract({ fallback: 'style-loader', use: 'css-loader!sass-loader' }) }