fix(js): do not override package.json type when defined in tsc executor (#27106)

<!-- 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 #23579
This commit is contained in:
Leosvel Pérez Espinosa 2024-07-25 12:23:28 +02:00 committed by GitHub
parent ebb42b73eb
commit 1774edd1a6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 59 additions and 2 deletions

View File

@ -146,10 +146,20 @@ describe('packaging libs', () => {
json.compilerOptions.module = 'esnext';
return json;
});
updateJson(`libs/${tscEsmLib}/package.json`, (json) => {
// check one lib without type, the build output should be set with type module
delete json.type;
return json;
});
updateJson(`libs/${swcEsmLib}/.swcrc`, (json) => {
json.module.type = 'es6';
return json;
});
updateJson(`libs/${swcEsmLib}/package.json`, (json) => {
// check one lib with the type set, the build output should be set with type module
json.type = 'module';
return json;
});
// Node ESM requires file extensions in imports so must add them before building
updateFile(
`libs/${tscEsmLib}/src/index.ts`,

View File

@ -331,6 +331,37 @@ describe('getUpdatedPackageJsonContent', () => {
},
});
});
it('should no override existing type', () => {
// Leave existing type untouched
expect(
getUpdatedPackageJsonContent(
{
name: 'test',
version: '0.0.1',
type: 'module',
},
{
main: 'proj/src/index.ts',
outputPath: 'dist/proj',
projectRoot: 'proj',
format: ['cjs'],
outputFileExtensionForCjs: '.cjs',
generateExportsField: true,
}
)
).toEqual({
name: 'test',
main: './src/index.cjs',
types: './src/index.d.ts',
version: '0.0.1',
type: 'module',
exports: {
'.': './src/index.cjs',
'./package.json': './package.json',
},
});
});
});
describe('updatePackageJson', () => {

View File

@ -96,6 +96,22 @@ export function updatePackageJson(
: { name: context.projectName, version: '0.0.1' };
}
if (packageJson.type === 'module') {
if (options.format?.includes('cjs')) {
logger.warn(
`Package type is set to "module" but "cjs" format is included. Going to use "esm" format instead. You can change the package type to "commonjs" or remove type in the package.json file.`
);
}
options.format = ['esm'];
} else if (packageJson.type === 'commonjs') {
if (options.format?.includes('esm')) {
logger.warn(
`Package type is set to "commonjs" but "esm" format is included. Going to use "cjs" format instead. You can change the package type to "module" or remove type in the package.json file.`
);
}
options.format = ['cjs'];
}
// update package specific settings
packageJson = getUpdatedPackageJsonContent(packageJson, options);
@ -278,7 +294,7 @@ export function getUpdatedPackageJsonContent(
packageJson.module ??= esmExports['.'];
if (!hasCjsFormat) {
packageJson.type = 'module';
packageJson.type ??= 'module';
packageJson.main ??= esmExports['.'];
}
@ -302,7 +318,7 @@ export function getUpdatedPackageJsonContent(
packageJson.main ??= cjsExports['.'];
if (!hasEsmFormat) {
packageJson.type = 'commonjs';
packageJson.type ??= 'commonjs';
}
if (options.generateExportsField) {