Skip to content

Commit

Permalink
Add module to specify for older systems
Browse files Browse the repository at this point in the history
PR-URL: #79
Credit: @mpodwysocki
Close: #79
Reviewed-by: @isaacs

EDIT(@isaacs): Add to docs
  • Loading branch information
mpodwysocki authored and isaacs committed Jul 3, 2024
1 parent 26efe04 commit 7004601
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 4 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ If the `selfLink` config is not explicitly set, and creating the
symlink fails (common on Windows systems where `fs.symlink()` may
require elevated permissions), then the error will be ignored.
### Old Style Exports
### Old Style Exports (top-level `main`, `module`, `types`)
Versions of node prior to 12.10.0, published in early to mid
2016, did not have support for `exports` as a means for defining
Expand All @@ -574,6 +574,12 @@ If there is a `commonjs` export of the `"."` subpath, and the
tshy will use that to set the `main` and `types` fields, for
compatibility with these tools.
Similarly, some tools rely on a top-level `module` field, which
is the ESM equivalent to `"main"`. If there is an `esm` export of
the `"."` subpath, and the `tshy.module` field in package.json is
not set to `false`, then tshy will use that to set the `module`
field, for compatibility with these tools.
**Warning: relying on top-level main/types will likely cause
incorrect types to be loaded in some scenarios.**
Expand All @@ -588,6 +594,11 @@ not created, or if the `"."` export does not support the
`commonjs` dialect, and `main` is explicitly set to `true`, then
the build will fail.
If the `esm` dialect is not built, or if a `"."` export is not
created, or if the `"."` export does not support the `esm`
dialect, and `module` is explicitly set to `true`, then the build
will fail.
For example, this config:
```json
Expand All @@ -605,6 +616,7 @@ will produce:
```json
{
"main": "./dist/commonjs/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/commonjs/index.d.ts",
"type": "module",
"exports": {
Expand Down
18 changes: 16 additions & 2 deletions src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,24 @@ export const setMain = (
delete pkg.types
}
pkg.type = pkg.type === 'commonjs' ? 'commonjs' : 'module'

// Set the package module to exports["."]
const importMod = resolveExport(pkg.exports['.'], ['import'])
const module = c?.module ?? !!importMod
if (module) {
if (!importMod) {
fail(`could not resolve exports['.'] for tshy.module 'import'`)
return process.exit(1)
}

pkg.module = importMod
} else {
if (c && c.module !== false) delete c.module
delete pkg.module
}
}

// These are all defined by exports, so it's just confusing otherwise
delete pkg.module
pkg.exports = getExports(config)

setMain(config, pkg as Package & { exports: ExportsSubpaths })
export default pkg.exports
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type TshyConfigMaybeGlobExports = {
dialects?: Dialect[]
selfLink?: boolean
main?: boolean
module?: boolean
commonjsDialects?: string[]
esmDialects?: string[]
project?: string
Expand Down
8 changes: 8 additions & 0 deletions tap-snapshots/test/exports.ts.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,14 @@ Array [
]
`

exports[`test/exports.ts > TAP > setting top level main > invalid module esm, no exports > must match snapshot 1`] = `
Array [
Array [
"could not resolve exports['.'] for tshy.module 'import'",
],
]
`

exports[`test/exports.ts > TAP > setting top level main > main explicit true, no commonjs module > must match snapshot 1`] = `
Array [
Array [
Expand Down
56 changes: 55 additions & 1 deletion test/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,13 @@ t.test('setting top level main', async t => {
tshy?: TshyConfig
exports: Record<string, ConditionalValue>
main?: string
module?: string
types?: string
type?: string
},
{
main?: string
module?: string
types?: string
},
boolean,
Expand Down Expand Up @@ -205,6 +207,58 @@ t.test('setting top level main', async t => {
{},
false,
],
[
'module defaults true',
{
exports: {
'.': {
require: { types: './r.d.ts', default: './r.js' },
import: { types: './i.d.ts', default: './i.js' },
},
},
},
{ main: './r.js', types: './r.d.ts', module: './i.js' },
true,
],
[
'module explicit true',
{
tshy: { module: true },
exports: {
'.': {
require: { types: './r.d.ts', default: './r.js' },
import: { types: './i.d.ts', default: './i.js' },
},
},
},
{ main: './r.js', types: './r.d.ts', module: './i.js' },
true,
],
[
'module explicit false, removes',
{
tshy: { module: 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' },
},
},
},
{ main: './r.js', types: './r.d.ts' },
true,
],
[
'invalid module esm, no exports',
{
tshy: { module: true },
exports: {},
},
{},
false,
],
[
'type defaults module',
{
Expand Down Expand Up @@ -232,7 +286,7 @@ t.test('setting top level main', async t => {
true,
],
[
'invalide type',
'invalid type',
{
type: 'invalid type',
exports: {},
Expand Down

0 comments on commit 7004601

Please sign in to comment.