chore(bundling): clean up package.json update function (#12152)

This commit is contained in:
Jack Hsu 2022-09-21 10:53:14 -04:00 committed by GitHub
parent 1485b84e15
commit 2d84bd3eb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 11 deletions

View File

@ -17,6 +17,8 @@ import { removeSync, writeJsonSync } from 'fs-extra';
import { getClientEnvironment } from '../../utils/environment-variables'; import { getClientEnvironment } from '../../utils/environment-variables';
import { createAsyncIterable } from '@nrwl/js/src/utils/create-async-iterable/create-async-iteratable'; import { createAsyncIterable } from '@nrwl/js/src/utils/create-async-iterable/create-async-iteratable';
const CJS_FILE_EXTENSION = '.cjs';
export async function* esbuildExecutor( export async function* esbuildExecutor(
_options: EsBuildExecutorOptions, _options: EsBuildExecutorOptions,
context: ExecutorContext context: ExecutorContext
@ -30,6 +32,7 @@ export async function* esbuildExecutor(
{ {
...options, ...options,
skipTypings: options.skipTypeCheck, skipTypings: options.skipTypeCheck,
outputFileExtensionForCjs: CJS_FILE_EXTENSION,
}, },
context context
); );
@ -185,7 +188,7 @@ function getOutfile(
return candidate; return candidate;
} else { } else {
const { dir, name } = parse(candidate); const { dir, name } = parse(candidate);
return `${dir}/${name}.cjs`; return `${dir}/${name}${CJS_FILE_EXTENSION}`;
} }
} }

View File

@ -117,4 +117,32 @@ describe('getUpdatedPackageJsonContent', () => {
}, },
}); });
}); });
it('should support different CJS file extension', () => {
const json = getUpdatedPackageJsonContent(
{
name: 'test',
version: '0.0.1',
},
{
main: 'proj/src/index.ts',
outputPath: 'dist/proj',
projectRoot: 'proj',
format: ['esm', 'cjs'],
outputFileExtensionForCjs: '.cjs',
generateExportsField: true,
}
);
expect(json).toEqual({
name: 'test',
main: './src/index.cjs',
module: './src/index.js',
types: './src/index.d.ts',
version: '0.0.1',
exports: {
'.': { require: './src/index.cjs', import: './src/index.js' },
},
});
});
}); });

View File

@ -22,13 +22,16 @@ function getMainFileDirRelativeToProjectRoot(
return relativeDir === '' ? `./` : `./${relativeDir}/`; return relativeDir === '' ? `./` : `./${relativeDir}/`;
} }
export type SupportedFormat = 'cjs' | 'esm';
export interface UpdatePackageJsonOption { export interface UpdatePackageJsonOption {
projectRoot: string; projectRoot: string;
outputPath: string;
main: string; main: string;
format?: string[]; format?: SupportedFormat[];
skipTypings?: boolean; outputPath: string;
outputFileName?: string; outputFileName?: string;
outputFileExtensionForCjs?: `.${string}`;
skipTypings?: boolean;
generateExportsField?: boolean; generateExportsField?: boolean;
updateBuildableProjectDepsInPackageJson?: boolean; updateBuildableProjectDepsInPackageJson?: boolean;
buildableProjectDepsInPackageJsonType?: 'dependencies' | 'peerDependencies'; buildableProjectDepsInPackageJsonType?: 'dependencies' | 'peerDependencies';
@ -105,15 +108,14 @@ export function getUpdatedPackageJsonContent(
exports['.']['import'] = mainJsFile; exports['.']['import'] = mainJsFile;
} }
// If CJS file ends with .cjs then use that, otherwise default to main file. // CJS output may have .cjs or .js file extensions.
// Bundlers like webpack, rollup and, esbuild supports .cjs for CJS and .js for ESM. // Bundlers like rollup and esbuild supports .cjs for CJS and .js for ESM.
// Compilers like tsc, swc do not have different file extensions. // Bundlers/Compilers like webpack, tsc, swc do not have different file extensions.
if (hasCjsFormat) { if (hasCjsFormat) {
const { dir, name } = parse(mainJsFile); const { dir, name } = parse(mainJsFile);
let cjsMain = `${dir}/${name}.cjs`; const cjsMain = `${dir}/${name}${
cjsMain = fileExists(join(options.outputPath, cjsMain)) options.outputFileExtensionForCjs ?? '.js'
? cjsMain }`;
: mainJsFile;
packageJson.main ??= cjsMain; packageJson.main ??= cjsMain;
exports['.']['require'] = cjsMain; exports['.']['require'] = cjsMain;
} }