[dev-tool] Implement esModuleInterop for sample transpilation#20391
Merged
witemple-msft merged 3 commits intoAzure:mainfrom Feb 17, 2022
Merged
[dev-tool] Implement esModuleInterop for sample transpilation#20391witemple-msft merged 3 commits intoAzure:mainfrom
esModuleInterop for sample transpilation#20391witemple-msft merged 3 commits intoAzure:mainfrom
Conversation
esModuleInterop for sample transpilation
joheredi
approved these changes
Feb 15, 2022
Member
Author
|
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 |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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 arequirecall), or if the module specifier is a relative path (in which case we use thedefaultproperty of the module object).This PR changes the behavior to emulate
esModuleInteropat compile-time. When considering a default import, we will simplyrequirethe module from the host package and check for an__esModuleproperty. If one exists, then we will transform the default import into aconst <name> = require("<moduleSpecifier>").defaultexpression. If one does not exist, then we will omit the.defaultand treat the import as if it were a simple require call. This mimics the actual runtime behavior of__importDefaultgenerated byesModuleInterop:In addition to this change, we also explicitly consider all modules in any
@azurenamespace to be ESMs without testing them. This supports RLC samples (CC @joheredi).Incidentally, this also fixes an issue in which ambient
.d.tsfiles insamples-devwould crash the sample transpiler, and it deletes thets-to-jscommand (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
esModuleInteropfunctionality:export defaultas 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 defaultand 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
@azurepackages. I am currently investigating some options to properly test the main feature of testing the module shape.