当子相关性需要时,Lodash意外地将自己注入全局

Che*_*aks 3 lodash webpack

我的应用程序依赖于库foolib,库foolib依赖于lodash并且需要它var _ = require('lodash')

要求fooliblodash将结果附加到窗口

我发现这是由于这个原因:

https://github.com/lodash/lodash/blob/45785a282442399cfca829aea496104003f773e2/dist/lodash.js#L17041-L17046

  // Some AMD build optimizers, like r.js, check for condition patterns like:
  if (typeof define == 'function' && typeof define.amd == 'object' && define.amd) {
    // Expose Lodash on the global object to prevent errors when Lodash is
    // loaded by a script tag in the presence of an AMD loader.
    // See http://requirejs.org/docs/errors.html#mismatch for more details.
    // Use `_.noConflict` to remove Lodash from the global object.
    root._ = _;
Run Code Online (Sandbox Code Playgroud)

在调试器触发时将调试器添加到条件结果体中,但导致if分支输入的条件现在返回 false

在调试器之前添加控制台日志以注销值,将其define显示为由webpack:///(webpack)/buildin/amd-define.js?0bba内容定义

module.exports = function() { throw new Error("define cannot be used indirect"); };



/*****************
 ** WEBPACK FOOTER
 ** (webpack)/buildin/amd-define.js
 ** module id = 875
 ** module chunks = 2
 **/
Run Code Online (Sandbox Code Playgroud)

由于我用的WebPack建筑,似乎意想不到的是,与该条件definedefine.amd情况是真实的

Che*_*aks 7

找到了解决方案:https:
//github.com/webpack/webpack/issues/138#issuecomment-160638284

module: {
    noParse: /node_modules\/lodash\/lodash\.js/,
}
Run Code Online (Sandbox Code Playgroud)

添加到webpack配置,问题解决了