🙋 feature request
Related: #7623, #8924, #7727
At the moment Parcel transforms import.meta.url (and the Node.js-specific cousins __dirname and __filename) with no user-land way to actually access the runtime values of these properties short of using eval to circumvent Parcel's AST walker. This has caused problems even within Parcel itself (#7948, #9172 (comment)), and Parcel uses a special __parcel__URL__ function to actually emit the runtime value.
Developers use import.meta.url and its cousins primarily for reflection purposes, i.e. they use it to change the behavior of their code based on the current state of the runtime. The main use cases I've seen are forking workers and accessing a location on disk relative to the runtime file-path. By setting these values at build time, Parcel removes what is in my opinion the main value of these properties; they essentially become useful for debugging and nothing else.
💁 Possible Solution
Just like how we have a "fake" module @parcel/service-worker to get the service worker manifest information at runtime, something like@parcel/reflection could exist where Parcel emits runtime reflection information.
import { importMeta } from '@parcel/reflection';
console.log(importMeta.url); // Actually useful now!
// Will be helpful if/once Parcel supports import.meta.resolve
console.log(importMeta.resolve('./runtime-thing-parcel-doesn't-know-about.js'));
🔦 Context
I'm trying to get a path relative to the location of the final JavaScript file in a Node.js environment. I could use any of __dirname, __filename, or import.meta.url to do this, but Parcel rewrites all of them. Although the value of something like new URL('other-asset', import.meta.url) is obvious, import.meta.url seems quite useless at the moment because Parcel has to rewrite it to a fake absolute path like file:///path/to/src.ts, which can't really be used for anything but logging.
Right now, I've resorted to doing the following:
import { fileURLToPath } from 'url';
const actualRelativePath = fileURLToPath(new __parcel__URL__('my/relative/path'));
I'd like to be able to do something less hacky.
🙋 feature request
Related: #7623, #8924, #7727
At the moment Parcel transforms
import.meta.url(and the Node.js-specific cousins__dirnameand__filename) with no user-land way to actually access the runtime values of these properties short of usingevalto circumvent Parcel's AST walker. This has caused problems even within Parcel itself (#7948, #9172 (comment)), and Parcel uses a special__parcel__URL__function to actually emit the runtime value.Developers use
import.meta.urland its cousins primarily for reflection purposes, i.e. they use it to change the behavior of their code based on the current state of the runtime. The main use cases I've seen are forking workers and accessing a location on disk relative to the runtime file-path. By setting these values at build time, Parcel removes what is in my opinion the main value of these properties; they essentially become useful for debugging and nothing else.💁 Possible Solution
Just like how we have a "fake" module
@parcel/service-workerto get the service worker manifest information at runtime, something like@parcel/reflectioncould exist where Parcel emits runtime reflection information.🔦 Context
I'm trying to get a path relative to the location of the final JavaScript file in a Node.js environment. I could use any of
__dirname,__filename, orimport.meta.urlto do this, but Parcel rewrites all of them. Although the value of something likenew URL('other-asset', import.meta.url)is obvious,import.meta.urlseems quite useless at the moment because Parcel has to rewrite it to a fake absolute path likefile:///path/to/src.ts, which can't really be used for anything but logging.Right now, I've resorted to doing the following:
I'd like to be able to do something less hacky.