webpack破天荒 [英] Webpack breaking change

查看:29
本文介绍了webpack破天荒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个Reaction应用程序,但每次运行NPM Start时都会收到此消息

找不到模块:错误:无法解析‘/Users/abdus/Documents/GitHub/keywords-tracker/node_modules/buffer-equal-constant-time’

中的‘BUFFER

突破性变化:webpack<;5默认包含node.js核心模块的PolyFill。 现在已经不是这样了。请验证您是否需要此模块,并为其配置多边形填充。

它为几个不同的模块提供相同的消息。我已尝试NPM安装这些模块,但错误仍然存在

推荐答案

这是我工作的webpack设置。您应该安装fallback

中列出的所有软件包
// const path = require("path");
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const webpack = require("webpack");

module.exports = {
  mode: "development",
  target: "web",
  entry: ["regenerator-runtime/runtime", "./src/index.js"],
  output: {
    filename: "bundle.js",
    path: path.join(__dirname, "dist"),
    publicPath: "/",
  },
  resolve: {
    extensions: [".js", ".css"],
    alias: {
      // add as many aliases as you like!
      components: path.resolve(__dirname, "src/components"),
    },
    fallback: {
      // path: require.resolve("path-browserify"),
      fs: false,
      assert: require.resolve("assert/"),
      os: require.resolve("os-browserify/browser"),
      constants: require.resolve("constants-browserify"),
      stream: require.resolve("stream-browserify"),
      crypto: require.resolve("crypto-browserify"),
      http: require.resolve("stream-http"),
      https: require.resolve("https-browserify"),
    },
  },
  // devtool: "eval-cheap-source-map",
  devtool: "eval",
  module: {
    rules: [
      { test: /.(js|jsx)/, loader: "babel-loader", exclude: /node_modules/ },
      { test: /.css$/, use: ["style-loader", "css-loader"] },
      //   {
      //     test: /.m?js/,
      //     resolve: {
      //         fullySpecified: false
      //     }
      // },
      {
        test: /.(woff(2)?|ttf|eot|jpg|jpeg|png|gif)(?v=d+.d+.d+)?$/,
        use: [
          {
            loader: "file-loader",
            options: {
              name: "[name].[contenthash].[ext]",
              outputPath: "fonts/",
            },
          },
        ],
      },
      {
        test: /.svg$/,
        use: [
          {
            loader: "svg-url-loader",
            options: {
              limit: 10000,
            },
          },
        ],
      },
      {
        test: /.json5$/i,
        loader: "json5-loader",
        type: "javascript/auto",
        options: {
          esModule: true,
        },
      },
    ],
  },
  devServer: {
    contentBase: path.join(__dirname, "build"),
    historyApiFallback: true,
    overlay: true,
  },

  plugins: [
    new HtmlWebpackPlugin({
      title: "NFT",
      template: "src/index.html",
    }),
    // new CopyWebpackPlugin({
    //   patterns: [{ from: "assets", to: "assets" }],
    // }),
    
  ],
};

您可以获得此webpack5-Boilerplate

  • 由于多边形填充太多,您可以使用node-polyfill-webpack-plugin包,而不是手动全部安装。而不是fallback属性

     const NodePolyfillPlugin = require("node-polyfill-webpack-plugin");
    
    plugins: [
      new HtmlWebpackPlugin({
        title: "esBUild",
        template: "src/index.html",
      }),
      // instead of fallback
      new NodePolyfillPlugin(),
    
      // new webpack.ProvidePlugin({
      // process: "process/browser",
      // Buffer: ["buffer", "Buffer"],
      // React: "react",
      }),
    ],
    

这篇关于webpack破天荒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