-
Notifications
You must be signed in to change notification settings - Fork 3.4k
[tsgen] Make commonjs module output compatible with tsc. #22988
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,30 +1,39 @@ | ||
| // 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'; | ||
|
|
||
| const module = await moduleFactory(); | ||
| // The imported file will either be an ES module or a CommonJS module depending | ||
| // on the test. | ||
| import moduleFactory from './embind_tsgen.js'; | ||
|
|
||
| // Test a few variations of passing value_objects with strings. | ||
| module.setValObj({ | ||
| bar: module.Bar.valueOne, | ||
| string: "ABCD", | ||
| callback: () => {} | ||
| }); | ||
| // Async IIFE is required for TSC with commonjs modules. This is not needed for | ||
| // ESM output since top level await can be used. | ||
| (async function() { | ||
|
|
||
| module.setValObj({ | ||
| bar: module.Bar.valueOne, | ||
| string: new Int8Array([65, 66, 67, 68]), | ||
| callback: () => {} | ||
| }); | ||
| const module = await moduleFactory(); | ||
|
|
||
| const valObj = module.getValObj(); | ||
| // TODO: remove the cast below when better definitions are generated for value | ||
| // objects. | ||
| const valString : string = valObj.string as string; | ||
| // Test a few variations of passing value_objects with strings. | ||
| module.setValObj({ | ||
| bar: module.Bar.valueOne, | ||
| string: "ABCD", | ||
| callback: () => {} | ||
| }); | ||
|
|
||
| // Ensure nonnull pointers do no need a cast or nullptr check to use. | ||
| const obj = module.getNonnullPointer(); | ||
| obj.delete(); | ||
| module.setValObj({ | ||
| bar: module.Bar.valueOne, | ||
| string: new Int8Array([65, 66, 67, 68]), | ||
| callback: () => {} | ||
| }); | ||
|
|
||
| console.log('ts ran'); | ||
| const valObj = module.getValObj(); | ||
| // TODO: remove the cast below when better definitions are generated for value | ||
| // objects. | ||
| const valString : string = valObj.string as string; | ||
|
|
||
| // Ensure nonnull pointers do no need a cast or nullptr check to use. | ||
| const obj = module.getNonnullPointer(); | ||
| obj.delete(); | ||
|
|
||
| console.log('ts ran'); | ||
|
|
||
| })(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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']], | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI: this roughly matches how emscripten output is used in g3. |
||
| 'esm': [['-sEXPORT_ES6'], ['--module', 'NodeNext', '--moduleResolution', 'nodenext']], | ||
| 'esm_with_jsgen': [['-sEXPORT_ES6', '-sEMBIND_AOT'], ['--module', 'NodeNext', '--moduleResolution', 'nodenext']] | ||
| }) | ||
| def test_embind_tsgen(self, opts): | ||
| def test_embind_tsgen_end_to_end(self, opts, tsc_opts): | ||
| # Check that TypeScript generation works and that the program is runs as | ||
| # expected. | ||
| self.emcc(test_file('other/embind_tsgen.cpp'), | ||
| ['-o', 'embind_tsgen.mjs', '-lembind', '--emit-tsd', 'embind_tsgen.d.ts'] + opts) | ||
| ['-o', 'embind_tsgen.js', '-lembind', '--emit-tsd', 'embind_tsgen.d.ts'] + opts) | ||
|
|
||
| # Test that the output compiles with a TS file that uses the defintions. | ||
| shutil.copyfile(test_file('other/embind_tsgen_main.ts'), 'main.ts') | ||
| # A package file with type=module is needed to enabled ES modules in TSC and | ||
| # also run the output JS file as a module in node. | ||
| shutil.copyfile(test_file('other/embind_tsgen_package.json'), 'package.json') | ||
| cmd = shared.get_npm_cmd('tsc') + ['embind_tsgen.d.ts', 'main.ts', '--module', 'NodeNext', '--moduleResolution', 'nodenext'] | ||
| if '-sEXPORT_ES6' in opts: | ||
| # A package file with type=module is needed to enabled ES modules in TSC and | ||
| # also run the output JS file as a module in node. | ||
| shutil.copyfile(test_file('other/embind_tsgen_package.json'), 'package.json') | ||
|
|
||
| cmd = shared.get_npm_cmd('tsc') + ['embind_tsgen.d.ts', 'main.ts', '--target', 'es2021'] + tsc_opts | ||
| shared.check_call(cmd) | ||
| actual = read_file('embind_tsgen.d.ts') | ||
| self.assertFileContents(test_file('other/embind_tsgen_module.d.ts'), actual) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.