【发布时间】:2023-03-16 22:55:02
【问题描述】:
我的 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() 函数时,我遇到了错误。
我正在关注 React By Example 一书。当我输入 window.jQuery 时,我在 jQuery 之后出现了一些奇怪的数字。但是当我只输入 jQuery 时,我会变得不确定。由于 bootstrap 4 和 $.ajax() 正在工作,我确信 jQuery 已包含在内,但不确定 jQuery 为何显示未定义。
谁能解释一下 jQuery 是如何被包含在内的以及我做错了什么。
【问题讨论】: