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

Improve portability of the UMD build #328

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
"no-undef": ["off"],
"no-unused-vars": ["off"]
}
},
{
"files": "index.js",
"globals": {"globalThis": false}
}
]
}
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@
};

/* istanbul ignore else */
if (typeof module === 'object' && typeof module.exports === 'object') {
if (
typeof module === 'object' &&
module != null &&
typeof module.exports === 'object'
) {
module.exports = mapping;
} else {
self.FantasyLand = mapping;
globalThis.FantasyLand = mapping;

Choose a reason for hiding this comment

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

Wouldn't be safer to use something like (globalThis || self)? Just in case...

Copy link
Member

Choose a reason for hiding this comment

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

If globalThis does not exist in the environment, globalThis || self throws a ReferenceError. We would need to use typeof globalThis to first check for the presence of globalThis. Is this worthwhile? I'm not sure.

Choose a reason for hiding this comment

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

Yeah, you are right. I thought short-circuiting would not throw.

Copy link

@diasbruno diasbruno Nov 10, 2024

Choose a reason for hiding this comment

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

It'd be worth if it needs to be backward-compatible. But it's hard to find a reliable - and nice - way.

}

} ());
8 changes: 6 additions & 2 deletions scripts/generate-js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ cat >index.js <<EOF
};

/* istanbul ignore else */
if (typeof module === 'object' && typeof module.exports === 'object') {
if (
typeof module === 'object' &&
module != null &&
typeof module.exports === 'object'
) {
module.exports = mapping;
} else {
self.FantasyLand = mapping;
globalThis.FantasyLand = mapping;
}

} ());
Expand Down