我有以下webpack配置:
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_PATHnode_modulesbackbone
@ ./~/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
调用作为我需要它。
Backbone依赖于jQuery,如果你使用其他类似jQuery的模块,如zepto,你需要给它jquery
作为别名。
From the Backbone wiki:
使用WebPack,解析。别名配置选项可以使用:
{ context: __dirname + "/app", entry: "./entry", output: { path: __dirname + "/dist", filename: "bundle.js" } resolve: { alias: { "jquery": "zepto" } } }