Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Call resolve, then require
  • Loading branch information
jclem committed Apr 21, 2021
commit 7e12bd73952c9992711142f5dec2f787345e8c5a
10 changes: 7 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2903,9 +2903,13 @@ const wrapRequire = new Proxy(require, {
return target.apply(thisArg, [moduleID]);
}
catch (err) {
return target.resolve(moduleID, {
paths: eval('module').paths.concat(process.cwd())
});
const modulePath = target.resolve.apply(thisArg, [
moduleID,
{
paths: eval('module').paths.concat(process.cwd())
}
]);
return target.apply(thisArg, [modulePath]);
}
},
get: (target, prop, receiver) => {
Expand Down
11 changes: 8 additions & 3 deletions src/wrap-require.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ export const wrapRequire = new Proxy(__non_webpack_require__, {
try {
return target.apply(thisArg, [moduleID])
Copy link
Copy Markdown

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 the try vs. catch block is backwards.

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

Copy link
Copy Markdown
Contributor Author

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:

const modulePath = 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]
  }
])

return target.apply(thisArg, [modulePath])

Copy link
Copy Markdown
Contributor Author

@jclem jclem Apr 21, 2021

Choose a reason for hiding this comment

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

Fixing in #136

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks!

} catch (err) {
return target.resolve(moduleID, {
paths: eval('module').paths.concat(process.cwd())
})
const modulePath = target.resolve.apply(thisArg, [
moduleID,
{
paths: eval('module').paths.concat(process.cwd())
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Question: Have you tried adding node_modules to the path here? I think 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
Copy Markdown
Contributor Author

@jclem jclem Apr 21, 2021

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 did not result in the ability to require('foo') and have it resolve to ./foo.js. Surprisingly, module.paths.push(process.cwd()) did have this effect, but not this method.

}
])

return target.apply(thisArg, [modulePath])
}
},

Expand Down