-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Support synchronous import.meta.resolve(β¦)
#8924
Comments
For a set of test cases, see: git clone https://github.com/lgarron/loadeverything.net && cd loadeverything.net
git checkout --detach d5dfd056f81c8c0c020f2962741fd325ed4c67ea
make test-parcel |
So it should behave like the existing |
Yeah, it's fairly similar. For relative paths (starting with
|
Feels like there are two possible meanings for
The first doesn't seem particularly useful given that The second seems a bit more useful, and more in line with the intent of the function. It would only return what the resolution is, without affecting code splitting, and matches its CJS counterpart Seems like Webpack has the same questions: webpack/webpack#16693. Perhaps it would be good to get alignment between bundlers on this before implementing it one way or the other. |
Hmm, I'd really love to see any approach that allows To me, the value of For example, I measured that a page like https://experiments.cubing.net/cubing.js/mark3/ needs three times the code if module splitting does not work across worker and non-worker code, which is a significant impact on end users, especially on cell networks or other slow connections. If I'm publishing a such an app myself, I can choose a bundler with settings that allows this to work. But it's very important to me that this also works out of the box for anyone using my underlying library in their own app. I'd like to be able to recommend Parcel as a zero-config bundler, and this functionality is table-stakes because there is no portable alternative syntax.
This was a concern in #7623 , which has stalled. Given that
I believe by far the best option is for bundlers to change the resolution and be involved in code splitting, so that the runtime resolution still points to the appropriate bundled entry file/resource. As far as bundling is concerned, it's essentially like a dynamic import (that happens not to actually load the import directly).
Thanks, I'll try to advocate for the same over there. Library authors like me will not be able to publish performant & portable ESM worker and WASM code unless bundlers support this consistently. |
If my reasoning is not persuasive, could I ask if you can think of an alternative way to publish code like the following to // Web worker
const workerPath = await import.meta.resolve("./client/worker.js");
const worker = new Worker(workerPath, { type: "module" });
worker.postMessage("hi");
// WASM
const wasmPath = await import.meta.resolve("./code.wasm");
const wasmModule = WebAssembly.compileStreaming(fetch(wasmPath), /* imports */);
console.log(wasmModule.exports.foo()); Note that:
Footnotes
|
We could have it work like
I think it's clear what this should do in an unbundled environment, but unclear what bundlers should do with it. Once you start having multiple modules in the same file, it gets murky what a "URL to a module" means. |
I really wish it did, but unfortunately it very much does not. π My impression is that bundlers have some reservations about supporting this syntax, particularly when the argument is not a relative path. I think In any case, is there anything you consider a cross-compatible approach for WASM?
Hmm, I think theres a misunderstanding here?
Suppose you turn every |
Yep that's what I meant. Say you have an input graph like this: // index.js
import {value} from './value';
const asyncValue = (await import('./async')).value;
value === asyncValue
// async.js
export {value} from './value';
// value.js
export const value = {}; In this case, the value and asyncValue will be equal because
async.bundle.js can assume that index.bundle.js is already loaded when it loads, and therefore the value.js module will already be loaded. This means value.js doesn't need to be split out into its own bundle. If async.js were instead a worker, it could not access an existing module instance for value.js, so then you'd have to bundle like this:
or like this:
My point was simply that bundling |
I'm curious about these tests. For example, I'm pretty sure |
Hmm, I don't entirely follow what this means. But in any case, I agree that it would be a surprise if
I'm not entirely sure, as I now seem to have issues with the Parcel build loading any code at all? git clone https://github.com/lgarron/loadeverything.net && cd loadeverything.net
git checkout --detach d5dfd056f81c8c0c020f2962741fd325ed4c67ea
make test-parcel This runs It results in an empty console. Not even "Pause on caught exceptions" catches anything, and I can't figure out why. π³ |
import.meta.resolve(β¦)
import.meta.resolve(β¦)
@devongovett, would you support a PR to implement this functionality? It would be really valuable for Parcel to support this, so I'd be willing to spend quite some time on it. |
@devongovett: I looked around the Parcel codebase for about an hour last night to see if I could put together a PR, but I couldn't figure out how to mirror the |
π feature request
Support
import.meta.resolve(β¦)
.π€ Expected Behavior
Here is a code snippet with a set of examples that work in all modern browsers as of today. The argument to
import.meta.resolve(β¦)
in each case is a file in the source graph:In this case, it's valuable to:
.js
and.wasm
, the two web-native languages.).js
(or.ts
,.css
, potentially more depending on plug-ins) argument, treat it as an entry point and bundle as appropriate. For other formats, output the file in the app build, with the argumentimport.meta.resolve(β¦)
appropriately re-written.π― Current Behavior
import.meta.resolve(β¦)
is not recognized and handled.π Possible Solution
I know Parcel uses
swc
, but it's not clear to me how much of the appropriate source graph functionality is implemented inswc
or Parcel. I've also filed an issue at swc-project/swc#7155π¦ Context
This functionality is critical for library authors (like me) who want to use web workers and WASM to ensure that end users have a good experience. As a library author, I want to ensure my code works in Parcel, but I don't have direct control over the Parcel configuration that apps use. So it's important to have something that works out of the box, and
import.meta.url(β¦)
is an unambiguous way to specify relative resources.This would also help resolve #7623 , in that it moves away from per-bundler hacks to a single pattern that is unambiguous and well supported in browsers.
π» Examples
See above.
The text was updated successfully, but these errors were encountered: