babel / proposals

✍️ Tracking the status of Babel's implementation of TC39 proposals (may be out of date)
https://github.com/tc39/proposals
432 stars 39 forks source link

import.meta: (Stage 3) #10

Open babel-bot opened 7 years ago

babel-bot commented 7 years ago

Original issue submitted by @hzoo in https://github.com/babel/babel/issues/5832

Original issue submitted by @hzoo in https://github.com/babel/babylon/issues/539

import.meta for stage 2 (Domenic Denicola) (@domenic)

Info

Proposed at TC39 Meeting: May 2017 Spec Repo: https://github.com/tc39/proposal-import-meta Spec Text: https://tc39.github.io/proposal-import-meta/ Moved to Stage 3 at the Sept 2017 meeting

Example

(async () => {
  const response = await fetch(new URL("../hamsters.jpg", import.meta.url));
  const blob = await response.blob();

  const size = import.meta.scriptElement.dataset.size || 300;

  const image = new Image();
  image.src = URL.createObjectURL(blob);
  image.width = image.height = size;

  document.body.appendChild(image);
})();

Parsing

Transform

jkrems commented 7 years ago

Any preferences for what this should desugar to in a babel world? A possible default implementation would just create an empty Object.create(null) object per module. I assume that anything else would be making too many assumptions (outside of webpack doing its own thing).

ljharb commented 7 years ago

I believe ModuleRecords should be frozen as well as being null objects.

jkrems commented 7 years ago

The import.meta object is an arbitrary object associated with a module, not the module record itself. The only thing the spec requires is that it was created as an ordinary object with null-prototype. The host may choose to freeze it but it's not required.

jkrems commented 7 years ago

See also "Will this be locked down": https://github.com/tc39/proposal-import-meta#will-this-object-be-locked-down-in-any-way

jamiebuilds commented 7 years ago

Since it's left up to the implementation, my opinion is that Babel should freeze it until either the spec is finalized or we discover a reason it can't be for Babel's purposes. We'll see what other implementations want to do and what the committee finally accepts.

domenic commented 7 years ago

The spec is finalized; it won't be locked down.

justinfagnani commented 6 years ago

Regarding the transform:

Not sure, maybe webpack should handle? Or in node?

We're in need of a simple transform to replace import.meta with an object that has a url property based on the file's location on disk. When we have something I can ping this issue again to see if it'd be useful enough for other people to contribute.