-
Notifications
You must be signed in to change notification settings - Fork 68
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
accept require.resolve implementation as option to importLocation et al. #1144
Conversation
793d7b0
to
c323f1b
Compare
@@ -87,6 +88,8 @@ export function scaffold( | |||
|
|||
const application = await loadLocation(readPowers, fixture, { | |||
dev: true, | |||
requireResolve: (from, specifier, options) => | |||
createRequire(from).resolve(specifier, options), |
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.
I like how easy it is to provide the implementation.
A more secure implementation would use options.paths
to limit the search space and guard return values by checking with String.startsWith
that it's not reaching beyond the project folder.
A cache lookup of known needed paths for specifiers is also an option.
99d349c
to
71e7adb
Compare
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.
I would like to fold requireResolve into the optional properties of readPowers. Otherwise, this looks good to me.
0181651
to
ea65eef
Compare
}; | ||
if (readPowers && readPowers.requireResolve) { | ||
require.resolve = freeze((specifier, options) => | ||
readPowers.requireResolve(location, specifier, options), |
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.
Now all that's left to do is convince Typescript I can call this function if I check the field exists even thought it's optional on readPowers
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.
it stopped complaining when I did this
if (typeof readPowers === 'object' && readPowers.requireResolve) {
const { requireResolve } = readPowers;
require.resolve = freeze((specifier, options) =>
requireResolve(location, specifier, options),
);
I don't appreciate what TS is trying to do for me here
I'm not sure I have much context to review this PR. Is there anything specific you'd like me to take a look at? |
@mhofman Automatons said you had overlap with the codebase that this touches. All I'm looking for is a skim and a veto if I did something you consider harmful. |
@kriskowal This is ready IMHO, I need the final review. |
Automatons are mistaken, I'll remove myself as reviewer. |
{ | ||
readPowers, | ||
}, |
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 this envelope still necessary?
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.
While not necessary, it's likely to save some people a bunch of time I lost when adding more items to parse
arguments and wondering why they're not getting passed to the parseCjs function because it's wrapped along the way.
I'd like to keep it.
packages/compartment-mapper/src/parse-cjs-shared-export-wrapper.js
Outdated
Show resolved
Hide resolved
50abfba
to
a3e5d2e
Compare
a3e5d2e
to
d1ad921
Compare
Receiving the implementation externally makes more sense than supporting require.resolve functionality endoEndo.
This PR accepts the implementation and, if not provided, defaults to a similar error Node would throw.