Skip to content

Commit afda290

Browse files
committed
feat(scripts): remove @internal stripping and enable no deps build needed for api generation
1 parent 131d20e commit afda290

7 files changed

Lines changed: 97 additions & 26 deletions

File tree

scripts/api-extractor/api-extractor.common.v-next.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@
99
},
1010
"dtsRollup": {
1111
"enabled": true,
12-
// TODO: replace following line with these 2 commented ones after all v9 is migrated to new build and .d.ts API stripping
1312
"untrimmedFilePath": "<projectFolder>/dist/index.d.ts",
14-
// "untrimmedFilePath": "<projectFolder>/dist/untrimmed.d.ts",
15-
// "publicTrimmedFilePath": "<projectFolder>/dist/index.d.ts",
1613
"omitTrimmingComments": false
1714
},
1815
"compiler": {

scripts/tasks/api-extractor.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ export function apiExtractor() {
147147
tsConfig,
148148
tsConfigPath,
149149
packageJson,
150+
definitionsRootPath: 'dist/out-tsc/types',
150151
});
151152

152153
config.compiler = compilerConfig;

scripts/tasks/ts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function prepareTsTaskConfig(options: TscTaskOptions) {
3434
if (hasNewCompilationSetup) {
3535
options.outDir = `${tsConfigOutDir}/${options.outDir}`;
3636
} else {
37-
// TODO: remove after all v9 is migrated to new build and .d.ts API stripping
37+
// TODO: remove after all v9 is migrated to new tsc processing
3838
options.baseUrl = '.';
3939
options.rootDir = './src';
4040
}

scripts/tasks/utils.spec.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { getTsPathAliasesApiExtractorConfig } from './utils';
2+
3+
type DeepPartial<T> = Partial<{ [P in keyof T]: DeepPartial<T[P]> }>;
4+
describe(`utils`, () => {
5+
describe(`#getTsPathAliasesApiExtractorConfig`, () => {
6+
type Options = Parameters<typeof getTsPathAliasesApiExtractorConfig>[0];
7+
function setup(options: DeepPartial<Options> = {}) {
8+
const defaults = {
9+
tsConfig: {
10+
compilerOptions: {
11+
outDir: '../../dist/out-tsc',
12+
...options.tsConfig?.compilerOptions,
13+
},
14+
},
15+
tsConfigPath: 'tsconfig.lib.json',
16+
packageJson: {
17+
name: '@proj/one',
18+
version: '0.0.1',
19+
main: 'lib/index.js',
20+
dependencies: { ...options.packageJson?.dependencies },
21+
peerDependencies: { ...options.packageJson?.peerDependencies },
22+
},
23+
definitionsRootPath: options.definitionsRootPath ?? 'dist/types',
24+
};
25+
26+
return getTsPathAliasesApiExtractorConfig(defaults as Options);
27+
}
28+
29+
it(`should set compilerOptions`, () => {
30+
const actual = setup();
31+
32+
expect(actual.overrideTsconfig.compilerOptions).toEqual(
33+
expect.objectContaining({ isolatedModules: false, skipLibCheck: false }),
34+
);
35+
});
36+
37+
it(`should override path aliases to emitted declaration files instead of source files`, () => {
38+
const actual = setup({ definitionsRootPath: 'dist/for/types' });
39+
40+
const newPaths = (actual.overrideTsconfig.compilerOptions.paths as unknown) as Record<string, string[]>;
41+
42+
const newPath = Object.values(newPaths)[0][0];
43+
expect(newPath).toMatch(new RegExp('^dist/for/types.+src/index.d.ts$', 'i'));
44+
});
45+
46+
it(`should set allowSyntheticDefaultImports if package has invalid deps/peerDeps`, () => {
47+
const actual = setup({ packageJson: { dependencies: { '@storybook/api': '6.5.0' } } });
48+
expect(actual.overrideTsconfig.compilerOptions).toEqual(
49+
expect.objectContaining({ allowSyntheticDefaultImports: true }),
50+
);
51+
});
52+
});
53+
});

scripts/tasks/utils.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,42 @@ function enableAllowSyntheticDefaultImports(options: { pkgJson: PackageJson }) {
3939
return shouldEnable ? { allowSyntheticDefaultImports: true } : null;
4040
}
4141

42+
const rootTsConfig = JSON.parse(
43+
fs.readFileSync(path.resolve(__dirname, '../../tsconfig.base.json'), 'utf-8'),
44+
) as TsConfig;
45+
46+
function createNormalizedTsPaths(options: { definitionsRootPath: string; rootTsConfig: TsConfig }) {
47+
const paths = (options.rootTsConfig.compilerOptions.paths as unknown) as Record<string, string[]>;
48+
49+
const normalizedPaths = Object.entries(paths).reduce((acc, [pkgName, pathAliases]) => {
50+
acc[pkgName] = [path.join(options.definitionsRootPath, pathAliases[0].replace('index.ts', 'index.d.ts'))];
51+
return acc;
52+
}, {} as typeof paths);
53+
54+
return normalizedPaths;
55+
}
56+
4257
export function getTsPathAliasesApiExtractorConfig(options: {
4358
tsConfig: TsConfig;
4459
tsConfigPath: string;
4560
packageJson: PackageJson;
61+
definitionsRootPath: string;
4662
}) {
63+
const hasNewCompilationSetup = ((options.tsConfig.compilerOptions as unknown) as { outDir: string }).outDir.includes(
64+
'dist/out-tsc',
65+
);
66+
// TODO: after all v9 is migrated to new tsc processing use only createNormalizedTsPaths
67+
const normalizedPaths = hasNewCompilationSetup
68+
? createNormalizedTsPaths({ definitionsRootPath: options.definitionsRootPath, rootTsConfig })
69+
: undefined;
70+
4771
/**
4872
* Customized TSConfig that uses `tsconfig.lib.json` as base with some required overrides:
4973
*
5074
* NOTES:
5175
* - `extends` is properly resolved via api-extractor which uses TS api
5276
* - `skipLibCheck` needs to be explicitly set to `false` so errors propagate to api-extractor
53-
* - `paths` is set to `undefined` so api-extractor won't use source files rather rollup-ed declaration files only
77+
* - `paths` is overriden to path mapping that points to generated declaration files. This also enables creation of dts rollup without a need of generating rollups for all dependencies 🫡
5478
*
5579
*/
5680
const apiExtractorTsConfig: TsConfig = {
@@ -71,7 +95,8 @@ export function getTsPathAliasesApiExtractorConfig(options: {
7195
/**
7296
* just-scripts provides invalid types for tsconfig, thus `paths` cannot be set to dictionary,nor null or `{}`
7397
*/
74-
paths: undefined,
98+
// @ts-expect-error - just-scripts provides invalid types
99+
paths: normalizedPaths,
75100
},
76101
};
77102

tools/generators/migrate-converged-pkg/index.spec.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -853,11 +853,6 @@ describe('migrate-converged-pkg generator', () => {
853853
expect(readJson(tree, apiExtractorConfigPath)).toMatchInlineSnapshot(`
854854
Object {
855855
"$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
856-
"dtsRollup": Object {
857-
"enabled": true,
858-
"publicTrimmedFilePath": "<projectFolder>/dist/index.d.ts",
859-
"untrimmedFilePath": "",
860-
},
861856
"extends": "@fluentui/scripts/api-extractor/api-extractor.common.v-next.json",
862857
}
863858
`);
@@ -1493,8 +1488,7 @@ describe('migrate-converged-pkg generator', () => {
14931488
},
14941489
"dtsRollup": Object {
14951490
"enabled": true,
1496-
"publicTrimmedFilePath": "<projectFolder>/dist/unstable.d.ts",
1497-
"untrimmedFilePath": "<projectFolder>/dist/unstable-untrimmed.d.ts",
1491+
"untrimmedFilePath": "<projectFolder>/dist/unstable.d.ts",
14981492
},
14991493
"extends": "@fluentui/scripts/api-extractor/api-extractor.common.v-next.json",
15001494
"mainEntryPointFilePath": "<projectFolder>/../../../dist/out-tsc/types/packages/react-components/<unscopedPackageName>/src/unstable/index.d.ts",

tools/generators/migrate-converged-pkg/index.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ function runMigrationOnProject(tree: Tree, schema: AssertedSchema, _userLog: Use
130130

131131
// update package npm scripts
132132
updatePackageJson(tree, optionsWithTsConfigs);
133-
updateApiExtractorForLocalBuilds(tree, optionsWithTsConfigs);
133+
updateApiExtractor(tree, optionsWithTsConfigs);
134134

135135
// setup storybook
136136
setupStorybook(tree, options);
@@ -155,12 +155,6 @@ const templates = {
155155
main: {
156156
$schema: 'https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json',
157157
extends: '@fluentui/scripts/api-extractor/api-extractor.common.v-next.json',
158-
// TODO: remove after all v9 is migrated to new build and .d.ts API stripping
159-
dtsRollup: {
160-
enabled: true,
161-
untrimmedFilePath: '',
162-
publicTrimmedFilePath: '<projectFolder>/dist/index.d.ts',
163-
},
164158
},
165159
unstable: {
166160
$schema: 'https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json',
@@ -174,8 +168,7 @@ const templates = {
174168
},
175169
dtsRollup: {
176170
enabled: true,
177-
untrimmedFilePath: '<projectFolder>/dist/unstable-untrimmed.d.ts',
178-
publicTrimmedFilePath: '<projectFolder>/dist/unstable.d.ts',
171+
untrimmedFilePath: '<projectFolder>/dist/unstable.d.ts',
179172
},
180173
},
181174
};
@@ -576,11 +569,11 @@ function setupUnstableApi(tree: Tree, options: NormalizedSchemaWithTsConfigs) {
576569
}
577570

578571
updateUnstablePackageJson();
579-
updateUnstableApiExtractorForLocalBuilds();
572+
updateUnstableApiExtractor();
580573

581574
return tree;
582575

583-
function updateUnstableApiExtractorForLocalBuilds() {
576+
function updateUnstableApiExtractor() {
584577
const apiExtractor = templates.apiExtractor();
585578

586579
writeJson(tree, joinPathFragments(options.paths.configRoot, 'api-extractor.unstable.json'), apiExtractor.unstable);
@@ -646,7 +639,6 @@ function updatePackageJson(tree: Tree, options: NormalizedSchemaWithTsConfigs) {
646639

647640
function setupScripts(json: PackageJson) {
648641
const scripts = {
649-
'generate-api': 'tsc -p ./tsconfig.lib.json --emitDeclarationOnly && just-scripts api-extractor',
650642
test: 'jest --passWithNoTests',
651643
'type-check': 'tsc -b tsconfig.json',
652644
};
@@ -686,12 +678,21 @@ function updatePackageJson(tree: Tree, options: NormalizedSchemaWithTsConfigs) {
686678
}
687679
}
688680

689-
function updateApiExtractorForLocalBuilds(tree: Tree, options: NormalizedSchemaWithTsConfigs) {
681+
function updateApiExtractor(tree: Tree, options: NormalizedSchemaWithTsConfigs) {
690682
const apiExtractor = templates.apiExtractor();
683+
const scripts = {
684+
'generate-api': 'tsc -p ./tsconfig.lib.json --emitDeclarationOnly && just-scripts api-extractor',
685+
};
691686

692687
tree.delete(joinPathFragments(options.paths.configRoot, 'api-extractor.local.json'));
693688
writeJson(tree, joinPathFragments(options.paths.configRoot, 'api-extractor.json'), apiExtractor.main);
694689

690+
updateJson(tree, options.paths.packageJson, (json: PackageJson) => {
691+
Object.assign(json.scripts, scripts);
692+
693+
return json;
694+
});
695+
695696
return tree;
696697
}
697698

0 commit comments

Comments
 (0)