Skip to content

Commit

Permalink
default main=true, if a "." commonjs export exists
Browse files Browse the repository at this point in the history
Fix: #8
  • Loading branch information
isaacs committed Oct 2, 2023
1 parent ad79324 commit 528f8d5
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 11 deletions.
11 changes: 10 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
// get the config and package and stuff

import fail from './fail.js'
import pkg from './package.js'
import sources from './sources.js'
import { Package, TshyConfig } from './types.js'
import validDialects from './valid-dialects.js'
import validExports from './valid-exports.js'

const validBoolean = (e: Record<string, any>, name: string) => {
const v = e[name]
if (v === undefined || typeof v === 'boolean') return true
fail(`tshy.${name} must be a boolean value if specified, got: ` + v)
return process.exit(1)
}

const validConfig = (e: any): e is TshyConfig =>
!!e &&
typeof e === 'object' &&
(e.exports === undefined || validExports(e['exports'])) &&
(e.dialects === undefined || validDialects(e['dialects'])) &&
(e.selfLink === undefined || typeof e.selfLink === 'boolean')
validBoolean(e, 'selfLink') &&
validBoolean(e, 'main')

const getConfig = (
pkg: Package,
Expand Down
6 changes: 3 additions & 3 deletions src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ export const setMain = (
c: TshyConfig | undefined,
pkg: Package & { exports: Record<string, Export> }
) => {
if (!!c?.main) {
c.main = true
const mod = resolveExport(pkg.exports['.'], ['require'])
const mod = resolveExport(pkg.exports['.'], ['require'])
const main = c?.main ?? !!mod
if (main) {
if (!mod) {
fail(`could not resolve exports['.'] for tshy.main 'require'`)
return process.exit(1)
Expand Down
4 changes: 4 additions & 0 deletions tap-snapshots/test/config.ts.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ tshy.dialects must be an array including "esm" and/or "commonjs", got: "yolo"
exports[`test/config.ts > TAP > {"config":{"exports":{"./blah":{"require":"./src/notallowed"}}},"sources":[],"ok":false} > must match snapshot 1`] = `
tshy.exports ./blah unbuilt exports must not be in ./src, and exports in src must be string values. got: {"require":"./src/notallowed"}
`

exports[`test/config.ts > TAP > {"config":{"main":"blah"},"sources":[],"ok":false} > must match snapshot 1`] = `
tshy.main must be a boolean value if specified, got: blah
`
4 changes: 2 additions & 2 deletions tap-snapshots/test/exports.ts.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
exports[`test/exports.ts > TAP > setting top level main > invalid main commonjs > must match snapshot 1`] = `
exports[`test/exports.ts > TAP > setting top level main > invalid main commonjs, no exports > must match snapshot 1`] = `
Array [
Array [
"could not resolve exports['.'] for tshy.main 'require'",
],
]
`

exports[`test/exports.ts > TAP > setting top level main > invalid main commonjs, no exports > must match snapshot 1`] = `
exports[`test/exports.ts > TAP > setting top level main > main explicit true, no commonjs module > must match snapshot 1`] = `
Array [
Array [
"could not resolve exports['.'] for tshy.main 'require'",
Expand Down
2 changes: 2 additions & 0 deletions test/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ const cases: [
exports: { '.': './src/main.ts' },
},
],
//@ts-expect-error
[{ main: 'blah' }, [], false, {}],
]

t.plan(cases.length)
Expand Down
39 changes: 34 additions & 5 deletions test/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ t.test('setting top level main', async t => {
boolean
][] = [
[
'no main set',
'main defaults true',
{
exports: {
'.': {
Expand All @@ -92,11 +92,27 @@ t.test('setting top level main', async t => {
},
},
},
{ main: './r.js', types: './r.d.ts' },
true,
],
[
'main explicit false, removes',
{
tshy: { main: false },
main: './r.js',
types: './r.d.ts',
exports: {
'.': {
require: { types: './r.d.ts', default: './r.js' },
import: { types: './i.d.ts', default: './i.js' },
},
},
},
{},
true,
],
[
'main commonjs',
'main explicit true',
{
tshy: { main: true },
exports: {
Expand Down Expand Up @@ -124,7 +140,7 @@ t.test('setting top level main', async t => {
true,
],
[
'invalid main=false',
'main=false, not set in pj already',
{
tshy: { main: false },
exports: {
Expand All @@ -135,10 +151,23 @@ t.test('setting top level main', async t => {
},
},
{},
true
true,
],
[
'main not set, no commonjs main export',
{
tshy: {},
exports: {
'.': {
import: { types: './i.d.ts', default: './i.js' },
},
},
},
{},
true,
],
[
'invalid main commonjs',
'main explicit true, no commonjs module',
{
tshy: { main: true },
exports: {
Expand Down

0 comments on commit 528f8d5

Please sign in to comment.