繁体   English   中英

如何从 Webpack 捆绑包中排除 Express 视图引擎

[英]How to exclude Express view engines from Webpack bundle

我正在使用 Webpack v4 捆绑我的 CLI 应用程序。 其中一个依赖项是 Express,这会导致警告:

WARNING in ./node_modules/express/lib/view.js 81:13-25
Critical dependency: the request of a dependency is an expression
 @ ./node_modules/express/lib/application.js
 @ ./node_modules/express/lib/express.js
 @ ./node_modules/express/index.js

这来自 Express 中的这一行

/**
 * Initialize a new `View` with the given `name`.
 *
 * Options:
 *
 *   - `defaultEngine` the default template engine name
 *   - `engines` template engine require() cache
 *   - `root` root path for view lookup
 *
 * @param {string} name
 * @param {object} options
 * @public
 */

function View(name, options) {
  var opts = options || {};

  this.defaultEngine = opts.defaultEngine;
  this.ext = extname(name);

  // ...

  if (!opts.engines[this.ext]) {
    // load engine
    var mod = this.ext.substr(1)
    debug('require "%s"', mod)

    // default engine export
    var fn = require(mod).__express // <-- this require is the problem

有很多问题询问如何通过根本不捆绑express或不捆绑 node_modules 中的任何内容来解决此问题。

对我来说,这会失败(我试图缩小我部署的文件占用空间),所以我想解决这个问题,同时将 express 保留在我的包中。 在我的情况下,我根本不使用视图引擎,并且这个需求仅存在于按需加载视图引擎,所以我真的只是想要 go 的警告。

如果我确信永远不会调用这个 require,我怎么能告诉 webpack 完全忽略它?

您可以尝试的是更改您的 webpack 配置模块规则,以便view单元使用null-loader

这当然会使View返回null但如果您从不触摸视图可能没问题。

例子。

rules: [
 {
   test: require.resolve("express/view"),
   use: 'null-loader',
  },
],

application

this.set('view', View); 希望在这里查看 null 不会引起问题。

然后在应用程序中提到View的唯一其他地方是在您说您没有使用的render中。 所以手指交叉不会造成任何副作用。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM