Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Javascript "new URL('...', import.meta.url)" syntax not working #7633

Open
jeb5 opened this issue Feb 1, 2022 · 7 comments
Open

Javascript "new URL('...', import.meta.url)" syntax not working #7633

jeb5 opened this issue Feb 1, 2022 · 7 comments

Comments

@jeb5
Copy link

jeb5 commented Feb 1, 2022

馃悰 Bug report

new URL('foo.jpg', import.meta.url) compiles without error, but throws a ERR_INVALID_URL error when run.

馃帥 Configuration (.babelrc, package.json, cli command)

No .babelrc, no .parceljs. package.json is at default npm init -y state.

I'm just running parcel build index.js

馃 Expected Behavior

new URL('foo.jpg', import.meta.url) compiles down to new URL("foo.d7483e92.jpg")

馃槸 Current Behavior

internal/url.js:279
  throw new ERR_INVALID_URL(input);
  ^

TypeError [ERR_INVALID_URL]: Invalid URL: /foo.d7483e92.jpg
    at onParseError (internal/url.js:279:9)
    at new URL (internal/url.js:355:5)
    at /****/*****/****/****/****/dist/index.js:1:1420
    at Object.<anonymous> (/****/*****/****/****/****/dist/index.js:1:1433)
    at Module._compile (internal/modules/cjs/loader.js:1072:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
    at Module.load (internal/modules/cjs/loader.js:937:32)
    at Function.Module._load (internal/modules/cjs/loader.js:778:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47 {
  input: '/foo.d7483e92.jpg',
  code: 'ERR_INVALID_URL'
}

馃拋 Possible Solution

???

馃敠 Context

I'm trying to use svg images in my web app, which are transformed into png images by a custom transformer plugin. I'm using query parameters. new URL('...', import.meta.url) seems like the best way to do this.

馃捇 Code Sample

| package.json
| index.js
| foo.jpg
// index.js
console.log(new URL("foo.jpg", import.meta.url));

馃實 Your Environment

Software Version(s)
Parcel 2.2.1
Node v14.17.6
npm 8.3.2
Operating System macOS 12.1
@yergom
Copy link

yergom commented Feb 1, 2022

I'm trying to do a similar thing to import an svg within a js file:

const info = new URL('info.svg', import.meta.url);

but at runtime I get

Uncaught TypeError: URL constructor: [object Object] is not a valid URL.

where the generated code is:

const info = new URL(require("10550ed8cba4ef4f"));

This error, together with #7587, makes it impossible? to embed svgs in react components

@afogel
Copy link

afogel commented Feb 2, 2022

I don't think that's necessarily a bug, yet -- did you try ./info.svg or ./foo.png as the URL? A similar syntax does work for me when working with images.

However, I wonder if you still have issues like I have in #7643 ?

@yergom
Copy link

yergom commented Feb 3, 2022

I've also tried to use the url directly in the jsx element, but there is no transformation performed in the resulting bundle (so the image cannot be found). I have the same issue as you have in #7643.

@cmkweber
Copy link

I'd also like to note that it seems Parcel removes the hash (#) and search (?) from the url, which can be problematic for .svg's that need to reference an id within the svg:

const info = new URL('info.svg#Icon', import.meta.url);

@afogel
Copy link

afogel commented Nov 11, 2022

this is still an open issue, would love to see movement/resolution. Thanks

@github-actions github-actions bot removed the Stale Inactive issues label Nov 11, 2022
@github-actions github-actions bot added the Stale Inactive issues label May 10, 2023
@parcel-bundler parcel-bundler deleted a comment from github-actions bot May 10, 2023
@mischnic mischnic removed the Stale Inactive issues label May 10, 2023
@github-actions github-actions bot added the Stale Inactive issues label Nov 7, 2023
@afogel
Copy link

afogel commented Nov 7, 2023

Still no updates, it seems.

@github-actions github-actions bot removed the Stale Inactive issues label Nov 7, 2023
@github-actions github-actions bot added the Stale Inactive issues label May 5, 2024
@parcel-bundler parcel-bundler deleted a comment from github-actions bot May 9, 2024
@mischnic mischnic removed the Stale Inactive issues label May 9, 2024
@mischnic
Copy link
Member

mischnic commented May 9, 2024

Can someone provide a complete code sample](https://minimum-reproduction.wtf/)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants