Skip to content

Commit 6a3afb2

Browse files
committed
Redesigned DTS Rollup settings to specify separate 3 publishing folders, with trimming now being optional
1 parent 1af5cae commit 6a3afb2

7 files changed

Lines changed: 161 additions & 63 deletions

File tree

‎apps/api-extractor/src/DebugRun.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ const extractor: Extractor = new Extractor(
5050
},
5151
dtsRollup: {
5252
enabled: true,
53-
outputFolder: path.join(__dirname, 'debug')
53+
publishFolderForInternal: path.join(__dirname, 'debug/internal'),
54+
publishFolderForBeta: path.join(__dirname, 'debug/beta'),
55+
publishFolderForPublic: path.join(__dirname, 'debug/public')
5456
}
5557
}
5658
);

‎apps/api-extractor/src/ExtractorContext.ts‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import * as ts from 'typescript';
55
import * as fsx from 'fs-extra';
66
import * as path from 'path';
7-
import { PackageJsonLookup } from '@microsoft/node-core-library';
7+
import { PackageJsonLookup, IPackageJson } from '@microsoft/node-core-library';
88

99
import { AstPackage } from './ast/AstPackage';
1010
import { DocItemLoader } from './DocItemLoader';
@@ -47,6 +47,12 @@ export interface IExtractorContextOptions {
4747
export class ExtractorContext {
4848
public typeChecker: ts.TypeChecker;
4949
public package: AstPackage;
50+
51+
/**
52+
* The parsed package.json file for this package.
53+
*/
54+
public readonly packageJson: IPackageJson;
55+
5056
/**
5157
* One DocItemLoader is needed per analyzer to look up external API members
5258
* as needed.
@@ -59,8 +65,6 @@ export class ExtractorContext {
5965

6066
public readonly validationRules: IExtractorValidationRulesConfig;
6167

62-
private _packageName: string;
63-
6468
// If the entry point is "C:\Folder\project\src\index.ts" and the nearest package.json
6569
// is "C:\Folder\project\package.json", then the packageFolder is "C:\Folder\project"
6670
private _packageFolder: string;
@@ -79,7 +83,7 @@ export class ExtractorContext {
7983
}
8084
this._packageFolder = folder;
8185

82-
this._packageName = this.packageJsonLookup.tryLoadPackageJsonFor(this._packageFolder)!.name;
86+
this.packageJson = this.packageJsonLookup.tryLoadPackageJsonFor(this._packageFolder)!;
8387

8488
this.docItemLoader = new DocItemLoader(this._packageFolder);
8589

@@ -108,7 +112,7 @@ export class ExtractorContext {
108112
* Returns the full name of the package being analyzed.
109113
*/
110114
public get packageName(): string {
111-
return this._packageName;
115+
return this.packageJson.name;
112116
}
113117

114118
/**

‎apps/api-extractor/src/extractor/Extractor.ts‎

Lines changed: 74 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@ import * as ts from 'typescript';
77
import lodash = require('lodash');
88
import colors = require('colors');
99

10-
import { JsonFile, JsonSchema } from '@microsoft/node-core-library';
10+
import {
11+
JsonFile,
12+
JsonSchema,
13+
Path
14+
} from '@microsoft/node-core-library';
1115
import {
1216
IExtractorConfig,
1317
IExtractorProjectConfig,
14-
IExtractorApiJsonFileConfig
18+
IExtractorApiJsonFileConfig,
19+
IExtractorDtsRollupConfig
1520
} from './IExtractorConfig';
1621
import { ExtractorContext } from '../ExtractorContext';
1722
import { ILogger } from './ILogger';
@@ -340,21 +345,73 @@ export class Extractor {
340345
}
341346
}
342347

343-
if (this.actualConfig.dtsRollup.enabled) {
348+
const dtsRollup: IExtractorDtsRollupConfig = this.actualConfig.dtsRollup!;
349+
if (dtsRollup.enabled) {
350+
let mainDtsRollupPath: string = dtsRollup.mainDtsRollupPath!;
351+
352+
if (!mainDtsRollupPath) {
353+
// If the mainDtsRollupPath is not specified, then infer it from the package.json file
354+
if (!context.packageJson.typings) {
355+
this._monitoredLogger.logError('Either the "mainDtsRollupPath" setting must be specified,'
356+
+ ' or else the package.json file must contain a "typings" field.');
357+
return false;
358+
}
359+
360+
// Resolve the "typings" field relative to package.json itself
361+
const resolvedTypings: string = path.resolve(context.packageFolder,
362+
context.packageJson.typings);
363+
364+
if (dtsRollup.trimming) {
365+
if (!Path.isUnder(resolvedTypings, dtsRollup.publishFolderForInternal!)) {
366+
this._monitoredLogger.logError('The "mainDtsRollupPath" setting was not specified.'
367+
+ ' In this case, the package.json "typings" field must point to a file under'
368+
+ ' the "publishFolderForInternal": ' + dtsRollup.publishFolderForInternal!);
369+
return false;
370+
}
371+
mainDtsRollupPath = path.relative(dtsRollup.publishFolderForInternal!, resolvedTypings);
372+
} else {
373+
if (!Path.isUnder(resolvedTypings, dtsRollup.publishFolder!)) {
374+
this._monitoredLogger.logError('The "mainDtsRollupPath" setting was not specified.'
375+
+ ' In this case, the package.json "typings" field must point to a file under'
376+
+ ' the "publishFolder": ' + dtsRollup.publishFolder!);
377+
return false;
378+
}
379+
mainDtsRollupPath = path.relative(dtsRollup.publishFolder!, resolvedTypings);
380+
}
381+
382+
this._monitoredLogger.logVerbose(`The "mainDtsRollupPath" setting was inferred'
383+
+ ' from package.json: ${mainDtsRollupPath}`);
384+
} else {
385+
this._monitoredLogger.logVerbose(`The "mainDtsRollupPath" is: ${mainDtsRollupPath}`);
386+
387+
if (!path.isAbsolute(mainDtsRollupPath)) {
388+
this._monitoredLogger.logError('The "mainDtsRollupPath" setting must be a relative path'
389+
+ ' that can be combined with "publishFolderForInternal", "publishFolderForBeta", '
390+
+ 'or "publishFolderForPublic".');
391+
return false;
392+
}
393+
}
394+
344395
const dtsRollupGenerator: DtsRollupGenerator = new DtsRollupGenerator(context);
345396
dtsRollupGenerator.analyze();
346397

347-
this._generateTypingsFile(dtsRollupGenerator,
348-
this.actualConfig.dtsRollup.dtsFilePathForPublic!,
349-
DtsRollupKind.PublicRelease);
398+
if (dtsRollup.trimming) {
399+
this._generateTypingsFile(dtsRollupGenerator,
400+
path.resolve(context.packageFolder, dtsRollup.publishFolderForPublic!, mainDtsRollupPath),
401+
DtsRollupKind.PublicRelease);
350402

351-
this._generateTypingsFile(dtsRollupGenerator,
352-
this.actualConfig.dtsRollup.dtsFilePathForPreview!,
353-
DtsRollupKind.PreviewRelease);
403+
this._generateTypingsFile(dtsRollupGenerator,
404+
path.resolve(context.packageFolder, dtsRollup.publishFolderForBeta!, mainDtsRollupPath),
405+
DtsRollupKind.PreviewRelease);
354406

355-
this._generateTypingsFile(dtsRollupGenerator,
356-
this.actualConfig.dtsRollup.dtsFilePathForInternal!,
357-
DtsRollupKind.InternalRelease);
407+
this._generateTypingsFile(dtsRollupGenerator,
408+
path.resolve(context.packageFolder, dtsRollup.publishFolderForInternal!, mainDtsRollupPath),
409+
DtsRollupKind.InternalRelease);
410+
} else {
411+
this._generateTypingsFile(dtsRollupGenerator,
412+
path.resolve(context.packageFolder, dtsRollup.publishFolder!, mainDtsRollupPath),
413+
DtsRollupKind.InternalRelease); // (no trimming)
414+
}
358415
}
359416

360417
if (this._localBuild) {
@@ -366,16 +423,14 @@ export class Extractor {
366423
}
367424
}
368425

369-
private _generateTypingsFile(dtsRollupGenerator: DtsRollupGenerator,
370-
dtsFilePath: string, dtsKind: DtsRollupKind): void {
371-
const dtsFilename: string = path.resolve(this._absoluteRootFolder,
372-
this.actualConfig.dtsRollup!.outputFolder, dtsFilePath);
426+
private _generateTypingsFile(dtsRollupGenerator: DtsRollupGenerator, mainDtsRollupFullPath: string,
427+
dtsKind: DtsRollupKind): void {
373428

374-
this._monitoredLogger.logVerbose(`Writing package typings: ${dtsFilename}`);
429+
this._monitoredLogger.logVerbose(`Writing package typings: ${mainDtsRollupFullPath}`);
375430

376-
fsx.mkdirsSync(path.dirname(dtsFilename));
431+
fsx.mkdirsSync(path.dirname(mainDtsRollupFullPath));
377432

378-
dtsRollupGenerator.writeTypingsFile(dtsFilename, dtsKind);
433+
dtsRollupGenerator.writeTypingsFile(mainDtsRollupFullPath, dtsKind);
379434
}
380435

381436
private _getShortFilePath(absolutePath: string): string {

‎apps/api-extractor/src/extractor/IExtractorConfig.ts‎

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,12 @@ export interface IExtractorApiJsonFileConfig {
178178
}
179179

180180
/**
181-
* Configures how the package typings (*.d.ts) will be generated.
181+
* Configures how the *.d.ts rollup files will be generated.
182+
*
182183
* @remarks
183184
* API Extractor can generate a single unified *.d.ts file that contains all
184-
* the exported typings for the package entry point. It can also remove
185-
* \@alpha \@beta \@internal definitions depending on the release type.
185+
* the exported typings for the package entry point. It can also trim
186+
* \@alpha, \@beta, and \@internal definitions according to the release type.
186187
*
187188
* @beta
188189
*/
@@ -193,43 +194,64 @@ export interface IExtractorDtsRollupConfig {
193194
enabled: boolean;
194195

195196
/**
196-
* Specifies where the *.d.ts files should be written.
197+
* If "trimming" is false (the default), then a single *.d.ts rollup file will be generated in the
198+
* "publishFolder". If "trimming" is true, then three separate *.d.ts rollups will be
199+
* generated in "publishFolderForInternal", "publishFolderForBeta", and "publishFolderForPublic".
197200
*
198-
* The default value is "./dist"
201+
* @remarks
202+
* In either case, "mainDtsRollupPath" indicates the relative file path.
199203
*/
200-
outputFolder?: string;
204+
trimming?: boolean;
205+
206+
/**
207+
* This setting is only used if "trimming" is false.
208+
* It indicates the folder where "npm publish" will be run. The default value is "./dist".
209+
*/
210+
publishFolder?: string;
201211

202212
/**
203-
* Specifies the *.d.ts file path used for an internal release.
204-
* The default value is "index-internal.d.ts".
213+
* This setting is only used if "trimming" is true.
214+
* It indicates the folder where "npm publish" will be run for an internal release.
215+
* The default value is "./dist/internal".
205216
*
206217
* @remarks
207-
* If the path is not an absolute path, it will be resolved relative to the outputFolder.
208-
* This output file will contain all definitions that are reachable from the entry point.
218+
* An internal release will contain all definitions that are reachable from the entry point.
209219
*/
210-
dtsFilePathForInternal?: string;
220+
publishFolderForInternal?: string;
211221

212222
/**
213-
* Specifies the output filename for a preview release.
214-
* The default value is "index-preview.d.ts".
223+
* This setting is only used if "trimming" is true.
224+
* It indicates the folder where "npm publish" will be run for a beta release.
225+
* The default value is "./dist/beta".
215226
*
216227
* @remarks
217-
* If the path is not an absolute path, it will be resolved relative to the outputFolder.
218-
* This output file will contain all definitions that are reachable from the entry point,
228+
* A beta release will contain all definitions that are reachable from the entry point,
219229
* except definitions marked as \@alpha or \@internal.
220230
*/
221-
dtsFilePathForPreview?: string;
231+
publishFolderForBeta?: string;
222232

223233
/**
224-
* Specifies the output filename for a public release.
225-
* The default value is "index-public.d.ts".
234+
* This setting is only used if "trimming" is true.
235+
* It indicates the folder where "npm publish" will be run for a public release.
236+
* The default value is "./dist/public".
226237
*
227238
* @remarks
228-
* If the path is not an absolute path, it will be resolved relative to the outputFolder.
229-
* This output file will contain all definitions that are reachable from the entry point,
239+
* A public release will contain all definitions that are reachable from the entry point,
230240
* except definitions marked as \@beta, \@alpha, or \@internal.
231241
*/
232-
dtsFilePathForPublic?: string;
242+
publishFolderForPublic?: string;
243+
244+
/**
245+
* Specifies the relative path for the *.d.ts rollup file to be generated for the
246+
* package's main entry point. The default value is an empty string, which causes
247+
* the path to be automatically inferred from the "typings" field of the project's
248+
* package.json file.
249+
*
250+
* @remarks
251+
* If specified, the value must be a relative path that can be combined with one of
252+
* the publish folder settings.
253+
*/
254+
mainDtsRollupPath?: string;
233255
}
234256

235257
/**

‎apps/api-extractor/src/extractor/api-extractor-defaults.json‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,14 @@
2828

2929
"dtsRollup": {
3030
"enabled": false,
31-
"outputFolder": "./dist",
32-
"dtsFilePathForInternal": "index-internal.d.ts",
33-
"dtsFilePathForPreview": "index-preview.d.ts",
34-
"dtsFilePathForPublic": "index-public.d.ts"
31+
"trimming": false,
32+
33+
"publishFolder": "./dist",
34+
35+
"publishFolderForInternal": "./dist/internal",
36+
"publishFolderForBeta": "./dist/beta",
37+
"publishFolderForPublic": "./dist/public",
38+
39+
"mainDtsRollupPath": ""
3540
}
3641
}

‎apps/api-extractor/src/extractor/api-extractor.schema.json‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,27 +129,35 @@
129129
},
130130

131131
"dtsRollup": {
132-
"description": "Configures how the package typings (*.d.ts) will be generated.",
132+
"description": "Configures how the *.d.ts rollup files will be generated.",
133133
"type": "object",
134134
"properties": {
135135
"enabled": {
136136
"description": "Whether to generate package typings. The default is false.",
137137
"type": "boolean"
138138
},
139-
"outputFolder": {
140-
"description": "Specifies where the *.d.ts files should be written. The default value is \"./dist\"",
139+
"trimming": {
140+
"description": "If \"trimming\" is false (the default), then a single *.d.ts rollup file will be generated in the \"publishFolder\". If \"trimming\" is true, then three separate *.d.ts rollups will be generated in \"publishFolderForInternal\", \"publishFolderForBeta\", and \"publishFolderForPublic\".",
141+
"type": "boolean"
142+
},
143+
"publishFolder": {
144+
"description": "This setting is only used if \"trimming\" is false. It indicates the folder where \"npm publish\" will be run. The default value is \"./dist\".",
145+
"type": "string"
146+
},
147+
"publishFolderForInternal": {
148+
"description": "This setting is only used if \"trimming\" is false. It indicates the folder where \"npm publish\" will be run for an internal release. The default value is \"./dist/internal\". An internal release will contain all definitions that are reachable from the entry point.",
141149
"type": "string"
142150
},
143-
"dtsFilePathForInternal": {
144-
"description": "Specifies the output filename for an internal release. The default value is \"index-internal.d.ts\". If the path is not an absolute path, it will be resolved relative to the outputFolder. This file will contain all definitions that are reachable from the entry point.",
151+
"publishFolderForBeta": {
152+
"description": "This setting is only used if \"trimming\" is false. It indicates the folder where \"npm publish\" will be run for a beta release. The default value is \"./dist/beta\". A beta release will contain all definitions that are reachable from the entry point, except definitions marked as @alpha or @internal.",
145153
"type": "string"
146154
},
147-
"dtsFilePathForPreview": {
148-
"description": "Specifies the output filename for a preview release. The default value is \"index-preview.d.ts\". If the path is not an absolute path, it will be resolved relative to the outputFolder. This file will contain all definitions that are reachable from the entry point, except definitions marked as @alpha or @internal",
155+
"publishFolderForPublic": {
156+
"description": "This setting is only used if \"trimming\" is false. It indicates the folder where \"npm publish\" will be run for a public release. The default value is \"./dist/public\". A public release will contain all definitions that are reachable from the entry point, except definitions marked as @beta, @alpha, or @internal.",
149157
"type": "string"
150158
},
151-
"dtsFilePathForPublic": {
152-
"description": "Specifies the output filename for a public release. The default value is \"index-public.d.ts\". If the path is not an absolute path, it will be resolved relative to the outputFolder. This file will contain all definitions that are reachable from the entry point, except definitions marked as @beta, @alpha, or @internal.",
159+
"mainDtsRollupPath": {
160+
"description": "Specifies the relative path for the *.d.ts rollup file to be generated for the package's main entry point. The default value is an empty string, which causes the path to be automatically inferred from the \"typings\" field of the project's package.json file. If specified, the value must be a relative path that can be combined with one of the publish folder settings.",
153161
"type": "string"
154162
}
155163
},

‎common/reviews/api/api-extractor.api.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,13 @@ interface IExtractorConfig {
188188

189189
// @beta
190190
interface IExtractorDtsRollupConfig {
191-
dtsFilePathForInternal?: string;
192-
dtsFilePathForPreview?: string;
193-
dtsFilePathForPublic?: string;
194191
enabled: boolean;
195-
outputFolder?: string;
192+
mainDtsRollupPath?: string;
193+
publishFolder?: string;
194+
publishFolderForBeta?: string;
195+
publishFolderForInternal?: string;
196+
publishFolderForPublic?: string;
197+
trimming?: boolean;
196198
}
197199

198200
// @public

0 commit comments

Comments
 (0)