Skip to content

Commit db6525e

Browse files
Switch NPM package to support only ESM (#3552)
1 parent 5ccf579 commit db6525e

25 files changed

+137
-163
lines changed

.babelrc-npm.json

+6-23
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,10 @@
11
{
2+
"presets": [
3+
["@babel/preset-env", { "modules": false, "targets": { "node": "14" } }]
4+
],
25
"plugins": [
6+
["./resources/add-extension-to-import-paths.js", { "extension": "js" }],
37
"@babel/plugin-transform-typescript",
4-
"./resources/inline-invariant"
5-
],
6-
"env": {
7-
"cjs": {
8-
"presets": [
9-
[
10-
"@babel/preset-env",
11-
{ "modules": "commonjs", "targets": { "node": "14" } }
12-
]
13-
],
14-
"plugins": [
15-
["./resources/add-extension-to-import-paths", { "extension": "js" }]
16-
]
17-
},
18-
"mjs": {
19-
"presets": [
20-
["@babel/preset-env", { "modules": false, "targets": { "node": "14" } }]
21-
],
22-
"plugins": [
23-
["./resources/add-extension-to-import-paths", { "extension": "mjs" }]
24-
]
25-
}
26-
}
8+
"./resources/inline-invariant.js"
9+
]
2710
}

.eslintrc.yml

+10-6
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ rules:
3535
node/no-exports-assign: error
3636
node/no-extraneous-import: error
3737
node/no-extraneous-require: error
38-
node/no-missing-import: error
38+
node/no-missing-import: [error, { allowModules: ['graphql'] }]
3939
node/no-missing-require: error
4040
node/no-new-require: error
4141
node/no-path-concat: error
@@ -81,7 +81,7 @@ rules:
8181

8282
# Static analysis
8383
# https://github.com/benmosher/eslint-plugin-import#static-analysis
84-
import/no-unresolved: error
84+
import/no-unresolved: [error, { ignore: ['graphql'] }]
8585
import/named: error
8686
import/default: error
8787
import/namespace: error
@@ -671,23 +671,24 @@ overrides:
671671
import/no-extraneous-dependencies: [error, { devDependencies: true }]
672672
import/no-nodejs-modules: off
673673
- files: 'integrationTests/*/**'
674+
parserOptions:
675+
sourceType: module
674676
env:
675677
node: true
676678
rules:
677679
node/no-sync: off
678-
node/no-missing-require: [error, { allowModules: ['graphql'] }]
679-
import/no-commonjs: off
680680
import/no-nodejs-modules: off
681681
no-console: off
682682
- files: 'benchmark/**'
683+
parserOptions:
684+
sourceType: module
683685
env:
684686
node: true
685687
rules:
686688
internal-rules/only-ascii: [error, { allowEmoji: true }]
687689
node/no-sync: off
688-
node/no-missing-require: off
690+
import/no-unresolved: off
689691
import/no-nodejs-modules: off
690-
import/no-commonjs: off
691692
no-console: off
692693
no-await-in-loop: off
693694
- files: 'resources/**'
@@ -730,3 +731,6 @@ overrides:
730731
# Ignore docusarus related webpack aliases
731732
import/no-unresolved:
732733
['error', { 'ignore': ['^@theme', '^@docusaurus', '^@generated'] }]
734+
- files: 'benchmark/benchmark.js'
735+
parserOptions:
736+
sourceType: script

benchmark/benchmark.js

+22-12
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,21 @@ function prepareBenchmarkProjects(revisionList) {
5858
path.join(projectPath, 'package.json'),
5959
'{ "private": true }',
6060
);
61-
exec(
62-
'npm --quiet install --ignore-scripts ' + prepareNPMPackage(revision),
63-
{ cwd: projectPath },
64-
);
6561
exec(`cp -R ${localDir('benchmark')} ${projectPath}`);
6662

63+
const packageJSON = {
64+
private: true,
65+
type: 'module',
66+
dependencies: {
67+
graphql: prepareNPMPackage(revision),
68+
},
69+
};
70+
fs.writeFileSync(
71+
path.join(projectPath, 'package.json'),
72+
JSON.stringify(packageJSON, null, 2),
73+
);
74+
exec('npm --quiet install --ignore-scripts ', { cwd: projectPath });
75+
6776
return { revision, projectPath };
6877
});
6978

