简体   繁体   English

如何将 Pixi.js 导入到 TypeScript 项目中?

[英]How do I import Pixi.js into a TypeScript project?

I'm new to Pixi.js but I have some past experience with TypeScript. I'm really struggling to import Pixi.js into my project.我是 Pixi.js 的新手,但我过去有一些使用 TypeScript 的经验。我真的很难将 Pixi.js 导入我的项目。

I got a small Pixi.js project running using a CDN import of Pixi and vanilla JavaScript, and now I'm trying to get that same project running on TypeScript. I think one of my options would be to use the CDN import of Pixi and then import the type definitions for Pixi, but I read in several places that the later versions of Pixi are already written in TypeScript, so I don't think it's a good option for me to use a JavaScript version of the library and then import my own TypeScript definitions.我使用 Pixi 和 vanilla JavaScript 的 CDN 导入运行了一个小的 Pixi.js 项目,现在我正试图让同一个项目在 TypeScript 上运行。我认为我的选择之一是使用 Pixi 的 CDN 导入和然后导入 Pixi 的类型定义,但我在几个地方读到 Pixi 的更高版本已经写在 TypeScript 中,所以我认为使用 JavaScript 版本的库然后导入我的库不是一个好的选择拥有 TypeScript 定义。

I tried using npm install pixi.js and then import * as PIXI from "pixi.js";我尝试使用npm install pixi.js ,然后import * as PIXI from "pixi.js"; but that gives me this TypeScript error: This module is declared with using 'export =', and can only be used with a default import when using the 'allowSyntheticDefaultImports' flag.但这给了我这个 TypeScript 错误: This module is declared with using 'export =', and can only be used with a default import when using the 'allowSyntheticDefaultImports' flag. and it also gives me this browser error when I force it to compile anyway: Uncaught TypeError: Failed to resolve module specifier "pixi.js". Relative references must start with either "/", "./", or "../".当我无论如何强制它编译时,它也会给我这个浏览器错误:未捕获的类型错误Uncaught TypeError: Failed to resolve module specifier "pixi.js". Relative references must start with either "/", "./", or "../". Uncaught TypeError: Failed to resolve module specifier "pixi.js". Relative references must start with either "/", "./", or "../". This error makes sense to me, because clearly telling the browser to find pixi.js will result in no file being found, since it's a Node.js module.这个错误对我来说很有意义,因为明确告诉浏览器查找pixi.js将导致找不到文件,因为它是一个 Node.js 模块。

I tried changing import * as PIXI from "pixi.js";我尝试import * as PIXI from "pixi.js"; to import PIXI from "pixi.js"; import PIXI from "pixi.js"; to get rid of the "only use default import" error, but that just gives me a "pixi.js has no default export" error, which seems to be in direct contrast with the error I was getting before, saying that it only has a default export...摆脱“仅使用默认导入”错误,但这只是给我一个“pixi.js 没有默认导出”错误,这似乎与我之前得到的错误形成鲜明对比,说它只有默认导出...

And even if I manage to get rid of the TypeScript errors, I still can't wrap my head around how this would function properly anyway since the browser has no idea what "pixi.js" is when it's referring to a Node module that doesn't even exist inside the browser...即使我设法摆脱了 TypeScript 错误,我仍然无法理解 function 是如何正确的,因为浏览器不知道“pixi.js”是什么,因为它指的是一个 Node 模块,它不甚至存在于浏览器中......

So all of this leads me to my question of how do I get a Pixi.js program running with TypeScript?所以所有这些都让我想到了如何让 Pixi.js 程序与 TypeScript 一起运行的问题? I've looked up tutorials many times but every single one includes something like Webpack, Browserify, etc. I would not like to use any bundlers/other dependencies at all, I simply want to write TypeScript code, compile it, and have it give me some.js files that I can pop directly into a.html file and run in my browser.我已经多次查找教程,但每个教程都包含 Webpack、Browserify 等内容。我根本不想使用任何捆绑器/其他依赖项,我只想编写 TypeScript 代码,编译它,然后让它给出我可以将一些.js 文件直接弹出到 a.html 文件中并在我的浏览器中运行。 Is this possible?这可能吗?

My findings thus far have been that what I'm looking for is (somehow) not possible.到目前为止,我的发现是我正在寻找的东西(不知何故)是不可能的。 I've found that my options are either to import the vanilla JavaScript version of Pixi and just go without type information (and do some hacky workarounds to get TypeScript to not think PIXI is undefined), or use a bundler like Webpack. Neither of those are ideal, and I have to think there's another option...我发现我的选择是要么导入 Pixi 的香草 JavaScript 版本,要么导入没有类型信息的 go(并做一些 hacky 变通办法让PIXI不认为 PIXI 是未定义的),或者使用像 Webpack 这样的捆绑器。这些都不是是理想的,我不得不认为还有另一种选择......

It would depend on your setup, but you could try something like this:这取决于您的设置,但您可以尝试以下操作:

import { Sprite } from '@pixi/sprite';
new Sprite()

or you could try importing all as PIXI like this或者您可以尝试像这样将所有内容都导入为 PIXI

import * as PIXI from 'pixi.js';
new PIXI.Sprite()

You must have figured this out but still I want this answer to be here for other to see.您一定已经弄清楚了,但我仍然希望这个答案在这里供其他人查看。

This is my demo project on github that you can clone and use.这是我在 github 上的演示项目,您可以克隆和使用它。 It has typescript and pixi.js installed.它安装了 typescript 和 pixi.js。 THIS PROJECT USES VITE instead of webpack which is very complicated.本项目使用 VITE 而不是 webpack,后者非常复杂。 pxts:pixi.js setup library pxts:pixi.js 安装库

Some of things you must keep in mind您必须牢记的一些事情

  • Pixi.js version 6 has typescript types bundled along. Pixi.js 版本 6 捆绑了 typescript 种类型。
  • Most of the examples on line are old and out of date大多数在线示例都是旧的和过时的
  • Latest pixi version is v 7.x for which there is no community support yet.最新的 pixi 版本是 v 7.x,尚无社区支持。
  • link for version 6.5.8 docs版本 6.5.8 文档的链接
  • While working with version 6.x you have to install Assets separately where as in Version 7.x its bundled in.在使用 6.x 版时,您必须单独安装资产,而在 7.x 版中它是捆绑在一起的。
  • OLD AND OUT OF DATE TUTORIALS ONLINE is the main reason for confusion.旧的和过时的在线教程是造成混淆的主要原因。 Do check which version you have got installed.请检查您安装的是哪个版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM