Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Support installed npm modules and relative require#135

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
jclem merged 25 commits intomainfromwrap-require
Apr 21, 2021
Merged

Conversation

jclem
Copy link
Contributor

@jclemjclem commentedApr 21, 2021
edited
Loading

This adds support for the following:

  • Requiring modules by a path relative to the working directoryrequire('./foo')
  • Requiring npm modules installed in the working directoryrequire('lodash')

This is accomplished by wrapping therequire passed to the script in a proxy.

  • When the script callsrequire with a path that starts with'.', we transform that module ID to the result ofpath.join(process.cwd(), moduleID) and then require the absolute path, instead.
  • When a script callsrequire for some non-relative path and an error is thrown, we catch that error and try again, this time addingprocess.cwd() to the list of paths searched-through.

Thanks to@joshmgross and@wraithgar for doing the real work here 😄

kittiphophum18 reacted with thumbs up emojiJamesMGreene and joshmgross reacted with hooray emoji
@github-actions
Copy link

github-actionsbot commentedApr 21, 2021
edited
Loading

Hello from actions/github-script! (3110e8d)

@jclemjclem marked this pull request as ready for reviewApril 21, 2021 20:38
@jclemjclem requested a review froma teamApril 21, 2021 20:38
@jclemjclem changed the titleWrap require to support relative requiresSupport installed npm modules and relative requireApr 21, 2021
@jclemjclem merged commit95fb649 intomainApr 21, 2021
@jclemjclem deleted the wrap-require branchApril 21, 2021 21:50
{
// Webpack does not have an escape hatch for getting the actual
// module, other than `eval`.
paths: eval('module').paths.concat(process.cwd())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Question: Have you tried addingnode_modules to the path here? Ithink that should work, while also preventing accidental resolutions to local modules, i.e.require('hi') =>require(process.cwd() + '/hi.js')? 🤔

Suggested change
paths:eval('module').paths.concat(process.cwd())
paths:eval('module').paths.concat(path.resolve(process.cwd(),'node_modules'))

Copy link
ContributorAuthor

@jclemjclemApr 21, 2021
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I may have mis-tested, but when I tested this, using this method didnot result in the ability torequire('foo') and have it resolve to./foo.js. Surprisingly,module.paths.push(process.cwd()) did have this effect, but not this method.

JamesMGreene reacted with heart emoji
}

try {
return target.apply(thisArg, [moduleID])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Concern: I feel like the order here of thetry vs.catch block is backwards.

When using arequire('lodash') from mygithub-script block now, that may end up requiring an incompatible version of the module if it exists as a dependency somewhere "near" to where thegithub-script code is executed rather than relying on the CWD'spackage.json file. 😬

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Ah this is a good point. Instead, we should perhaps remove this entire try/catch construct and just do this:

constmodulePath=target.resolve.apply(thisArg,[moduleID,{// Webpack does not have an escape hatch for getting the actual// module, other than `eval`.paths:[process.cwd(), ...eval('module').paths]}])returntarget.apply(thisArg,[modulePath])

JamesMGreene reacted with heart emoji
Copy link
ContributorAuthor

@jclemjclemApr 21, 2021
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Fixing in#136

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Thanks!

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@JamesMGreeneJamesMGreeneJamesMGreene left review comments

@joshmgrossjoshmgrossjoshmgross approved these changes

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@jclem@JamesMGreene@joshmgross

[8]ページ先頭

©2009-2025 Movatter.jp