Webpack无法找到代码块。

3

我正在尝试在基于这个起始套件的react.js应用程序中设置webpack。

当我启动开发服务器时,应用程序构建成功,并且我可以看到生成的块列表,但是应用程序不会加载,并且在控制台中出现了一个错误,指示找不到块。

我的webpack.config.js

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const WebpackMd5Hash = require('webpack-md5-hash');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const Dotenv = require('dotenv-webpack');

const SRC_DIR = path.join(__dirname, '/src');
const PUB_DIR = path.join(__dirname, '/public');
const DIST_DIR = path.join(__dirname, '/dist');

module.exports = {
  entry: { main: `${SRC_DIR}/index.js` },
  output: {
    path: DIST_DIR,
    chunkFilename: '[chunkhash].chunk.js',

  },
  resolve: {
    alias: {
      src: path.resolve(__dirname, 'src/')
    }
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: ['babel-loader', 'source-map-loader'],

      },
      {
        test: /\.(png|jpg)$/,
        use: {
          loader: 'url-loader',
        },
      },
      {
        test: /\.scss$/,
        use: [
          'style-loader',
          MiniCssExtractPlugin.loader,
          'css-loader',
          'sass-loader',
        ],
      },
      {
        test: /\.css$/,

        use: [
          { loader: 'style-loader' },
          {
            loader: 'css-loader',
            options: {
              modules: true
            },
          },
          { loader: 'sass-loader' }
        ]
      }
    ],
  },
  optimization: {
    splitChunks: {
      cacheGroups: {
        css: {
          test: /\.(css|sass|scss)$/,
          name: 'commons',
          chunks: 'all',
          minChunks: 2,
          enforce: true,
        },
      },
    },
  },
  plugins: [
    new CleanWebpackPlugin({
      cleanAfterEveryBuildPatterns: ['dist'],
    }),
    new MiniCssExtractPlugin({
      filename: 'style.[contenthash].css',
    }),
    new HtmlWebpackPlugin({
      inject: true,
      title: 'React-Redux-Webpack-Starter-Kit',
      template: `${PUB_DIR}/index.html`,
      filename: 'index.html',
      favicon: `${PUB_DIR}/favicon.ico`,
      meta: {
        viewport: 'width=device-width, initial-scale=1, shrink-to-fit=no',
      },
    }),
    new WebpackMd5Hash(),
    new Dotenv(),
  ],
  devServer: {
    port: 3003,
    historyApiFallback: true,
  },

};

