Skip to content

Commit

Permalink
npm esm tag
Browse files Browse the repository at this point in the history
  • Loading branch information
PabloSzx committed Dec 8, 2021
1 parent 90bd6ff commit 1a74a91
Show file tree
Hide file tree
Showing 20 changed files with 274 additions and 25 deletions.
8 changes: 8 additions & 0 deletions .babelrc-npm.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
"plugins": [
["./resources/add-extension-to-import-paths", { "extension": "mjs" }]
]
},
"esm": {
"presets": [
["@babel/preset-env", { "modules": false, "targets": { "node": "12" } }]
],
"plugins": [
["./resources/add-extension-to-import-paths", { "extension": "js" }]
]
}
}
}
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/node_modules
/coverage
/npmDist
/npmEsmDist
/denoDist
/npm
/deno
Expand Down
4 changes: 4 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,10 @@ rules:
yield-star-spacing: off

overrides:
- files:
- 'integrationTests/node-esm/**/*.js'
parserOptions:
sourceType: module
- files: '**/*.ts'
parser: '@typescript-eslint/parser'
parserOptions:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
/node_modules
/coverage
/npmDist
/npmEsmDist
/denoDist
/npm
/deno
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/node_modules
/coverage
/npmDist
/npmEsmDist
/denoDist
/npm
/deno
11 changes: 11 additions & 0 deletions integrationTests/integration-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ describe('Integration Tests', () => {
path.join(tmpDir, 'graphql.tgz'),
);

const esmDistDir = path.resolve('./npmEsmDist');
const esmArchiveName = exec(`npm --quiet pack ${esmDistDir}`, {
cwd: tmpDir,
});

fs.renameSync(
path.join(tmpDir, esmArchiveName),
path.join(tmpDir, 'graphql-esm.tgz'),
);

function testOnNodeProject(projectName) {
const projectPath = path.join(__dirname, projectName);

Expand All @@ -45,5 +55,6 @@ describe('Integration Tests', () => {

testOnNodeProject('ts');
testOnNodeProject('node');
testOnNodeProject('node-esm');
testOnNodeProject('webpack');
});
36 changes: 36 additions & 0 deletions integrationTests/node-esm/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* eslint-disable node/no-missing-import, import/no-unresolved, node/no-unsupported-features/es-syntax */

import { deepStrictEqual, strictEqual } from 'assert';

import { version } from 'version';
import { schema } from 'schema';

import { graphqlSync } from 'graphql';

// Import without explicit extension
import { isPromise } from 'graphql/jsutils/isPromise';

// Import package.json
import pkg from 'graphql/package.json';

deepStrictEqual(`${version}-esm`, pkg.version);

const result = graphqlSync({
schema,
source: '{ hello }',
rootValue: { hello: 'world' },
});

deepStrictEqual(result, {
data: {
__proto__: null,
hello: 'world',
},
});

strictEqual(isPromise(Promise.resolve()), true);

// The possible promise rejection is handled by "--unhandled-rejections=strict"
import('graphql/jsutils/isPromise').then((isPromisePkg) => {
strictEqual(isPromisePkg.isPromise(Promise.resolve()), true);
});
15 changes: 15 additions & 0 deletions integrationTests/node-esm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"type": "module",
"description": "graphql-js ESM should work on all supported node versions",
"scripts": {
"test": "node test.js"
},
"dependencies": {
"graphql": "file:../graphql-esm.tgz",
"node-12": "npm:[email protected]",
"node-14": "npm:[email protected]",
"node-16": "npm:[email protected]",
"schema": "file:./schema",
"version": "file:./version"
}
}
11 changes: 11 additions & 0 deletions integrationTests/node-esm/schema/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "schema",
"exports": {
".": {
"import": "./schema.mjs"
}
},
"peerDependencies": {
"graphql": "*"
}
}
3 changes: 3 additions & 0 deletions integrationTests/node-esm/schema/schema.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { buildSchema } from 'graphql/utilities';

