-
-
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
feat(@parcel/transformer-js): Rewrite __dirname
/__filename
to be relative to asset.filePath
#7727
Conversation
|
__dirname
to be relative to projectRoot
__dirname
/__filename
to be relative to projectRoot
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good overall!
IMO relativeContext
doesn't really convey that this is about __dirname
and __filename
🤷♂️
There's a similar existing test here:
it('should insert global variables when needed', async function () { |
So duplicate that and add a package.json with that new config option, then you can delete the example you've committed.
Regarding the path
import: this is similar to how an import for buffer
is added for the Buffer
global, that is done here:
"Buffer" => { |
Ok, so I pushed some more changes to the Rust code and I have two problems now:
Let me explain. When we have a source file at Here is the relevant line: https://github.com/LekoArts/parcel/blob/eca555e83e8ba3ac7ad0eded8dd9c0723a21713e/packages/transformers/js/core/src/global_replacer.rs#L144
How can I get the I'm not seeing my Rust code changes in the tests. I think I've added the test and the new option correctly, but when I print out the |
Let me know @mischnic if my above questions/comments make sense and you can help me? Appreciate it a lot! Thanks |
The transformer doesn't know the final path of the bundle that the asset will be put into. This is only known after bundling/packaging (https://parceljs.org/plugin-system/overview/), so you have to do the I don't know why the changes wouldn't show up in the tests (apart from forgetting to recompile the Rust code), so I guess it has to be that your change doesn't modify the behaviour somehow. |
Ok, thanks for that information. I'll look into the tests, I probably made an error somewhere. I'm trying to wrap my head around how to go about this then. Do I need to keep the changes I made to the transformer so far and add some "identifier" so that later the packager can modify/replace things, or do I completely remove the Rust logic and make all my changes in the packager? From my understand I'd need both, so that in the transformer I actually replace the call and then the packager can change the import path? |
Exactly. The logic to determine whether Then in the packager, one possibility would be replacing that placeholder literally or (though somewhat sketchy) do the same as
|
Ok, I was sick 🤧 for a bit, I'll start working on this again now |
Ok, so I got it working in my example (currently still committed in this PR) in how I expect it: But I'll remove the example and add tests. The API and naming etc. is pretty gross and I'm also still using same placeholder everywhere, not per asset. So it works, but still lot to do. |
Thanks! Will look into it and add a test |
Good catch @mischnic 👍 The issue was that I've used pathdiff::diff_paths for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me!
Awesome 🎉 I'll let you re-run the CI, otherwise with my commit trigger I'd invalidate your review |
), | ||
); | ||
wrapped = wrapped.replace('$parcel$dirnameReplace', relPath); | ||
wrapped = wrapped.replace('$parcel$filenameReplace', relPath); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason we have two different variables that get replaced with the same path? Were these meant to be different, or do we only need one of them?
|
||
*self.has_node_replacements = true; | ||
} | ||
"__dirname" => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some of this could probably be combined with the above but ok for now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good overall! Thanks again!
I will optimize this (i.e. address the two new review comments) in a follow-up PR -- I'd like to get this into a nightly release so that we can try it out at Gatsby :) |
I think you'll need to re-run a bunch of times for the flaky tests 😅 |
With #7931 merged I guess I need to change some |
Sorry, I forgot that merging it would break this PR 😬 |
Looks like it compiles, the only breaking change from the swc update was related to |
Awesome. I'll do that in the follow-up PR. Thanks for merging and help on the PR <3 |
The nightly release that would include this failed here: https://github.com/parcel-bundler/parcel/actions/runs/2151865409 |
↪️ Pull Request
This adds a way of accomplishing #7611 - this functionality will be enabled by default in node environment.
Fixes #7611
💻 Examples
See linked issue, but TL;DR is:
Some code in e.g.
<root>/index.ts
has a${__dirname}/data
string in it.When it'll be compiled to e.g.
<root>/dist
the path should be changed to:🚨 Test instructions
I've added a test to the integration tests, it has to have the node env (so e.g.
engines: node
key).✔️ PR Todo