Skip to content

Commit

Permalink
Switch NPM package to support only ESM (#3552)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov authored May 2, 2022
1 parent 5ccf579 commit db6525e
Show file tree
Hide file tree
Showing 25 changed files with 137 additions and 163 deletions.
29 changes: 6 additions & 23 deletions .babelrc-npm.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
{
"presets": [
["@babel/preset-env", { "modules": false, "targets": { "node": "14" } }]
],
"plugins": [
["./resources/add-extension-to-import-paths.js", { "extension": "js" }],
"@babel/plugin-transform-typescript",
"./resources/inline-invariant"
],
"env": {
"cjs": {
"presets": [
[
"@babel/preset-env",
{ "modules": "commonjs", "targets": { "node": "14" } }
]
],
"plugins": [
["./resources/add-extension-to-import-paths", { "extension": "js" }]
]
},
"mjs": {
"presets": [
["@babel/preset-env", { "modules": false, "targets": { "node": "14" } }]
],
"plugins": [
["./resources/add-extension-to-import-paths", { "extension": "mjs" }]
]
}
}
"./resources/inline-invariant.js"
]
}
16 changes: 10 additions & 6 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ rules:
node/no-exports-assign: error
node/no-extraneous-import: error
node/no-extraneous-require: error
node/no-missing-import: error
node/no-missing-import: [error, { allowModules: ['graphql'] }]
node/no-missing-require: error
node/no-new-require: error
node/no-path-concat: error
Expand Down Expand Up @@ -81,7 +81,7 @@ rules:

# Static analysis
# https://github.com/benmosher/eslint-plugin-import#static-analysis
import/no-unresolved: error
import/no-unresolved: [error, { ignore: ['graphql'] }]
import/named: error
import/default: error
import/namespace: error
Expand Down Expand Up @@ -671,23 +671,24 @@ overrides:
import/no-extraneous-dependencies: [error, { devDependencies: true }]
import/no-nodejs-modules: off
- files: 'integrationTests/*/**'
parserOptions:
sourceType: module
env:
node: true
rules:
node/no-sync: off
node/no-missing-require: [error, { allowModules: ['graphql'] }]
import/no-commonjs: off
import/no-nodejs-modules: off
no-console: off
- files: 'benchmark/**'
parserOptions:
sourceType: module
env:
node: true
rules:
internal-rules/only-ascii: [error, { allowEmoji: true }]
node/no-sync: off
node/no-missing-require: off
import/no-unresolved: off
import/no-nodejs-modules: off
import/no-commonjs: off
no-console: off
no-await-in-loop: off
- files: 'resources/**'
Expand Down Expand Up @@ -730,3 +731,6 @@ overrides:
# Ignore docusarus related webpack aliases
import/no-unresolved:
['error', { 'ignore': ['^@theme', '^@docusaurus', '^@generated'] }]
- files: 'benchmark/benchmark.js'
parserOptions:
sourceType: script
34 changes: 22 additions & 12 deletions benchmark/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,21 @@ function prepareBenchmarkProjects(revisionList) {
path.join(projectPath, 'package.json'),
'{ "private": true }',
);
exec(
'npm --quiet install --ignore-scripts ' + prepareNPMPackage(revision),
{ cwd: projectPath },
);
exec(`cp -R ${localDir('benchmark')} ${projectPath}`);

const packageJSON = {
private: true,
type: 'module',
dependencies: {
graphql: prepareNPMPackage(revision),
},
};
fs.writeFileSync(
path.join(projectPath, 'package.json'),
JSON.stringify(packageJSON, null, 2),
);
exec('npm --quiet install --ignore-scripts ', { cwd: projectPath });

return { revision, projectPath };
});

