Webpack 4-模块解析失败:意外字符''

时间:2018-06-19 11:47:11

标签: webpack webpack-dev-server webpack-style-loader

尝试捆绑我的应用程序时,我遇到了麻烦。

  

未捕获的错误:模块构建失败(来自   ./node_modules/css-loader/index.js):ModuleParseError:模块解析   失败:意外字符''

     

./ src / static / css / main.css模块构建失败(来自   ./node_modules/css-loader/index.js):ModuleParseError:模块解析   失败:意外字符''

     

./ src / static / fonts / CHILLER.TTF 1:0模块解析失败:意外   字符''

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

module.exports = {
  module: {
    rules: [

      {
          test: /\.(png|jp?g|svg)(\?v=\d+\.\d+\.\d+)?$/,
          use : [{
              loader: 'file-loader',
            options: {
                name: '[name].[ext]'
            }}
            ]
      },
      {
        test: /\.(woff|woff2|eot|ttf)?$/,
        loader: 'file-loader',
        options: {
          name: "[name].[ext]"
        }
        },
      {
        test: /\.css$/,
        use: ExtractTextPlugin.extract({
          fallback: "style-loader",
          use: "css-loader"
        })
      },
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader'
        }
      }

    ]
  },
  plugins: [
    new HtmlWebpackPlugin({ 
      template: './src/index.html', 
      filename: './index.html' 
    }),
    new ExtractTextPlugin('style.css')
  ]
}

2 个答案:

答案 0 :(得分:0)

您需要适当的加载程序来加载不同的文件类型。 对于CSS,您可以只使用css-loader和style-loader。 这是CSS加载的工作示例 https://www.codeqna.com/2018/06/working-with-webpack-4-and-npm.html

答案 1 :(得分:0)

此配置适用于我。 Webpack 4.16

webpack.config.js

   const path = require('path');
    const webpack = require('webpack');

    module.exports = {

        entry: ['babel-polyfill', './src/index.js'],
        output: {
            path: path.resolve(__dirname, 'dist'),
            filename: 'bundle.js'
        },
        module: {
            rules: [
                { test: /.css$/, use: ['style-loader', 'css-loader'] },
                {
                    test: /.js?$/,
                    exclude: /(node_modules)/,
                    loader: 'babel-loader',
                    query: {
                        presets: [
                            'env', 'react', 'stage-0'
                        ],
                        plugins: [
                            'transform-class-properties',
                            'transform-decorators-legacy',
                        ]

                    }
                },

            ],
        },
        resolve: {
            alias: {

            }
        },
        stats: 'normal'
    };

src / .babelrc

{
    "presets": [
        "env",
        "react",
        "stage-0"
    ],
    "plugins": [
        "transform-class-properties",
        "transform-decorators-legacy",
    ],
    "env": {
        "test": {
            "plugins": [
                "transform-es2015-modules-commonjs"
            ]
        },

    }