message: cli: allow optimized type definition builds across packages
diff: @@ -71,20 +71,29 @@ function prepareApiExtractor() {
  return apiExtractor!;
  }
  -export async function buildTypeDefinitions() {
  - const { Extractor, ExtractorConfig } = prepareApiExtractor();
  +export async function buildTypeDefinitions(
  + targetDirs: string[] = [paths.targetDir],
  +) {
  + const { Extractor, ExtractorConfig, CompilerState } = prepareApiExtractor();
  - const distTypesPackageDir = paths.resolveTargetRoot(
  - 'dist-types',
  - relativePath(paths.targetRoot, paths.targetDir),
  + const packageDirs = targetDirs.map(dir =>
  + relativePath(paths.targetRoot, dir),
  + );
  + const entryPoints = packageDirs.map(dir =>
  + paths.resolveTargetRoot('dist-types', dir, 'src/index.d.ts'),
  );
  - const entryPoint = resolvePath(distTypesPackageDir, 'src/index.d.ts');
  +
  + let compilerState;
  +
  + for (const packageDir of packageDirs) {
  + const targetDir = paths.resolveTargetRoot(packageDir);
  + const targetTypesDir = paths.resolveTargetRoot('dist-types', packageDir);
  + const entryPoint = resolvePath(targetTypesDir, 'src/index.d.ts');
  const declarationsExist = await fs.pathExists(entryPoint);
  if (!declarationsExist) {
  - const path = relativePath(paths.targetDir, entryPoint);
  throw new Error(
  - `No declaration files found at ${path}, be sure to run ${chalk.bgRed.white(
  + `No declaration files found at ${entryPoint}, be sure to run ${chalk.bgRed.white(
  'yarn tsc',
  )} to generate .d.ts files before packaging`,
  );
  @@ -102,23 +111,30 @@ export async function buildTypeDefinitions() {
  dtsRollup: {
  enabled: true,
  - untrimmedFilePath: paths.resolveTarget('dist/index.alpha.d.ts'),
  - betaTrimmedFilePath: paths.resolveTarget('dist/index.beta.d.ts'),
  - publicTrimmedFilePath: paths.resolveTarget('dist/index.d.ts'),
  + untrimmedFilePath: resolvePath(targetDir, 'dist/index.alpha.d.ts'),
  + betaTrimmedFilePath: resolvePath(targetDir, 'dist/index.beta.d.ts'),
  + publicTrimmedFilePath: resolvePath(targetDir, 'dist/index.d.ts'),
  },
  newlineKind: 'lf',
  - projectFolder: paths.targetDir,
  + projectFolder: targetDir,
  },
  - configObjectFullPath: paths.targetDir,
  - packageJsonFullPath: paths.resolveTarget('package.json'),
  + configObjectFullPath: targetDir,
  + packageJsonFullPath: resolvePath(targetDir, 'package.json'),
  });
  + if (!compilerState) {
  + compilerState = CompilerState.create(extractorConfig, {
  + additionalEntryPoints: entryPoints,
  + });
  + }
  +
  const typescriptDir = paths.resolveTargetRoot('node_modules/typescript');
  const hasTypescript = await fs.pathExists(typescriptDir);
  const extractorResult = Extractor.invoke(extractorConfig, {
  typescriptCompilerFolder: hasTypescript ? typescriptDir : undefined,
  + compilerState,
  localBuild: false,
  showVerboseMessages: false,
  showDiagnostics: false,
  @@ -131,7 +147,7 @@ export async function buildTypeDefinitions() {
  let text = `${message.text} (${message.messageId})`;
  if (message.sourceFilePath) {
  text += ' at ';
  - text += relativePath(distTypesPackageDir, message.sourceFilePath);
  + text += relativePath(targetTypesDir, message.sourceFilePath);
  if (message.sourceFileLine) {
  text += `:${message.sourceFileLine}`;
  if (message.sourceFileColumn) {
  @@ -159,3 +175,4 @@ export async function buildTypeDefinitions() {
  );
  }
  }
  +}
  