Skip to content

Commit 3e16133

Browse files
authored
Merge pull request #3494 from iclanton/typings-generator-outputs
[typings-generator] Allow typings to be output to secondary folders.
2 parents fc9db95 + b756631 commit 3e16133

19 files changed

+152
-38
lines changed

build-tests/localization-plugin-test-03/webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ function generateConfiguration(mode, outputFolderName) {
117117
},
118118
typingsOptions: {
119119
generatedTsFolder: path.resolve(__dirname, 'temp', 'loc-json-ts'),
120+
secondaryGeneratedTsFolders: ['lib'],
120121
sourceRoot: path.resolve(__dirname, 'src'),
121122
exportAsDefault: true
122123
},
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@rushstack/heft-sass-plugin",
5+
"comment": "Add an option to output typings to additional output folders.",
6+
"type": "minor"
7+
}
8+
],
9+
"packageName": "@rushstack/heft-sass-plugin"
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@rushstack/localization-utilities",
5+
"comment": "Add an option to output typings to additional output folders.",
6+
"type": "minor"
7+
}
8+
],
9+
"packageName": "@rushstack/localization-utilities"
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@rushstack/localization-utilities",
5+
"comment": "",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@rushstack/localization-utilities"
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@rushstack/typings-generator",
5+
"comment": "Add an option to output typings to additional output folders.",
6+
"type": "minor"
7+
}
8+
],
9+
"packageName": "@rushstack/typings-generator"
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@rushstack/typings-generator",
5+
"comment": "",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@rushstack/typings-generator"
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@rushstack/webpack4-localization-plugin",
5+
"comment": "Add an option to output typings to additional output folders.",
6+
"type": "minor"
7+
}
8+
],
9+
"packageName": "@rushstack/webpack4-localization-plugin"
10+
}

