Skip to content

Commit

Permalink
fix(js): package type is module should emit esm (#21473)
Browse files Browse the repository at this point in the history
(cherry picked from commit d1bf692)
  • Loading branch information
xiongemi authored and FrozenPandaz committed Feb 3, 2024
1 parent 9b04eb9 commit 5df1dc1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
14 changes: 7 additions & 7 deletions packages/rollup/src/executors/rollup/rollup.impl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('rollupExecutor', () => {
),
[],
context,
{ name: 'example' },
{ name: 'example', version: '1.0' },
'/root/src',
[]
);
Expand Down Expand Up @@ -81,7 +81,7 @@ describe('rollupExecutor', () => {
),
[],
context,
{ name: 'example' },
{ name: 'example', version: '1.0' },
'/root/src',
[]
);
Expand Down Expand Up @@ -117,7 +117,7 @@ describe('rollupExecutor', () => {
),
[],
context,
{ name: 'example' },
{ name: 'example', version: '1.0' },
'/root/src',
[]
);
Expand Down Expand Up @@ -146,7 +146,7 @@ describe('rollupExecutor', () => {
},
[],
context,
{ name: 'example' },
{ name: 'example', version: '1.0' },
'/root/src',
[]
);
Expand All @@ -165,7 +165,7 @@ describe('rollupExecutor', () => {
),
[],
context,
{ name: 'example' },
{ name: 'example', version: '1.0' },
'/root/src',
['lodash']
);
Expand All @@ -186,7 +186,7 @@ describe('rollupExecutor', () => {
),
[],
context,
{ name: 'example' },
{ name: 'example', version: '1.0' },
'/root/src',
['lodash']
);
Expand All @@ -207,7 +207,7 @@ describe('rollupExecutor', () => {
),
[],
context,
{ name: 'example' },
{ name: 'example', version: '1.0' },
'/root/src',
['lodash']
);
Expand Down
21 changes: 19 additions & 2 deletions packages/rollup/src/executors/rollup/rollup.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand Down Expand Up @@ -158,7 +159,7 @@ export function createRollupOptions(
options: NormalizedRollupExecutorOptions,
dependencies: DependentBuildableProjectNode[],
context: ExecutorContext,
packageJson: any,
packageJson: PackageJson,
sourceRoot: string,
npmDeps: string[]
): rollup.InputOptions[] {
Expand All @@ -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.
Expand Down

0 comments on commit 5df1dc1

Please sign in to comment.