Skip to content
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(js): warn users when additionalEntryPoints do not match any files #19107

Merged
merged 1 commit into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions packages/js/src/executors/tsc/tsc.batch-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
22 changes: 9 additions & 13 deletions packages/js/src/executors/tsc/tsc.impl.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand All @@ -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;
1 change: 1 addition & 0 deletions packages/js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
22 changes: 22 additions & 0 deletions packages/js/src/utils/package-json/create-entry-points.ts
Original file line number Diff line number Diff line change
@@ -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;
}
16 changes: 5 additions & 11 deletions packages/rollup/src/executors/rollup/lib/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -47,7 +48,10 @@ export function normalizeRollupExecutorOptions(
projectRoot,
outputPath,
skipTypeCheck: options.skipTypeCheck || false,
additionalEntryPoints: createEntryPoints(options, context),
additionalEntryPoints: createEntryPoints(
options.additionalEntryPoints,
context.root
),
};
}

Expand Down Expand Up @@ -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,
});
}