From 226dfaf7a4d52e1d44d9c85d08cc2697893b1fb6 Mon Sep 17 00:00:00 2001 From: Jack Hsu Date: Mon, 28 Aug 2023 10:36:18 -0400 Subject: [PATCH] feat(js): carry-over known build options (main, tsConfig, etc.) when running setup-build generator (#18742) --- docs/generated/manifests/packages.json | 4 +- docs/generated/packages-metadata.json | 4 +- .../esbuild/generators/configuration.json | 2 +- .../rollup/generators/configuration.json | 8 +++- e2e/rollup/src/rollup.test.ts | 4 +- packages/esbuild/generators.json | 6 +-- .../generators/configuration/configuration.ts | 17 +++++--- .../src/generators/setup-build/generator.ts | 15 +++++-- packages/rollup/generators.json | 6 +-- .../configuration/configuration.spec.ts | 43 +++++++++++++++++++ .../generators/configuration/configuration.ts | 27 ++++++++---- .../src/generators/configuration/schema.d.ts | 1 + .../src/generators/configuration/schema.json | 9 ++++ 13 files changed, 113 insertions(+), 33 deletions(-) diff --git a/docs/generated/manifests/packages.json b/docs/generated/manifests/packages.json index 11b3103a043f7..c0a17ec7bc62f 100644 --- a/docs/generated/manifests/packages.json +++ b/docs/generated/manifests/packages.json @@ -641,7 +641,7 @@ "/packages/esbuild/generators/configuration": { "description": "Add esbuild configuration to a project.", "file": "generated/packages/esbuild/generators/configuration.json", - "hidden": true, + "hidden": false, "name": "configuration", "originalFilePath": "/packages/esbuild/src/generators/configuration/schema.json", "path": "/packages/esbuild/generators/configuration", @@ -2377,7 +2377,7 @@ "/packages/rollup/generators/configuration": { "description": "Add rollup configuration to a project.", "file": "generated/packages/rollup/generators/configuration.json", - "hidden": true, + "hidden": false, "name": "configuration", "originalFilePath": "/packages/rollup/src/generators/configuration/schema.json", "path": "/packages/rollup/generators/configuration", diff --git a/docs/generated/packages-metadata.json b/docs/generated/packages-metadata.json index afa0604275d90..b7fe8d96eaa25 100644 --- a/docs/generated/packages-metadata.json +++ b/docs/generated/packages-metadata.json @@ -630,7 +630,7 @@ { "description": "Add esbuild configuration to a project.", "file": "generated/packages/esbuild/generators/configuration.json", - "hidden": true, + "hidden": false, "name": "configuration", "originalFilePath": "/packages/esbuild/src/generators/configuration/schema.json", "path": "esbuild/generators/configuration", @@ -2351,7 +2351,7 @@ { "description": "Add rollup configuration to a project.", "file": "generated/packages/rollup/generators/configuration.json", - "hidden": true, + "hidden": false, "name": "configuration", "originalFilePath": "/packages/rollup/src/generators/configuration/schema.json", "path": "rollup/generators/configuration", diff --git a/docs/generated/packages/esbuild/generators/configuration.json b/docs/generated/packages/esbuild/generators/configuration.json index 0dd6daf91f77e..9cf9da59d3e84 100644 --- a/docs/generated/packages/esbuild/generators/configuration.json +++ b/docs/generated/packages/esbuild/generators/configuration.json @@ -68,8 +68,8 @@ "presets": [] }, "description": "Add esbuild configuration to a project.", - "hidden": true, "implementation": "/packages/esbuild/src/generators/configuration/configuration.ts", + "hidden": false, "path": "/packages/esbuild/src/generators/configuration/schema.json", "type": "generator" } diff --git a/docs/generated/packages/rollup/generators/configuration.json b/docs/generated/packages/rollup/generators/configuration.json index e17a829963cb2..87e125f21aabf 100644 --- a/docs/generated/packages/rollup/generators/configuration.json +++ b/docs/generated/packages/rollup/generators/configuration.json @@ -70,14 +70,20 @@ "description": "The build target to add.", "type": "string", "default": "build" + }, + "format": { + "description": "The format to build the library (esm or cjs).", + "type": "array", + "items": { "type": "string", "enum": ["esm", "cjs"] }, + "default": ["esm"] } }, "required": [], "presets": [] }, "description": "Add rollup configuration to a project.", - "hidden": true, "implementation": "/packages/rollup/src/generators/configuration/configuration.ts", + "hidden": false, "path": "/packages/rollup/src/generators/configuration/schema.json", "type": "generator" } diff --git a/e2e/rollup/src/rollup.test.ts b/e2e/rollup/src/rollup.test.ts index 5d9b38955d3c7..b7253ebc63c06 100644 --- a/e2e/rollup/src/rollup.test.ts +++ b/e2e/rollup/src/rollup.test.ts @@ -47,7 +47,7 @@ describe('Rollup Plugin', () => { `generate @nx/rollup:configuration ${myPkg} --target=node --tsConfig=libs/${myPkg}/tsconfig.lib.json --main=libs/${myPkg}/src/index.ts --compiler=swc` ); rmDist(); - runCLI(`build ${myPkg}`); + runCLI(`build ${myPkg} --format=cjs,esm --generateExportsField`); output = runCommand(`node dist/libs/${myPkg}/index.cjs.js`); expect(output).toMatch(/Hello/); @@ -61,7 +61,7 @@ describe('Rollup Plugin', () => { `generate @nx/rollup:configuration ${myPkg} --target=node --tsConfig=libs/${myPkg}/tsconfig.lib.json --main=libs/${myPkg}/src/index.ts --compiler=tsc` ); rmDist(); - runCLI(`build ${myPkg}`); + runCLI(`build ${myPkg} --format=cjs,esm --generateExportsField`); output = runCommand(`node dist/libs/${myPkg}/index.cjs.js`); expect(output).toMatch(/Hello/); }, 500000); diff --git a/packages/esbuild/generators.json b/packages/esbuild/generators.json index fd8a040874bbe..7588f2902124e 100644 --- a/packages/esbuild/generators.json +++ b/packages/esbuild/generators.json @@ -12,8 +12,7 @@ "aliases": ["esbuild-project"], "factory": "./src/generators/configuration/configuration#compat", "schema": "./src/generators/configuration/schema.json", - "description": "Add esbuild configuration to a project.", - "hidden": true + "description": "Add esbuild configuration to a project." } }, "generators": { @@ -28,8 +27,7 @@ "aliases": ["esbuild-project"], "factory": "./src/generators/configuration/configuration", "schema": "./src/generators/configuration/schema.json", - "description": "Add esbuild configuration to a project.", - "hidden": true + "description": "Add esbuild configuration to a project." } } } diff --git a/packages/esbuild/src/generators/configuration/configuration.ts b/packages/esbuild/src/generators/configuration/configuration.ts index d855d685a8f2b..9233c6bc8b808 100644 --- a/packages/esbuild/src/generators/configuration/configuration.ts +++ b/packages/esbuild/src/generators/configuration/configuration.ts @@ -51,14 +51,19 @@ function addBuildTarget(tree: Tree, options: EsBuildProjectSchema) { version: '0.0.1', }); } - const tsConfig = getTsConfigFile(tree, options); + + const prevBuildOptions = project.targets?.[options.buildTarget]?.options; + + const tsConfig = prevBuildOptions?.tsConfig ?? getTsConfigFile(tree, options); const buildOptions: EsBuildExecutorOptions = { - main: getMainFile(tree, options), - outputPath: joinPathFragments( - 'dist', - project.root === '.' ? options.project : project.root - ), + main: prevBuildOptions?.main ?? getMainFile(tree, options), + outputPath: + prevBuildOptions?.outputPath ?? + joinPathFragments( + 'dist', + project.root === '.' ? options.project : project.root + ), outputFileName: 'main.js', tsConfig, assets: [], diff --git a/packages/js/src/generators/setup-build/generator.ts b/packages/js/src/generators/setup-build/generator.ts index 91925f36fac3d..f84c8e7e038bb 100644 --- a/packages/js/src/generators/setup-build/generator.ts +++ b/packages/js/src/generators/setup-build/generator.ts @@ -21,11 +21,14 @@ export async function setupBuildGenerator( const tasks: GeneratorCallback[] = []; const project = readProjectConfiguration(tree, options.project); const buildTarget = options.buildTarget ?? 'build'; + const prevBuildOptions = project.targets?.[buildTarget]?.options; project.targets ??= {}; let mainFile: string; - if (options.main) { + if (prevBuildOptions?.main) { + mainFile = prevBuildOptions.main; + } else if (options.main) { mainFile = options.main; } else { const root = project.sourceRoot ?? project.root; @@ -48,7 +51,9 @@ export async function setupBuildGenerator( } let tsConfigFile: string; - if (options.tsConfig) { + if (prevBuildOptions?.tsConfig) { + tsConfigFile = prevBuildOptions.tsConfig; + } else if (options.tsConfig) { tsConfigFile = options.tsConfig; } else { for (const f of [ @@ -97,6 +102,7 @@ export async function setupBuildGenerator( buildTarget: options.buildTarget, project: options.project, skipFormat: true, + skipValidation: true, }); tasks.push(task); break; @@ -106,9 +112,12 @@ export async function setupBuildGenerator( const task = await configurationGenerator(tree, { buildTarget: options.buildTarget, main: mainFile, + tsConfig: tsConfigFile, project: options.project, - skipFormat: true, compiler: 'tsc', + format: ['cjs', 'esm'], + skipFormat: true, + skipValidation: true, }); tasks.push(task); break; diff --git a/packages/rollup/generators.json b/packages/rollup/generators.json index 7f6dcad5e4ff6..28fe87e7adc5f 100644 --- a/packages/rollup/generators.json +++ b/packages/rollup/generators.json @@ -12,8 +12,7 @@ "aliases": ["rollup-project"], "factory": "./src/generators/configuration/configuration#compat", "schema": "./src/generators/configuration/schema.json", - "description": "Add rollup configuration to a project.", - "hidden": true + "description": "Add rollup configuration to a project." } }, "generators": { @@ -28,8 +27,7 @@ "aliases": ["rollup-project"], "factory": "./src/generators/configuration/configuration", "schema": "./src/generators/configuration/schema.json", - "description": "Add rollup configuration to a project.", - "hidden": true + "description": "Add rollup configuration to a project." } } } diff --git a/packages/rollup/src/generators/configuration/configuration.spec.ts b/packages/rollup/src/generators/configuration/configuration.spec.ts index b9dfb242104d0..fc70c47b374da 100644 --- a/packages/rollup/src/generators/configuration/configuration.spec.ts +++ b/packages/rollup/src/generators/configuration/configuration.spec.ts @@ -3,6 +3,7 @@ import { readJson, readProjectConfiguration, Tree, + updateProjectConfiguration, writeJson, } from '@nx/devkit'; import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; @@ -99,4 +100,46 @@ describe('configurationGenerator', () => { }, }); }); + + it('should carry over known executor options from existing build target', async () => { + updateProjectConfiguration(tree, 'mypkg', { + root: 'libs/mypkg', + sourceRoot: 'libs/mypkg/src', + targets: { + build: { + executor: '@nx/js:tsc', + options: { + main: 'libs/mypkg/src/custom.ts', + outputPath: 'dist/custom', + tsConfig: 'libs/mypkg/src/tsconfig.custom.json', + additionalEntryPoints: ['libs/mypkg/src/extra.ts'], + generateExportsField: true, + }, + }, + }, + }); + + await configurationGenerator(tree, { + project: 'mypkg', + buildTarget: 'build', + skipValidation: true, + }); + + const project = readProjectConfiguration(tree, 'mypkg'); + + expect(project.targets).toMatchObject({ + build: { + executor: '@nx/rollup:rollup', + outputs: ['{options.outputPath}'], + defaultConfiguration: 'production', + options: { + main: 'libs/mypkg/src/custom.ts', + outputPath: 'dist/custom', + tsConfig: 'libs/mypkg/src/tsconfig.custom.json', + additionalEntryPoints: ['libs/mypkg/src/extra.ts'], + generateExportsField: true, + }, + }, + }); + }); }); diff --git a/packages/rollup/src/generators/configuration/configuration.ts b/packages/rollup/src/generators/configuration/configuration.ts index dffa22a43c91e..863d7896336be 100644 --- a/packages/rollup/src/generators/configuration/configuration.ts +++ b/packages/rollup/src/generators/configuration/configuration.ts @@ -50,19 +50,30 @@ function addBuildTarget(tree: Tree, options: RollupProjectSchema) { version: '0.0.1', }); } - const tsConfig = - options.tsConfig ?? joinPathFragments(project.root, 'tsconfig.lib.json'); + + const prevBuildOptions = project.targets?.[options.buildTarget]?.options; const buildOptions: RollupExecutorOptions = { - main: options.main ?? joinPathFragments(project.root, 'src/main.ts'), - outputPath: joinPathFragments( - 'dist', - project.root === '.' ? project.name : project.root - ), + main: + options.main ?? + prevBuildOptions?.main ?? + joinPathFragments(project.root, 'src/main.ts'), + outputPath: + prevBuildOptions?.outputPath ?? + joinPathFragments( + 'dist', + project.root === '.' ? project.name : project.root + ), + tsConfig: + options.tsConfig ?? + prevBuildOptions?.tsConfig ?? + joinPathFragments(project.root, 'tsconfig.lib.json'), + additionalEntryPoints: prevBuildOptions?.additionalEntryPoints, + generateExportsField: prevBuildOptions?.generateExportsField, compiler: options.compiler ?? 'babel', - tsConfig, project: `${project.root}/package.json`, external: options.external, + format: options.format, }; if (options.rollupConfig) { diff --git a/packages/rollup/src/generators/configuration/schema.d.ts b/packages/rollup/src/generators/configuration/schema.d.ts index d148f215e7156..a8c334224420b 100644 --- a/packages/rollup/src/generators/configuration/schema.d.ts +++ b/packages/rollup/src/generators/configuration/schema.d.ts @@ -10,4 +10,5 @@ export interface RollupProjectSchema { external?: string[]; rollupConfig?: string; buildTarget?: string; + format?: ('cjs' | 'esm')[]; } diff --git a/packages/rollup/src/generators/configuration/schema.json b/packages/rollup/src/generators/configuration/schema.json index fd0817057a1f8..c12857e6da8ee 100644 --- a/packages/rollup/src/generators/configuration/schema.json +++ b/packages/rollup/src/generators/configuration/schema.json @@ -71,6 +71,15 @@ "description": "The build target to add.", "type": "string", "default": "build" + }, + "format": { + "description": "The format to build the library (esm or cjs).", + "type": "array", + "items": { + "type": "string", + "enum": ["esm", "cjs"] + }, + "default": ["esm"] } }, "required": []