2016-10-18 57 views
0

我有以下的WebPack配置:的WebPack加載失敗骨幹沒有jQuery的

module.exports = { 
    entry: "./league/index.ts", 
    output: { 
     path: "./", 
     filename: "bundle.js" 
    }, 
    resolve: { 
     // Add `.ts` and `.tsx` as a resolvable extension. 
     extensions: ["", ".ts", ".js"] 
    }, 
    module: { 
     loaders: [ 
      // all files with a `.ts` or `.tsx` extension will be handled by `ts-loader` 
      { test: /\.ts?$/, loader: "ts-loader" } 
     ] 
    } 
}; 

當我運行webpack,我得到這個錯誤(實際項目路徑與PROJECT_PATH取代了隱私):

ERROR in ./~/backbone/backbone.js 
Module not found: Error: Cannot resolve module 'jquery' in PROJECT_PATH\node_modules\backbone 
@ ./~/backbone/backbone.js 17:4-21:6 

原因是這個代碼在backbone.js

// Set up Backbone appropriately for the environment. Start with AMD. 
    if (typeof define === 'function' && define.amd) { 
    define(['underscore', 'jquery', 'exports'], function(_, $, exports) { 
     // Export global even in AMD case in case this script is loaded with 
     // others that may still expect a global Backbone. 
     root.Backbone = factory(root, exports, _, $); 
    }); 

    // Next for Node.js or CommonJS. jQuery may not be needed as a module. 
    } else if (typeof exports !== 'undefined') { 
    var _ = require('underscore'), $; 
    try { $ = require('jquery'); } catch (e) {} 
    factory(root, exports, _, $); 

    // Finally, as a browser global. 
    } 

jQuery不是我需要的依賴關係,但webpack正在解釋require調用,因爲我需要它。

回答

1

骨幹依賴於jQuery,如果您使用另一個類似於jQuery的模塊(如zepto),則需要將其作爲別名jquery

Backbone wiki

使用的WebPack,所述resolve.alias配置選項可用於:

{ 
    context: __dirname + "/app", 
    entry: "./entry", 
    output: { 
     path: __dirname + "/dist", 
     filename: "bundle.js" 
    } 
    resolve: { 
     alias: { 
      "jquery": "zepto" 
     } 
    } 
}