简体   繁体   English

在Webpack中使用jquery-mobile

[英]Using jquery-mobile with Webpack

I am trying to load jquery-mobile using webpack with no luck so far. 我试图使用webpack加载jquery-mobile到目前为止没有运气。 I am aware that jquery-mobile depends on jquery-ui which in turn depends on jquery. 我知道jquery-mobile依赖于jquery-ui,而jquery-ui又依赖于jquery。

How do I set up such a scenario in Webpack? 如何在Webpack中设置这样的场景?

Here is my webpack.config.js : 这是我的webpack.config.js

var webpack = require('webpack');
var path = require('path');
var bower_dir = __dirname + '/bower_components';

module.exports = {

    context: __dirname,
    entry: './assets/js/index.js',         
    output: {
        path: path.resolve('./assets/bundles/'),
        filename: "[name]-[hash].js",
    },
    plugins: [
        new BundleTracker({filename: './webpack-stats.json'}),
        new webpack.ProvidePlugin({
            $: 'jquery',
            jQuery: 'jquery',
            'window.jQuery': 'jquery'
        })
    ],
    module: {
        loaders: [
            {test: /[\/\\]node_modules[\/\\]some-module[\/\\]index\.js$/, loader: "imports?this=>window"}
        ]
    },
    resolve: {
        alias: {
            'jquery': bower_dir + '/jquery/src/jquery.js',
            'jquery-ui': bower_dir + '/jquery-ui/jquery-ui.js',
            'jquery-mobile': bower_dir + '/jquery-mobile/js/jquery.mobile.js'
        }
    }
};

Any help would be much appreciated. 任何帮助将非常感激。

Thank you all in advance. 谢谢大家。

If you just add the scripts you need to the page in the correct order, you don't need to worry about that in webpack. 如果您只是按正确的顺序将所需的脚本添加到页面中,则无需在webpack中担心。

All you have to do is tell webpack that those modules are loaded from external references, like so: 您所要做的就是告诉webpack这些模块是从外部引用加载的,如下所示:

{
  externals: {
    'jquery': 'jQuery'
  } 
}

This tells webpack that every time you require('jquery') it will return a globally available variable jQuery. 这告诉webpack每次需要('jquery')它将返回一个全局可用的变量jQuery。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM