Skip to content

Commit

Permalink
Error on invalid main field export (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi authored Oct 6, 2023
1 parent 6caf939 commit edf5464
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
{
Expand Down
3 changes: 2 additions & 1 deletion src/lib/wildcard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
hasAvailableExtension,
nonNullable,
} from '../utils'
import { logger } from '../logger'

// TODO: support nested wildcard exportsCondition (e.g. './foo/*')
const getWildcardExports = (
Expand Down Expand Up @@ -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',
Expand Down
3 changes: 3 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
9 changes: 9 additions & 0 deletions test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 4 additions & 0 deletions test/integration/esm-pkg-cjs-main-field/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": "module",
"main": "./dist/index.cjs"
}
1 change: 1 addition & 0 deletions test/integration/esm-pkg-cjs-main-field/src/index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.value = 'cjs'

0 comments on commit edf5464

Please sign in to comment.