From edf5464e576ba80884beb956d61dd5c1c347224c Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Sat, 7 Oct 2023 01:06:46 +0200 Subject: [PATCH] Error on invalid main field export (#271) --- src/exports.ts | 6 +++++- src/lib/wildcard.ts | 3 ++- src/utils.ts | 3 +++ test/integration.test.ts | 9 +++++++++ test/integration/esm-pkg-cjs-main-field/package.json | 4 ++++ test/integration/esm-pkg-cjs-main-field/src/index.cjs | 1 + 6 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 test/integration/esm-pkg-cjs-main-field/package.json create mode 100644 test/integration/esm-pkg-cjs-main-field/src/index.cjs diff --git a/src/exports.ts b/src/exports.ts index f803af5c..04539cae 100644 --- a/src/exports.ts +++ b/src/exports.ts @@ -6,7 +6,7 @@ import type { PackageType, ParsedExportCondition, } from './types' -import { filenameWithoutExtension } from './utils' +import { exit, filenameWithoutExtension, hasCjsExtension } from './utils' export function getTypings(pkg: PackageMetadata) { return pkg.types || pkg.typings @@ -185,6 +185,10 @@ export function getExportPaths( Object.assign(pathsMap, paths) } + if (!isCjsPackage && pkg.main && hasCjsExtension(pkg.main)) { + exit('Cannot export main field with .cjs extension in ESM package, only .mjs and .js extensions are allowed') + } + // main export '.' from main/module/typings const defaultMainExport = constructFullExportCondition( { diff --git a/src/lib/wildcard.ts b/src/lib/wildcard.ts index 43aa8f8f..ddeba9e8 100644 --- a/src/lib/wildcard.ts +++ b/src/lib/wildcard.ts @@ -8,6 +8,7 @@ import { hasAvailableExtension, nonNullable, } from '../utils' +import { logger } from '../logger' // TODO: support nested wildcard exportsCondition (e.g. './foo/*') const getWildcardExports = ( @@ -84,7 +85,7 @@ export async function resolveWildcardExports( const hasWildcard = !!exportsCondition['./*'] if (hasWildcard) { - console.warn( + logger.warn( `The wildcard export "./*" is experimental and may change or be removed at any time.\n` + 'To open an issue, please visit https://github.com/huozhi/bunchee/issues' + '.\n', diff --git a/src/utils.ts b/src/utils.ts index 98de707c..c2cc78ae 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -139,3 +139,6 @@ export const fileExtension = (file: string | undefined) => export const hasAvailableExtension = (filename: string): boolean => availableExtensions.includes(path.extname(filename).slice(1)) + +export const hasCjsExtension = (filename: string): boolean => + path.extname(filename) === '.cjs' \ No newline at end of file diff --git a/test/integration.test.ts b/test/integration.test.ts index 3e1ceaf5..5a2175ee 100644 --- a/test/integration.test.ts +++ b/test/integration.test.ts @@ -284,6 +284,15 @@ const testCases: { expect(stderr).toContain(log) }, }, + { + name: 'esm-pkg-cjs-main-field', + args: [], + async expected(_, { stderr }) { + expect(stderr).toContain( + 'Cannot export main field with .cjs extension in ESM package, only .mjs and .js extensions are allowed', + ) + } + } ] async function runBundle( diff --git a/test/integration/esm-pkg-cjs-main-field/package.json b/test/integration/esm-pkg-cjs-main-field/package.json new file mode 100644 index 00000000..1bd141ab --- /dev/null +++ b/test/integration/esm-pkg-cjs-main-field/package.json @@ -0,0 +1,4 @@ +{ + "type": "module", + "main": "./dist/index.cjs" +} \ No newline at end of file diff --git a/test/integration/esm-pkg-cjs-main-field/src/index.cjs b/test/integration/esm-pkg-cjs-main-field/src/index.cjs new file mode 100644 index 00000000..41984bc0 --- /dev/null +++ b/test/integration/esm-pkg-cjs-main-field/src/index.cjs @@ -0,0 +1 @@ +exports.value = 'cjs'