【问题标题】:Node.js lazy require moduleNode.js 懒惰的 require 模块
【发布时间】:2018-11-25 21:47:01
【问题描述】:

我正在写一个惰性模块 require() 或 import

const lazy = new Proxy({}, 
  {
    get: function (target, name) {
      console.log('lazy require', { target, name })
      return require(name)
    }
  }
)

/**
  * @param {string} Module Name
  * @example const expo = requirez('expo')
  */
export default function requirez(name) {
    return lazy[name]
}

奇怪的是,当我运行它时,我得到:

找不到模块“。”

console.log 语句记录:

懒惰要求{target: {…}, name: "./Linking"}

所以require(name) 应该被称为:require("./Linking")

与错误指示的require(".") 不同。

【问题讨论】:

    标签: node.js module lazy-loading require


    【解决方案1】:

    找到一个相关的错误报告:

    https://github.com/webpack/webpack/issues/4921

    由于节点 require 树分辨率是静态评估/分析的,并且 webpack 假设它在动态分辨率上失败。

    此外,在浏览器上,webpack 必须在在浏览器中运行之前转译所需的包,因此动态惰性要求在转译后无法运行。您将缺少该所需模块的转译源。

    我尝试过使用import(),但它也有错误:

    https://github.com/webpack/webpack/issues/4292#issuecomment-280165950

    【讨论】:

      猜你喜欢
      • 2018-08-15
      • 2010-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-27
      • 2019-12-28
      相关资源
      最近更新 更多