[dev-tool] Implement esModuleInterop
for sample transpilation
#20391
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 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.