Skip to content

Commit 506e6a5

Browse files
authored
[typings-generator] Add some options to typings generation and introduce a Heft plugin for generating loc file typings. (microsoft#4894)
* Include an option for a documentation comment for typings default exports. * Add options to parseAndGenerateTypings return. * Include a inferDefaultExportInterfaceNameFromFilename option for TypingsGenerator. * Add a heft plugin for generating localization file tyings. * Clean up inferred exports names. * Fix an issue and leave a note. * Update Readme. * Rename getVerbose to getVerboseOutput. * General updates and cleanup to make the change not breaking. * Introduce a test for StringValuesTypingsGenerator. * Rush change. * Code cleanup. * Random code cleanup in API Extractor. * Add a Text.splitByNewLines function to NCL. * Code cleanup. * Add slashNormalizedBuildFolderPath property to HeftConfiguration. * Tweak schema. * Add a test case for the loc typings generator plugin. * Work around an API Extractor issue.
1 parent 2f08f7b commit 506e6a5

File tree

67 files changed

+1438
-203
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+1438
-203
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ These GitHub repositories provide supplementary resources for Rush Stack:
6565
| [/heft-plugins/heft-dev-cert-plugin](./heft-plugins/heft-dev-cert-plugin/) | [![npm version](https://badge.fury.io/js/%40rushstack%2Fheft-dev-cert-plugin.svg)](https://badge.fury.io/js/%40rushstack%2Fheft-dev-cert-plugin) | [changelog](./heft-plugins/heft-dev-cert-plugin/CHANGELOG.md) | [@rushstack/heft-dev-cert-plugin](https://www.npmjs.com/package/@rushstack/heft-dev-cert-plugin) |
6666
| [/heft-plugins/heft-jest-plugin](./heft-plugins/heft-jest-plugin/) | [![npm version](https://badge.fury.io/js/%40rushstack%2Fheft-jest-plugin.svg)](https://badge.fury.io/js/%40rushstack%2Fheft-jest-plugin) | [changelog](./heft-plugins/heft-jest-plugin/CHANGELOG.md) | [@rushstack/heft-jest-plugin](https://www.npmjs.com/package/@rushstack/heft-jest-plugin) |
6767
| [/heft-plugins/heft-lint-plugin](./heft-plugins/heft-lint-plugin/) | [![npm version](https://badge.fury.io/js/%40rushstack%2Fheft-lint-plugin.svg)](https://badge.fury.io/js/%40rushstack%2Fheft-lint-plugin) | [changelog](./heft-plugins/heft-lint-plugin/CHANGELOG.md) | [@rushstack/heft-lint-plugin](https://www.npmjs.com/package/@rushstack/heft-lint-plugin) |
68+
| [/heft-plugins/heft-localization-typings-plugin](./heft-plugins/heft-localization-typings-plugin/) | [![npm version](https://badge.fury.io/js/%40rushstack%2Fheft-localization-typings-plugin.svg)](https://badge.fury.io/js/%40rushstack%2Fheft-localization-typings-plugin) | [changelog](./heft-plugins/heft-localization-typings-plugin/CHANGELOG.md) | [@rushstack/heft-localization-typings-plugin](https://www.npmjs.com/package/@rushstack/heft-localization-typings-plugin) |
6869
| [/heft-plugins/heft-sass-plugin](./heft-plugins/heft-sass-plugin/) | [![npm version](https://badge.fury.io/js/%40rushstack%2Fheft-sass-plugin.svg)](https://badge.fury.io/js/%40rushstack%2Fheft-sass-plugin) | [changelog](./heft-plugins/heft-sass-plugin/CHANGELOG.md) | [@rushstack/heft-sass-plugin](https://www.npmjs.com/package/@rushstack/heft-sass-plugin) |
6970
| [/heft-plugins/heft-serverless-stack-plugin](./heft-plugins/heft-serverless-stack-plugin/) | [![npm version](https://badge.fury.io/js/%40rushstack%2Fheft-serverless-stack-plugin.svg)](https://badge.fury.io/js/%40rushstack%2Fheft-serverless-stack-plugin) | [changelog](./heft-plugins/heft-serverless-stack-plugin/CHANGELOG.md) | [@rushstack/heft-serverless-stack-plugin](https://www.npmjs.com/package/@rushstack/heft-serverless-stack-plugin) |
7071
| [/heft-plugins/heft-storybook-plugin](./heft-plugins/heft-storybook-plugin/) | [![npm version](https://badge.fury.io/js/%40rushstack%2Fheft-storybook-plugin.svg)](https://badge.fury.io/js/%40rushstack%2Fheft-storybook-plugin) | [changelog](./heft-plugins/heft-storybook-plugin/CHANGELOG.md) | [@rushstack/heft-storybook-plugin](https://www.npmjs.com/package/@rushstack/heft-storybook-plugin) |

apps/api-extractor/src/analyzer/Span.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// See LICENSE in the project root for license information.
33

44
import * as ts from 'typescript';
5-
import { InternalError, Sort } from '@rushstack/node-core-library';
5+
import { InternalError, Sort, Text } from '@rushstack/node-core-library';
66

77
import { IndentedWriter } from '../generators/IndentedWriter';
88

@@ -637,12 +637,7 @@ export class Span {
637637
}
638638

639639
private _getTrimmed(text: string): string {
640-
const trimmed: string = text.replace(/\r?\n/g, '\\n');
641-
642-
if (trimmed.length > 100) {
643-
return trimmed.substr(0, 97) + '...';
644-
}
645-
return trimmed;
640+
return Text.truncateWithEllipsis(Text.convertToLf(text), 100);
646641
}
647642

648643
private _getSubstring(startIndex: number, endIndex: number): string {

apps/heft/src/configuration/HeftConfiguration.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// See LICENSE in the project root for license information.
33

44
import * as path from 'path';
5-
import { type IPackageJson, PackageJsonLookup, InternalError } from '@rushstack/node-core-library';
5+
import { type IPackageJson, PackageJsonLookup, InternalError, Path } from '@rushstack/node-core-library';
66
import { Terminal, type ITerminalProvider, type ITerminal } from '@rushstack/terminal';
77
import { trueCasePathSync } from 'true-case-path';
88
import { type IRigConfig, RigConfig } from '@rushstack/rig-package';
@@ -30,8 +30,8 @@ export interface IHeftConfigurationInitializationOptions {
3030
*/
3131
export class HeftConfiguration {
3232
private _buildFolderPath!: string;
33+
private _slashNormalizedBuildFolderPath: string | undefined;
3334
private _projectConfigFolderPath: string | undefined;
34-
private _cacheFolderPath: string | undefined;
3535
private _tempFolderPath: string | undefined;
3636
private _rigConfig: IRigConfig | undefined;
3737
private _globalTerminal!: Terminal;
@@ -45,6 +45,17 @@ export class HeftConfiguration {
4545
return this._buildFolderPath;
4646
}
4747

48+
/**
49+
* {@link HeftConfiguration.buildFolderPath} with all path separators converted to forward slashes.
50+
*/
51+
public get slashNormalizedBuildFolderPath(): string {
52+
if (!this._slashNormalizedBuildFolderPath) {
53+
this._slashNormalizedBuildFolderPath = Path.convertToSlashes(this.buildFolderPath);
54+
}
55+
56+
return this._slashNormalizedBuildFolderPath;
57+
}
58+
4859
/**
4960
* The path to the project's "config" folder.
5061
*/

apps/heft/src/utilities/GitUtilities.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import * as path from 'path';
55
import type { ChildProcess, SpawnSyncReturns } from 'child_process';
66
import { default as getGitRepoInfo, type GitRepoInfo as IGitRepoInfo } from 'git-repo-info';
7-
import { Executable, FileSystem, InternalError, Path } from '@rushstack/node-core-library';
7+
import { Executable, FileSystem, InternalError, Path, Text } from '@rushstack/node-core-library';
88
import { default as ignore, type Ignore as IIgnoreMatcher } from 'ignore';
99

1010
// Matches lines starting with "#" and whitepace lines
@@ -282,7 +282,7 @@ export class GitUtilities {
282282

283283
const foundIgnorePatterns: string[] = [];
284284
if (gitIgnoreContent) {
285-
const gitIgnorePatterns: string[] = gitIgnoreContent.split(/\r?\n/g);
285+
const gitIgnorePatterns: string[] = Text.splitByNewLines(gitIgnoreContent);
286286
for (const gitIgnorePattern of gitIgnorePatterns) {
287287
// Ignore whitespace-only lines and comments
288288
if (gitIgnorePattern.length === 0 || GITIGNORE_IGNORABLE_LINE_REGEX.test(gitIgnorePattern)) {

build-tests/localization-plugin-test-02/build.js

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Defines configuration used by core Heft.
3+
*/
4+
{
5+
"$schema": "https://developer.microsoft.com/json-schemas/heft/v0/heft.schema.json",
6+
7+
// TODO: Add comments
8+
"phasesByName": {
9+
"build": {
10+
"cleanFiles": [{ "includeGlobs": ["dist", "lib", "lib-commonjs"] }],
11+
12+
"tasksByName": {
13+
"loc-typings": {
14+
"taskPlugin": {
15+
"pluginPackage": "@rushstack/heft-localization-typings-plugin",
16+
"options": {
17+
"generatedTsFolder": "temp/loc-json-ts"
18+
}
19+
}
20+
},
21+
"typescript": {
22+
"taskDependencies": ["loc-typings"],
23+
"taskPlugin": {
24+
"pluginPackage": "@rushstack/heft-typescript-plugin"
25+
}
26+
},
27+
"lint": {
28+
"taskDependencies": ["typescript"],
29+
"taskPlugin": {
30+
"pluginPackage": "@rushstack/heft-lint-plugin"
31+
}
32+
},
33+
"webpack": {
34+
"taskDependencies": ["typescript"],
35+
"taskPlugin": {
36+
"pluginPackage": "@rushstack/heft-webpack4-plugin"
37+
}
38+
}
39+
}
40+
}
41+
}
42+
}

build-tests/localization-plugin-test-02/config/rush-project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"operationSettings": [
55
{
66
"operationName": "_phase:build",
7-
"outputFolderNames": ["lib", "dist"]
7+
"outputFolderNames": ["lib", "dist-dev", "dist-prod"]
88
}
99
]
1010
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Configures the TypeScript plugin for Heft. This plugin also manages linting.
3+
*/
4+
{
5+
"$schema": "https://developer.microsoft.com/json-schemas/heft/v0/typescript.schema.json",
6+
7+
"staticAssetsToCopy": {
8+
"fileExtensions": [".resx", ".json", ".resjson"]
9+
}
10+
}

build-tests/localization-plugin-test-02/package.json

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,24 @@
44
"version": "0.1.0",
55
"private": true,
66
"scripts": {
7-
"build": "node build.js",
8-
"serve": "node serve.js",
9-
"_phase:build": "node build.js"
7+
"build": "heft build --clean",
8+
"start": "heft start",
9+
"_phase:build": "heft run --only build -- --clean"
1010
},
1111
"dependencies": {
12-
"@rushstack/webpack4-localization-plugin": "workspace:*",
13-
"@rushstack/webpack4-module-minifier-plugin": "workspace:*",
12+
"@rushstack/heft-lint-plugin": "workspace:*",
13+
"@rushstack/heft-localization-typings-plugin": "workspace:*",
14+
"@rushstack/heft-typescript-plugin": "workspace:*",
15+
"@rushstack/heft-webpack4-plugin": "workspace:*",
16+
"@rushstack/heft": "workspace:*",
1417
"@rushstack/node-core-library": "workspace:*",
1518
"@rushstack/set-webpack-public-path-plugin": "^4.1.16",
19+
"@rushstack/webpack4-localization-plugin": "workspace:*",
20+
"@rushstack/webpack4-module-minifier-plugin": "workspace:*",
1621
"@types/lodash": "4.14.116",
1722
"@types/webpack-env": "1.18.0",
1823
"html-webpack-plugin": "~4.5.2",
1924
"lodash": "~4.17.15",
20-
"ts-loader": "6.0.0",
2125
"typescript": "~5.4.2",
2226
"webpack-bundle-analyzer": "~4.5.0",
2327
"webpack-cli": "~3.3.2",

build-tests/localization-plugin-test-02/serve.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

build-tests/localization-plugin-test-02/src/indexA.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { string1 } from './strings1.loc.json';
2-
import * as strings3 from './strings3.loc.json';
2+
import * as strings3 from './strings3.resjson';
33
import * as strings5 from './strings5.resx';
44

55
console.log(string1);

build-tests/localization-plugin-test-02/src/indexB.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { string1, string2 } from './strings3.loc.json';
1+
import { string1, string2 } from './strings3.resjson';
22
const strings4: string = require('./strings4.loc.json');
33

44
console.log(string1);

build-tests/localization-plugin-test-02/src/strings3.loc.json

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"string1": "string three with a \\ backslash",
3+
"_string1.comment": "the third string",
4+
5+
"string2": "string four with an ' apostrophe",
6+
"_string2.comment": "the fourth string",
7+
8+
"string3": "UNUSED STRING",
9+
"_string3.comment": "UNUSED STRING"
10+
}

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

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,13 @@ const HtmlWebpackPlugin = require('html-webpack-plugin');
1212
function generateConfiguration(mode, outputFolderName) {
1313
return {
1414
mode: mode,
15-
module: {
16-
rules: [
17-
{
18-
test: /\.tsx?$/,
19-
loader: require.resolve('ts-loader'),
20-
exclude: /(node_modules)/,
21-
options: {
22-
compiler: require.resolve('typescript'),
23-
logLevel: 'ERROR',
24-
configFile: path.resolve(__dirname, 'tsconfig.json')
25-
}
26-
}
27-
]
28-
},
29-
resolve: {
30-
extensions: ['.js', '.jsx', '.json', '.ts', '.tsx']
31-
},
3215
entry: {
33-
'localization-test-A': path.join(__dirname, 'src', 'indexA.ts'),
34-
'localization-test-B': path.join(__dirname, 'src', 'indexB.ts'),
35-
'localization-test-C': path.join(__dirname, 'src', 'indexC.ts')
16+
'localization-test-A': `${__dirname}/lib/indexA.js`,
17+
'localization-test-B': `${__dirname}/lib/indexB.js`,
18+
'localization-test-C': `${__dirname}/lib/indexC.js`
3619
},
3720
output: {
38-
path: path.join(__dirname, outputFolderName),
21+
path: `${__dirname}/${outputFolderName}`,
3922
filename: '[name]_[locale]_[contenthash].js',
4023
chunkFilename: '[id].[name]_[locale]_[contenthash].js'
4124
},
@@ -84,22 +67,17 @@ function generateConfiguration(mode, outputFolderName) {
8467
normalizeResxNewlines: 'crlf',
8568
ignoreMissingResxComments: true
8669
},
87-
typingsOptions: {
88-
generatedTsFolder: path.resolve(__dirname, 'temp', 'loc-json-ts'),
89-
sourceRoot: path.resolve(__dirname, 'src'),
90-
processComment: (comment) => (comment ? `${comment} (processed)` : comment)
91-
},
9270
localizationStats: {
93-
dropPath: path.resolve(__dirname, 'temp', 'localization-stats.json')
71+
dropPath: `${__dirname}/temp/localization-stats.json`
9472
},
9573
ignoreString: (filePath, stringName) => stringName === '__IGNORED_STRING__'
9674
}),
9775
new BundleAnalyzerPlugin({
9876
openAnalyzer: false,
9977
analyzerMode: 'static',
100-
reportFilename: path.resolve(__dirname, 'temp', 'stats.html'),
78+
reportFilename: `${__dirname}/temp/stats.html`,
10179
generateStatsFile: true,
102-
statsFilename: path.resolve(__dirname, 'temp', 'stats.json'),
80+
statsFilename: `${__dirname}/temp/stats.json`,
10381
logLevel: 'error'
10482
}),
10583
new SetPublicPathPlugin({
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@microsoft/api-extractor",
5+
"comment": "",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@microsoft/api-extractor"
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": "@microsoft/rush",
5+
"comment": "",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@microsoft/rush"
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/heft-config-file",
5+
"comment": "",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@rushstack/heft-config-file"
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@rushstack/heft-localization-typings-plugin",
5+
"comment": "Initial release.",
6+
"type": "minor"
7+
}
8+
],
9+
"packageName": "@rushstack/heft-localization-typings-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/heft-sass-plugin",
5+
"comment": "",
6+
"type": "none"
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/heft",
5+
"comment": "Add a `slashNormalizedBuildFolderPath` property to `HeftConfiguration`.",
6+
"type": "minor"
7+
}
8+
],
9+
"packageName": "@rushstack/heft"
10+
}

0 commit comments

Comments
 (0)