【发布时间】:2021-08-08 06:11:09
【问题描述】:
我已经被这个问题困扰了好几天了,我已经从搜索中尝试了几十种不同的修复方法。首先,我使用 Hugo、Webpack、Node.js 和 Babel。目前,至少一切都在编译,但是当我查看 localhost 控制台时,我看到了这些错误:
Uncaught ReferenceError: jQuery is not defined
at Object.609 (bundle.js:2)
at n (bundle.js:2)
at Object.83 (bundle.js:2)
at n (bundle.js:2)
at Module.150 (bundle.js:2)
at n (bundle.js:2)
at bundle.js:2
at bundle.js:2
609 @ bundle.js:2
n @ bundle.js:2
83 @ bundle.js:2
n @ bundle.js:2
150 @ bundle.js:2
n @ bundle.js:2
(anonymous) @ bundle.js:2
(anonymous) @ bundle.js:2
localhost/:1 Refused to apply style from 'http://localhost:1313/scss/node_modules/bootstrap-icons/font/bootstrap-icons.css' because its MIME type ('text/plain') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
localhost/:1 Refused to apply style from 'http://localhost:1313/node_modules/animate.css/source/animate.css' because its MIME type ('text/plain') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
localhost/:1 Refused to apply style from 'http://localhost:1313/home/elijah/GolandProjects/ReplayRLB/website/testsite1/assets/plugins/magnific-popup/magnific-popup.css' because its MIME type ('text/plain') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
现在我写这篇文章的主要理由是解决 jQuery 问题,但深入了解其他问题也很棒,因为我也被困了一段时间。
webpack.config.js
const path = require('path');
const webpack = require('webpack');
// const WebpackManifestPlugin = require('webpack-manifest-plugin');
module.exports = {
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: ['babel-loader'],
}
]
},
entry: './assets/js/script.js',
output: {
path: path.resolve(__dirname, 'assets', 'js'),
filename: 'bundle.js',
},
externals: {
jquery: 'jQuery',
},
/* plugins: [
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery'
})
]*/
};
我从 script.js(又名主 js 文件)导入:
app.use("/static", express.static('./static/'));
import $ from "../plugins/jquery/jquery.min.js";
import jQuery from "../plugins/jquery/jquery.min.js";
import "../plugins/@popperjs/core/lib/popper.js";
import "../plugins/google-map/map.js";
import "../plugins/magnific-popup/jquery.magnific-popup.min";
import Shuffle from "../plugins/shuffle/shuffle.min.js";
import "../plugins/slick/slick.min.js";
import lozad from "lozad";
//Pricing Scripts
import "../js/pricing/boosting.js";
import "../js/pricing/tourneyTitle.js";
import "../js/pricing/placements.js";
import "../js/pricing/coaching.js";
/* ========================================================================= */
/* jQuery load initialize
/* ========================================================================= */
window.$ = require('../plugins/jquery/jquery.min');
window.jQuery = $;
require('./script.js');
/* ========================================================================= */
/* shuffle load initialize
/* ========================================================================= */
const shuffleInstance = new Shuffle(document.getElementById('grid'), {
itemSelector: '.js-item',
sizer: '.js-shuffle-sizer',
});
/* ========================================================================= */
/* lazy load initialize
/* ========================================================================= */
const observer = lozad(); // lazy loads elements with default selector as ".lozad"
observer.observe();
为我的主要 js 源调用 bundle
<script src="./assets/js/bundle.js"></script>
请注意,这不在静态/资产中,因为当我尝试它不起作用并给我一个错误说您无法将静态文件发送到客户端或网络时(类似这些行)。我的 assets dir 相当于我认为的 src 目录。
【问题讨论】:
标签: javascript jquery node.js webpack hugo