Expand Down Expand Up @@ -334,21 +343,21 @@ function grey(str) {

function sampleModule(modulePath) {
const sampleCode = `
const assert = require('assert');
import assert from 'assert';
assert(global.gc);
assert(process.send);
const module = require('${modulePath}');
clock(7, module.measure); // warm up
import { benchmark } from '${modulePath}';
clock(7, benchmark.measure); // warm up
global.gc();
process.nextTick(() => {
const memBaseline = process.memoryUsage().heapUsed;
const clocked = clock(module.count, module.measure);
const clocked = clock(benchmark.count, benchmark.measure);
process.send({
name: module.name,
clocked: clocked / module.count,
memUsed: (process.memoryUsage().heapUsed - memBaseline) / module.count,
name: benchmark.name,
clocked: clocked / benchmark.count,
memUsed: (process.memoryUsage().heapUsed - memBaseline) / benchmark.count,
});
});
Expand All @@ -369,6 +378,7 @@ function sampleModule(modulePath) {
'--no-concurrent-sweeping',
'--predictable',
'--expose-gc',
'--input-type=module',
'--eval',
sampleCode,
],
Expand Down
10 changes: 4 additions & 6 deletions benchmark/buildASTSchema-benchmark.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
'use strict';
import { parse } from 'graphql/language/parser.js';
import { buildASTSchema } from 'graphql/utilities/buildASTSchema.js';

const { parse } = require('graphql/language/parser.js');
const { buildASTSchema } = require('graphql/utilities/buildASTSchema.js');

const { bigSchemaSDL } = require('./fixtures.js');
import { bigSchemaSDL } from './fixtures.js';

const schemaAST = parse(bigSchemaSDL);

module.exports = {
export const benchmark = {
name: 'Build Schema from AST',
count: 10,
measure() {
Expand Down
8 changes: 3 additions & 5 deletions benchmark/buildClientSchema-benchmark.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict';
import { buildClientSchema } from 'graphql/utilities/buildClientSchema.js';

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

const { bigSchemaIntrospectionResult } = require('./fixtures.js');

module.exports = {
export const benchmark = {
name: 'Build Schema from Introspection',
count: 10,
measure() {
Expand Down
13 changes: 5 additions & 8 deletions benchmark/fixtures.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
'use strict';
import fs from 'fs';

const fs = require('fs');
const path = require('path');

exports.bigSchemaSDL = fs.readFileSync(
path.join(__dirname, 'github-schema.graphql'),
export const bigSchemaSDL = fs.readFileSync(
new URL('github-schema.graphql', import.meta.url),
'utf8',
);

exports.bigSchemaIntrospectionResult = JSON.parse(
fs.readFileSync(path.join(__dirname, 'github-schema.json'), 'utf8'),
export const bigSchemaIntrospectionResult = JSON.parse(
fs.readFileSync(new URL('github-schema.json', import.meta.url), 'utf8'),
);
18 changes: 7 additions & 11 deletions benchmark/introspectionFromSchema-benchmark.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
'use strict';
import { executeSync } from 'graphql/execution/execute.js';
import { parse } from 'graphql/language/parser.js';
import { buildSchema } from 'graphql/utilities/buildASTSchema.js';
import { getIntrospectionQuery } from 'graphql/utilities/getIntrospectionQuery.js';

const { parse } = require('graphql/language/parser.js');
const { executeSync } = require('graphql/execution/execute.js');
const { buildSchema } = require('graphql/utilities/buildASTSchema.js');
const {
getIntrospectionQuery,
} = require('graphql/utilities/getIntrospectionQuery.js');

const { bigSchemaSDL } = require('./fixtures.js');
import { bigSchemaSDL } from './fixtures.js';

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

module.exports = {
export const benchmark = {
name: 'Execute Introspection Query',
count: 10,
count: 30,
measure() {
executeSync({ schema, document });
},
Expand Down
10 changes: 3 additions & 7 deletions benchmark/parser-benchmark.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
'use strict';

const { parse } = require('graphql/language/parser.js');
const {
getIntrospectionQuery,
} = require('graphql/utilities/getIntrospectionQuery.js');
import { parse } from 'graphql/language/parser.js';
import { getIntrospectionQuery } from 'graphql/utilities/getIntrospectionQuery.js';

const introspectionQuery = getIntrospectionQuery();

module.exports = {
export const benchmark = {
name: 'Parse introspection query',
count: 1000,
measure() {
Expand Down
16 changes: 6 additions & 10 deletions benchmark/validateGQL-benchmark.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
'use strict';
import { parse } from 'graphql/language/parser.js';
import { buildSchema } from 'graphql/utilities/buildASTSchema.js';
import { getIntrospectionQuery } from 'graphql/utilities/getIntrospectionQuery.js';
import { validate } from 'graphql/validation/validate.js';

const { parse } = require('graphql/language/parser.js');
const { validate } = require('graphql/validation/validate.js');
const { buildSchema } = require('graphql/utilities/buildASTSchema.js');
const {
getIntrospectionQuery,
} = require('graphql/utilities/getIntrospectionQuery.js');

const { bigSchemaSDL } = require('./fixtures.js');
import { bigSchemaSDL } from './fixtures.js';

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

module.exports = {
export const benchmark = {
name: 'Validate Introspection Query',
count: 50,
measure() {
Expand Down
12 changes: 5 additions & 7 deletions benchmark/validateInvalidGQL-benchmark.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict';
import { parse } from 'graphql/language/parser.js';
import { buildSchema } from 'graphql/utilities/buildASTSchema.js';
import { validate } from 'graphql/validation/validate.js';

const { parse } = require('graphql/language/parser.js');
const { validate } = require('graphql/validation/validate.js');
const { buildSchema } = require('graphql/utilities/buildASTSchema.js');

const { bigSchemaSDL } = require('./fixtures.js');
import { bigSchemaSDL } from './fixtures.js';

const schema = buildSchema(bigSchemaSDL, { assumeValid: true });
const queryAST = parse(`
Expand All @@ -21,7 +19,7 @@ const queryAST = parse(`
}
`);

module.exports = {
export const benchmark = {
name: 'Validate Invalid Query',
count: 50,
measure() {
Expand Down
10 changes: 4 additions & 6 deletions benchmark/validateSDL-benchmark.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
'use strict';
import { parse } from 'graphql/language/parser.js';
import { validateSDL } from 'graphql/validation/validate.js';

const { parse } = require('graphql/language/parser.js');
const { validateSDL } = require('graphql/validation/validate.js');

const { bigSchemaSDL } = require('./fixtures.js');
import { bigSchemaSDL } from './fixtures.js';

const sdlAST = parse(bigSchemaSDL);

module.exports = {
export const benchmark = {
name: 'Validate SDL Document',
count: 10,
measure() {
Expand Down
10 changes: 4 additions & 6 deletions benchmark/visit-benchmark.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict';
import { parse } from 'graphql/language/parser.js';
import { visit } from 'graphql/language/visitor.js';

const { parse } = require('graphql/language/parser.js');
const { visit } = require('graphql/language/visitor.js');

const { bigSchemaSDL } = require('./fixtures.js');
import { bigSchemaSDL } from './fixtures.js';

const documentAST = parse(bigSchemaSDL);

Expand All @@ -16,7 +14,7 @@ const visitor = {
},
};

module.exports = {
export const benchmark = {
name: 'Visit all AST nodes',
count: 10,
measure() {
Expand Down
10 changes: 4 additions & 6 deletions benchmark/visitInParallel-benchmark.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict';
import { parse } from 'graphql/language/parser.js';
import { visit, visitInParallel } from 'graphql/language/visitor.js';

const { parse } = require('graphql/language/parser.js');
const { visit, visitInParallel } = require('graphql/language/visitor.js');

const { bigSchemaSDL } = require('./fixtures.js');
import { bigSchemaSDL } from './fixtures.js';

const documentAST = parse(bigSchemaSDL);

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

module.exports = {
export const benchmark = {
name: 'Visit all AST nodes in parallel',
count: 10,
measure() {
Expand Down
12 changes: 6 additions & 6 deletions integrationTests/node/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';
/* eslint-disable simple-import-sort/imports */
import assert from 'assert';
import { readFileSync } from 'fs';

const assert = require('assert');
const { readFileSync } = require('fs');

const { version, graphqlSync } = require('graphql');
const { buildSchema } = require('graphql/utilities');
import { graphqlSync } from 'graphql';
import { buildSchema } from 'graphql/utilities';
import { version } from 'graphql/version';

assert.deepStrictEqual(
version,
Expand Down
1 change: 1 addition & 0 deletions integrationTests/node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"private": true,
"description": "graphql-js should work on all supported node versions",
"type": "module",
"scripts": {
"test": "node test.js"
},
Expand Down
9 changes: 5 additions & 4 deletions integrationTests/node/test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
'use strict';
import childProcess from 'child_process';
import fs from 'fs';

const childProcess = require('child_process');

const graphqlPackageJSON = require('graphql/package.json');
const graphqlPackageJSON = JSON.parse(
fs.readFileSync('./node_modules/graphql/package.json', 'utf-8'),
);

const nodeVersions = graphqlPackageJSON.engines.node
.split(' || ')
Expand Down
Loading

0 comments on commit db6525e

Please sign in to comment.