Bus*_*eBu 6 jquery jquery-mobile webpack
我试图用webpack加载jquery-mobile.但到目前为止没有运气.
这是我的jquery和jquery-mobile的webpack.config代码:
loaders: [
{
test: /jquery.mobile.js$/,
loader: "imports?define=>false,this=>window"
},
resolve: {
alias: {
"jquery": "jquery/src/jquery",
"jquery-mobile": "jquery-mobile/dist/jquery.mobile"
},
},
plugins: [
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
"window.jQuery": "jquery"
}),
]
Run Code Online (Sandbox Code Playgroud)
这是jquery.mobile文件中的函数,它会导致麻烦:
(function ( root, doc, factory ) {
if ( typeof define === "function" && define.amd ) {
// AMD. Register as an anonymous module.
define( [ "jquery" ], function ( $ ) {
factory( $, root, doc );
return $.mobile;
});
} else {
// Browser globals
factory( root.jQuery, root, doc );
}
}( this, document, function ( jQuery, window, document, undefined ) {
(function( $ ) {
$.mobile = {};
}( jQuery ));
Run Code Online (Sandbox Code Playgroud)
问题是root.jQuery未定义.在那个函数里面"这个"==="窗口",当我用imports-loader注入这个=>窗口时,我检查了一下.
另一个奇怪的时刻:如果我用"窗口"替换"this",就像那样:
}( window, document, function ( jQuery, window, document, undefined ) {
Run Code Online (Sandbox Code Playgroud)
一切都变好了.但我无法修改jquery.mobile文件,这可能会在将来造成麻烦.
非常感谢任何帮助,谢谢!
以下需要 imports-loader ( npm install imports-loader --save)
jQuery mobile 期望this成为全局上下文 ( window):
require("imports?this=>window!jquery-mobile/dist/jquery.mobile.js");
Run Code Online (Sandbox Code Playgroud)
参考: https: //webpack.github.io/docs/shimming-modules.html