Skip to content

Commit

Permalink
fix(js): do not override package.json type when defined in tsc execut…
Browse files Browse the repository at this point in the history
…or (#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

(cherry picked from commit 1774edd)
  • Loading branch information
leosvelperez authored and FrozenPandaz committed Jul 30, 2024
1 parent 801001d commit 2240eed
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
10 changes: 10 additions & 0 deletions e2e/js/src/js-packaging.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down
31 changes: 31 additions & 0 deletions packages/js/src/utils/package-json/update-package-json.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
20 changes: 18 additions & 2 deletions packages/js/src/utils/package-json/update-package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -278,7 +294,7 @@ export function getUpdatedPackageJsonContent(
packageJson.module ??= esmExports['.'];

if (!hasCjsFormat) {
packageJson.type = 'module';
packageJson.type ??= 'module';
packageJson.main ??= esmExports['.'];
}

Expand All @@ -302,7 +318,7 @@ export function getUpdatedPackageJsonContent(

packageJson.main ??= cjsExports['.'];
if (!hasEsmFormat) {
packageJson.type = 'commonjs';
packageJson.type ??= 'commonjs';
}

if (options.generateExportsField) {
Expand Down

0 comments on commit 2240eed

Please sign in to comment.