Skip to content

Commit

Permalink
fix(rollup): always generate package.json when using @nx/rollup:rollup (
Browse files Browse the repository at this point in the history
#26940)

Prior to Nx 19.4, the `@nx/rollup:rollup` executor generates
`package.json` outside of the actual Rollup build. Nx 19.4 changed this
to be a proper Rollup plugin in order to support inferred targets
better. This led to a slight regression, where projects that override
`plugins` array in their `rollup.config.js` file will also remove the
`generatePackageJson` plugin.

This PR brings the behavior back, so the `package.json` file is _always_
generated regardless of how the `plugins` array is customized.

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #

(cherry picked from commit 34da542)
  • Loading branch information
jaysoo authored and FrozenPandaz committed Jul 16, 2024
1 parent 596f643 commit 338f040
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
21 changes: 21 additions & 0 deletions e2e/rollup/src/rollup-legacy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
});
});
20 changes: 20 additions & 0 deletions packages/rollup/src/executors/rollup/rollup.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
);
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
Expand Down

0 comments on commit 338f040

Please sign in to comment.