diff --git a/e2e/rollup/src/rollup-legacy.test.ts b/e2e/rollup/src/rollup-legacy.test.ts index d2170774d61975..ac11f8a6c3ce6b 100644 --- a/e2e/rollup/src/rollup-legacy.test.ts +++ b/e2e/rollup/src/rollup-legacy.test.ts @@ -198,4 +198,25 @@ describe('Rollup Plugin', () => { checkFilesExist(`dist/test/index.js`); }); + + it('should always generate package.json even if the plugin is removed from rollup config file (Nx < 19.4 behavior)', () => { + const jsLib = uniq('jslib'); + runCLI(`generate @nx/js:lib ${jsLib} --bundler rollup --verbose`); + updateFile( + `libs/${jsLib}/rollup.config.js`, + `module.exports = (config) => ({ + ...config, + // Filter out the plugin, but the @nx/rollup:rollup executor should add it back + plugins: config.plugins.filter((p) => p.name !== 'rollup-plugin-nx-generate-package-json'), + })` + ); + updateJson(join('libs', jsLib, 'project.json'), (config) => { + config.targets.build.options.rollupConfig = `libs/${jsLib}/rollup.config.js`; + return config; + }); + + expect(() => runCLI(`build ${jsLib}`)).not.toThrow(); + + checkFilesExist(`dist/libs/${jsLib}/package.json`); + }); }); diff --git a/packages/rollup/src/executors/rollup/rollup.impl.ts b/packages/rollup/src/executors/rollup/rollup.impl.ts index ddedb0e32c7112..5c87a62a9fa95a 100644 --- a/packages/rollup/src/executors/rollup/rollup.impl.ts +++ b/packages/rollup/src/executors/rollup/rollup.impl.ts @@ -10,6 +10,7 @@ import { import { loadConfigFile } from '@nx/devkit/src/utils/config-utils'; import { createAsyncIterable } from '@nx/devkit/src/utils/async-iterable'; import { withNx } from '../../plugins/with-nx/with-nx'; +import { pluginName as generatePackageJsonPluginName } from '../../plugins/package-json/generate-package-json'; import { calculateProjectBuildableDependencies } from '@nx/js/src/utils/buildable-libs-utils'; export async function* rollupExecutor( @@ -100,6 +101,14 @@ export async function createRollupOptions( const rollupConfig = withNx(options, {}, dependencies); + // `generatePackageJson` is a plugin rather than being embedded into @nx/rollup:rollup. + // Make sure the plugin is always present to keep the previous before of Nx < 19.4, where it was not a plugin. + const generatePackageJsonPlugin = Array.isArray(rollupConfig.plugins) + ? rollupConfig.plugins.find( + (p) => p['name'] === generatePackageJsonPluginName + ) + : null; + const userDefinedRollupConfigs = options.rollupConfig.map((plugin) => loadConfigFile(plugin) ); @@ -122,6 +131,17 @@ export async function createRollupOptions( }; } } + + if ( + generatePackageJsonPlugin && + Array.isArray(finalConfig.plugins) && + !finalConfig.plugins.some( + (p) => p['name'] === generatePackageJsonPluginName + ) + ) { + finalConfig.plugins.push(generatePackageJsonPlugin); + } + return finalConfig; } diff --git a/packages/rollup/src/plugins/package-json/generate-package-json.ts b/packages/rollup/src/plugins/package-json/generate-package-json.ts index e17ec8e2982275..d9f4f3417357c0 100644 --- a/packages/rollup/src/plugins/package-json/generate-package-json.ts +++ b/packages/rollup/src/plugins/package-json/generate-package-json.ts @@ -12,12 +12,14 @@ export interface GeneratePackageJsonOptions { additionalEntryPoints?: string[]; } +export const pluginName = 'rollup-plugin-nx-generate-package-json'; + export function generatePackageJson( options: GeneratePackageJsonOptions, packageJson: PackageJson ): Plugin { return { - name: 'rollup-plugin-nx-generate-package-json', + name: pluginName, writeBundle: () => { updatePackageJson(options, packageJson); }, diff --git a/packages/rollup/src/plugins/with-nx/with-nx.ts b/packages/rollup/src/plugins/with-nx/with-nx.ts index 7882e23066cc97..9bbf1786170870 100644 --- a/packages/rollup/src/plugins/with-nx/with-nx.ts +++ b/packages/rollup/src/plugins/with-nx/with-nx.ts @@ -286,15 +286,10 @@ function createTsCompilerOptions( options: RollupWithNxPluginOptions, dependencies?: DependentBuildableProjectNode[] ) { - const compilerOptionPaths = computeCompilerOptionsPaths( - config, - dependencies ?? [] - ); const compilerOptions = { rootDir: projectRoot, allowJs: options.allowJs, declaration: true, - paths: compilerOptionPaths, }; if (config.options.module === ts.ModuleKind.CommonJS) { compilerOptions['module'] = 'ESNext';