Skip to content

Commit 6e61742

Browse files
committed
npm esm tag
1 parent 085c9ef commit 6e61742

22 files changed

+308
-30
lines changed

.babelrc-npm.json

+8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@
2222
"plugins": [
2323
["./resources/add-extension-to-import-paths", { "extension": "mjs" }]
2424
]
25+
},
26+
"esm": {
27+
"presets": [
28+
["@babel/preset-env", { "modules": false, "targets": { "node": "12" } }]
29+
],
30+
"plugins": [
31+
["./resources/add-extension-to-import-paths", { "extension": "js" }]
32+
]
2533
}
2634
}
2735
}

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/node_modules
44
/coverage
55
/npmDist
6+
/npmEsmDist
67
/denoDist
78
/npm
89
/deno

.eslintrc.yml

+4
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,10 @@ rules:
442442
yield-star-spacing: off
443443

444444
overrides:
445+
- files:
446+
- 'integrationTests/node-esm/**/*.js'
447+
parserOptions:
448+
sourceType: module
445449
- files: '**/*.ts'
446450
parser: '@typescript-eslint/parser'
447451
parserOptions:

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@
1414
/denoDist
1515
/npm
1616
/deno
17+
/npmEsmDist

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
/node_modules
55
/coverage
66
/npmDist
7+
/npmEsmDist
78
/denoDist
89
/npm
910
/deno

integrationTests/integration-test.js

