简体   繁体   English

如何用包裹加载多张图片?

[英]How to load multiple images with parcel?

I have a pixi.js app that depends on multiple images which are inside a folder and i want to use parcel bundler.我有一个 pixi.js 应用程序,它依赖于文件夹内的多个图像,我想使用包裹捆绑器。 I can import them one by one ( import logo from "./color/btc.png" works as expected) but when i tried to import folder, parcel give this error.我可以一个一个地导入它们( import logo from "./color/btc.png"按预期工作)但是当我尝试导入文件夹时,包裹给出了这个错误。


  /Users/user1/Desktop/pixi001/src/app.js:2:20
    1 | import { Application, Container, Sprite } from "pixi.js";
  > 2 | import logos from "./color/*.png";
  >   |                    ^^^^^^^^^^^^^^^
    3 | const app = new Application({
    4 |     width: window.innerWidth,

@parcel/resolver-default: Cannot load file './color/*.png' in './src'.
💡 Did you mean './color/d.png'?
💡 Did you mean './color/r.png'?

Is there anyway to achieve this with parcel bundler?有没有办法用包裹捆绑器来实现这个目标?

This is the rest of the code.这是代码的rest。

import { Application, Container, Sprite } from "pixi.js";
import logos from "./color/*.png";
const app = new Application({
    width: window.innerWidth,
    height: window.innerHeight,
    resizeTo: window,
    backgroundColor: 0xffffff,
    resolution: 1,
    backgroundAlpha: 0
});
document.body.appendChild(app.view);
const container = new Container();
app.stage.addChild(container);
logos.forEach(logo => {
    const sprite = new Sprite.from(logo);
    sprite.x = Math.random() * innerWidth;
    sprite.y = Math.random() * innerHeight;
    container.addChild(sprite);
});

You can get this working by using Parcel's glob specifiers feature.您可以使用 Parcel 的glob 说明符功能来实现这一点。 Here's what you need to do:这是您需要做的:

  1. Add @parcel/resolver-glob to the "resolvers" pipeline of your .parcelrc file.@parcel/resolver-glob添加到.parcelrc文件的"resolvers"管道中。 If you don't have a .parcelrc file yet, create one at the root of your project that looks like this:如果您还没有.parcelrc文件,请在项目的根目录下创建一个,如下所示:
     { "extends": "@parcel/config-default", "resolvers": ["@parcel/resolver-glob", "..."] }
    If you restart parcel, it should automatically install the @parcel/resolver-glob package into your project, but in case that fails, you can always add it manually with yarn add / npm install /etc.如果您重新启动 parcel,它应该会自动将@parcel/resolver-glob package 安装到您的项目中,但如果失败,您始终可以使用yarn add / npm install /etc 手动添加它。
  2. Import references to the files in your folder using this syntax:使用以下语法导入对文件夹中文件的引用:
     import * as logos from "./colors/*.png";
    If the colors folder contains two files a.png and b.png , then the logos variable at runtime will be an object that looks like this:如果colors文件夹包含两个文件a.pngb.png ,那么运行时的logos变量将是一个 object ,如下所示:
     { a: "string url to a.png", b: "string url to b.png" }
  3. Iterate over the values of the logos object, and do whatever you want with the image URLs, eg:遍历logos object 的值,并对图像 URL 做任何你想做的事情,例如:
     Object.values(logos).forEach((logoUrl) => { const sprite = new Sprite.from(logoUrl); //... });

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

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