From d2c863b4371d4fcb4b0312965f5d432349a0ecff Mon Sep 17 00:00:00 2001 From: Jack Hsu Date: Mon, 24 Jun 2024 13:01:34 -0400 Subject: [PATCH] fix(bundling): rename aliases for @nx/rollup:convert-to-inferred generator --- .../convert-to-inferred.spec.ts | 53 ++++++++++++++++- ...act-rollup-config-from-executor-options.ts | 12 +++- .../src/plugins/with-nx/with-nx-options.ts | 58 ++++++++++++++++++- 3 files changed, 118 insertions(+), 5 deletions(-) diff --git a/packages/rollup/src/generators/convert-to-inferred/convert-to-inferred.spec.ts b/packages/rollup/src/generators/convert-to-inferred/convert-to-inferred.spec.ts index fbaf582b9a0be..8c875cf4bf053 100644 --- a/packages/rollup/src/generators/convert-to-inferred/convert-to-inferred.spec.ts +++ b/packages/rollup/src/generators/convert-to-inferred/convert-to-inferred.spec.ts @@ -40,7 +40,7 @@ function createProject(tree: Tree, opts: Partial = {}) { projectOpts.targetOptions.outputPath ??= `dist/${projectOpts.root}`; projectOpts.targetOptions.tsConfig ??= `${projectOpts.root}/tsconfig.lib.json`; projectOpts.targetOptions.compiler ??= 'babel'; - projectOpts.targetOptions.format ??= ['esm']; + projectOpts.targetOptions.format ??= projectOpts.targetOptions.f ?? ['esm']; projectOpts.targetOptions.external ??= []; projectOpts.targetOptions.assets ??= []; } @@ -516,6 +516,57 @@ describe('Rollup - Convert Executors To Plugin', () => { " `); }); + + it('should rename aliases to the original option name', async () => { + const project = createProject(tree, { + name: 'mypkg', + root: 'mypkg', + targetOptions: { + entryFile: 'mypkg/src/foo.ts', + f: ['cjs'], + exports: true, + }, + }); + + await convertToInferred(tree, { project: project.name }); + + expect(readNxJson(tree).plugins).toEqual([ + { + options: { + targetName: 'build', + }, + plugin: '@nx/rollup/plugin', + }, + ]); + expect(tree.read('mypkg/rollup.config.js', 'utf-8')) + .toMatchInlineSnapshot(` + "const { withNx } = require('@nx/rollup/with-nx'); + + // These options were migrated by @nx/rollup:convert-to-inferred from project.json + const options = { + main: './src/index.ts', + format: ['cjs'], + generateExportsField: true, + outputPath: '../dist/mypkg', + tsConfig: './tsconfig.lib.json', + compiler: 'babel', + external: [], + assets: [], + }; + + const config = withNx(options, { + // Provide additional rollup configuration here. See: https://rollupjs.org/configuration-options + // e.g. + // output: { sourcemap: true }, + }); + + module.exports = config; + " + `); + expect(tree.exists('otherpkg1/rollup.config.js')).toBe(false); + expect(tree.exists('otherpkg2/rollup.config.js')).toBe(false); + expect(readProjectConfiguration(tree, project.name).targets).toEqual({}); + }); }); describe('all projects', () => { diff --git a/packages/rollup/src/generators/convert-to-inferred/lib/extract-rollup-config-from-executor-options.ts b/packages/rollup/src/generators/convert-to-inferred/lib/extract-rollup-config-from-executor-options.ts index 5eb1bbbd46d41..df9694f4630d8 100644 --- a/packages/rollup/src/generators/convert-to-inferred/lib/extract-rollup-config-from-executor-options.ts +++ b/packages/rollup/src/generators/convert-to-inferred/lib/extract-rollup-config-from-executor-options.ts @@ -2,6 +2,12 @@ import { joinPathFragments, stripIndents, Tree } from '@nx/devkit'; import { RollupExecutorOptions } from '../../../executors/rollup/schema'; import { normalizePathOptions } from './normalize-path-options'; +const aliases = { + entryFile: 'main', + exports: 'generateExportsField', + f: 'format', +}; + export function extractRollupConfigFromExecutorOptions( tree: Tree, options: RollupExecutorOptions, @@ -36,7 +42,11 @@ export function extractRollupConfigFromExecutorOptions( for (const [key, value] of Object.entries(options)) { if (key === 'watch') continue; delete options[key]; - defaultOptions[key] = value; + if (aliases[key]) { + defaultOptions[aliases[key]] = value; + } else { + defaultOptions[key] = value; + } } let configurationOptions: Record>; if (hasConfigurations) { diff --git a/packages/rollup/src/plugins/with-nx/with-nx-options.ts b/packages/rollup/src/plugins/with-nx/with-nx-options.ts index 520b2bdc9d19b..9558d569ef521 100644 --- a/packages/rollup/src/plugins/with-nx/with-nx-options.ts +++ b/packages/rollup/src/plugins/with-nx/with-nx-options.ts @@ -1,24 +1,76 @@ -// TODO: Add TSDoc export interface RollupWithNxPluginOptions { + /** + * Additional entry-points to add to exports field in the package.json file. + * */ additionalEntryPoints?: string[]; + /** + * Allow JavaScript files to be compiled. + */ allowJs?: boolean; + /** + * List of static assets. + */ assets?: any[]; + /** + * Whether to set rootmode to upward. See https://babeljs.io/docs/en/options#rootmode + */ babelUpwardRootMode?: boolean; + /** + * Which compiler to use. + */ compiler?: 'babel' | 'tsc' | 'swc'; + /** + * Delete the output path before building. Defaults to true. + */ deleteOutputPath?: boolean; + /** + * A list of external modules that will not be bundled (`react`, `react-dom`, etc.). Can also be set to `all` (bundle nothing) or `none` (bundle everything). + */ external?: string[] | 'all' | 'none'; + /** + * CSS files will be extracted to the output folder. Alternatively custom filename can be provided (e.g. styles.css) + */ extractCss?: boolean | string; + /** + * List of module formats to output. Defaults to matching format from tsconfig (e.g. CJS for CommonJS, and ESM otherwise). + */ format?: ('cjs' | 'esm')[]; + /** + * Update the output package.json file's 'exports' field. This field is used by Node and bundles. + */ generateExportsField?: boolean; + /** + * Sets `javascriptEnabled` option for less loader + */ javascriptEnabled?: boolean; + /** + * The path to the entry file, relative to project. + */ main: string; - /** @deprecated Do not set this. The package.json file in project root is detected automatically. */ + /** + * The path to package.json file. + * @deprecated Do not set this. The package.json file in project root is detected automatically. + */ project?: string; + /** + * Name of the main output file. Defaults same basename as 'main' file. + */ outputFileName?: string; + /** + * The output path of the generated files. + */ outputPath: string; - rollupConfig?: string | string[]; + /** + * Whether to skip TypeScript type checking. + */ skipTypeCheck?: boolean; + /** + * Prevents 'type' field from being added to compiled package.json file. Use this if you are having an issue with this field. + */ skipTypeField?: boolean; + /** + * The path to tsconfig file. + */ tsConfig: string; }