-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(scripts): remove @internal stripping and enable no deps build needed for api generation #25575
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { getTsPathAliasesApiExtractorConfig } from './utils'; | ||
|
||
type DeepPartial<T> = Partial<{ [P in keyof T]: DeepPartial<T[P]> }>; | ||
describe(`utils`, () => { | ||
describe(`#getTsPathAliasesApiExtractorConfig`, () => { | ||
type Options = Parameters<typeof getTsPathAliasesApiExtractorConfig>[0]; | ||
function setup(options: DeepPartial<Options> = {}) { | ||
const defaults = { | ||
tsConfig: { | ||
compilerOptions: { | ||
outDir: '../../dist/out-tsc', | ||
...options.tsConfig?.compilerOptions, | ||
}, | ||
}, | ||
tsConfigPath: 'tsconfig.lib.json', | ||
packageJson: { | ||
name: '@proj/one', | ||
version: '0.0.1', | ||
main: 'lib/index.js', | ||
dependencies: { ...options.packageJson?.dependencies }, | ||
peerDependencies: { ...options.packageJson?.peerDependencies }, | ||
}, | ||
definitionsRootPath: options.definitionsRootPath ?? 'dist/types', | ||
}; | ||
|
||
return getTsPathAliasesApiExtractorConfig(defaults as Options); | ||
} | ||
|
||
it(`should set compilerOptions`, () => { | ||
const actual = setup(); | ||
|
||
expect(actual.overrideTsconfig.compilerOptions).toEqual( | ||
expect.objectContaining({ isolatedModules: false, skipLibCheck: false }), | ||
); | ||
}); | ||
|
||
it(`should override path aliases to emitted declaration files instead of source files`, () => { | ||
const actual = setup({ definitionsRootPath: 'dist/for/types' }); | ||
|
||
const newPaths = (actual.overrideTsconfig.compilerOptions.paths as unknown) as Record<string, string[]>; | ||
|
||
const newPath = Object.values(newPaths)[0][0]; | ||
expect(newPath).toMatch(new RegExp('^dist/for/types.+src/index.d.ts$', 'i')); | ||
}); | ||
|
||
it(`should set allowSyntheticDefaultImports if package has invalid deps/peerDeps`, () => { | ||
const actual = setup({ packageJson: { dependencies: { '@storybook/api': '6.5.0' } } }); | ||
expect(actual.overrideTsconfig.compilerOptions).toEqual( | ||
expect.objectContaining({ allowSyntheticDefaultImports: true }), | ||
); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,18 +39,42 @@ function enableAllowSyntheticDefaultImports(options: { pkgJson: PackageJson }) { | |
return shouldEnable ? { allowSyntheticDefaultImports: true } : null; | ||
} | ||
|
||
const rootTsConfig = JSON.parse( | ||
fs.readFileSync(path.resolve(__dirname, '../../tsconfig.base.json'), 'utf-8'), | ||
) as TsConfig; | ||
|
||
function createNormalizedTsPaths(options: { definitionsRootPath: string; rootTsConfig: TsConfig }) { | ||
const paths = (options.rootTsConfig.compilerOptions.paths as unknown) as Record<string, string[]>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
const normalizedPaths = Object.entries(paths).reduce((acc, [pkgName, pathAliases]) => { | ||
acc[pkgName] = [path.join(options.definitionsRootPath, pathAliases[0].replace('index.ts', 'index.d.ts'))]; | ||
return acc; | ||
}, {} as typeof paths); | ||
|
||
return normalizedPaths; | ||
} | ||
|
||
export function getTsPathAliasesApiExtractorConfig(options: { | ||
tsConfig: TsConfig; | ||
tsConfigPath: string; | ||
packageJson: PackageJson; | ||
definitionsRootPath: string; | ||
}) { | ||
const hasNewCompilationSetup = ((options.tsConfig.compilerOptions as unknown) as { outDir: string }).outDir.includes( | ||
'dist/out-tsc', | ||
); | ||
// TODO: after all v9 is migrated to new tsc processing use only createNormalizedTsPaths | ||
const normalizedPaths = hasNewCompilationSetup | ||
? createNormalizedTsPaths({ definitionsRootPath: options.definitionsRootPath, rootTsConfig }) | ||
: undefined; | ||
|
||
/** | ||
* Customized TSConfig that uses `tsconfig.lib.json` as base with some required overrides: | ||
* | ||
* NOTES: | ||
* - `extends` is properly resolved via api-extractor which uses TS api | ||
* - `skipLibCheck` needs to be explicitly set to `false` so errors propagate to api-extractor | ||
* - `paths` is set to `undefined` so api-extractor won't use source files rather rollup-ed declaration files only | ||
* - `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 🫡 | ||
* | ||
*/ | ||
const apiExtractorTsConfig: TsConfig = { | ||
|
@@ -71,7 +95,8 @@ export function getTsPathAliasesApiExtractorConfig(options: { | |
/** | ||
* just-scripts provides invalid types for tsconfig, thus `paths` cannot be set to dictionary,nor null or `{}` | ||
*/ | ||
paths: undefined, | ||
// @ts-expect-error - just-scripts provides invalid types | ||
paths: normalizedPaths, | ||
}, | ||
}; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -130,7 +130,7 @@ function runMigrationOnProject(tree: Tree, schema: AssertedSchema, _userLog: Use | |
|
||
// update package npm scripts | ||
updatePackageJson(tree, optionsWithTsConfigs); | ||
updateApiExtractorForLocalBuilds(tree, optionsWithTsConfigs); | ||
updateApiExtractor(tree, optionsWithTsConfigs); | ||
|
||
// setup storybook | ||
setupStorybook(tree, options); | ||
|
@@ -155,12 +155,6 @@ const templates = { | |
main: { | ||
$schema: 'https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json', | ||
extends: '@fluentui/scripts/api-extractor/api-extractor.common.v-next.json', | ||
// TODO: remove after all v9 is migrated to new build and .d.ts API stripping | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we wont stript |
||
dtsRollup: { | ||
enabled: true, | ||
untrimmedFilePath: '', | ||
publicTrimmedFilePath: '<projectFolder>/dist/index.d.ts', | ||
}, | ||
}, | ||
unstable: { | ||
$schema: 'https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json', | ||
|
@@ -174,8 +168,7 @@ const templates = { | |
}, | ||
dtsRollup: { | ||
enabled: true, | ||
untrimmedFilePath: '<projectFolder>/dist/unstable-untrimmed.d.ts', | ||
publicTrimmedFilePath: '<projectFolder>/dist/unstable.d.ts', | ||
untrimmedFilePath: '<projectFolder>/dist/unstable.d.ts', | ||
}, | ||
}, | ||
}; | ||
|
@@ -576,11 +569,11 @@ function setupUnstableApi(tree: Tree, options: NormalizedSchemaWithTsConfigs) { | |
} | ||
|
||
updateUnstablePackageJson(); | ||
updateUnstableApiExtractorForLocalBuilds(); | ||
updateUnstableApiExtractor(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove |
||
|
||
return tree; | ||
|
||
function updateUnstableApiExtractorForLocalBuilds() { | ||
function updateUnstableApiExtractor() { | ||
const apiExtractor = templates.apiExtractor(); | ||
|
||
writeJson(tree, joinPathFragments(options.paths.configRoot, 'api-extractor.unstable.json'), apiExtractor.unstable); | ||
|
@@ -646,7 +639,6 @@ function updatePackageJson(tree: Tree, options: NormalizedSchemaWithTsConfigs) { | |
|
||
function setupScripts(json: PackageJson) { | ||
const scripts = { | ||
'generate-api': 'tsc -p ./tsconfig.lib.json --emitDeclarationOnly && just-scripts api-extractor', | ||
test: 'jest --passWithNoTests', | ||
'type-check': 'tsc -b tsconfig.json', | ||
}; | ||
|
@@ -686,12 +678,21 @@ function updatePackageJson(tree: Tree, options: NormalizedSchemaWithTsConfigs) { | |
} | ||
} | ||
|
||
function updateApiExtractorForLocalBuilds(tree: Tree, options: NormalizedSchemaWithTsConfigs) { | ||
function updateApiExtractor(tree: Tree, options: NormalizedSchemaWithTsConfigs) { | ||
const apiExtractor = templates.apiExtractor(); | ||
const scripts = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. collocate to proper boundary so its possible to run only particular migrations if needed |
||
'generate-api': 'tsc -p ./tsconfig.lib.json --emitDeclarationOnly && just-scripts api-extractor', | ||
}; | ||
|
||
tree.delete(joinPathFragments(options.paths.configRoot, 'api-extractor.local.json')); | ||
writeJson(tree, joinPathFragments(options.paths.configRoot, 'api-extractor.json'), apiExtractor.main); | ||
|
||
updateJson(tree, options.paths.packageJson, (json: PackageJson) => { | ||
Object.assign(json.scripts, scripts); | ||
|
||
return json; | ||
}); | ||
|
||
return tree; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we wont strip
@internal
apis thus removing