【问题标题】:How to use webpack with download jquery?如何使用 webpack 下载 jquery?
【发布时间】:2019-02-23 20:34:57
【问题描述】:

下载了jQueryjQuery_cookie 的缩小版。尝试对它们使用webpack,但出现错误:

./src/js/jquery_cookie.js 中的错误
找不到模块:错误:无法解析“C:\Users\MSI-PC\Documents\code_site\server2\src\js”中的“jquery”
@ ./src/js/jquery_cookie.js 11:2-29
@ ./src/js/index.js

index.js:

require('./jquery-3.2.1.min.js');
require('./jquery_cookie.js');

网站:

node_modules/
dist/
public/
src/
   css/
   ejs/
   js/
      index.js
      jquery-3.2.1.min.js
      jquery_cookie.js
server.js
package.js
webpack.config.js

webpack.config.js

var HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = { 
    entry: './src/js/index.js',    
    devtool: 'source-map',
    output: {publicPath: '/dist/'},  
    module: {
        rules: [{test: /\.ejs$/,
                 use: ['ejs-loader']}]
    },

    plugins: [
        new HtmlWebpackPlugin({
            template: './src/ejs/index.ejs',
            minify: {collapseWhitespace: true}
        })
    ]
}

已经用 ìmport jquery from './jquery-3.2.1.min.js' 测试过,但没有用。

还测试了将jquery_cookie插入到jQuery的根目录中,但这也不起作用。

最后尝试的是将此window.$ = window.jQuery = require("jquery"); 添加到我的index.js 中,但这也失败了。

编辑

编辑以添加新闻信息。测试安装 jquery npm install jquery 并要求他进入我的index.js。启动 webpack 时没有出现错误。但是我的其他 API,如 semantic-uijQuery-ui 和其他使用 jQuery 的文件 .js 不起作用。我的控制台返回错误:

ReferenceError: jQuery is not defined jquery-ui-1.12.1.min.js:14:3
ReferenceError: jQuery is not defined semantic.min.js:11:1
ReferenceError: $ is not defined client.js:96 

还测试了插入webpack.config.js 加载程序expose-loader 但也失败了:

test: require.resolve('jquery'),
                use: [{
                    loader: 'expose-loader',
                    options: 'jQuery'
                },{
                    loader: 'expose-loader',
                    options: '$'
                }]

带插件:

new webpack.ProvidePlugin({
            $: "jquery",  
            jQuery: "jquery" 
        })

【问题讨论】:

  • 请分享 webpack 配置。
  • 完成添加 webpack 配置
  • 你到底在用 jquery 做什么?通常,如果要将 jquery 导入文件,可以简单地运行 npm install 命令来获取 jquery。
  • 已经测试过了,但是在导出jQuery globaly 之后我会遇到问题。我不知道如何管理它。我使用expose-loader 进行了测试,但我的任何页面或 API 都没有找到 jQuery...

标签: javascript jquery webpack node-modules


【解决方案1】:

你不需要 webpack 来使用 jquery。只需使用 script 标记并指向您的库。

插入它的最佳位置是在 body 标记的末尾之前。

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

教程:https://www.w3schools.com/jquery/jquery_get_started.asp

阅读更多:https://jquery.com/download/

【讨论】:

  • 目前,这是我使用 jquery 的方式。但我想限制我的script 标签。那么,我需要做些什么来解决我的问题?
  • 如果是这样,请检查 Egor 答案。
【解决方案2】:

我相信问题出在这里:https://github.com/carhartl/jquery-cookie/blob/master/src/jquery.cookie.js#L14

jquery_cookie 正在尝试require('jquery'),但node_modules 中没有。你应该安装它:

npm i jquery

这个错误应该不会再出现了。

附:可能有一个解决方案使用webpackresolve.alias 或类似的东西,以便在请求require('jquery') 时使用您的文件,但我看不出有理由不做常规npm install 以支持本地下载jquery 文件。不值得麻烦。

【讨论】:

  • 感谢您的回答。不幸的是,这并没有奏效。我编辑了我的问题以改进它。
猜你喜欢
  • 2018-12-05
  • 2017-06-26
  • 2015-11-01
  • 1970-01-01
  • 2015-10-16
  • 2019-01-04
  • 2018-08-10
  • 2013-07-25
  • 2017-10-02
相关资源
最近更新 更多