简体   繁体   English

当应用程序需要配置时,Webpack5 防止后备包被解析

[英]Webpack5 prevent fallback packages from being resolved when config is required in application

In Webpack 5, polyfill of core node modules are removed, instead required packages are required to listed in resolve.fallback property.在 Webpack 5 中,移除了核心节点模块的 polyfill,而是需要在 resolve.fallback 属性中列出所需的包。 Below is resolve property of webpack.config.client.js file.下面是 webpack.config.client.js 文件的解析属性。

resolve: {
  fallback: {
    path: require.resolve("path-browserify"),
    crypto: require.resolve("crypto-browserify"),
    "babel-polyfill": require.resolve("@babel/polyfill"),
    buffer: require.resolve("buffer/"),
    stream: require.resolve("stream-browserify"),
  }
}

I use webpack-dev-middleware and webpack-hot-middleware along with express to achieve benefit of HMR for my ssr application.我使用 webpack-dev-middleware 和 webpack-hot-middleware 以及 express 来为我的 ssr 应用程序实现 HMR 的好处。

import express from "express";
import webpack from "webpack";
...
const app = express();
...
const webpackDevConfig = require("../webpack/webpack.config.client");
const compiler = webpack(webpackDevConfig);
app.use(
  require("webpack-dev-middleware")(compiler, {
    publicPath: webpackDevConfig.output.publicPath,
  }),
);
app.use(require("webpack-hot-middleware")(compiler));
...

When the config file is imported, those modules in fallback property are resolved and returned as number.导入配置文件时,后备属性中的那些模块将被解析并以数字形式返回。 And webpack throws an error when the config object is passed to its constructor since configuration.resolve.fallback property should be non empty string, but numbers were given.并且 webpack 在将配置 object 传递给其构造函数时引发错误,因为configuration.resolve.fallback属性应该是非空字符串,但给出了数字。

Below is the actual resolve.fallback property passed, and the error returned.下面是实际传递的 resolve.fallback 属性,并返回了错误。

{
  path: 79936 (which suppose to be "/some path/node_module/path/index.js",
  crypto: 32640,
  'babel-polyfill': 71202,
  buffer: 30816,
  stream: 78787
}

ValidationError: Invalid configuration object. Webpack has been initialized using a configuration object that does not match the API schema.
 - configuration.resolve.fallback should be one of these:
   [object { alias, name, onlyModule? }, ...] | object { <key>: [non-empty string, ...] | false | non-empty string }
   -> Redirect module requests. 
.....

Use Webpack Magic Comment , then webpack won't bundle the imported libraries.使用Webpack Magic Comment ,则 webpack 不会捆绑导入的库。

const webpackDevConfig = require(/* webpackIgnore: true */ "../webpack/webpack.config.client");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Webpack 配置中的“回退”规则 - "Fallback" rule in Webpack config 从 webpack4 -&gt; webpack5 迁移,捆绑文件在被请求时会有错误的 url? - Migration from webpack4 -> webpack5, bundled file will have a faulty url when it gets requested? webpack5 不从.json 文件导入内容? - webpack5 does not import content from .json file? 如何从 Angular 应用程序中查看 webpack 配置? - How to see webpack config from angular application? 如何防止机密数据包含在WebPack包中 - How to prevent confidential data from being included in WebPack bundles 如何在 webpack 中将 config.js 文件作为外部文件(在运行时需要它而不是捆绑) - How to have a config.js file as an external (it being required during run time and not bundled) in webpack 在 azure devops 中创建构建管道时反应应用程序 webpack5 配置出现问题 - Issue with react app webpack5 configuration when creating build pipelines in azure devops 如何防止在 webpack 构建期间丢弃未使用的代码? - How to prevent unused code from being dropped during webpack build? Webpack - Monorepo 中所有包的一个配置 - Webpack - one config for all packages in a Monorepo 将 Webpack5 与 Next.JS 应用程序一起使用时出现 Storybook 错误 + Typescript - Storybook error when using Webpack5 with Next.JS app + Typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM