Skip to content

Commit a1d4aa3

Browse files
authoredApr 9, 2024··
feat: support ESM-only packages (#701)
1 parent 81f806c commit a1d4aa3

14 files changed

+158
-9
lines changed
 

‎playground/browser-bundle/package.json

+9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@
44
"private": true,
55
"license": "MIT",
66
"type": "module",
7+
"exports": {
8+
".": {
9+
"source": "./src/index.js",
10+
"import": "./dist/index.js",
11+
"require": "./dist/index.cjs",
12+
"default": "./dist/index.js"
13+
},
14+
"./package.json": "./package.json"
15+
},
716
"main": "./dist/index.cjs",
817
"module": "./dist/index.js",
918
"source": "./src/index.js",

‎playground/extra-bundles/package.json

+9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@
77
"main": "./dist/index.js",
88
"module": "./dist/index.mjs",
99
"source": "./src/index.js",
10+
"exports": {
11+
".": {
12+
"source": "./src/index.js",
13+
"import": "./dist/index.mjs",
14+
"require": "./dist/index.js",
15+
"default": "./dist/index.js"
16+
},
17+
"./package.json": "./package.json"
18+
},
1019
"scripts": {
1120
"build": "run-s clean && pkg build --strict && pkg check --strict",
1221
"clean": "rimraf dist"

‎playground/js/package.json

+9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@
44
"private": true,
55
"license": "MIT",
66
"type": "commonjs",
7+
"exports": {
8+
".": {
9+
"source": "./src/index.js",
10+
"import": "./dist/index.mjs",
11+
"require": "./dist/index.js",
12+
"default": "./dist/index.js"
13+
},
14+
"./package.json": "./package.json"
15+
},
716
"main": "./dist/index.js",
817
"module": "./dist/index.mjs",
918
"source": "./src/index.js",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import {defineConfig} from '@sanity/pkg-utils'
2+
3+
export default defineConfig({
4+
tsconfig: 'tsconfig.dist.json',
5+
})

‎playground/reference-esm-only/package.json

+46-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,50 @@
22
"name": "reference-esm-only",
33
"version": "0.0.0",
44
"private": true,
5-
"description": "A reference implementation of TypeScript 5.4 best practice for a module that is ESM by default, and does not offer CJS."
5+
"description": "A reference implementation of TypeScript 5.4 best practice for a module that is ESM by default, and does not offer CJS.",
6+
"license": "MIT",
7+
"sideEffects": false,
8+
"type": "module",
9+
"exports": {
10+
".": {
11+
"source": "./src/index.ts",
12+
"browser": {
13+
"source": "./src/index.ts",
14+
"import": "./dist/index.browser.js"
15+
},
16+
"import": "./dist/index.js",
17+
"default": "./dist/index.js"
18+
},
19+
"./extra": {
20+
"source": "./src/extra.ts",
21+
"browser": {
22+
"source": "./src/extra.ts",
23+
"import": "./dist/extra.browser.js"
24+
},
25+
"import": "./dist/extra.js",
26+
"default": "./dist/extra.js"
27+
},
28+
"./package.json": "./package.json"
29+
},
30+
"main": "./dist/index.js",
31+
"types": "./dist/index.d.ts",
32+
"typesVersions": {
33+
"*": {
34+
"extra": [
35+
"./dist/extra.d.ts"
36+
]
37+
}
38+
},
39+
"files": [
40+
"dist",
41+
"src"
42+
],
43+
"scripts": {
44+
"build": "run-s clean && pkg build --strict && pkg check --strict",
45+
"clean": "rimraf dist"
46+
},
47+
"devDependencies": {
48+
"@tsconfig/node-lts": "20.1.3",
49+
"typescript": "5.4.4"
50+
}
651
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/** @public */
2+
export const format = process.env.PKG_FORMAT as string
3+
4+
/** @public */
5+
export const runtime = process.env.PKG_RUNTIME as string
6+
7+
/** @public */
8+
export const path = process.env.PKG_FILE_PATH as string
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/** @public */
2+
export const format = process.env.PKG_FORMAT as string
3+
4+
/** @public */
5+
export const runtime = process.env.PKG_RUNTIME as string
6+
7+
/** @public */
8+
export const path = process.env.PKG_FILE_PATH as string
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "./tsconfig.settings",
3+
"include": ["./src"],
4+
"compilerOptions": {
5+
"outDir": "./dist",
6+
"rootDir": "."
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "./tsconfig.settings",
3+
"include": ["./src"],
4+
"compilerOptions": {
5+
"noEmit": true
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "@tsconfig/node-lts/tsconfig.json",
3+
"compilerOptions": {
4+
"module": "Preserve",
5+
"moduleResolution": "Bundler",
6+
"esModuleInterop": true,
7+
"resolveJsonModule": true,
8+
"allowSyntheticDefaultImports": true
9+
}
10+
}

‎pnpm-lock.yaml

+8-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/node/core/pkg/parseExports.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,21 @@ export function parseExports(options: {
2121
require: pkg.main && pkg.browser[pkg.main],
2222
import: pkg.module && pkg.browser[pkg.module],
2323
},
24-
require: pkg.main,
25-
import: pkg.module,
26-
default: pkg.module || pkg.main || '',
24+
require: legacyExports
25+
? pkg.main
26+
: typeof pkg.exports?.['.'] === 'object' && 'require' in pkg.exports['.']
27+
? pkg.exports['.'].require
28+
: '',
29+
import: legacyExports
30+
? pkg.module
31+
: typeof pkg.exports?.['.'] === 'object' && 'import' in pkg.exports['.']
32+
? pkg.exports['.'].import
33+
: '',
34+
default: legacyExports
35+
? pkg.module || pkg.main || ''
36+
: typeof pkg.exports?.['.'] === 'object' && 'default' in pkg.exports['.']
37+
? pkg.exports['.'].default || ''
38+
: pkg.module || pkg.main || '',
2739
}
2840

2941
const extraExports: (PkgExport & {_path: string})[] = []

‎test/parseExports.test.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe('parseExports', () => {
3131
types: undefined,
3232
source: './src/index.ts',
3333
browser: undefined,
34-
import: undefined,
34+
import: '',
3535
require: './dist/index.js',
3636
default: './dist/index.js',
3737
},
@@ -124,9 +124,7 @@ describe('parseExports', () => {
124124
}
125125

126126
expect(() => parseExports({pkg, strict: true, legacyExports: false})).toThrow(
127-
'\n- package.json: mismatch between "main" and "exports.require". These must be equal.' +
128-
'\n- package.json: mismatch between "module" and "exports.import" These must be equal.' +
129-
'\n- package.json: `exports["./package.json"] must be "./package.json".' +
127+
'\n- package.json: `exports["./package.json"] must be "./package.json".' +
130128
'\n- package.json with `type: "commonjs"` - `exports["."].require` must end with ".js"' +
131129
'\n- package.json with `type: "commonjs"` - `exports["."].import` must end with ".mjs"',
132130
)

‎test/parseTasks.test.ts

+14
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@ test('should parse tasks (type: module)', () => {
1515
'./dist/index.cjs': './dist/index.browser.cjs',
1616
'./dist/index.js': './dist/index.browser.js',
1717
},
18+
exports: {
19+
'.': {
20+
source: './src/index.ts',
21+
browser: {
22+
source: './src/index.ts',
23+
import: './dist/index.browser.js',
24+
require: './dist/index.browser.cjs',
25+
},
26+
import: './dist/index.js',
27+
require: './dist/index.cjs',
28+
default: './dist/index.js',
29+
},
30+
'./package.json': './package.json',
31+
},
1832
}
1933

2034
const exports = parseExports({pkg, strict: true, legacyExports: false})

0 commit comments

Comments
 (0)
Please sign in to comment.