+11
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ describe('Integration Tests', () => {
2727
path.join(tmpDir, 'graphql.tgz'),
2828
);
2929

30+
const esmDistDir = path.resolve('./npmEsmDist');
31+
const esmArchiveName = exec(`npm --quiet pack ${esmDistDir}`, {
32+
cwd: tmpDir,
33+
});
34+
35+
fs.renameSync(
36+
path.join(tmpDir, esmArchiveName),
37+
path.join(tmpDir, 'graphql-esm.tgz'),
38+
);
39+
3040
function testOnNodeProject(projectName) {
3141
const projectPath = path.join(__dirname, projectName);
3242

@@ -44,5 +54,6 @@ describe('Integration Tests', () => {
4454

4555
testOnNodeProject('ts');
4656
testOnNodeProject('node');
57+
testOnNodeProject('node-esm');
4758
testOnNodeProject('webpack');
4859
});

integrationTests/node-esm/index.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* eslint-disable node/no-missing-import, import/no-unresolved, node/no-unsupported-features/es-syntax */
2+
3+
import { deepStrictEqual, strictEqual } from 'assert';
4+
5+
import { version } from 'version';
6+
import { schema } from 'schema';
7+
8+
import { graphqlSync } from 'graphql';
9+
10+
// Import without explicit extension
11+
import { isPromise } from 'graphql/jsutils/isPromise';
12+
13+
// Import package.json
14+
import pkg from 'graphql/package.json';
15+
16+
deepStrictEqual(version, pkg.version);
17+
18+
const result = graphqlSync({
19+
schema,
20+
source: '{ hello }',
21+
rootValue: { hello: 'world' },
22+
});
23+
24+
deepStrictEqual(result, {
25+
data: {
26+
__proto__: null,
27+
hello: 'world',
28+
},
29+
});
30+
31+
strictEqual(isPromise(Promise.resolve()), true);
32+
33+
// The possible promise rejection is handled by "--unhandled-rejections=strict"
34+
import('graphql/jsutils/isPromise').then((isPromisePkg) => {
35+
strictEqual(isPromisePkg.isPromise(Promise.resolve()), true);
36+
});
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"type": "module",
3+
"description": "graphql-js ESM should work on all supported node versions",
4+
"scripts": {
5+
"test": "node test.js"
6+
},
7+
"dependencies": {
8+
"graphql": "file:../graphql-esm.tgz",
9+
"node-12": "npm:[email protected]",
10+
"node-14": "npm:[email protected]",
11+
"node-16": "npm:[email protected]",
12+
"schema": "file:./schema",
13+
"version": "file:./version"
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "schema",
3+
"exports": {
4+
".": {
5+
"import": "./schema.mjs"
6+
}
7+
},
8+
"peerDependencies": {
9+
"graphql": "*"
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { buildSchema } from 'graphql/utilities';
2+
3+
export const schema = buildSchema('type Query { hello: String }');

integrationTests/node-esm/test.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { execSync } from 'child_process';
2+
import { readFileSync } from 'fs';
3+
import { resolve } from 'path';
4+
5+
const { dependencies } = JSON.parse(
6+
readFileSync(resolve('package.json'), 'utf-8'),
7+
);
8+
9+
const nodeVersions = Object.keys(dependencies)
10+
.filter((pkg) => pkg.startsWith('node-'))
11+
.sort((a, b) => b.localeCompare(a));
12+
13+
for (const version of nodeVersions) {
14+
console.log(`Testing on ${version} ...`);
15+
16+
const nodePath = resolve('node_modules', version, 'bin/node');
17+
execSync(
18+
nodePath +
19+
' --experimental-json-modules --unhandled-rejections=strict index.js',
20+
{ stdio: 'inherit' },
21+
);
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "bar",
3+
"type": "module",
4+
"main": "./version.js",
5+
"peerDependencies": {
6+
"graphql": "*"
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/* eslint-disable import/no-unresolved, node/no-missing-import */
2+
3+
import { version } from 'graphql';
4+
5+
export { version };

integrationTests/ts/esm.ts

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import type { ExecutionResult } from 'graphql-esm/execution';
2+
3+
import { graphqlSync } from 'graphql-esm';
4+
import {
5+
GraphQLString,
6+
GraphQLSchema,
7+
GraphQLObjectType,
8+
} from 'graphql-esm/type';
9+
10+
const queryType: GraphQLObjectType = new GraphQLObjectType({
11+
name: 'Query',
12+
fields: () => ({
13+
sayHi: {
14+
type: GraphQLString,
15+
args: {
16+
who: {
17+
type: GraphQLString,
18+
defaultValue: 'World',
19+
},
20+
},
21+
resolve(_root, args: { who: string }) {
22+
return 'Hello ' + args.who;
23+
},
24+
},
25+
}),
26+
});
27+
28+
const schema: GraphQLSchema = new GraphQLSchema({ query: queryType });
29+
30+
const result: ExecutionResult = graphqlSync({
31+
schema,
32+
source: `
33+
query helloWho($who: String){
34+
test(who: $who)
35+
}
36+
`,
37+
variableValues: { who: 'Dolly' },
38+
});

integrationTests/ts/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
},
77
"dependencies": {
88
"graphql": "file:../graphql.tgz",
9+
"graphql-esm": "file:../graphql-esm.tgz",
910
"typescript-4.1": "npm:[email protected]",
1011
"typescript-4.2": "npm:[email protected]",
1112
"typescript-4.3": "npm:[email protected]",
12-
"typescript-4.4": "npm:[email protected]"
13+
"typescript-4.4": "npm:[email protected]",
14+
"typescript-4.5": "npm:[email protected]"
1315
}
1416
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// eslint-disable-next-line node/no-missing-import, import/no-unresolved
2+
import { graphqlSync } from 'graphql-esm';
3+
4+
// eslint-disable-next-line node/no-missing-import, import/no-unresolved
5+
import { buildSchema } from 'graphql-esm/utilities/buildASTSchema';
6+
7+
const schema = buildSchema('type Query { hello: String }');
8+
9+
export const result = graphqlSync({
10+
schema,
11+
source: '{ hello }',
12+
rootValue: { hello: 'world' },
13+
});

integrationTests/webpack/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
},
77
"dependencies": {
88
"graphql": "file:../graphql.tgz",
9+
"graphql-esm": "file:../graphql-esm.tgz",
910
"webpack": "5.x.x",
1011
"webpack-cli": "4.x.x"
1112
}

integrationTests/webpack/test.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,23 @@
33
const assert = require('assert');
44

55
// eslint-disable-next-line node/no-missing-require
6-
const { result } = require('./dist/main.js');
6+
const { result: cjs } = require('./dist/cjs.js');
77

8-
assert.deepStrictEqual(result, {
8+
assert.deepStrictEqual(cjs, {
99
data: {
1010
__proto__: null,
1111
hello: 'world',
1212
},
1313
});
14+
15+
// eslint-disable-next-line node/no-missing-require
16+
const { result: esm } = require('./dist/esm.js');
17+
18+
assert.deepStrictEqual(esm, {
19+
data: {
20+
__proto__: null,
21+
hello: 'world',
22+
},
23+
});
24+
1425
console.log('Test script: Got correct result from Webpack bundle!');

integrationTests/webpack/webpack.config.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{
22
"mode": "production",
3-
"entry": "./entry.js",
3+
"entry": {
4+
"cjs": "./entry.js",
5+
"esm": "./entry-esm.mjs"
6+
},
47
"output": {
58
"libraryTarget": "commonjs2"
69
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"prettier:check": "prettier --check .",
4545
"check:spelling": "cspell --cache --no-progress '**/*'",
4646
"check:integrations": "npm run build:npm && npm run build:deno && mocha --full-trace integrationTests/*-test.js",
47-
"build:npm": "node resources/build-npm.js",
47+
"build:npm": "node resources/build-npm.js && node resources/build-npm.js --esm-only",
4848
"build:deno": "node resources/build-deno.js",
4949
"gitpublish:npm": "bash ./resources/gitpublish.sh npm npmDist",
5050
"gitpublish:deno": "bash ./resources/gitpublish.sh deno denoDist"

0 commit comments

Comments
 (0)