Skip to content

Commit

Permalink
fix(js): package type is module should emit esm
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongemi committed Jan 31, 2024
1 parent 8d4869d commit 0581889
Showing 1 changed file with 19 additions and 2 deletions.
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 0581889

Please sign in to comment.