gpt4 book ai didi

jQuery 与 Webpack 2

转载 作者:行者123 更新时间:2023-12-01 00:17:38 25 4
gpt4 key购买 nike

我的 webpack.config.js 如下所示:

var debug = process.env.NODE_ENV !== "production";
var webpack = require('webpack');
var path = require('path');
var node_dir = __dirname + '/node_modules';
const autoprefixer = require('autoprefixer');
const ProvidePlugin = require('webpack/lib/ProvidePlugin');

module.exports = {
context: path.join(__dirname, "src"),
devtool: debug ? "inline-sourcemap" : null,
entry: ['tether',
'font-awesome-loader',"bootstrap-loader","./js/client.js"],
resolve: {
extensions: ['.js', '.styl'],
alias: {
'jquery': node_dir + '/jquery/src/jquery.js',
}
},
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel-loader',
query: {
presets: ['react', 'es2015', 'stage-0'],
plugins: ['react-html-attrs', 'transform-decorators-legacy', 'transform-class-properties'],
}
},
{ test: /\.css$/, use: ['style-loader', 'css-loader', 'postcss-loader'] },
{ test: /\.scss$/, use: ['style-loader', 'css-loader', 'postcss-loader', 'sass-loader'] },
{
test: /\.woff2?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
use: 'url-loader?limit=10000',
},
{
test: /\.(ttf|eot|svg)(\?[\s\S]+)?$/,
use: 'file-loader',
},
{
test: /\.styl$/,
use: [
'style-loader',
'css-loader',
{
loader: 'stylus-loader',
/*options: {
use: [stylus_plugin()],
},*/
},
],
},
// Use one of these to serve jQuery for Bootstrap scripts:

// Bootstrap 4
{ test: /bootstrap\/dist\//, use: 'imports-loader?jQuery=jquery' },
]
},
output: {
path: __dirname + "/dist/",
filename: "client.min.js"
},
plugins: [
//new webpack.optimize.DedupePlugin(),
//new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.UglifyJsPlugin({ mangle: false, sourcemap: false }),
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
"window.jQuery": "jquery",
Tether: "tether",
"window.Tether": "tether",
Alert: "exports-loader?Alert!bootstrap/js/dist/alert",
Button: "exports-loader?Button!bootstrap/js/dist/button",
Carousel: "exports-loader?Carousel!bootstrap/js/dist/carousel",
Collapse: "exports-loader?Collapse!bootstrap/js/dist/collapse",
Dropdown: "exports-loader?Dropdown!bootstrap/js/dist/dropdown",
Modal: "exports-loader?Modal!bootstrap/js/dist/modal",
Popover: "exports-loader?Popover!bootstrap/js/dist/popover",
Scrollspy: "exports-loader?Scrollspy!bootstrap/js/dist/scrollspy",
Tab: "exports-loader?Tab!bootstrap/js/dist/tab",
Tooltip: "exports-loader?Tooltip!bootstrap/js/dist/tooltip",
Util: "exports-loader?Util!bootstrap/js/dist/util",
}),
new webpack.LoaderOptionsPlugin({
postcss: [autoprefixer],
})
],
};
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>React, Babel, Webpack 2</title>
</head>
<body class="container">
<div id="app"></div>
<script src="jquery.timeago.js"></script>
<script src="client.min.js"></script>

</body>
</html>

但是当我尝试使用 jQuery.timeago() 函数时,我遇到了以下错误。

enter image description here

我正在阅读《React By Examples》一书。当我输入 window.jQuery 时,我在 jQuery 之后出现一些奇怪的数字。但是当我只输入 jQuery 时,我就变得未定义了。由于 bootstrap 4 和 $.ajax() 正在工作,我确信 jQuery 已包含在内,但不确定为什么 jQuery 显示未定义。

任何人都可以解释一下如何包含 jQuery 以及我做错了什么。

最佳答案

将插件添加到 webpack 配置的插件部分,如下所示:

var webpack = require('webpack')
...

plugins: [
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
})
]

编辑:

您有循环依赖:

timeago: jquery
client: timeago jquery

但是jqueryclient是同一个包。尝试在 html 中切换包含 js 包,如下所示:

<script src="client.min.js"></script>
<script src="jquery.timeago.js"></script>

而不是:

<script src="jquery.timeago.js"></script>
<script src="client.min.js"></script>

如果这没有帮助或者您遇到其他错误,您需要将 jquery 提取到单独的包中并在 timeago 之前包含它。另一种方法是像老派一样包含 jquery 例如来自 cdn 的内容,并使 webpack 知道这一点,如下所示:

externals: {
jquery: 'jQuery'
}

要将 jquery 提取到单独的包中并使其对 timeago 可见,请使用如下所示的包含序列:

<script src="common.js"></script>
<script src="jquery.timeago.js"></script>
<script src="client.min.js"></script>

并使用 webpack 通用 block 插件,如下所示:

entry: {
common: ["jquery"],
...
},
plugins: [
new webpack.optimize.CommonsChunkPlugin({
name: "common",
filename: "common.js",
minChunks: Infinity,
})
],

关于jQuery 与 Webpack 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44084544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com