webpack - underscore - 在相互依存的情況下,Lodash意外地將自己注入全球



webpack lodash import (1)

我的應用程序依賴於庫foolib ,庫foolib依賴lodash,並要求通過var _ = require('lodash')

需要foolib結果就是把自己附在窗戶上

我發現這是因為:

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._ = _;

在調試器觸發中將調試器添加到條件結果體中,但現在導致if分支的條件返回false

在調試器之前添加控制台日誌以註銷define的值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
 **/

因為我正在用webpack構建,所以define和define.amd的條件是真的,這似乎是意外的






lodash