我的 packate.json

    {
  "name": "FillRX",
  "author": "Zoran Jeremic",
  "email": "zoran.jeremic@gmail.com",
  "licence": "UNLICENSED",
  "version": "1.0.0",
  "private": true,
  "scripts": {
    "lint": "eslint ./src",
    "start": "webpack-dev-server --open --mode development --watch",
    "prod": "webpack --mode production",
    "build": "cross-env NODE_ENV=production && npm run prod",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "ie 11",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "ie 11",
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "dependencies": {
    "@date-io/core": "^2.5.0",
    "@date-io/moment": "^1.3.13",
    "@fullcalendar/core": "^4.4.0",
    "@fullcalendar/daygrid": "^4.4.0",
    "@fullcalendar/interaction": "^4.4.0",
    "@fullcalendar/list": "^4.4.0",
    "@fullcalendar/react": "^4.4.0",
    "@fullcalendar/timegrid": "^4.4.0",
    "@fullcalendar/timeline": "^4.4.0",
    "@material-ui/core": "^4.9.9",
    "@material-ui/icons": "^4.9.1",
    "@material-ui/lab": "^4.0.0-alpha.48",
    "@material-ui/pickers": "^3.2.10",
    "@material-ui/styles": "^4.9.6",
    "@mdx-js/react": "^1.5.8",
    "@react-pdf/renderer": "^1.6.8",
    "@redux-saga/core": "^1.1.3",
    "apexcharts": "^3.18.0",
    "axios": "^0.19.2",
    "axios-mock-adapter": "^1.17.0",
    "babel-loader": "^8.1.0",
    "change-case": "^4.1.1",
    "chart.js": "^2.9.3",
    "clsx": "^1.1.0",
    "css-loader": "^4.2.1",
    "dotenv-webpack": "^2.0.0",
    "draft-js": "^0.11.5",
    "formik": "^2.1.4",
    "history": "^4.10.1",
    "immer": "^6.0.3",
    "immutable": "^4.0.0-rc.12",
    "js-cookie": "^2.2.1",
    "jsonwebtoken": "^8.5.1",
    "jss": "^10.1.1",
    "jss-rtl": "^0.3.0",
    "jwt-decode": "^2.2.0",
    "lodash": "^4.17.19",
    "moment": "^2.24.0",
    "nan": "^2.14.1",
    "node-sass": "^4.14.1",
    "notistack": "^0.9.9",
    "nprogress": "^0.2.0",
    "prismjs": "^1.20.0",
    "prop-types": "^15.7.2",
    "react": "^16.13.1",
    "react-apexcharts": "^1.3.7",
    "react-app-polyfill": "^1.0.6",
    "react-beautiful-dnd": "^13.0.0",
    "react-big-calendar": "^0.24.1",
    "react-chartjs-2": "^2.9.0",
    "react-dom": "^16.13.1",
    "react-draft-wysiwyg": "^1.14.4",
    "react-dropzone": "^10.2.2",
    "react-feather": "^2.0.3",
    "react-helmet": "^5.2.1",
    "react-markdown": "^4.3.1",
    "react-modal-image": "^2.5.0",
    "react-perfect-scrollbar": "^1.5.8",
    "react-quill": "^1.3.5",
    "react-redux": "^7.2.0",
    "react-router": "^5.1.2",
    "react-router-dom": "^5.1.2",
    "react-scripts": "^3.4.1",
    "redux": "^4.0.5",
    "redux-devtools-extension": "^2.13.8",
    "redux-form": "^8.3.2",
    "redux-logger": "^3.0.6",
    "redux-persist": "^6.0.0",
    "redux-saga": "^1.1.3",
    "redux-thunk": "^2.3.0",
    "sass-loader": "^9.0.3",
    "socket.io-client": "^2.3.0",
    "source-map-loader": "^1.0.1",
    "uuid": "^7.0.3",
    "webpack-dev-server": "^3.11.0",
    "webpack-md5-hash": "^0.0.6",
    "yup": "^0.28.3"
  },
  "devDependencies": {
    "clean-webpack-plugin": "^3.0.0",
    "dotenv": "^8.2.0",
    "eslint": "^6.8.0",
    "eslint-config-airbnb": "^18.1.0",
    "eslint-config-prettier": "^6.10.1",
    "eslint-plugin-import": "^2.20.2",
    "eslint-plugin-jsx-a11y": "^6.2.3",
    "eslint-plugin-mdx": "^1.6.8",
    "eslint-plugin-prettier": "^3.1.2",
    "eslint-plugin-react": "^7.19.0",
    "eslint-plugin-react-hooks": "^2.5.1",
    "html-webpack-plugin": "^4.3.0",
    "mdx-loader": "^3.0.2",
    "mini-css-extract-plugin": "^0.9.0",
    "prettier": "^1.19.1",
    "typescript": "^3.8.3",
    "webpack": "^4.44.1",
    "webpack-cli": "^3.3.12"
  }
}

这是我在控制台中得到的错误信息:

在此输入图片描述

所有未找到的代码块都是在应用程序构建期间生成的,但我想我的配置无法找到它们:

在此输入图片描述

我以前没有使用过 webpack,所以我不知道如何调查这个问题,因为错误信息并没有指出可能存在的问题,而且谷歌搜索给出的结果表明存在网络问题,但我不认为这是问题的原因。

非常感谢任何帮助和建议。


尝试添加:output: { path: DIST_DIR, chunkFilename: '[chunkhash].chunk.js', publicPath: "/" }, - simbathesailor
1
就是这样。你救了我的一天。你想把它写成答案,这样我就可以接受它吗? - zoran jeremic
当然,我正在添加它。 - simbathesailor
1个回答

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接