我正在使用browserify将前端代码捆绑在一起。到目前为止一切顺利,但是我在混合使用npm和非npm包时遇到了困难。例如,使用npm版本的jQuery与非CJS版本的jQuery插件。
我的当前解决方案是在package.json
中使用browser
键指向jQuery的dist文件夹,然后使用browserify-shim
将其作为插件的依赖项添加。
除了我目前的解决方案之外,还有更简单的方法吗?
编辑:我目前正尝试使用npm和package.json
来管理所有依赖项,因此不想在此项目中使用bower。叫我疯子:)
package.json
{
"dependencies": {
"jquery": "~2.1.0",
"browserify": "latest",
"browserify-shim": "^3.5.0",
"jquery-waypoints": "git@github.com:imakewebthings/jquery-waypoints.git",
"jquery-validation": "git://github.com/jzaefferer/jquery-validation"
},
"browser": {
"jquery": "./node_modules/jquery/dist/jquery.js",
"jquery-waypoints": "./node_modules/jquery-waypoints/waypoints.js",
"jquery-validate": "./node_modules/jquery-validation/build/release.js"
},
"browserify-shim": {
"jquery": "$",
"jquery-waypoints": {
"depends": [
"jquery"
]
},
"jquery-validate": {
"depends": [
"jquery"
]
}
},
"browserify": {
"transform": [
"browserify-shim"
]
}
}
./node_modules/jquery/dist/jquery.js
别名来代替jquery
是否会在最终捆绑包中创建重复的 jQuery 副本?或者,package.json
中有什么问题? - Michael Martin-Smuckerjquery
),但我不喜欢通过shim配置手动配置事物 - 我很懒 :)。祝你好运! - Nick Tomlinbrowser
指令可能不必要。除此之外,这就是正确的代码。 - YPCrumble