-
Notifications
You must be signed in to change notification settings - Fork 11.9k
@ngtools/webpack: Enable the emit of declaration files #32763
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| * found in the LICENSE file at https://angular.dev/license | ||
| */ | ||
|
|
||
| import * as fs from 'node:fs'; | ||
| import * as path from 'node:path'; | ||
| import type { LoaderContext } from 'webpack'; | ||
| import { AngularPluginSymbol, FileEmitterCollection } from './symbol'; | ||
|
|
@@ -72,6 +73,18 @@ export function angularWebpackLoader( | |
| ); | ||
| } | ||
|
|
||
| // Write the declaration file in the target dir | ||
| if (result.declaration && fileEmitter.compilerOptions.declaration) { | ||
| let target = this.resourcePath.replace('.ts', '.d.ts'); | ||
| if (fileEmitter.compilerOptions.declarationDir) { | ||
| if (!fileEmitter.compilerOptions.baseUrl) { | ||
| throw new Error('When declarationDir is specified, baseUrl is required as well'); | ||
| } | ||
| const relDir = path.relative(fileEmitter.compilerOptions.baseUrl, target); | ||
| target = path.join(fileEmitter.compilerOptions.declarationDir, relDir); | ||
| } | ||
| fs.writeFileSync(target, result.declaration); | ||
|
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. This synchronous file write has two main problems:
Ideally, this should be an asynchronous operation using As a minimal fix that addresses the potential crash, you could use
Author
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. Obviously I expect a cached mechanism to avoid to emit the files immediately or before returning the async call. Please point to a possible usage of output-only caching support already part of the project, if one. |
||
| } | ||
| callback(undefined, resultContent, resultMap); | ||
| }) | ||
| .catch((err) => { | ||
|
|
||
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.
Using
String.prototype.replace()with a string argument is not robust. It only replaces the first occurrence and doesn't handle different TypeScript file extensions like.mtsor.ctscorrectly. A regular expression should be used to safely replace the file extension and handle all variants.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.
good bot