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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

non-static import() becomes a regular require #8316

Open
mischnic opened this issue Jul 17, 2022 · 3 comments
Open

non-static import() becomes a regular require #8316

mischnic opened this issue Jul 17, 2022 · 3 comments

Comments

@mischnic
Copy link
Member

This no longer works:

	const url = 'https://cdn.skypack.dev/jszip@^3.10.0';
	const {default: JSZip} = await import(url);

because import is turned into a require

Screen Shot 5

I just found out that this is even documented: https://en.parceljs.org/javascript.html

Screen Shot 8

but it fails as is:

Screen Shot 7

Generated code:

Screen Shot 6

Originally posted by @fregante in #7064 (comment)

@mischnic mischnic changed the title @mischnic can this issue be reopened? That no longer works: non-static import() becomes a regular require Jul 17, 2022
@mischnic
Copy link
Member Author

I just found out that this is even documented: en.parceljs.org/javascript.html

(That are the docs for Parcel 1.x.)

@github-actions github-actions bot added Stale Inactive issues and removed Stale Inactive issues labels Jan 14, 2023
@nanoskript
Copy link

As an egregious workaround, the dynamic import can be wrapped inside an eval:

await eval(`import("https://cdn.jsdelivr.net/pyodide/v0.23.2/full/pyodide.js")`);

@broofa
Copy link

broofa commented Sep 14, 2024

Riffing on @nanoskript's workaround, the following provides a dynamicImport() that has the same signature as native import()... so maybe somewhat less "egregious"? 🤷

  const dynamicImport = new Function('a', 'b', 'return import(a, b);');

  // ... then use just like import():

  await dynamicImport("https://cdn.jsdelivr.net/pyodide/v0.23.2/full/pyodide.js")

(Note: new Function() has all the same code smells as eval(), so this is just putting a bit of lipstick on that code-pig.)

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

3 participants