fix(rollup): always generate package.json when using @nx/rollup:rollup (#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 #
This commit is contained in:
parent
46497356ce
commit
34da542ce6
@ -198,4 +198,25 @@ describe('Rollup Plugin', () => {
|
|||||||
|
|
||||||
checkFilesExist(`dist/test/index.js`);
|
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`);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -10,6 +10,7 @@ import {
|
|||||||
import { loadConfigFile } from '@nx/devkit/src/utils/config-utils';
|
import { loadConfigFile } from '@nx/devkit/src/utils/config-utils';
|
||||||
import { createAsyncIterable } from '@nx/devkit/src/utils/async-iterable';
|
import { createAsyncIterable } from '@nx/devkit/src/utils/async-iterable';
|
||||||
import { withNx } from '../../plugins/with-nx/with-nx';
|
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';
|
import { calculateProjectBuildableDependencies } from '@nx/js/src/utils/buildable-libs-utils';
|
||||||
|
|
||||||
export async function* rollupExecutor(
|
export async function* rollupExecutor(
|
||||||
@ -100,6 +101,14 @@ export async function createRollupOptions(
|
|||||||
|
|
||||||
const rollupConfig = withNx(options, {}, dependencies);
|
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) =>
|
const userDefinedRollupConfigs = options.rollupConfig.map((plugin) =>
|
||||||
loadConfigFile(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;
|
return finalConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -12,12 +12,14 @@ export interface GeneratePackageJsonOptions {
|
|||||||
additionalEntryPoints?: string[];
|
additionalEntryPoints?: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const pluginName = 'rollup-plugin-nx-generate-package-json';
|
||||||
|
|
||||||
export function generatePackageJson(
|
export function generatePackageJson(
|
||||||
options: GeneratePackageJsonOptions,
|
options: GeneratePackageJsonOptions,
|
||||||
packageJson: PackageJson
|
packageJson: PackageJson
|
||||||
): Plugin {
|
): Plugin {
|
||||||
return {
|
return {
|
||||||
name: 'rollup-plugin-nx-generate-package-json',
|
name: pluginName,
|
||||||
writeBundle: () => {
|
writeBundle: () => {
|
||||||
updatePackageJson(options, packageJson);
|
updatePackageJson(options, packageJson);
|
||||||
},
|
},
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user