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

add exports field to support Node.js ESM #3217

Closed
wants to merge 2 commits into from
Closed
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
39 changes: 39 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,45 @@
},
"sideEffects": false,
"homepage": "https://github.com/graphql/graphql-js",
"exports": {
".": {
"require": "./index.js",
"import": "./index.mjs"
},
"./package.json": "./package.json",
"./error": {
"require": "./error/index.js",
"import": "./error/index.mjs"
},
"./execution": {
"require": "./execution/index.js",
"import": "./execution/index.mjs"
},
"./language": {
"require": "./language/index.js",
"import": "./language/index.mjs"
},
"./subscription": {
"require": "./subscription/index.js",
"import": "./subscription/index.mjs"
},
"./type": {
"require": "./type/index.js",
"import": "./type/index.mjs"
},
"./utilities": {
"require": "./utilities/index.js",
"import": "./utilities/index.mjs"
},
"./validation": {
"require": "./validation/index.js",
"import": "./validation/index.mjs"
},
"./*": {
"require": "./*.js",
"import": "./*.mjs"
}
},
"bugs": {
"url": "https://github.com/graphql/graphql-js/issues"
},
Expand Down
21 changes: 18 additions & 3 deletions resources/build-npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ if (require.main === module) {
fs.rmSync('./npmDist', { recursive: true, force: true });
fs.mkdirSync('./npmDist');

const packageJSON = buildPackageJSON();

const srcFiles = readdirRecursive('./src', { ignoreDir: /^__.*__$/ });

const packageJSON = buildPackageJSON(srcFiles);

for (const filepath of srcFiles) {
const srcPath = path.join('./src', filepath);
const destPath = path.join('./npmDist', filepath);
Expand Down Expand Up @@ -91,7 +92,7 @@ function babelBuild(srcPath, options) {
return code + '\n';
}

function buildPackageJSON() {
function buildPackageJSON(srcFiles) {
const packageJSON = JSON.parse(
fs.readFileSync(require.resolve('../package.json'), 'utf-8'),
);
Expand Down Expand Up @@ -122,5 +123,19 @@ function buildPackageJSON() {
packageJSON.publishConfig = { tag: publishTag };
}

/**
* This prevents breaking previous versions of imports with explicit extensions
* Like `require("graphql/language/parser.js")`
*/
for (const srcFile of srcFiles) {
if (srcFile.endsWith('.ts')) {
const srcFilePath = srcFile.slice(0, srcFile.length - 3);
packageJSON.exports[`./${srcFilePath}.js`] = {
require: `./${srcFilePath}.js`,
import: `./${srcFilePath}.mjs`,
};
}
}

return packageJSON;
}