fix(plugin-metadata): emit .js extensions for dynamic imports under nodenext (#3364)#3404
Open
maruthang wants to merge 1 commit intonestjs:masterfrom
Open
fix(plugin-metadata): emit .js extensions for dynamic imports under nodenext (#3364)#3404maruthang wants to merge 1 commit intonestjs:masterfrom
maruthang wants to merge 1 commit intonestjs:masterfrom
Conversation
…odenext resolution (nestjs#3364) Under `moduleResolution: 'node16'` or `'nodenext'`, ESM-style imports require explicit file extensions. The PluginMetadataGenerator previously emitted dynamic `import("./hello.dto")` calls without an extension, causing TypeScript diagnostics in the generated metadata file and `ERR_MODULE_NOT_FOUND` at runtime when the metadata is executed. Detect the consuming project's `moduleResolution` from the supplied `ts.Program` and, when it is Node16 or NodeNext, walk both the collected metadata AST and the visitor `typeImports` map to append `.js` to relative specifiers that lack an extension. Bare specifiers, absolute paths, and already-extended specifiers (`.js`, `.mjs`, `.cjs`, `.json`, ...) are left untouched, so projects using classic / node10 / bundler resolution are unaffected. Closes nestjs#3364
Member
|
could you please target v12.0.0 |
4 tasks
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.
PR Checklist
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Issue Number: #3364
PluginMetadataGeneratorprinted visitor-supplied dynamicimport("./hello.dto")ts.CallExpressions and thetypeImportsstrings verbatim. UndermoduleResolution: 'node16' | 'nodenext', ESM specifiers must include explicit file extensions — so the generatedmetadata.tsfailed type-check and threwERR_MODULE_NOT_FOUNDat runtime.Two prior attempts (#3378, #3365) tried to suppress the error with
@ts-nocheck; both were closed because the maintainer wanted the underlying generation fixed instead.What is the new behavior?
The generator now reads
programRef.getCompilerOptions().moduleResolution. When it'sNode16orNodeNext, it walks the metadata tree:import()CallExpressionwhose specifier is a relative path without an extension, rewrite the specifier to append.js.typeImportsmap (raw strings of the shapeawait import(...)), regex-rewrite the innerimport(\"...\")specifier the same way.Bare specifiers, absolute paths, and already-extended specifiers (
.js,.mjs,.cjs,.json,.ts,.tsx, etc.) are skipped, so classicnode10andbundlerresolution are unaffected.Five small pure helpers are exported alongside the rewrite logic, covered by 18 new unit tests in
test/lib/compiler/plugins/plugin-metadata-generator.spec.ts. Full local suite: 21 suites, 166 passed, 3 pre-existing skips, 0 failures.Does this PR introduce a breaking change?
The rewrite only activates when
moduleResolutionisNode16orNodeNext. Defaultnoderesolution behavior is identical to before.Other information
The same fix likely applies to
v12.0.0. Happy to open a backport PR if desired.Closes #3364