未捕获引用错误:未在webpack JS捆绑包中定义函数
我最近开始使用Webpack来管理我的包,在从我编译的JS包中调用函数时遇到了一些问题 我的目标是在网站中使用pixi.js和fullpage.js,通过CDN可以做得很好,但我一直在使用Webpack来学习和跟上时代,等等 调用由Webpack编译的外部库的相关函数时收到错误 package.json未捕获引用错误:未在webpack JS捆绑包中定义函数,webpack,package,webpack-4,fullpage.js,pixi.js,Webpack,Package,Webpack 4,Fullpage.js,Pixi.js,我最近开始使用Webpack来管理我的包,在从我编译的JS包中调用函数时遇到了一些问题 我的目标是在网站中使用pixi.js和fullpage.js,通过CDN可以做得很好,但我一直在使用Webpack来学习和跟上时代,等等 调用由Webpack编译的外部库的相关函数时收到错误 package.json "name": "aspect", "version": "1.0.0", "description": "Aspect Store landing page",
"name": "aspect",
"version": "1.0.0",
"description": "Aspect Store landing page",
"main": "index.js",
"license": "MIT",
"devDependencies": {
"webpack": "^4.41.5",
"webpack-cli": "^3.3.10",
"@babel/core": "^7.7.7",
"@babel/preset-env": "^7.7.7",
"autoprefixer": "^9.7.3",
"babel-loader": "^8.0.6",
"postcss": "^7.0.26",
"postcss-cli": "^6.1.3",
"postcss-loader": "^3.0.0"
},
"dependencies": {
"jquery": "^3.4.1",
"fullpage.js": "^3.0.8",
"pixi.js": "^5.2.0",
"tailwindcss": "^1.1.4"
},
"scripts": {
"dev": "postcss css/tailwind.css -o public/build/tailwind.css webpack --mode development",
"build": "webpack --mode production"
}
}
webpack.config.js
var webpack = require('webpack');
var path = require('path');
module.exports = {
plugins: [
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery'
})
],
module: {
rules: [{
test: /\.css$/,
//exclude: /node_modules/,
use: [{
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
},
{
loader: 'style-loader',
},
{
loader: 'css-loader',
options: {
importLoaders: 1,
}
},
{
loader: 'postcss-loader'
}
],
}]
}
}
window.jQuery = $;
window.$ = $;
import fullpage from 'fullpage.js';
import * as PIXI from 'pixi.js';
index.js
var webpack = require('webpack');
var path = require('path');
module.exports = {
plugins: [
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery'
})
],
module: {
rules: [{
test: /\.css$/,
//exclude: /node_modules/,
use: [{
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
},
{
loader: 'style-loader',
},
{
loader: 'css-loader',
options: {
importLoaders: 1,
}
},
{
loader: 'postcss-loader'
}
],
}]
}
}
window.jQuery = $;
window.$ = $;
import fullpage from 'fullpage.js';
import * as PIXI from 'pixi.js';
index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Aspect</title>
<link rel="stylesheet" type="text/css" href="public/build/tailwind.css">
<script src="../dist/main.js"></script>
<!--<script src="https://cdnjs.cloudflare.com/ajax/libs/pixi.js/5.1.3/pixi.min.js"></script>-->
</head>
<body>
<main id="aspect">
<div class="section text-indigo-200">
<div class="object-center" id="aspectContainer"></div>
</div>
<div class="section text-indigo-200">Some section</div>
</main>
<script type="text/javascript">
const app = new PIXI.Application({
width: window.innerWidth, height: window.innerHeight
});
document.getElementById('aspectContainer').appendChild(app.view);
let bol = false;
// create a texture from an image path
const texture = PIXI.Texture.from('images/aspect_logo.png');
// create a new Sprite using the texture
const logo = new PIXI.Sprite(texture);
// center the sprites anchor point
logo.anchor.set(0.5);
// move the sprite to the center of the screen
logo.x = app.screen.width / 2;
logo.y = app.screen.height / 2;
logo.blendMode = PIXI.BLEND_MODES.ADD;
app.stage.addChild(logo);
</script>
<script>
$(document).ready(function() {
new fullpage("#aspect", {
verticalCentered: true,
scrollOverflow: true
});
fullpage_api.setAllowScrolling(true);
});
</script>
</body>
</html>
面貌
某段
const app=新的PIXI.Application({
宽度:window.innerWidth,高度:window.innerHeight
});
document.getElementById('aspectContainer').appendChild(app.view);
设bol=false;
//从图像路径创建纹理
const texture=PIXI.texture.from('images/aspect_logo.png');
//使用纹理创建新的精灵
const logo=新的PIXI.Sprite(纹理);
//将精灵定位点居中
logo.anchor.set(0.5);
//将精灵移动到屏幕中央
logo.x=app.screen.width/2;
logo.y=app.screen.height/2;
logo.blendMode=PIXI.BLEND_MODES.ADD;
app.stage.addChild(徽标);
$(文档).ready(函数(){
新的完整页面(“方面”{
垂直中心:对,
scrollOverflow:真
});
整页api.setAllowScrolling(真);
});
您需要向页面公开该函数。按照此线程上的答案进行操作:从您的屏幕截图中,我看到在出现另一个错误之前:“PIXI未定义”。似乎您正在index.js中导入PIXI
和fullpage
,但您没有将它们暴露在html页面的上下文中。例如,请打开chrome devtools,并在控制台中检查是否定义了PIXI等。感谢您的回答,我们对“在html页面中将它们暴露在上下文中”做了一些深入的研究,并找到了这个答案,它正好做到了这一点!()