Skip to content

Commit b1e6703

Browse files
committed
- Collected .d.ts content
- Possible fix to write .d.ts near the original source file - Passing tsconfig info to honor declaration Dir
1 parent 1789897 commit b1e6703

3 files changed

Lines changed: 29 additions & 1 deletion

File tree

packages/ngtools/webpack/src/ivy/loader.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9+
import * as fs from 'node:fs';
910
import * as path from 'node:path';
1011
import type { LoaderContext } from 'webpack';
1112
import { AngularPluginSymbol, FileEmitterCollection } from './symbol';
@@ -72,6 +73,21 @@ export function angularWebpackLoader(
7273
);
7374
}
7475

76+
// Write the declaration file in the target dir
77+
if (result.declaration && fileEmitter.compilerOptions.declaration) {
78+
let target = this.resourcePath.replace('.ts', '.d.ts');
79+
if (fileEmitter.compilerOptions.declarationDir) {
80+
if (!fileEmitter.compilerOptions.baseUrl) {
81+
throw new Error('When declarationDir is specified, baseUrl is required as well');
82+
}
83+
const relDir = path.relative(fileEmitter.compilerOptions.baseUrl, target);
84+
target = path.join(fileEmitter.compilerOptions.declarationDir, relDir);
85+
}
86+
if (!fs.existsSync(path.dirname(target))) {
87+
fs.mkdirSync(path.dirname(target), { recursive: true });
88+
}
89+
fs.writeFileSync(target, result.declaration);
90+
}
7591
callback(undefined, resultContent, resultMap);
7692
})
7793
.catch((err) => {

packages/ngtools/webpack/src/ivy/plugin.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export class AngularWebpackPlugin {
8585
private readonly requiredFilesToEmit = new Set<string>();
8686
private readonly requiredFilesToEmitCache = new Map<string, EmitFileResult | undefined>();
8787
private readonly fileEmitHistory = new Map<string, FileEmitHistoryItem>();
88+
private compilerOptions!: CompilerOptions;
8889

8990
constructor(options: Partial<AngularWebpackPluginOptions> = {}) {
9091
this.pluginOptions = {
@@ -186,6 +187,7 @@ export class AngularWebpackPlugin {
186187

187188
// Setup and read TypeScript and Angular compiler configuration
188189
const { compilerOptions, rootNames, errors } = this.loadConfiguration();
190+
this.compilerOptions = compilerOptions;
189191

190192
// Create diagnostics reporter and report configuration file errors
191193
const diagnosticsReporter = createDiagnosticsReporter(compilation, (diagnostic) =>
@@ -325,6 +327,8 @@ export class AngularWebpackPlugin {
325327
compilation.compiler.webpack.NormalModule.getCompilationHooks(compilation).loader.tap(
326328
PLUGIN_NAME,
327329
(context) => {
330+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
331+
fileEmitters!.compilerOptions = this.compilerOptions;
328332
const loaderContext = context as typeof context & {
329333
[AngularPluginSymbol]?: FileEmitterCollection;
330334
};
@@ -681,13 +685,16 @@ export class AngularWebpackPlugin {
681685

682686
let content: string | undefined;
683687
let map: string | undefined;
688+
let declaration: string | undefined;
684689
program.emit(
685690
sourceFile,
686691
(filename, data) => {
687692
if (filename.endsWith('.map')) {
688693
map = data;
689694
} else if (filename.endsWith('.js')) {
690695
content = data;
696+
} else if (filename.endsWith('.d.ts')) {
697+
declaration = data;
691698
}
692699
},
693700
undefined,
@@ -705,7 +712,7 @@ export class AngularWebpackPlugin {
705712
...getExtraDependencies(sourceFile),
706713
].map(externalizePath);
707714

708-
return { content, map, dependencies, hash };
715+
return { content, map, declaration, dependencies, hash };
709716
};
710717
}
711718

packages/ngtools/webpack/src/ivy/symbol.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9+
import { CompilerOptions } from 'typescript';
10+
911
export const AngularPluginSymbol: unique symbol = Symbol.for('@ngtools/webpack[angular-compiler]');
1012

1113
export interface EmitFileResult {
1214
content?: string;
1315
map?: string;
16+
declaration?: string;
1417
dependencies: readonly string[];
1518
hash?: Uint8Array;
1619
}
@@ -36,6 +39,8 @@ export class FileEmitterRegistration {
3639
export class FileEmitterCollection {
3740
#registrations: FileEmitterRegistration[] = [];
3841

42+
public compilerOptions!: CompilerOptions;
43+
3944
register(): FileEmitterRegistration {
4045
const registration = new FileEmitterRegistration();
4146
this.#registrations.push(registration);

0 commit comments

Comments
 (0)