Closed tommmyy closed 4 years ago
Currently you'd have to use url:./logo.png
but we should probably fallback to it by default.
we should probably fallback to it by default
This was a deliberate change, see https://github.com/parcel-bundler/parcel/pull/4055:
... Most importantly, it made it impossible for us to ever add support for a new file format without technically causing a breaking change. For example, if we wanted to add an SVG transformer that returned JSX, we could not without breaking existing apps that relied on it returning a URL. ...
@mischnic but in build mode, it will return an exception when including the png:
TypeError: Cannot read property 'getBundleURL' of undefined
at Object.2f227246e2f7deca2cb087b1905a229e (widget-hero.05c09092.js:1)
at n.parcelRequire.n.parcelRequire (app-demo.aee4f364.js:1)
at app-demo.aee4f364.js:1
Using the following fixes the problem.
--no-scope-hoisting
error related to https://github.com/parcel-bundler/parcel/issues/4199
Currently you'd have to use
url: ./logo.png
but we should probably fallback to it by default.
So, for anyone wondering, that is meant to be the import path:
import logo from 'url: ~/src/logo.png';
LE: Nevermind, this doesn't work. How should we import images so they work with Parcel v2?
LE2: Removing the space between url:
and ~/src/
fixed the problem.
First of all, no space between url:
and your path. Unless there is another issue I don't know about.
First of all, no space between
url:
and your path. Unless there is another issue I don't know about.
Removing the space fixed the issue, thank you. I really had to search and read through a lot of GitHub issues before I found out how to load images in v2.
A bit offtopic: So far v2 looks to be very, very buggy. The cache is weird (sometimes I have to delete the cache folder for it to be able to build again), occasionally it throws random errors during build and I do have to change parts of my code to switch from v1 to v2, which is weird for a zero-configuration bundler.
Even now, I run parcel, it says build is fine, I save one file without making any changes, it says build failed and now throws the error which also existed on the initial "successful" build.
Is there a way with config to make url:
not required? I can see the stability benefits of not being too magical here, but in my case I want my library's examples to be build-able very simply using parcel, but also support embedding within a larger web-pack (dojo) project. Presumably there is a way to explicitly match transformers for images in the config, but I'm struggling with small amount of v2 docs
Is there a way with config to make url: not required
Yes .parcelrc:
{
"extends": "@parcel/config-default",
"transformers": {
"*": ["@parcel/transformer-raw"]
}
}
(The only reason for the explicit protocol is that supporting another file type natively would be a breaking change because doing import ... from "./xyz.foo
wouldn't return an url anymore).
Thankyou! I think requiring being explicit does make sense for something as open ended as file types.
Am now using .parcelrc
to get what I want:
{
"extends": "@parcel/config-default",
"transformers": {
"*.png": ["@parcel/transformer-raw"]
}
}
Closing this since it has turned into an migration Q&A.
Please open a new issue with a reproduction if TypeError: Cannot read property 'getBundleURL' of undefined
is still happening.
How to import multiple images?
I was able to do it like this in Parcel v1:
import * as sprites from './assets/*.png';
🐛 bug report
I am not able to load image in JSX file.
🎛 Configuration (.babelrc, package.json, cli command)
🤔 Expected Behavior
Should load an image url.
😯 Current Behavior
🔦 Context
Parcel is used in monorepo.
💻 Code Sample
🌍 Your Environment