webpack 在 react.js 中无法正常工作

webpack is not working properly in react.js

我已经使用 create-react-app 命令创建了一个 "hello-world" React 应用程序,然后我尝试使用 webpack 运行 相同的文件,但它无法正常工作,像 .ico.css 文件在屏幕上 not rendering。请帮我解决这个问题。

webpack.config.js

const path = require('path');

module.exports = {
  entry: './src/index.js',
  output: {
    path: path.join(__dirname, 'public'),
    filename: 'bundle.js'
  },
  module: {
    rules: [{
      loader: 'babel-loader',
      test: /\.js$/,
      exclude: /node_modules/
    },
    {
      test: /\.css$/,
      exclude: /node_modules/,
      use: [
        {
          loader: 'style-loader'
        },
        {
          loader: 'css-loader',
          options: {
            modules: true,
            // camelCase: true,
            sourceMap: true
          }
        }
      ]
    },
    {
      test: /\.svg$/,
      loaders: [
        'babel-loader',
        {
          loader: 'react-svg-loader',
          query: {
            jsx: true
          }
        },
      ]
    },
    {
      test: /\.json$/,
      loader: 'json-loader',
    },
    {
      test: /\.(jpg|jpeg|gif|png|ico)$/,
      exclude: /node_modules/,
      loader:'file-loader?name=img/[path][name].[ext]&context=./app/images'
   }
  ]
  },
  devtool: 'cheap-module-eval-source-map',
  devServer: {
    contentBase: path.join(__dirname, 'public')
  }
};

package.json

{
  "name": "indecision-app",
  "version": "1.0.0",
  "main": "index.js",
  "author": "Andrew Mead",
  "license": "MIT",
  "scripts": {
    "serve": "live-server public/",
    "build": "webpack",
    "start": "webpack-dev-server"
  },
  "dependencies": {
    "json-loader": "^0.5.7",
    "live-server": "^1.2.0",
    "react": "15.6.1",
    "react-dom": "15.6.1",
    "react-svg-loader": "^2.1.0",
    "style-loader": "^0.20.3",
    "validator": "8.0.0"
  },
  "devDependencies": {
    "babel-cli": "6.24.1",
    "babel-core": "6.25.0",
    "babel-loader": "7.1.1",
    "babel-preset-env": "1.5.2",
    "babel-preset-react": "6.24.1",
    "css-loader": "^0.28.11",
    "file-loader": "^1.1.11",
    "html-webpack-plugin": "^3.2.0",
    "webpack": "^2.7.0",
    "webpack-cli": "^2.0.14",
    "webpack-dev-server": "2.5.1"
  }
}

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="theme-color" content="#000000">
    <link rel="manifest" href="%PUBLIC_URL%/manifest.json">
    <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
    <title>React App</title>
  </head>
  <body>
    <noscript>
      You need to enable JavaScript to run this app.
    </noscript>
    <div id="root"></div>
    <script src="/bundle.js"></script>
  </body>
</html>

.babelrc

{
  "presets": ["env", "react"]
}

创建 create-react-app 时剩余的文件与 auto-generated 相同。帮我解决这个问题

  1. Git 提交所有更改
  2. 运行 在 cmd 上执行此命令:> npm 运行 eject
  3. Go生成的文件夹名称:config>webpack.config.dev.js>line#166(近似)CSS编译器 在选项对象上附加代码

    importLoaders: 1,
    modules: true,
    localIdentName: '[name]__[local]__[hash:base64:5]',

  4. 并添加配置>webpack.config.prod.js> line#183(近似)css编译器 将以上代码附加到选项对象

    importLoaders: 1,
    modules: true,
    localIdentName: '[name]__[local]__[hash:base64:5]',

  5. 然后重启服务器。

  6. 现在添加 class 这样的。

    import classes from './App.css';
    <div className={classes.App}>

好吧,create-react-app 已经有了他的 webpack 配置、babel 配置以及它自己的 node_modules 中的其他所有内容。

我看到您更改了启动和构建命令。 create-react-app 在这里设置的默认命令已经运行调用 webpack 的脚本(使用 babel 等),配置正确(处理 .ico 文件和其他所有内容)

如果您想自己处理,可以弹出 create-react-app。这意味着将所有配置文件和脚本放在项目的根目录下,并删除 create-react-app 为您安装的模块。您可以了解更多 here and about the command that ejects the project for you here

如果你想在不弹出的情况下更改webpack配置,你可以使用react-app-rewired。 (因为我想保留来自 create-react-app 仓库的更新,所以我正在使用这个替代方案)