-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Import with type text, bytes, and URL #9444
Comments
Re: the URL import, using a
For a module, not using the mutable URL representation would seem particularly important, I’d think? |
A string representation of the URL would entirely satisfy the motivating use cases. |
Looking at https://fetch.spec.whatwg.org/#body-mixin I wonder if we want I'm not sure I understand how Do we need to solve @domenic's #7017 about feature detection at the same time? There's also #4321 from @Jamesernator and #7706 from @7ombie. These all look like duplicates, but I'm fine with keeping them open until we have some kind of plan. One thing that's raised in the latter that's important here is what to do about MIME types. Would we not check response MIME types for these, similar to Fetch? Or would we try to enforce something? cc @whatwg/modules |
Good question. The semantics would actually be the same. One piece of motivation is that this form is more "declarative"-looking and therefore statically analyzable (which should mostly help build tools, given that not enough information is available for a prefetcher to use this). See more information about motivation (for a previous iteration of this idea) at https://github.com/tc39/proposal-asset-references . Also note that some people in TC39 are considering whether we should propose some other syntax for this, besides using import attributes. |
Yes, this would provide a statically analyzable alternate route to the same url value, analogous to static vs dynamic import. This is less interesting for the web than it is interesting because it establishes a convention that build tooling would benefit from. For example, a bundler that takes a whole web application directory tree and generates a new tree, the bundler would be able to discern the dependency and rewrite the URL. For a bundler that takes a whole web application tree and generates a single JavaScript file, it would have the option of embedding the underlying data URL. That’s to say, any static syntax that reveals the url of an asset in a way that implies a dependency needs to be arranged by a bundler is an improvement on the status quo. This is one of the options we are considering. As @annevk mentions in chat, this approach has the disadvantage of introducing a code path under the host import hook that bypasses a fetch. For this reason, the alternative approach is to introduce another import phase, as we do with The implication for Module virtualization is that an [added:] The implication for Module virtualization if we pursue |
The suggestions there had quite a different flavour given at the time JSON modules were proposed to be derived based on MIME type, rather than the current approach that uses import attributes (which MIME type must agree with). This new style with import attributes is strictly more useful as one can interpret essentially anything as an array buffer/text regardless of it's actual MIME type. e.g. In my previous suggestion, text would only be successfully imported if it were As such that old issue can be closed in strong favour of this one.
For urls there's obviously nothing to do as no fetching is involved. For array buffers, checking MIME types is undesirable as people might be loading any content for some processing (e.g. images, audio, application specific formats, are all reasonable reasons to import array buffers). For text checking the type/essence is similar to array buffers, any MIME type (not just Alternatively for text, we could have a separate attribute that indicates what format to decode as (potentially useful if the server doesn't know what charset files are using). import someText from "./file.txt" with { type: "text", encoding: "utf16" };
import someText from "./oldData.dat" with { type: "text", encoding: "latin2" };
// Would default to utf8 naturally so these would be equivalent
import someText2 from "./file2.ini" with { type: "text", encoding: "utf8" };
import someText2 from "./file2.ini" with { type: "text" }; |
In Bun v1.1.5, we are adding bundler & runtime support for |
Kindly consider TC 39 Stage 1 immutable ArrayBuffer for |
Sorry if this is a dumb question, but why do we care about the MIME type for raw bytes and UTF-8? I thought that was a security concern that stemmed from the fact that browsers parse the result. If we just get the bytes or characters we asked for (like a static fetch), I'm not sure why it needs to be any tighter than that. All a static analyzer would see is a filepath that ends on some extension (say |
I find it reasonable to enable or even encourage |
@kriskowal - That makes perfect sense. There's a benefit in being able to opt into extra checks and balances, but no reason to require them. |
Something that probably should be done with import imageUrl from "./image.png" with { type: "url", preloadAs: "image" };
import workerUrl from "./worker.js" with { type: "url", preloadmoduleAs: "worker" };
// Or with source phase imports: https://github.com/tc39/proposal-esm-phase-imports
import source workerSrc from "./worker.js" with { preloadmoduleAs: "worker" }; |
I’m working with a group of TC39 delegates on what we call Module Harmony, an effort to make proposals pertaining to the module system coherent. I am consequently looking for the right venue to propose and establish a precedent for a host integration with modules, specifically to address the portability of code that uses the module system to express a dependency upon plain text, bytes, or references to assets. Concretely, I would like to propose that:
Such that:
So that a module can express these kinds of dependency in a way that is portable. Specifically, I aim for a program to be run on the server side and the client side of a web application, both raw and thru an optimizing translation (e.g., bundling). With import attributes, ECMA 262 is already sufficiently expressive to allow a host integration to address this problem without additional features, and would be coherent with future 262 proposals, particularly virtual module sources.
The text was updated successfully, but these errors were encountered: