简体   繁体   English

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

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

I'm bundling my CLI app using Webpack v4.我正在使用 Webpack v4 捆绑我的 CLI 应用程序。 One of the dependencies is Express, and this causes a warning:其中一个依赖项是 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

That comes from this line within Express:这来自 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

There's quite a few questions asking about how to fix this by not bundling express at all, or not bundling anything from node_modules.有很多问题询问如何通过根本不捆绑express或不捆绑 node_modules 中的任何内容来解决此问题。

For me that would defeat the point (I'm trying to shrink my deployed file footprint), so I want to fix this whilst keeping express inside my bundle.对我来说,这会失败(我试图缩小我部署的文件占用空间),所以我想解决这个问题,同时将 express 保留在我的包中。 In my case I don't use view engines at all, and this require exists solely to load view engines on demand, so I really just want the warning to go away.在我的情况下,我根本不使用视图引擎,并且这个需求仅存在于按需加载视图引擎,所以我真的只是想要 go 的警告。

If I'm confident that this require will never be called, how can I tell webpack to ignore it completely?如果我确信永远不会调用这个 require,我怎么能告诉 webpack 完全忽略它?

What you could maybe try is alter you webpack config module rules so that view unit uses the null-loader您可以尝试的是更改您的 webpack 配置模块规则,以便view单元使用null-loader

This will of course make View return null but if you never touch views it might be ok.这当然会使View返回null但如果您从不触摸视图可能没问题。

example.例子。

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

Looking at applicationapplication

this.set('view', View); hopefully View been null here doesn't cause issues.希望在这里查看 null 不会引起问题。

The only other place View is then mentioned in application is then in render that you say your not using.然后在应用程序中提到View的唯一其他地方是在您说您没有使用的render中。 So fingers crossed this won't cause any side effects.所以手指交叉不会造成任何副作用。

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

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