parcel: Error when referencing images in javascript in parcel.
❔ Question
Why don’t images files referenced in javascript show up when using parcel?
🔦 Context
I am writing a shooter game that involves generating a plane sprite and the javascript to it is shown below
var plane = new Image()
plane.src = "static/assets/jplane.png"
ctx.drawImage(plane, 0, 0, 98, 98, 300, 300, planeScale*98, planeScale*98)
I referenced the javascript file in my index.html
<script src="./index.js"></script>
The problem is, the image displays when I open the index.html file through my browser, but it failed when I run parcel index.html
💻 Code Sample
🌍 Your Environment
Software | Version(s) |
---|---|
Parcel | 1.12.3 |
Node | v10.6.0 |
npm/Yarn | 1.16.0 |
Operating System | Mac OS |
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 2
- Comments: 16 (3 by maintainers)
Do something like this (you need to tell parcel to include that asset in the build output):
Maybe something like https://en.parceljs.org/module_resolution.html#glob-file-paths?
Since the image was in “…/static/assets” directory relative to the javascript file, I had to change the path you gave me to “…/static/assets/jplane.png”. After that, it worked. Thanks!
Not sure of it solves it, but do you have type=“module” in your script tag?
During bundling, Parcel has to determine that parameter of the require without executing the code.
Therefore,
image.src = require(myImage)
doesn’t work, because the value ofmyImage
isn’t known and even if that variable was looked up,speaker.image
isn’t known at bundle time.There’s a bunch of parcel plugins to handle copying static assets (which isn’t the same as being able to pull them through the optimization pipeline, but may solve some of the same uses cases where you don’t want to use modules).
However, although there were decent ones for v1 (see https://github.com/parcel-bundler/parcel/issues/3056#issuecomment-678915568), there’s like 5 different ones for v2 that are all super limited and don’t even support watching.
The best solution seems to be something like
If there’s any better options (esp being able to optimize images), I’m all ears.
Hello,
I need to use something like that but I don’t fully understand how it works so my attempts fail…
In short, I need to change the src of my image depending on the result of a function that returns the name of the image. When I use
import image from "/assets/note-8.png"
document.querySelector("#demo").src = image
it works. But it’s useless to my goal. I programmatically determine which image to use, so I would like toimport images from "/assets/*.png"
and then access a precise image with something likeimage["myFunction()"]
But that doesn’t work. Evenimage["note-8"]
doesn’t work. How can I access a precise image?Edit : turns out it’s just that my path was wrong in the import.
import image from "./assets/note-8.png"
works.The sentence you’re replying is asking for a way to do it without modules…
If you just need to reference some static files in your JavaScript, you can also use this Parcel plugin to copy them over to a static location:
https://github.com/elwin013/parcel-plugin-static-files-copy