diff --git a/e2e/js/src/js-packaging.test.ts b/e2e/js/src/js-packaging.test.ts index ddf4bf6f4d06e..a910b336fc414 100644 --- a/e2e/js/src/js-packaging.test.ts +++ b/e2e/js/src/js-packaging.test.ts @@ -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`, diff --git a/packages/js/src/utils/package-json/update-package-json.spec.ts b/packages/js/src/utils/package-json/update-package-json.spec.ts index 68745bdea4906..2a9b8020615b0 100644 --- a/packages/js/src/utils/package-json/update-package-json.spec.ts +++ b/packages/js/src/utils/package-json/update-package-json.spec.ts @@ -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', () => { diff --git a/packages/js/src/utils/package-json/update-package-json.ts b/packages/js/src/utils/package-json/update-package-json.ts index 0dbdf64615cda..14366be160fb5 100644 --- a/packages/js/src/utils/package-json/update-package-json.ts +++ b/packages/js/src/utils/package-json/update-package-json.ts @@ -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) {