-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[dev-tool] Implement esModuleInterop
for sample transpilation
#20391
[dev-tool] Implement esModuleInterop
for sample transpilation
#20391
Conversation
esModuleInterop
for sample transpilation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀
I'm not 100% sure how to go about testing the runtime-module testing, but this doesn't cause any new errors in the generation for form recognizer or text analytics. It does generate the correct require calls for purview and eventhub, even for the |
dev-Sentinel-2022-09-01-preview (Azure#20391) * Adds base for updating Microsoft.SecurityInsights from version preview/2022-08-01-preview to version 2022-09-01-preview * Updates readme * Updates API version in new specs and examples * Add eventGroupingSettings to NRT alert rules (Azure#20422) * Update AutomationRules.json (Azure#20257) * Update AutomationRules.json * fix typo * prettier * fix linter errors * Update example file to show the 2 new condition types Co-authored-by: matanpa <[email protected]> Co-authored-by: ityankel <[email protected]>
This PR implements some improvements to the way that the dev-tool sample transpiler handles default imports (
import x from "x";
). Currently, we simply don't allow these imports unless the module specifier refers to a node built-in module (in which case we treat it as if it were arequire
call), or if the module specifier is a relative path (in which case we use thedefault
property of the module object).This PR changes the behavior to emulate
esModuleInterop
at compile-time. When considering a default import, we will simplyrequire
the module from the host package and check for an__esModule
property. If one exists, then we will transform the default import into aconst <name> = require("<moduleSpecifier>").default
expression. If one does not exist, then we will omit the.default
and treat the import as if it were a simple require call. This mimics the actual runtime behavior of__importDefault
generated byesModuleInterop
:In addition to this change, we also explicitly consider all modules in any
@azure
namespace to be ESMs without testing them. This supports RLC samples (CC @joheredi).Incidentally, this also fixes an issue in which ambient
.d.ts
files insamples-dev
would crash the sample transpiler, and it deletes thets-to-js
command (because it had fallen out of sync with the sample transpiler and is not in use).CC @deyaaeldeen and @maorleger , as this PR addresses some issues you have seen.
Packages impacted by this PR
All of them, especially
@azure/dev-tool
.Issues associated with this PR
Closes #18877
Closes #18878
Closes #19594
Describe the problem that is addressed by this PR
Several packages depend on modules that aren't supported without proper default-import and
esModuleInterop
functionality:export default
as part of their programming confention.This PR supports those use cases.
What are the possible designs available to address the problem? If there are more than one possible design, why was the one in this PR chosen?
I had four options:
export =
or anexport default
and pick the correct runtime representation for it.export =
or anexport default
.I discarded solutions 1 and4. 1 is too expensive. 4 introduces too much complexity to the end-configuration of each package (the goal of the dev-tool sample transpiler is to make it simpler for package owners to maintain their samples).
Between 2 and 3, 3 was simpler, but I ultimately decided to implement 2 because it is a relatively simple, zero-configuration solution.
Are there test cases added in this PR? (If not, why?)
Yes. Additional file inputs and expectations have been added to test imports of
@azure
packages. I am currently investigating some options to properly test the main feature of testing the module shape.