Skip to content

Commit

Permalink
fix(js): do not override package.json type when defined in tsc executor
Browse files Browse the repository at this point in the history
  • Loading branch information
leosvelperez committed Jul 25, 2024
1 parent 3890edc commit 29ec754
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 29ec754

Please sign in to comment.