Skip to content

Commit

Permalink
feat(js): carry-over known build options (main, tsConfig, etc.) when …
Browse files Browse the repository at this point in the history
…running setup-build generator (#18742)
  • Loading branch information
jaysoo authored Aug 28, 2023
1 parent 960d80b commit 226dfaf
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 33 deletions.
4 changes: 2 additions & 2 deletions docs/generated/manifests/packages.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions docs/generated/packages-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
8 changes: 7 additions & 1 deletion docs/generated/packages/rollup/generators/configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
4 changes: 2 additions & 2 deletions e2e/rollup/src/rollup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/);

Expand All @@ -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);
Expand Down
6 changes: 2 additions & 4 deletions packages/esbuild/generators.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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."
}
}
}
17 changes: 11 additions & 6 deletions packages/esbuild/src/generators/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
Expand Down
15 changes: 12 additions & 3 deletions packages/js/src/generators/setup-build/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 [
Expand Down Expand Up @@ -97,6 +102,7 @@ export async function setupBuildGenerator(
buildTarget: options.buildTarget,
project: options.project,
skipFormat: true,
skipValidation: true,
});
tasks.push(task);
break;
Expand All @@ -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;
Expand Down
6 changes: 2 additions & 4 deletions packages/rollup/generators.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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."
}
}
}
43 changes: 43 additions & 0 deletions packages/rollup/src/generators/configuration/configuration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
readJson,
readProjectConfiguration,
Tree,
updateProjectConfiguration,
writeJson,
} from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
Expand Down Expand Up @@ -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,
},
},
});
});
});
27 changes: 19 additions & 8 deletions packages/rollup/src/generators/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions packages/rollup/src/generators/configuration/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export interface RollupProjectSchema {
external?: string[];
rollupConfig?: string;
buildTarget?: string;
format?: ('cjs' | 'esm')[];
}
9 changes: 9 additions & 0 deletions packages/rollup/src/generators/configuration/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": []
Expand Down

1 comment on commit 226dfaf

@vercel
Copy link

@vercel vercel bot commented on 226dfaf Aug 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-nrwl.vercel.app
nx.dev
nx-dev-git-master-nrwl.vercel.app
nx-five.vercel.app

Please sign in to comment.