Skip to content

Commit 18ba315

Browse files
authored
feat(js): add plugin option to skip build checks when inferring the build task (#32358)
Add a new option `skipBuildCheck` to the `@nx/js/typescript` plugin build options to allow skipping the check for a buildable setup and always infer a build task.
1 parent 42ec4d9 commit 18ba315

2 files changed

Lines changed: 90 additions & 6 deletions

File tree

packages/js/src/plugins/typescript/plugin.spec.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5395,6 +5395,80 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
53955395
`);
53965396
});
53975397

5398+
it('should consider a package buildable when main points to source file when skipBuildCheck is true', async () => {
5399+
await applyFilesToTempFsAndContext(tempFs, context, {
5400+
'libs/my-lib/tsconfig.json': '{}',
5401+
'libs/my-lib/tsconfig.lib.json': JSON.stringify({
5402+
compilerOptions: { outDir: 'dist' },
5403+
include: ['src/**/*.ts'],
5404+
}),
5405+
'libs/my-lib/package.json': JSON.stringify({
5406+
main: 'src/index.ts',
5407+
}),
5408+
'libs/my-lib/src/index.ts': 'export const hello = "world";',
5409+
});
5410+
5411+
expect(
5412+
await invokeCreateNodesOnMatchingFiles(context, {
5413+
typecheck: false,
5414+
build: {
5415+
skipBuildCheck: true,
5416+
},
5417+
})
5418+
).toMatchInlineSnapshot(`
5419+
{
5420+
"projects": {
5421+
"libs/my-lib": {
5422+
"projectType": "library",
5423+
"targets": {
5424+
"build": {
5425+
"cache": true,
5426+
"command": "tsc --build tsconfig.lib.json",
5427+
"dependsOn": [
5428+
"^build",
5429+
],
5430+
"inputs": [
5431+
"{projectRoot}/package.json",
5432+
"{projectRoot}/tsconfig.lib.json",
5433+
"{projectRoot}/src/**/*.ts",
5434+
"^production",
5435+
{
5436+
"externalDependencies": [
5437+
"typescript",
5438+
],
5439+
},
5440+
],
5441+
"metadata": {
5442+
"description": "Builds the project with \`tsc\`.",
5443+
"help": {
5444+
"command": "npx tsc --build --help",
5445+
"example": {
5446+
"args": [
5447+
"--force",
5448+
],
5449+
},
5450+
},
5451+
"technologies": [
5452+
"typescript",
5453+
],
5454+
},
5455+
"options": {
5456+
"cwd": "libs/my-lib",
5457+
},
5458+
"outputs": [
5459+
"{projectRoot}/dist",
5460+
],
5461+
"syncGenerators": [
5462+
"@nx/js:typescript-sync",
5463+
],
5464+
},
5465+
},
5466+
},
5467+
},
5468+
}
5469+
`);
5470+
});
5471+
53985472
it('should handle relative paths correctly when main points to transpiled output', async () => {
53995473
await applyFilesToTempFsAndContext(tempFs, context, {
54005474
'libs/my-lib/tsconfig.json': '{}',

packages/js/src/plugins/typescript/plugin.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export interface TscPluginOptions {
6161
configName?: string;
6262
buildDepsName?: string;
6363
watchDepsName?: string;
64+
skipBuildCheck?: boolean;
6465
};
6566
verboseOutput?: boolean;
6667
}
@@ -78,6 +79,7 @@ interface NormalizedPluginOptions {
7879
configName: string;
7980
buildDepsName?: string;
8081
watchDepsName?: string;
82+
skipBuildCheck?: boolean;
8183
};
8284
verboseOutput: boolean;
8385
}
@@ -469,11 +471,12 @@ function buildTscTargets(
469471
);
470472
if (
471473
context.configFiles.some((f) => f === buildConfigPath) &&
472-
isValidPackageJsonBuildConfig(
473-
retrieveTsConfigFromCache(buildConfigPath, context.workspaceRoot),
474-
context.workspaceRoot,
475-
projectRoot
476-
)
474+
(options.build.skipBuildCheck ||
475+
isValidPackageJsonBuildConfig(
476+
retrieveTsConfigFromCache(buildConfigPath, context.workspaceRoot),
477+
context.workspaceRoot,
478+
projectRoot
479+
))
477480
) {
478481
dependsOn.unshift(options.build.targetName);
479482
}
@@ -519,7 +522,12 @@ function buildTscTargets(
519522
if (
520523
options.build &&
521524
basename(configFilePath) === options.build.configName &&
522-
isValidPackageJsonBuildConfig(tsConfig, context.workspaceRoot, projectRoot)
525+
(options.build.skipBuildCheck ||
526+
isValidPackageJsonBuildConfig(
527+
tsConfig,
528+
context.workspaceRoot,
529+
projectRoot
530+
))
523531
) {
524532
internalProjectReferences ??= resolveInternalProjectReferences(
525533
tsConfig,
@@ -1302,6 +1310,7 @@ function normalizePluginOptions(
13021310
configName: defaultBuildConfigName,
13031311
buildDepsName: 'build-deps',
13041312
watchDepsName: 'watch-deps',
1313+
skipBuildCheck: false,
13051314
};
13061315
// Build target is not enabled by default
13071316
if (!pluginOptions.build) {
@@ -1312,6 +1321,7 @@ function normalizePluginOptions(
13121321
configName: pluginOptions.build.configName ?? defaultBuildConfigName,
13131322
buildDepsName: pluginOptions.build.buildDepsName ?? 'build-deps',
13141323
watchDepsName: pluginOptions.build.watchDepsName ?? 'watch-deps',
1324+
skipBuildCheck: pluginOptions.build.skipBuildCheck ?? false,
13151325
};
13161326
}
13171327

0 commit comments

Comments
 (0)