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

[tsgen] Make commonjs module output compatible with tsc. #22988

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

brendandahl
Copy link
Collaborator

This enables commonjs modules to be imported into TypeScript using the same syntax as ESM modules e.g.

import moduleFactory from './embind_tsgen.js';

This enables commonjs modules to be imported into TypeScript using the same
syntax as ESM modules e.g.

`import moduleFactory from './embind_tsgen.js';`
module.exports = %(EXPORT_NAME)s;
else if (typeof define === 'function' && define['amd'])
// This default export allows TS to import this commonjs style module.
module.exports.default = %(EXPORT_NAME)s;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we both of these though? It seems a little odd..

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, for the module to work in both node and TypeScript. There's a whole article on why it's hard to support both https://www.typescriptlang.org/docs/handbook/modules/appendices/esm-cjs-interop.html

Copy link
Collaborator

Choose a reason for hiding this comment

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

I wonder if its worth include that link here, or elaborating on the comment. e.g. "This might seem redundant but without this it is not possible to ...".

@@ -1,30 +1,36 @@
// Example TS program that consumes the emscripten-generated module to to
// illustrate how the type definitions are used and test they are workings as
// expected.
import moduleFactory from './embind_tsgen.mjs';
import moduleFactory from './embind_tsgen.js';
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we not be testing both of these options?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ESM and commonjs are still both tested, package.json determines if it's a one or the other.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I see.. so this .js file can be either one, depending on the test?

Perhaps add a comment to that effect here?

@@ -3385,21 +3385,24 @@ def test_jspi_add_function(self):
self.do_runf('other/test_jspi_add_function.c', 'done')

@parameterized({
'': [[]],
'with_jsgen': [['-sEMBIND_AOT']]
'commonjs': [['-sMODULARIZE'], ['--module', 'commonjs', '--moduleResolution', 'node']],
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

FYI: this roughly matches how emscripten output is used in g3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants