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 all commits
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
15 changes: 12 additions & 3 deletions lib/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,16 @@ 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 🙂

let HelperClass;
// check if the helper is the built-in, use the require() syntax.
if (moduleName.startsWith('./helper/')) {
HelperClass = require(moduleName); }
else {
// check if the new syntax export default HelperName is used and loads the Helper, otherwise loads the module that used old syntax export = HelperName.
HelperClass = require(moduleName).default || require(moduleName); }

if (HelperClass._checkRequirements) {
const requirements = HelperClass._checkRequirements();
if (requirements) {
Expand Down Expand Up @@ -351,8 +360,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