Webpack - $ 不是函数 [英] Webpack - $ is not a function

查看:42
本文介绍了Webpack - $ 不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将现有的 web 应用从 requirejs 迁移到 webpack.

I am moving an existing webapp from requirejs to webpack.

我对 jQuery 的导入/填充方式有疑问.

I have an issue with the way jQuery is imported/shimmed.

在我捆绑的 js 中,我从 bootstrap javascript 中收到 $ is not a function 错误.

In my bundled js, I am getting a $ is not a function error from within the bootstrap javascript.

当我在捆绑脚本中 console.log($) 时,它会显示一个空对象:object {}

When I console.log($) from within the bundled script it reveals an empty object: object {}

我假设这是因为 jQuery 没有导出任何内容,因为它传统上会将 $ 设置为 window 对象并完成.

I am assuming that this is because nothing is exported from jQuery as it would traditionally set $ to the window object and be done.

一些研究指出我使用 webpack 插件(请参阅下面的 webpack.config.js),但这似乎并没有解决问题.

Some research pointed me to use webpack plugins (see my webpack.config.js below) but this doesn't appear to solve the issue.

我的设置有什么问题吗?

Is there anything wrong in my setup?

谢谢

我的 webpack.config.js:

My webpack.config.js:

    var path = require('path');
var webpack = require('webpack');
module.exports = {
    //devtool: 'source-map',

entry: ['./mobile.js'],
resolve: {
    root: "./js/",
    alias: {

        jquery: 'libs/jquery-1.9.1',
        underscore: "libs/lodash-1.0.1",
        //backbone: 'libs/backbone-1.0.0',
        backbone: 'libs/backbone-0.9.10',
        bootstrap: "libs/bootstrap-2.3.1",
        ......
    }
},
resolveLoader: {
    root: path.join(__dirname, 'node_modules')
},
output: {
    path: './',
    filename: 'bundle.js'
},
plugins: [
    new webpack.ProvidePlugin({
        $: "jquery",
        jQuery: "jquery",
        "window.jQuery": "jquery"
    })
],
module : {
    loaders: [
        { test: /\.html$/, loader: "text" },
        { test: /\.json$/, loader: "json" }
    ]


   }
}

有问题的编译包代码:

/***/ function(module, exports, __webpack_require__) {

    /* WEBPACK VAR INJECTION */(function(__webpack_provided_window_dot_jQuery) {/* ===================================================
     * bootstrap-transition.js v2.3.1
     * http://twitter.github.com/bootstrap/javascript.html#transitions
     * ===================================================
     * Copyright 2012 Twitter, Inc.
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     * http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     * ========================================================== */


    !function ($) {

        console.log($); //THIS GIVES THE EMPTY OBJECT {}
      "use strict"; // jshint ;_;


      /* CSS TRANSITION SUPPORT (http://www.modernizr.com/)
       * ======================================================= */

      $(function () { //$ IS NOT A FUNCTION OCCURS HERE

        $.support.transition = (function () {
.........
.........

推荐答案

你也可以试试exports-loader:

npm install --save-dev exports-loader

和配置:

{
   include: require.resolve('libs/jquery-1.9.1'),
   loader: 'exports-loader?window.jQuery'
}

<小时>

OR 问题可能在于 ProviderPlugin 不读取 jquery 别名,所以尝试:


OR the problem can be in that ProviderPlugin doesn't read jquery alias, so try:

new webpack.ProvidePlugin({
    $: "libs/jquery-1.9.1",
    jQuery: "libs/jquery-1.9.1",
    "window.jQuery": "libs/jquery-1.9.1"
})

这篇关于Webpack - $ 不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