Skip to content
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

fix: handle importing custom helper nicely #3691

Merged
merged 2 commits into from
Jul 2, 2023
Merged
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
10 changes: 7 additions & 3 deletions lib/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,11 @@ function createHelpers(config) {
} else {
moduleName = `./helper/${helperName}`; // built-in helper
}
const HelperClass = require(moduleName);

// @ts-ignore
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we avoid of the // @ts-ignore by something like this:

const getModule = (moduleName) => {
  const module = require(moduleName);
  return module.default ? module : { default: module };
};

const isHelperModule = moduleName.startsWith('./helper/');

const HelperClass = isHelperModule
  ? require(moduleName)
  : getModule(moduleName).default;

?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Screenshot 2023-06-07 at 16 14 41
it doesn't resolves the issue unfortunately.

Copy link
Contributor

@EgorBodnar EgorBodnar Jun 7, 2023

Choose a reason for hiding this comment

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

Oh... I see. Hard situation.

A good time to think about switching to TypeScript and solve the typing issue the correct way 🙂

// this handles the loading of custom helper. Either old syntax `export = HelperName` or new syntax `export default HelperName`
const HelperClass = moduleName.startsWith('./helper/') ? require(moduleName) : require(moduleName).default || require(moduleName);
kobenguyent marked this conversation as resolved.
Show resolved Hide resolved

if (HelperClass._checkRequirements) {
const requirements = HelperClass._checkRequirements();
if (requirements) {
Expand Down Expand Up @@ -351,8 +355,8 @@ function loadSupportObject(modulePath, supportObjectName) {
}
}
if (typeof obj !== 'function'
&& Object.getPrototypeOf(obj) !== Object.prototype
&& !Array.isArray(obj)
&& Object.getPrototypeOf(obj) !== Object.prototype
&& !Array.isArray(obj)
) {
const methods = getObjectMethods(obj);
Object.keys(methods)
Expand Down