From 5df1dc1f75217320e093aab9f57496419b4fd44f Mon Sep 17 00:00:00 2001 From: Emily Xiong Date: Fri, 2 Feb 2024 11:31:31 -0500 Subject: [PATCH] fix(js): package type is module should emit esm (#21473) (cherry picked from commit d1bf692704366a9a7be7698633b81a0ea63d3349) --- .../src/executors/rollup/rollup.impl.spec.ts | 14 ++++++------- .../src/executors/rollup/rollup.impl.ts | 21 +++++++++++++++++-- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/packages/rollup/src/executors/rollup/rollup.impl.spec.ts b/packages/rollup/src/executors/rollup/rollup.impl.spec.ts index aaa69f167b2c2..d647bb0a896b7 100644 --- a/packages/rollup/src/executors/rollup/rollup.impl.spec.ts +++ b/packages/rollup/src/executors/rollup/rollup.impl.spec.ts @@ -44,7 +44,7 @@ describe('rollupExecutor', () => { ), [], context, - { name: 'example' }, + { name: 'example', version: '1.0' }, '/root/src', [] ); @@ -81,7 +81,7 @@ describe('rollupExecutor', () => { ), [], context, - { name: 'example' }, + { name: 'example', version: '1.0' }, '/root/src', [] ); @@ -117,7 +117,7 @@ describe('rollupExecutor', () => { ), [], context, - { name: 'example' }, + { name: 'example', version: '1.0' }, '/root/src', [] ); @@ -146,7 +146,7 @@ describe('rollupExecutor', () => { }, [], context, - { name: 'example' }, + { name: 'example', version: '1.0' }, '/root/src', [] ); @@ -165,7 +165,7 @@ describe('rollupExecutor', () => { ), [], context, - { name: 'example' }, + { name: 'example', version: '1.0' }, '/root/src', ['lodash'] ); @@ -186,7 +186,7 @@ describe('rollupExecutor', () => { ), [], context, - { name: 'example' }, + { name: 'example', version: '1.0' }, '/root/src', ['lodash'] ); @@ -207,7 +207,7 @@ describe('rollupExecutor', () => { ), [], context, - { name: 'example' }, + { name: 'example', version: '1.0' }, '/root/src', ['lodash'] ); diff --git a/packages/rollup/src/executors/rollup/rollup.impl.ts b/packages/rollup/src/executors/rollup/rollup.impl.ts index 5b9ee1ddeba72..e431e9d4ccd6f 100644 --- a/packages/rollup/src/executors/rollup/rollup.impl.ts +++ b/packages/rollup/src/executors/rollup/rollup.impl.ts @@ -15,6 +15,8 @@ import { DependentBuildableProjectNode, } from '@nx/js/src/utils/buildable-libs-utils'; import nodeResolve from '@rollup/plugin-node-resolve'; +import type { PackageJson } from 'nx/src/utils/package-json'; +import { typeDefinitions } from '@nx/js/src/plugins/rollup/type-definitions'; import { AssetGlobPattern, RollupExecutorOptions } from './schema'; import { runRollup } from './lib/run-rollup'; @@ -26,7 +28,6 @@ import { analyze } from './lib/analyze-plugin'; import { deleteOutputDir } from '../../utils/fs'; import { swc } from './lib/swc-plugin'; import { updatePackageJson } from './lib/update-package-json'; -import { typeDefinitions } from '@nx/js/src/plugins/rollup/type-definitions'; export type RollupExecutorEvent = { success: boolean; @@ -158,7 +159,7 @@ export function createRollupOptions( options: NormalizedRollupExecutorOptions, dependencies: DependentBuildableProjectNode[], context: ExecutorContext, - packageJson: any, + packageJson: PackageJson, sourceRoot: string, npmDeps: string[] ): rollup.InputOptions[] { @@ -178,6 +179,22 @@ export function createRollupOptions( options.format = readCompatibleFormats(config); } + 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']; + } + return options.format.map((format, idx) => { // Either we're generating only one format, so we should bundle types // OR we are generating dual formats, so only bundle types for CJS.