Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(js): package type is module should emit esm #21473

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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