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

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Apr 21, 2021
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
Add a catch to support requiring installed modules
  • Loading branch information
jclem committed Apr 21, 2021
commit c1c139b0abfbbdac4ee6ab5d5d83739bc2e56854
36 changes: 33 additions & 3 deletions 36 dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2427,7 +2427,7 @@ exports.request = request;
/***/ }),

/***/ 272:
/***/ (function(__unusedmodule, __webpack_exports__, __webpack_require__) {
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
__webpack_require__.r(__webpack_exports__);
Expand Down Expand Up @@ -2455,13 +2455,20 @@ function callAsyncFunction(args, source) {
var external_path_ = __webpack_require__(622);

// CONCATENATED MODULE: ./src/wrap-require.ts
/* module decorator */ module = __webpack_require__.hmd(module);

const wrapRequire = new Proxy(require, {
apply: (target, thisArg, [moduleID]) => {
if (moduleID.startsWith('.')) {
moduleID = Object(external_path_.join)(process.cwd(), moduleID);
return target.apply(thisArg, [moduleID]);
}
try {
return target.apply(thisArg, [moduleID]);
}
catch (err) {
return target.resolve(moduleID, { paths: [...module.paths, process.cwd()] });
}
return target.apply(thisArg, [moduleID]);
},
get: (target, prop, receiver) => {
Reflect.get(target, prop, receiver);
Expand Down Expand Up @@ -2494,7 +2501,7 @@ async function main() {
// Using property/value shorthand on `require` (e.g. `{require}`) causes compilation errors.
const result = await callAsyncFunction({
require: wrapRequire,
nativeRequire: require,
__original_require__: require,
github,
context: lib_github.context,
core: core,
Expand Down Expand Up @@ -8803,5 +8810,28 @@ function regExpEscape (s) {
/******/ };
/******/ }();
/******/
/******/ /* webpack/runtime/harmony module decorator */
/******/ !function() {
/******/ __webpack_require__.hmd = function(module) {
/******/ module = Object.create(module);
/******/ if (!module.children) module.children = [];
/******/ Object.defineProperty(module, 'loaded', {
/******/ enumerable: true,
/******/ get: function () { return module.l; }
/******/ });
/******/ Object.defineProperty(module, 'id', {
/******/ enumerable: true,
/******/ get: function () { return module.i; }
/******/ });
/******/ Object.defineProperty(module, 'exports', {
/******/ enumerable: true,
/******/ set: function () {
/******/ throw new Error('ES Modules may not assign module.exports or exports.*, Use ESM export syntax, instead: ' + module.id);
/******/ }
/******/ });
/******/ return module;
/******/ };
/******/ }();
/******/
/******/ }
);
8 changes: 7 additions & 1 deletion 8 src/wrap-require.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ export const wrapRequire = new Proxy(__non_webpack_require__, {
apply: (target, thisArg, [moduleID]) => {
if (moduleID.startsWith('.')) {
moduleID = path.join(process.cwd(), moduleID)
jclem marked this conversation as resolved.
Show resolved Hide resolved
return target.apply(thisArg, [moduleID])
}

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 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
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
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

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: [...module.paths, process.cwd()]})
}
return target.apply(thisArg, [moduleID])
},

get: (target, prop, receiver) => {
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.