Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(esm): added export mapping to package.json for proper cjs/esm entrypoints #4081

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
197 changes: 195 additions & 2 deletions resources/build-npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function buildPackage(outDir: string, isESMOnly: boolean): void {
const versionTag = splittedTag[2] ?? splittedTag[0];
assert(
['alpha', 'beta', 'rc'].includes(versionTag) ||
versionTag.startsWith('experimental-'),
versionTag.startsWith('experimental-'),
`"${versionTag}" tag is not supported.`,
);
assert.equal(
Expand Down Expand Up @@ -105,8 +105,201 @@ function buildPackage(outDir: string, isESMOnly: boolean): void {
packageJSON.version += '+esm';
} else {
delete packageJSON.type;
packageJSON.main = 'index';
packageJSON.main = 'index.js';
packageJSON.module = 'index.mjs';
packageJSON.types = 'index.d.ts';
packageJSON.exports = {
'.': {
import: {
types: './index.d.ts',
default: './index.mjs'
},
require: {
types: './index.d.ts',
default: './index.js'
}
},
'./graphql': {
import: {
types: './graphql.d.ts',
default: './graphql.mjs'
},
require: {
types: './graphql.d.ts',
default: './graphql.js'
}
},
'./version': {
import: {
types: './version.d.ts',
default: './version.mjs'
},
require: {
types: './version.d.ts',
default: './version.js'
}
},
'./error': {
import: {
types: './error/index.d.ts',
default: './error/index.mjs'
},
require: {
types: './error/index.d.ts',
default: './error/index.js'
}
},
'./error/*': {
import: {
types: './error/*.d.ts',
default: './error/*.mjs'
},
require: {
types: './error/*.d.ts',
default: './error/*.js'
}
},
'./execution': {
import: {
types: './execution/index.d.ts',
default: './execution/index.mjs'
},
require: {
types: './execution/index.d.ts',
default: './execution/index.js'
}
},
'./execution/*': {
import: {
types: './execution/*.d.ts',
default: './execution/*.mjs'
},
require: {
types: './execution/*.d.ts',
default: './execution/*.js'
}
},
'./jsutils': {
import: {
types: './jsutils/index.d.ts',
default: './jsutils/index.mjs'
},
require: {
types: './jsutils/index.d.ts',
default: './jsutils/index.js'
}
},
'./jsutils/*': {
import: {
types: './jsutils/*.d.ts',
default: './jsutils/*.mjs'
},
require: {
types: './jsutils/*.d.ts',
default: './jsutils/*.js'
}
},
'./language': {
import: {
types: './language/index.d.ts',
default: './language/index.mjs'
},
require: {
types: './language/index.d.ts',
default: './language/index.js'
}
},
'./language/*': {
import: {
types: './language/*.d.ts',
default: './language/*.mjs'
},
require: {
types: './language/*.d.ts',
default: './language/*.js'
}
},
'./subscription': {
import: {
types: './subscription/index.d.ts',
default: './subscription/index.mjs'
},
require: {
types: './subscription/index.d.ts',
default: './subscription/index.js'
}
},
'./subscription/*': {
import: {
types: './subscription/*.d.ts',
default: './subscription/*.mjs'
},
require: {
types: './subscription/*.d.ts',
default: './subscription/*.js'
}
},
'./type': {
import: {
types: './type/index.d.ts',
default: './type/index.mjs'
},
require: {
types: './type/index.d.ts',
default: './type/index.js'
}
},
'./type/*': {
import: {
types: './type/*.d.ts',
default: './type/*.mjs'
},
require: {
types: './type/*.d.ts',
default: './type/*.js'
}
},
'./utilities': {
import: {
types: './utilities/index.d.ts',
default: './utilities/index.mjs'
},
require: {
types: './utilities/index.d.ts',
default: './utilities/index.js'
}
},
'./utilities/*': {
import: {
types: './utilities/*.d.ts',
default: './utilities/*.mjs'
},
require: {
types: './utilities/*.d.ts',
default: './utilities/*.js'
}
},
'./validation': {
import: {
types: './validation/index.d.ts',
default: './validation/index.mjs'
},
require: {
types: './validation/index.d.ts',
default: './validation/index.js'
}
},
'./validation/*': {
import: {
types: './validation/*.d.ts',
default: './validation/*.mjs'
},
require: {
types: './validation/*.d.ts',
default: './validation/*.js'
}
}
}
emitTSFiles({ outDir, module: 'commonjs', extension: '.js' });
emitTSFiles({ outDir, module: 'es2020', extension: '.mjs' });
}
Expand Down
13 changes: 12 additions & 1 deletion resources/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,25 @@ export function writeGeneratedFile(filepath: string, body: string): void {
fs.writeFileSync(filepath, formatted);
}

interface PackageJsonExportMapping {
import: {
types: string;
default: string;
};
require: {
types: string;
default: string;
}
}

interface PackageJSON {
description: string;
version: string;
private?: boolean;
repository?: { url?: string };
scripts?: { [name: string]: string };
type?: string;
exports: { [path: string]: string };
exports: { [path: string]: string | PackageJsonExportMapping };
types?: string;
typesVersions: { [ranges: string]: { [path: string]: Array<string> } };
devDependencies?: { [name: string]: string };
Expand Down