From 133759d5a8604557d4593ba29c98d05ae959ca51 Mon Sep 17 00:00:00 2001 From: Jack Hsu Date: Mon, 11 Sep 2023 10:11:42 -0400 Subject: [PATCH] feat(js): warn users when additionalEntryPoints do not match any files --- .../js/src/executors/tsc/tsc.batch-impl.ts | 16 +++++++++----- packages/js/src/executors/tsc/tsc.impl.ts | 22 ++++++++----------- packages/js/src/index.ts | 1 + .../utils/package-json/create-entry-points.ts | 22 +++++++++++++++++++ .../src/executors/rollup/lib/normalize.ts | 16 +++++--------- 5 files changed, 47 insertions(+), 30 deletions(-) create mode 100644 packages/js/src/utils/package-json/create-entry-points.ts diff --git a/packages/js/src/executors/tsc/tsc.batch-impl.ts b/packages/js/src/executors/tsc/tsc.batch-impl.ts index 5c965644796cba..4e5b48af7cbae2 100644 --- a/packages/js/src/executors/tsc/tsc.batch-impl.ts +++ b/packages/js/src/executors/tsc/tsc.batch-impl.ts @@ -4,10 +4,7 @@ import type { BatchExecutorTaskResult } from 'nx/src/config/misc-interfaces'; import { getLastValueFromAsyncIterableIterator } from 'nx/src/utils/async-iterator'; import { updatePackageJson } from '../../utils/package-json/update-package-json'; import type { ExecutorOptions } from '../../utils/schema'; -import { - createEntryPoints, - determineModuleFormatFromTsConfig, -} from './tsc.impl'; +import { determineModuleFormatFromTsConfig } from './tsc.impl'; import { TypescripCompilationLogger, TypescriptCompilationResult, @@ -23,6 +20,7 @@ import { watchTaskProjectsFileChangesForAssets, watchTaskProjectsPackageJsonFileChanges, } from './lib/batch'; +import { createEntryPoints } from '../../utils/package-json/create-entry-points'; export async function* tscBatchExecutor( taskGraph: TaskGraph, @@ -87,7 +85,10 @@ export async function* tscBatchExecutor( updatePackageJson( { ...taskInfo.options, - additionalEntryPoints: createEntryPoints(taskInfo.options, context), + additionalEntryPoints: createEntryPoints( + taskInfo.options.additionalEntryPoints, + context.root + ), format: [determineModuleFormatFromTsConfig(tsConfig)], // As long as d.ts files match their .js counterparts, we don't need to emit them. // TSC can match them correctly based on file names. @@ -127,7 +128,10 @@ export async function* tscBatchExecutor( updatePackageJson( { ...t.options, - additionalEntryPoints: createEntryPoints(t.options, context), + additionalEntryPoints: createEntryPoints( + t.options.additionalEntryPoints, + context.root + ), format: [determineModuleFormatFromTsConfig(t.options.tsConfig)], // As long as d.ts files match their .js counterparts, we don't need to emit them. // TSC can match them correctly based on file names. diff --git a/packages/js/src/executors/tsc/tsc.impl.ts b/packages/js/src/executors/tsc/tsc.impl.ts index 7c167ad3df3382..5da8e71ecc56cc 100644 --- a/packages/js/src/executors/tsc/tsc.impl.ts +++ b/packages/js/src/executors/tsc/tsc.impl.ts @@ -1,5 +1,4 @@ import * as ts from 'typescript'; -import { sync as globSync } from 'fast-glob'; import { ExecutorContext } from '@nx/devkit'; import type { TypeScriptCompilationOptions } from '@nx/workspace/src/utilities/typescript/compilation'; import { CopyAssetsHandler } from '../../utils/assets/copy-assets-handler'; @@ -19,6 +18,7 @@ import { compileTypeScriptFiles } from '../../utils/typescript/compile-typescrip import { watchForSingleFileChanges } from '../../utils/watch-for-single-file-changes'; import { getCustomTrasformersFactory, normalizeOptions } from './lib'; import { readTsConfig } from '../../utils/typescript/ts-config'; +import { createEntryPoints } from '../../utils/package-json/create-entry-points'; export function determineModuleFormatFromTsConfig( absolutePathToTsConfig: string @@ -112,7 +112,10 @@ export async function* tscExecutor( updatePackageJson( { ...options, - additionalEntryPoints: createEntryPoints(options, context), + additionalEntryPoints: createEntryPoints( + options.additionalEntryPoints, + context.root + ), format: [determineModuleFormatFromTsConfig(options.tsConfig)], // As long as d.ts files match their .js counterparts, we don't need to emit them. // TSC can match them correctly based on file names. @@ -141,7 +144,10 @@ export async function* tscExecutor( updatePackageJson( { ...options, - additionalEntryPoints: createEntryPoints(options, context), + additionalEntryPoints: createEntryPoints( + options.additionalEntryPoints, + context.root + ), // As long as d.ts files match their .js counterparts, we don't need to emit them. // TSC can match them correctly based on file names. skipTypings: true, @@ -165,14 +171,4 @@ export async function* tscExecutor( return yield* typescriptCompilation.iterator; } -export function createEntryPoints( - options: { additionalEntryPoints?: string[] }, - context: ExecutorContext -): string[] { - if (!options.additionalEntryPoints?.length) return []; - return globSync(options.additionalEntryPoints, { - cwd: context.root, - }); -} - export default tscExecutor; diff --git a/packages/js/src/index.ts b/packages/js/src/index.ts index b5233e45bd1a9e..3664ecd10ae922 100644 --- a/packages/js/src/index.ts +++ b/packages/js/src/index.ts @@ -10,6 +10,7 @@ export * from './utils/typescript/ast-utils'; export * from './utils/package-json'; export * from './utils/assets'; export * from './utils/package-json/update-package-json'; +export * from './utils/package-json/create-entry-points'; export { libraryGenerator } from './generators/library/library'; export { initGenerator } from './generators/init/init'; diff --git a/packages/js/src/utils/package-json/create-entry-points.ts b/packages/js/src/utils/package-json/create-entry-points.ts new file mode 100644 index 00000000000000..6f907aa92760db --- /dev/null +++ b/packages/js/src/utils/package-json/create-entry-points.ts @@ -0,0 +1,22 @@ +import { sync as globSync } from 'fast-glob'; +import { logger } from '@nx/devkit'; + +export function createEntryPoints( + additionalEntryPoints: undefined | string[], + root: string +): string[] { + if (!additionalEntryPoints?.length) return []; + const files = []; + // NOTE: calling globSync for each pattern is slower than calling it all at once. + // We're doing it this way in order to show a warning for unmatched patterns. + // If a pattern is unmatched, it is very likely a mistake by the user. + // Performance impact should be negligible since there shouldn't be that many entry points. + // Benchmarks show only 1-3% difference in execution time. + for (const pattern of additionalEntryPoints) { + const matched = globSync([pattern], { cwd: root }); + if (!matched.length) + logger.warn(`The pattern ${pattern} did not match any files.`); + files.push(...matched); + } + return files; +} diff --git a/packages/rollup/src/executors/rollup/lib/normalize.ts b/packages/rollup/src/executors/rollup/lib/normalize.ts index 2ce057633a2b75..653d81f069bdc2 100644 --- a/packages/rollup/src/executors/rollup/lib/normalize.ts +++ b/packages/rollup/src/executors/rollup/lib/normalize.ts @@ -4,6 +4,7 @@ import { statSync } from 'fs'; import { ExecutorContext, normalizePath } from '@nx/devkit'; import type { AssetGlobPattern, RollupExecutorOptions } from '../schema'; +import { createEntryPoints } from '@nx/js'; export interface NormalizedRollupExecutorOptions extends RollupExecutorOptions { entryRoot: string; @@ -47,7 +48,10 @@ export function normalizeRollupExecutorOptions( projectRoot, outputPath, skipTypeCheck: options.skipTypeCheck || false, - additionalEntryPoints: createEntryPoints(options, context), + additionalEntryPoints: createEntryPoints( + options.additionalEntryPoints, + context.root + ), }; } @@ -108,13 +112,3 @@ export function normalizeAssets( } }); } - -function createEntryPoints( - options: { additionalEntryPoints?: string[] }, - context: ExecutorContext -): string[] { - if (!options.additionalEntryPoints?.length) return []; - return globSync(options.additionalEntryPoints, { - cwd: context.root, - }); -}