export const schema = buildSchema('type Query { hello: String }');
22 changes: 22 additions & 0 deletions integrationTests/node-esm/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { execSync } from 'child_process';
import { readFileSync } from 'fs';
import { resolve } from 'path';

const { dependencies } = JSON.parse(
readFileSync(resolve('package.json'), 'utf-8'),
);

const nodeVersions = Object.keys(dependencies)
.filter((pkg) => pkg.startsWith('node-'))
.sort((a, b) => b.localeCompare(a));

for (const version of nodeVersions) {
console.log(`Testing on ${version} ...`);

const nodePath = resolve('node_modules', version, 'bin/node');
execSync(
nodePath +
' --experimental-json-modules --unhandled-rejections=strict index.js',
{ stdio: 'inherit' },
);
}
8 changes: 8 additions & 0 deletions integrationTests/node-esm/version/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "bar",
"type": "module",
"main": "./version.js",
"peerDependencies": {
"graphql": "*"
}
}
5 changes: 5 additions & 0 deletions integrationTests/node-esm/version/version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* eslint-disable import/no-unresolved, node/no-missing-import */

import { version } from 'graphql';

export { version };
38 changes: 38 additions & 0 deletions integrationTests/ts/esm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type { ExecutionResult } from 'graphql-esm/execution';

import { graphqlSync } from 'graphql-esm';
import {
GraphQLString,
GraphQLSchema,
GraphQLObjectType,
} from 'graphql-esm/type';

const queryType: GraphQLObjectType = new GraphQLObjectType({
name: 'Query',
fields: () => ({
sayHi: {
type: GraphQLString,
args: {
who: {
type: GraphQLString,
defaultValue: 'World',
},
},
resolve(_root, args: { who: string }) {
return 'Hello ' + args.who;
},
},
}),
});

const schema: GraphQLSchema = new GraphQLSchema({ query: queryType });

const result: ExecutionResult = graphqlSync({
schema,
source: `
query helloWho($who: String){
test(who: $who)
}
`,
variableValues: { who: 'Dolly' },
});
1 change: 1 addition & 0 deletions integrationTests/ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
},
"dependencies": {
"graphql": "file:../graphql.tgz",
"graphql-esm": "file:../graphql-esm.tgz",
"typescript-4.1": "npm:[email protected]",
"typescript-4.2": "npm:[email protected]",
"typescript-4.3": "npm:[email protected]",
Expand Down
13 changes: 13 additions & 0 deletions integrationTests/webpack/entry-esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// eslint-disable-next-line node/no-missing-import, import/no-unresolved
import { graphqlSync } from 'graphql-esm';

// eslint-disable-next-line node/no-missing-import, import/no-unresolved
import { buildSchema } from 'graphql-esm/utilities/buildASTSchema';

const schema = buildSchema('type Query { hello: String }');

export const result = graphqlSync({
schema,
source: '{ hello }',
rootValue: { hello: 'world' },
});
1 change: 1 addition & 0 deletions integrationTests/webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
},
"dependencies": {
"graphql": "file:../graphql.tgz",
"graphql-esm": "file:../graphql-esm.tgz",
"webpack": "5.x.x",
"webpack-cli": "4.x.x"
}
Expand Down
15 changes: 13 additions & 2 deletions integrationTests/webpack/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,23 @@
const assert = require('assert');

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

assert.deepStrictEqual(result, {
assert.deepStrictEqual(cjs, {
data: {
__proto__: null,
hello: 'world',
},
});

// eslint-disable-next-line node/no-missing-require
const { result: esm } = require('./dist/esm.js');

assert.deepStrictEqual(esm, {
data: {
__proto__: null,
hello: 'world',
},
});

console.log('Test script: Got correct result from Webpack bundle!');
5 changes: 4 additions & 1 deletion integrationTests/webpack/webpack.config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"mode": "production",
"entry": "./entry.js",
"entry": {
"cjs": "./entry.js",
"esm": "./entry-esm.mjs"
},
"output": {
"libraryTarget": "commonjs2"
}
Expand Down
Loading

0 comments on commit 1a74a91

Please sign in to comment.