0

我收到以下错误:

./common/app.css 中的错误 模块解析失败:E:\universal-starter\common\app.css Unexpected token (1:5) 您可能需要适当的加载程序来处理此文件类型。

| body {
|     background-color: orange;
| }

我的 App.js 文件:

import React from 'react';
require('./app.css');

const App = () => <div>Hello from React</div>;

export default App;

我的 webpack.config:

const webpack = require('webpack');
const path = require('path');
const ExtractTextWebpackPlugin = require('extract-text-webpack-plugin');
const HTMLWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  devtool: 'inline-source-map',
  entry: [
    'react-hot-loader/patch',
    'webpack-dev-server/client?http://localhost:3001',
    'webpack/hot/only-dev-server',
    './client/index',
  ],
  target: 'web',

  module: {
    rules: [
      {
        enforce: 'pre',
        exclude: /node_modules/,
        loader: 'eslint-loader',
        options: {
          emitWarning: true
        },
        test: /\.(js|jsx)$/
      },
      {
        exclude: /node_modules/,
        test: /\.js?$/,
        use: ['babel-loader']},

//为什么这是错误的并导致问题?!?!?!?!

      // embed styles in styles folder
      {

        test: /\.css$/,
        use: ExtractTextWebpackPlugin.extract({
          fallback: 'style-loader',
          use: ['css-loader']
        })
      },

      // fonts
      {
        test: /\.(woff|woff2|eot|ttf|svg)$/,
        exclude: /node_modules/,
        loader: 'url-loader?limit=1024&name=fonts/[name].[ext]'
      },
      // examine img file size
      {
        test: /\.(jpe?g|png|svg|gif)$/,
        use: [{
          loader: 'url-loader',
          options: {
            limit: 40000,
            name: 'images/[name].[hash].[ext]',
          }
        },
        {
          loader: 'image-webpack-loader',
        }
        ]
      }
    ],
  },

  plugins: [
    new ExtractTextWebpackPlugin('./css/styles.css'),
    new webpack.NamedModulesPlugin(),
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoEmitOnErrorsPlugin(),
    new HTMLWebpackPlugin({
      template: './client/template/index.html',
    }),
    new webpack.DefinePlugin({
      'process.env': { BUILD_TARGET: JSON.stringify('client') },
    }),
  ],

  devServer: {
    host: 'localhost',
    port: 3001,
    historyApiFallback: true,
    hot: true,
  },
  output: {
    path: path.join(__dirname, '.build'),
    publicPath: 'http://localhost:3001/',
    filename: 'client.js',
  },
};

到目前为止,对 Webpack 3 的帮助很少。有什么想法吗?谢谢!!

4

1 回答 1

0

试试这个:在你的 App.js 文件中,替换require('./app.css');import './app.css'

于 2017-07-06T01:40:54.077 回答