无法为 lib 导入插件
Can't import plugin for lib
我尝试将 'pixi-spine' 插件导入到我导入 PIXI 库的文件中。但我做不到。我使用 webpack 来打包文件。我尝试了另一种变体来做到这一点,但我有这个错误
import * as PIXI from 'pixi.js';
import {Spine} from 'pixi-spine';
import * as spineJSON1 from '../../../../../src/theme/assets/spine/ship_1_col_1.json';
export function newAnimations() {
const app = new PIXI.Application();
document.body.appendChild(app.view);
// load spine data
app.loader
.add('ship_1_col_1', spineJSON1)
// .add('ship_1_col_1', 'src/theme/assets/spine/ship_1_col_1.json')
.load(onAssetsLoaded);
app.stage.interactive = true;
app.stage.buttonMode = true;
function onAssetsLoaded(loader, res) {
const ship_1_col_1 = new PIXI.spine.Spine(res.ship_1_col_1.spineData);
// set current skin
ship_1_col_1.skeleton.setSkinByName('ship_1_col_1');
ship_1_col_1.skeleton.setSlotsToSetupPose();
// set the position
ship_1_col_1.x = 400;
ship_1_col_1.y = 600;
ship_1_col_1.scale.set(1.5);
// play animation
ship_1_col_1.state.setAnimation(0, 'idle', true);
app.stage.addChild(ship_1_col_1);
app.stage.on('pointertap', () => {
// change current skin
const currentSkinName = goblin.skeleton.skin.name;
const newSkinName = (currentSkinName === 'goblin' ? 'goblingirl' : 'goblin');
ship_1_col_1.skeleton.setSkinByName(newSkinName);
ship_1_col_1.skeleton.setSlotsToSetupPose();
});
}
}
但是我有错误来自 'pixi-spine' - PIXI 未定义
从5版本开始,PixiJS不再在全局命名空间中提供PIXI
[With] Webpack and 3rd-party plugins, like
pixi-spine, you might have difficulties building the global PIXI
object resulting in a runtime error ReferenceError: PIXI is not
defined
. Usually this can be resolved by using Webpack shimming
globals.
For instance, here's your import code:
import * as PIXI from 'pixi.js';
import 'pixi-spine'; // or other plugins that need global 'PIXI' to be defined first
Add a plugins
section to your webpack.config.js to let know
Webpack that the global PIXI
variable make reference to pixi.js
module. For instance:
const webpack = require('webpack');
module.exports = {
entry: '...',
output: {
...
},
plugins: [
new webpack.ProvidePlugin({
PIXI: 'pixi.js'
}) ] }
我尝试将 'pixi-spine' 插件导入到我导入 PIXI 库的文件中。但我做不到。我使用 webpack 来打包文件。我尝试了另一种变体来做到这一点,但我有这个错误
import * as PIXI from 'pixi.js';
import {Spine} from 'pixi-spine';
import * as spineJSON1 from '../../../../../src/theme/assets/spine/ship_1_col_1.json';
export function newAnimations() {
const app = new PIXI.Application();
document.body.appendChild(app.view);
// load spine data
app.loader
.add('ship_1_col_1', spineJSON1)
// .add('ship_1_col_1', 'src/theme/assets/spine/ship_1_col_1.json')
.load(onAssetsLoaded);
app.stage.interactive = true;
app.stage.buttonMode = true;
function onAssetsLoaded(loader, res) {
const ship_1_col_1 = new PIXI.spine.Spine(res.ship_1_col_1.spineData);
// set current skin
ship_1_col_1.skeleton.setSkinByName('ship_1_col_1');
ship_1_col_1.skeleton.setSlotsToSetupPose();
// set the position
ship_1_col_1.x = 400;
ship_1_col_1.y = 600;
ship_1_col_1.scale.set(1.5);
// play animation
ship_1_col_1.state.setAnimation(0, 'idle', true);
app.stage.addChild(ship_1_col_1);
app.stage.on('pointertap', () => {
// change current skin
const currentSkinName = goblin.skeleton.skin.name;
const newSkinName = (currentSkinName === 'goblin' ? 'goblingirl' : 'goblin');
ship_1_col_1.skeleton.setSkinByName(newSkinName);
ship_1_col_1.skeleton.setSlotsToSetupPose();
});
}
}
但是我有错误来自 'pixi-spine' - PIXI 未定义
从5版本开始,PixiJS不再在全局命名空间中提供PIXI
[With] Webpack and 3rd-party plugins, like pixi-spine, you might have difficulties building the global
PIXI
object resulting in a runtime errorReferenceError: PIXI is not defined
. Usually this can be resolved by using Webpack shimming globals.For instance, here's your import code:
import * as PIXI from 'pixi.js'; import 'pixi-spine'; // or other plugins that need global 'PIXI' to be defined first
Add a
plugins
section to your webpack.config.js to let know Webpack that the globalPIXI
variable make reference topixi.js
module. For instance:const webpack = require('webpack'); module.exports = { entry: '...', output: { ... }, plugins: [ new webpack.ProvidePlugin({ PIXI: 'pixi.js' }) ] }