webpack配置踩坑心得_exclude和You may need an appropriate loader to handle this file type.

问题描述

在导入 bootstrap.css 的时候,报错:此处 bootstrap 为 3.3.7 版本

ERROR in ./node_modules/bootstrap/dist/css/bootstrap.css 7:5
Module parse failed: Unexpected token (7:5)
You may need an appropriate loader to handle this file type.
|  */
| /*! normalize.css v3.0.3 | MIT License | github.com/necolas/normalize.css */

> html {
> |   font-family: sans-serif;
> |   -webkit-text-size-adjust: 100%;
>  @ ./src/components/CmtList.jsx 28:0-55 29:12-19
>  @ ./src/index.js
>  @ multi (webpack)-dev-server/client?http://localhost:8081 (webpack)/hot/dev-server.js ./src

可是我明明配置了 css-loader sass-loader less-loader file-loader url-loader 等各种loader。。。 

出错原因及解决办法

{
    test: /\.css$/,
    use: [
            'style-loader',
            'css-loader?modules&localIdentName=[path][name]-[local]-[hash:6]'
    ],
    exclude: /(node_modules|bower_components)/
}

在解析 css ,ttf 等各种静态资源的时候,不要添加 exclude 排除项,bootstrap.css 位于 node_modules 文件夹中,排除之后 css-loader 将不起作用。

应该去掉 exclude 配置:

{
    test: /\.css$/,
    use: [
            'style-loader',
            'css-loader?modules&localIdentName=[path][name]-[local]-[hash:6]'
    ]
}
发布了51 篇原创文章 · 获赞 27 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_39446719/article/details/88418444