@@ -334,21 +343,21 @@ function grey(str) {
334343

335344
function sampleModule(modulePath) {
336345
const sampleCode = `
337-
const assert = require('assert');
338-
346+
import assert from 'assert';
339347
assert(global.gc);
340348
assert(process.send);
341-
const module = require('${modulePath}');
342349
343-
clock(7, module.measure); // warm up
350+
import { benchmark } from '${modulePath}';
351+
352+
clock(7, benchmark.measure); // warm up
344353
global.gc();
345354
process.nextTick(() => {
346355
const memBaseline = process.memoryUsage().heapUsed;
347-
const clocked = clock(module.count, module.measure);
356+
const clocked = clock(benchmark.count, benchmark.measure);
348357
process.send({
349-
name: module.name,
350-
clocked: clocked / module.count,
351-
memUsed: (process.memoryUsage().heapUsed - memBaseline) / module.count,
358+
name: benchmark.name,
359+
clocked: clocked / benchmark.count,
360+
memUsed: (process.memoryUsage().heapUsed - memBaseline) / benchmark.count,
352361
});
353362
});
354363
@@ -369,6 +378,7 @@ function sampleModule(modulePath) {
369378
'--no-concurrent-sweeping',
370379
'--predictable',
371380
'--expose-gc',
381+
'--input-type=module',
372382
'--eval',
373383
sampleCode,
374384
],

benchmark/buildASTSchema-benchmark.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
'use strict';
1+
import { parse } from 'graphql/language/parser.js';
2+
import { buildASTSchema } from 'graphql/utilities/buildASTSchema.js';
23

3-
const { parse } = require('graphql/language/parser.js');
4-
const { buildASTSchema } = require('graphql/utilities/buildASTSchema.js');
5-
6-
const { bigSchemaSDL } = require('./fixtures.js');
4+
import { bigSchemaSDL } from './fixtures.js';
75

86
const schemaAST = parse(bigSchemaSDL);
97

10-
module.exports = {
8+
export const benchmark = {
119
name: 'Build Schema from AST',
1210
count: 10,
1311
measure() {

benchmark/buildClientSchema-benchmark.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
'use strict';
1+
import { buildClientSchema } from 'graphql/utilities/buildClientSchema.js';
22

3-
const { buildClientSchema } = require('graphql/utilities/buildClientSchema.js');
3+
import { bigSchemaIntrospectionResult } from './fixtures.js';
44

5-
const { bigSchemaIntrospectionResult } = require('./fixtures.js');
6-
7-
module.exports = {
5+
export const benchmark = {
86
name: 'Build Schema from Introspection',
97
count: 10,
108
measure() {

benchmark/fixtures.js

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
'use strict';
1+
import fs from 'fs';
22

3-
const fs = require('fs');
4-
const path = require('path');
5-
6-
exports.bigSchemaSDL = fs.readFileSync(
7-
path.join(__dirname, 'github-schema.graphql'),
3+
export const bigSchemaSDL = fs.readFileSync(
4+
new URL('github-schema.graphql', import.meta.url),
85
'utf8',
96
);
107

11-
exports.bigSchemaIntrospectionResult = JSON.parse(
12-
fs.readFileSync(path.join(__dirname, 'github-schema.json'), 'utf8'),
8+
export const bigSchemaIntrospectionResult = JSON.parse(
9+
fs.readFileSync(new URL('github-schema.json', import.meta.url), 'utf8'),
1310
);

benchmark/introspectionFromSchema-benchmark.js

+7-11
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
1-
'use strict';
1+
import { executeSync } from 'graphql/execution/execute.js';
2+
import { parse } from 'graphql/language/parser.js';
3+
import { buildSchema } from 'graphql/utilities/buildASTSchema.js';
4+
import { getIntrospectionQuery } from 'graphql/utilities/getIntrospectionQuery.js';
25

3-
const { parse } = require('graphql/language/parser.js');
4-
const { executeSync } = require('graphql/execution/execute.js');
5-
const { buildSchema } = require('graphql/utilities/buildASTSchema.js');
6-
const {
7-
getIntrospectionQuery,
8-
} = require('graphql/utilities/getIntrospectionQuery.js');
9-
10-
const { bigSchemaSDL } = require('./fixtures.js');
6+
import { bigSchemaSDL } from './fixtures.js';
117

128
const schema = buildSchema(bigSchemaSDL, { assumeValid: true });
139
const document = parse(getIntrospectionQuery());
1410

15-
module.exports = {
11+
export const benchmark = {
1612
name: 'Execute Introspection Query',
17-
count: 10,
13+
count: 30,
1814
measure() {
1915
executeSync({ schema, document });
2016
},

benchmark/parser-benchmark.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
'use strict';
2-
3-
const { parse } = require('graphql/language/parser.js');
4-
const {
5-
getIntrospectionQuery,
6-
} = require('graphql/utilities/getIntrospectionQuery.js');
1+
import { parse } from 'graphql/language/parser.js';
2+
import { getIntrospectionQuery } from 'graphql/utilities/getIntrospectionQuery.js';
73

84
const introspectionQuery = getIntrospectionQuery();
95

10-
module.exports = {
6+
export const benchmark = {
117
name: 'Parse introspection query',
128
count: 1000,
139
measure() {

benchmark/validateGQL-benchmark.js

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
'use strict';
1+
import { parse } from 'graphql/language/parser.js';
2+
import { buildSchema } from 'graphql/utilities/buildASTSchema.js';
3+
import { getIntrospectionQuery } from 'graphql/utilities/getIntrospectionQuery.js';
4+
import { validate } from 'graphql/validation/validate.js';
25

3-
const { parse } = require('graphql/language/parser.js');
4-
const { validate } = require('graphql/validation/validate.js');
5-
const { buildSchema } = require('graphql/utilities/buildASTSchema.js');
6-
const {
7-
getIntrospectionQuery,
8-
} = require('graphql/utilities/getIntrospectionQuery.js');
9-
10-
const { bigSchemaSDL } = require('./fixtures.js');
6+
import { bigSchemaSDL } from './fixtures.js';
117

128
const schema = buildSchema(bigSchemaSDL, { assumeValid: true });
139
const queryAST = parse(getIntrospectionQuery());
1410

15-
module.exports = {
11+
export const benchmark = {
1612
name: 'Validate Introspection Query',
1713
count: 50,
1814
measure() {

benchmark/validateInvalidGQL-benchmark.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
'use strict';
1+
import { parse } from 'graphql/language/parser.js';
2+
import { buildSchema } from 'graphql/utilities/buildASTSchema.js';
3+
import { validate } from 'graphql/validation/validate.js';
24

3-
const { parse } = require('graphql/language/parser.js');
4-
const { validate } = require('graphql/validation/validate.js');
5-
const { buildSchema } = require('graphql/utilities/buildASTSchema.js');
6-
7-
const { bigSchemaSDL } = require('./fixtures.js');
5+
import { bigSchemaSDL } from './fixtures.js';
86

97
const schema = buildSchema(bigSchemaSDL, { assumeValid: true });
108
const queryAST = parse(`
@@ -21,7 +19,7 @@ const queryAST = parse(`
2119
}
2220
`);
2321

24-
module.exports = {
22+
export const benchmark = {
2523
name: 'Validate Invalid Query',
2624
count: 50,
2725
measure() {

benchmark/validateSDL-benchmark.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
'use strict';
1+
import { parse } from 'graphql/language/parser.js';
2+
import { validateSDL } from 'graphql/validation/validate.js';
23

3-
const { parse } = require('graphql/language/parser.js');
4-
const { validateSDL } = require('graphql/validation/validate.js');
5-
6-
const { bigSchemaSDL } = require('./fixtures.js');
4+
import { bigSchemaSDL } from './fixtures.js';
75

86
const sdlAST = parse(bigSchemaSDL);
97

10-
module.exports = {
8+
export const benchmark = {
119
name: 'Validate SDL Document',
1210
count: 10,
1311
measure() {

benchmark/visit-benchmark.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict';
1+
import { parse } from 'graphql/language/parser.js';
2+
import { visit } from 'graphql/language/visitor.js';
23

3-
const { parse } = require('graphql/language/parser.js');
4-
const { visit } = require('graphql/language/visitor.js');
5-
6-
const { bigSchemaSDL } = require('./fixtures.js');
4+
import { bigSchemaSDL } from './fixtures.js';
75

86
const documentAST = parse(bigSchemaSDL);
97

@@ -16,7 +14,7 @@ const visitor = {
1614
},
1715
};
1816

19-
module.exports = {
17+
export const benchmark = {
2018
name: 'Visit all AST nodes',
2119
count: 10,
2220
measure() {

benchmark/visitInParallel-benchmark.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict';
1+
import { parse } from 'graphql/language/parser.js';
2+
import { visit, visitInParallel } from 'graphql/language/visitor.js';
23

3-
const { parse } = require('graphql/language/parser.js');
4-
const { visit, visitInParallel } = require('graphql/language/visitor.js');
5-
6-
const { bigSchemaSDL } = require('./fixtures.js');
4+
import { bigSchemaSDL } from './fixtures.js';
75

86
const documentAST = parse(bigSchemaSDL);
97

@@ -16,7 +14,7 @@ const visitors = new Array(50).fill({
1614
},
1715
});
1816

19-
module.exports = {
17+
export const benchmark = {
2018
name: 'Visit all AST nodes in parallel',
2119
count: 10,
2220
measure() {

integrationTests/node/index.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
'use strict';
1+
/* eslint-disable simple-import-sort/imports */
2+
import assert from 'assert';
3+
import { readFileSync } from 'fs';
24

3-
const assert = require('assert');
4-
const { readFileSync } = require('fs');
5-
6-
const { version, graphqlSync } = require('graphql');
7-
const { buildSchema } = require('graphql/utilities');
5+
import { graphqlSync } from 'graphql';
6+
import { buildSchema } from 'graphql/utilities';
7+
import { version } from 'graphql/version';
88

99
assert.deepStrictEqual(
1010
version,

integrationTests/node/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"private": true,
33
"description": "graphql-js should work on all supported node versions",
4+
"type": "module",
45
"scripts": {
56
"test": "node test.js"
67
},

integrationTests/node/test.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
'use strict';
1+
import childProcess from 'child_process';
2+
import fs from 'fs';
23

3-
const childProcess = require('child_process');
4-
5-
const graphqlPackageJSON = require('graphql/package.json');
4+
const graphqlPackageJSON = JSON.parse(
5+
fs.readFileSync('./node_modules/graphql/package.json', 'utf-8'),
6+
);
67

78
const nodeVersions = graphqlPackageJSON.engines.node
89
.split(' || ')

0 commit comments

Comments
 (0)