簡體   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