common/reviews/api/localization-utilities.api.md

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
```ts
66

77
import { ITerminal } from '@rushstack/node-core-library';
8+
import { ITypingsGeneratorBaseOptions } from '@rushstack/typings-generator';
89
import { NewlineKind } from '@rushstack/node-core-library';
910
import { StringValuesTypingsGenerator } from '@rushstack/typings-generator';
1011

@@ -76,25 +77,17 @@ export interface IPseudolocaleOptions {
7677
}
7778

7879
// @public (undocumented)
79-
export interface ITypingsGeneratorOptions {
80+
export interface ITypingsGeneratorOptions extends ITypingsGeneratorBaseOptions {
8081
// (undocumented)
8182
exportAsDefault?: boolean;
8283
// (undocumented)
83-
generatedTsFolder: string;
84-
// (undocumented)
85-
globsToIgnore?: string[];
86-
// (undocumented)
8784
ignoreMissingResxComments?: boolean | undefined;
8885
// (undocumented)
8986
ignoreString?: IgnoreStringFunction;
9087
// (undocumented)
9188
processComment?: (comment: string | undefined, resxFilePath: string, stringName: string) => string | undefined;
9289
// (undocumented)
9390
resxNewlineNormalization?: NewlineKind | undefined;
94-
// (undocumented)
95-
srcFolder: string;
96-
// (undocumented)
97-
terminal?: ITerminal;
9891
}
9992

10093
// @public (undocumented)

common/reviews/api/typings-generator.api.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,31 @@ export interface IStringValueTypings {
2727
}
2828

2929
// @public (undocumented)
30-
export interface ITypingsGeneratorOptions<TTypingsResult = string | undefined> {
31-
// (undocumented)
32-
fileExtensions: string[];
33-
// @deprecated (undocumented)
34-
filesToIgnore?: string[];
30+
export interface ITypingsGeneratorBaseOptions {
3531
// (undocumented)
3632
generatedTsFolder: string;
3733
// (undocumented)
38-
getAdditionalOutputFiles?: (relativePath: string) => string[];
39-
// (undocumented)
4034
globsToIgnore?: string[];
4135
// (undocumented)
42-
parseAndGenerateTypings: (fileContents: string, filePath: string, relativePath: string) => TTypingsResult | Promise<TTypingsResult>;
36+
secondaryGeneratedTsFolders?: string[];
4337
// (undocumented)
4438
srcFolder: string;
4539
// (undocumented)
4640
terminal?: ITerminal;
4741
}
4842

43+
// @public (undocumented)
44+
export interface ITypingsGeneratorOptions<TTypingsResult = string | undefined> extends ITypingsGeneratorBaseOptions {
45+
// (undocumented)
46+
fileExtensions: string[];
47+
// @deprecated (undocumented)
48+
filesToIgnore?: string[];
49+
// (undocumented)
50+
getAdditionalOutputFiles?: (relativePath: string) => string[];
51+
// (undocumented)
52+
parseAndGenerateTypings: (fileContents: string, filePath: string, relativePath: string) => TTypingsResult | Promise<TTypingsResult>;
53+
}
54+
4955
// @public
5056
export class StringValuesTypingsGenerator extends TypingsGenerator {
5157
constructor(options: IStringValuesTypingsGeneratorOptions);

common/reviews/api/webpack4-localization-plugin.api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ export interface ITypingsGenerationOptions {
134134
// @deprecated (undocumented)
135135
ignoreString?: (resxFilePath: string, stringName: string) => boolean;
136136
processComment?: (comment: string | undefined, resxFilePath: string, stringName: string) => string | undefined;
137+
secondaryGeneratedTsFolders?: string[];
137138
sourceRoot?: string;
138139
}
139140

heft-plugins/heft-sass-plugin/src/SassTypingsGenerator.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ export interface ISassConfiguration {
2424
*/
2525
generatedTsFolder?: string;
2626

27+
/**
28+
* Optional additional folders to which Sass typings should be output.
29+
*/
30+
secondaryGeneratedTsFolders?: string[];
31+
2732
/**
2833
* Output directories for compiled CSS
2934
*/
@@ -103,6 +108,7 @@ export class SassTypingsGenerator extends StringValuesTypingsGenerator {
103108
exportAsDefaultInterfaceName,
104109
fileExtensions,
105110
filesToIgnore: sassConfiguration.excludeFiles,
111+
secondaryGeneratedTsFolders: sassConfiguration.secondaryGeneratedTsFolders,
106112

107113
getAdditionalOutputFiles: getCssPaths,
108114

heft-plugins/heft-sass-plugin/src/SassTypingsPlugin.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type {
1111
} from '@rushstack/heft';
1212
import { ConfigurationFile, PathResolutionMethod } from '@rushstack/heft-config-file';
1313
import { JsonSchema } from '@rushstack/node-core-library';
14+
1415
import { ISassConfiguration, SassTypingsGenerator } from './SassTypingsGenerator';
1516
import { Async } from './utilities/Async';
1617

heft-plugins/heft-sass-plugin/src/schemas/heft-sass-plugin.schema.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@
2727
"description": "Output directory for generated Sass typings."
2828
},
2929

30+
"secondaryGeneratedTsFolders": {
31+
"type": "array",
32+
"description": "Optional additional folders to which Sass typings should be output.",
33+
"items": {
34+
"type": "string"
35+
}
36+
},
37+
3038
"exportAsDefault": {
3139
"type": "boolean",
3240
"description": "Determines whether export values are wrapped in a default property, or not."

heft-plugins/heft-sass-plugin/src/schemas/templates/sass.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
*/
2525
// "generatedTsFolder": "temp/sass-ts/",
2626

27+
/**
28+
* Optional additional folders to which Sass typings should be output.
29+
*/
30+
// "secondaryGeneratedTsFolders": [],
31+
2732
/**
2833
* Determines whether export values are wrapped in a default property, or not.
2934
*

libraries/localization-utilities/src/TypingsGenerator.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22
// See LICENSE in the project root for license information.
33

4-
import { StringValuesTypingsGenerator, IStringValueTyping } from '@rushstack/typings-generator';
5-
import { ITerminal, NewlineKind } from '@rushstack/node-core-library';
4+
import {
5+
StringValuesTypingsGenerator,
6+
IStringValueTyping,
7+
ITypingsGeneratorBaseOptions
8+
} from '@rushstack/typings-generator';
9+
import { NewlineKind } from '@rushstack/node-core-library';
610

711
import type { IgnoreStringFunction, ILocalizationFile } from './interfaces';
812
import { parseLocFile } from './LocFileParser';
913

1014
/**
1115
* @public
1216
*/
13-
export interface ITypingsGeneratorOptions {
14-
srcFolder: string;
15-
generatedTsFolder: string;
16-
terminal?: ITerminal;
17+
export interface ITypingsGeneratorOptions extends ITypingsGeneratorBaseOptions {
1718
exportAsDefault?: boolean;
18-
globsToIgnore?: string[];
1919
resxNewlineNormalization?: NewlineKind | undefined;
2020
ignoreMissingResxComments?: boolean | undefined;
2121
ignoreString?: IgnoreStringFunction;

libraries/typings-generator/src/TypingsGenerator.ts

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,26 @@ import * as chokidar from 'chokidar';
1919
/**
2020
* @public
2121
*/
22-
export interface ITypingsGeneratorOptions<TTypingsResult = string | undefined> {
22+
export interface ITypingsGeneratorBaseOptions {
2323
srcFolder: string;
2424
generatedTsFolder: string;
25+
secondaryGeneratedTsFolders?: string[];
26+
globsToIgnore?: string[];
27+
terminal?: ITerminal;
28+
}
29+
30+
/**
31+
* @public
32+
*/
33+
export interface ITypingsGeneratorOptions<TTypingsResult = string | undefined>
34+
extends ITypingsGeneratorBaseOptions {
2535
fileExtensions: string[];
2636
parseAndGenerateTypings: (
2737
fileContents: string,
2838
filePath: string,
2939
relativePath: string
3040
) => TTypingsResult | Promise<TTypingsResult>;
3141
getAdditionalOutputFiles?: (relativePath: string) => string[];
32-
terminal?: ITerminal;
33-
globsToIgnore?: string[];
3442
/**
3543
* @deprecated
3644
*
@@ -209,9 +217,9 @@ export class TypingsGenerator {
209217
}
210218

211219
public getOutputFilePaths(relativePath: string): string[] {
212-
const typingsFile: string = this._getTypingsFilePath(relativePath);
220+
const typingsFilePaths: Iterable<string> = this._getTypingsFilePaths(relativePath);
213221
const additionalPaths: string[] | undefined = this._options.getAdditionalOutputFiles?.(relativePath);
214-
return additionalPaths ? [typingsFile, ...additionalPaths] : [typingsFile];
222+
return additionalPaths ? [...typingsFilePaths, ...additionalPaths] : Array.from(typingsFilePaths);
215223
}
216224

217225
private async _reprocessFiles(relativePaths: Iterable<string>): Promise<void> {
@@ -271,12 +279,13 @@ export class TypingsGenerator {
271279
typingsData
272280
].join(EOL);
273281

274-
const generatedTsFilePath: string = this._getTypingsFilePath(relativePath);
275-
276-
await FileSystem.writeFileAsync(generatedTsFilePath, prefixedTypingsData, {
277-
ensureFolderExists: true,
278-
convertLineEndings: NewlineKind.OsDefault
279-
});
282+
const generatedTsFilePaths: Iterable<string> = this._getTypingsFilePaths(relativePath);
283+
for (const generatedTsFilePath of generatedTsFilePaths) {
284+
await FileSystem.writeFileAsync(generatedTsFilePath, prefixedTypingsData, {
285+
ensureFolderExists: true,
286+
convertLineEndings: NewlineKind.OsDefault
287+
});
288+
}
280289
} catch (e) {
281290
this._options.terminal!.writeError(
282291
`Error occurred parsing and generating typings for file "${resolvedPath}": ${e}`
@@ -297,8 +306,15 @@ export class TypingsGenerator {
297306
}
298307
}
299308

300-
private _getTypingsFilePath(relativePath: string): string {
301-
return path.resolve(this._options.generatedTsFolder, `${relativePath}.d.ts`);
309+
private *_getTypingsFilePaths(relativePath: string): Iterable<string> {
310+
const { generatedTsFolder, secondaryGeneratedTsFolders } = this._options;
311+
const dtsFilename: string = `${relativePath}.d.ts`;
312+
yield path.resolve(generatedTsFolder, dtsFilename);
313+
if (secondaryGeneratedTsFolders) {
314+
for (const secondaryGeneratedTsFolder of secondaryGeneratedTsFolders) {
315+
yield path.resolve(secondaryGeneratedTsFolder, dtsFilename);
316+
}
317+
}
302318
}
303319

304320
private _normalizeFileExtensions(fileExtensions: string[]): string[] {

libraries/typings-generator/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22
// See LICENSE in the project root for license information.
33

4-
export { ITypingsGeneratorOptions, TypingsGenerator } from './TypingsGenerator';
4+
export { ITypingsGeneratorBaseOptions, ITypingsGeneratorOptions, TypingsGenerator } from './TypingsGenerator';
55

66
export {
77
IStringValueTyping,

webpack/localization-plugin-4/src/LocalizationPlugin.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,17 @@ export class LocalizationPlugin implements Webpack.Plugin {
167167
this._options.typingsOptions.sourceRoot
168168
);
169169
}
170+
171+
const secondaryGeneratedTsFolders: string[] | undefined =
172+
this._options.typingsOptions.secondaryGeneratedTsFolders;
173+
if (secondaryGeneratedTsFolders) {
174+
for (let i: number = 0; i < secondaryGeneratedTsFolders.length; i++) {
175+
const secondaryGeneratedTsFolder: string = secondaryGeneratedTsFolders[i];
176+
if (!path.isAbsolute(secondaryGeneratedTsFolder)) {
177+
secondaryGeneratedTsFolders[i] = path.resolve(compiler.context, secondaryGeneratedTsFolder);
178+
}
179+
}
180+
}
170181
}
171182

172183
// https://github.com/webpack/webpack-dev-server/pull/1929/files#diff-15fb51940da53816af13330d8ce69b4eR66
@@ -179,6 +190,7 @@ export class LocalizationPlugin implements Webpack.Plugin {
179190
typingsPreprocessor = new TypingsGenerator({
180191
srcFolder: this._options.typingsOptions.sourceRoot || compiler.context,
181192
generatedTsFolder: this._options.typingsOptions.generatedTsFolder,
193+
secondaryGeneratedTsFolders: this._options.typingsOptions.secondaryGeneratedTsFolders,
182194
exportAsDefault: this._options.typingsOptions.exportAsDefault,
183195
globsToIgnore: this._options.globsToIgnore,
184196
ignoreString: this._options.ignoreString,

webpack/localization-plugin-4/src/interfaces.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ export interface ITypingsGenerationOptions {
3232
*/
3333
generatedTsFolder: string;
3434

35+
/**
36+
* Optional additional folders into which `.d.ts` files for loc files should be dropped.
37+
*/
38+
secondaryGeneratedTsFolders?: string[];
39+
3540
/**
3641
* This optional property overrides the compiler context for discovery of localization files
3742
* for which typings should be generated.

0 commit comments

Comments
 (0)