Skip to content

Commit d482585

Browse files
committed
Fully switch to ESM (scripts, tests, etc.)
1 parent 1f2c843 commit d482585

File tree

259 files changed

+1468
-1412
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

259 files changed

+1468
-1412
lines changed

.eslintrc.yml

+5-4
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ rules:
3838
node/no-exports-assign: error
3939
node/no-extraneous-import: error
4040
node/no-extraneous-require: error
41-
node/no-missing-import: [error, { allowModules: ['graphql'] }]
41+
node/no-missing-import: off # Blocked by https://github.com/mysticatea/eslint-plugin-node/issues/248
4242
node/no-missing-require: error
4343
node/no-new-require: error
4444
node/no-path-concat: error
@@ -84,7 +84,7 @@ rules:
8484

8585
# Static analysis
8686
# https://github.com/benmosher/eslint-plugin-import#static-analysis
87-
import/no-unresolved: [error, { ignore: ['graphql'] }]
87+
import/no-unresolved: off # blocked by https://github.com/import-js/eslint-plugin-import/issues/2170
8888
import/named: error
8989
import/default: error
9090
import/namespace: error
@@ -130,7 +130,6 @@ rules:
130130
import/extensions:
131131
- error
132132
- ignorePackages
133-
- ts: never # TODO: remove once TS supports extensions
134133
import/order: [error, { newlines-between: always-and-inside-groups }]
135134
import/newline-after-import: error
136135
import/prefer-default-export: off
@@ -696,8 +695,11 @@ overrides:
696695
node: true
697696
rules:
698697
node/no-sync: off
698+
node/no-extraneous-import: [error, { allowModules: ['graphql'] }]
699699
import/no-unresolved: off
700+
import/no-namespace: off
700701
import/no-nodejs-modules: off
702+
import/no-extraneous-dependencies: off
701703
- files: 'resources/**'
702704
env:
703705
node: true
@@ -735,7 +737,6 @@ overrides:
735737
version: detect
736738
rules:
737739
node/no-unpublished-require: off
738-
node/no-missing-import: off
739740
import/no-default-export: off
740741
import/no-commonjs: off
741742
import/no-nodejs-modules: off

.mocharc.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
fail-zero: true
22
throw-deprecation: true
33
check-leaks: true
4-
require:
5-
- 'ts-node/register/transpile-only'
64
extension:
7-
- 'ts'
5+
- ts
6+
node-option:
7+
- 'loader=ts-node/esm/transpile-only'

integrationTests/integration-test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import * as childProcess from 'node:child_process';
2-
import * as fs from 'node:fs';
3-
import * as os from 'node:os';
4-
import * as path from 'node:path';
1+
import childProcess from 'node:child_process';
2+
import fs from 'node:fs';
3+
import os from 'node:os';
4+
import path from 'node:path';
55

66
import { describe, it } from 'mocha';
77

@@ -28,7 +28,7 @@ describe('Integration Tests', () => {
2828
);
2929

3030
function testOnNodeProject(projectName: string) {
31-
const projectPath = path.join(__dirname, projectName);
31+
const projectPath = new URL(projectName, import.meta.url).pathname;
3232

3333
const packageJSONPath = path.join(projectPath, 'package.json');
3434
const packageJSON = JSON.parse(fs.readFileSync(packageJSONPath, 'utf-8'));

integrationTests/webpack/test.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import assert from 'assert';
22

3-
// eslint-disable-next-line import/no-unresolved, node/no-missing-import
43
import mainCJS from './dist/main.cjs';
54

65
assert.deepStrictEqual(mainCJS.result, {

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "A Query Language and Runtime which can target any service.",
55
"license": "MIT",
66
"private": true,
7+
"type": "module",
78
"typesVersions": {
89
">=4.4.0": {
910
"*": [

resources/benchmark.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import * as assert from 'node:assert';
2-
import * as cp from 'node:child_process';
3-
import * as fs from 'node:fs';
4-
import * as os from 'node:os';
5-
import * as path from 'node:path';
6-
import * as url from 'node:url';
7-
8-
import { git, localRepoPath, npm } from './utils';
1+
import assert from 'node:assert';
2+
import cp from 'node:child_process';
3+
import fs from 'node:fs';
4+
import os from 'node:os';
5+
import path from 'node:path';
6+
import url from 'node:url';
7+
8+
import { git, localRepoPath, npm } from './utils.js';
99

1010
const NS_PER_SEC = 1e9;
1111
const LOCAL = 'local';

resources/build-deno.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import * as fs from 'node:fs';
2-
import * as path from 'node:path';
1+
import fs from 'node:fs';
2+
import path from 'node:path';
33

4-
import * as ts from 'typescript';
4+
import ts from 'typescript';
55

6-
import { addExtensionToImportPaths } from './add-extension-to-import-paths';
7-
import { inlineInvariant } from './inline-invariant';
8-
import { readdirRecursive, showDirStats, writeGeneratedFile } from './utils';
6+
import { changeExtensionInImportPaths } from './change-extension-in-import-paths.js';
7+
import { inlineInvariant } from './inline-invariant.js';
8+
import { readdirRecursive, showDirStats, writeGeneratedFile } from './utils.js';
99

1010
fs.rmSync('./denoDist', { recursive: true, force: true });
1111
fs.mkdirSync('./denoDist');
@@ -22,7 +22,7 @@ for (const filepath of srcFiles) {
2222
);
2323

2424
const transformed = ts.transform(sourceFile, [
25-
addExtensionToImportPaths({ extension: '.ts' }),
25+
changeExtensionInImportPaths({ extension: '.ts' }),
2626
inlineInvariant,
2727
]);
2828
const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });

resources/build-npm.ts

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
import * as assert from 'node:assert';
2-
import * as fs from 'node:fs';
3-
import * as path from 'node:path';
1+
import assert from 'node:assert';
2+
import fs from 'node:fs';
3+
import path from 'node:path';
44

5-
import * as ts from 'typescript';
5+
import ts from 'typescript';
66

7-
import { addExtensionToImportPaths } from './add-extension-to-import-paths';
8-
import { inlineInvariant } from './inline-invariant';
7+
import { inlineInvariant } from './inline-invariant.js';
98
import {
109
localRepoPath,
1110
readdirRecursive,
1211
readPackageJSON,
1312
showDirStats,
1413
writeGeneratedFile,
15-
} from './utils';
14+
} from './utils.js';
1615

1716
fs.rmSync('./npmDist', { recursive: true, force: true });
1817
fs.mkdirSync('./npmDist');
@@ -52,7 +51,7 @@ tsHost.writeFile = writeGeneratedFile;
5251

5352
const tsProgram = ts.createProgram(['src/index.ts'], tsOptions, tsHost);
5453
const tsResult = tsProgram.emit(undefined, undefined, undefined, undefined, {
55-
after: [addExtensionToImportPaths({ extension: '.js' }), inlineInvariant],
54+
after: [inlineInvariant],
5655
});
5756
assert(
5857
!tsResult.emitSkipped,
@@ -96,7 +95,6 @@ function buildPackageJSON() {
9695
'*': { '*': [notSupportedTSVersionFile] },
9796
};
9897

99-
packageJSON.type = 'module';
10098
packageJSON.exports = {};
10199

102100
for (const filepath of readdirRecursive('./src', { ignoreDir: /^__.*__$/ })) {

resources/add-extension-to-import-paths.ts renamed to resources/change-extension-in-import-paths.ts

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
import * as assert from 'node:assert';
2-
import * as util from 'node:util';
1+
import assert from 'node:assert';
2+
import util from 'node:util';
33

4-
import * as ts from 'typescript';
4+
import ts from 'typescript';
55

66
/**
77
* Adds extension to all paths imported inside MJS files
88
*
99
* Transforms:
1010
*
1111
* ```
12-
* import { foo } from './bar';
13-
* export { foo } from './bar';
12+
* import { foo } from './bar.js';
13+
* export { foo } from './bar.js';
1414
* ```
1515
*
1616
* to:
1717
*
1818
* ```
19-
* import { foo } from './bar.mjs';
20-
* export { foo } from './bar.mjs';
19+
* import { foo } from './bar.ts';
20+
* export { foo } from './bar.ts';
2121
* ```
2222
*
2323
*/
24-
export function addExtensionToImportPaths(config: { extension: string }) {
24+
export function changeExtensionInImportPaths(config: { extension: string }) {
2525
const { extension } = config;
2626
return (context: ts.TransformationContext) => {
2727
const { factory } = context;
@@ -35,13 +35,15 @@ export function addExtensionToImportPaths(config: { extension: string }) {
3535
function visitNode(node: ts.Node): ts.Node {
3636
const source: string | undefined = (node as any).moduleSpecifier?.text;
3737
if (source?.startsWith('./') || source?.startsWith('../')) {
38+
const newSource = source.replace(/\.js$/, extension);
39+
3840
if (ts.isImportDeclaration(node)) {
3941
return factory.updateImportDeclaration(
4042
node,
4143
node.decorators,
4244
node.modifiers,
4345
node.importClause,
44-
ts.createStringLiteral(source + extension),
46+
factory.createStringLiteral(newSource),
4547
node.assertClause,
4648
);
4749
}
@@ -52,7 +54,7 @@ export function addExtensionToImportPaths(config: { extension: string }) {
5254
node.modifiers,
5355
node.isTypeOnly,
5456
node.exportClause,
55-
ts.createStringLiteral(source + extension),
57+
factory.createStringLiteral(newSource),
5658
node.assertClause,
5759
);
5860
}

resources/diff-npm-package.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import * as assert from 'node:assert';
2-
import * as childProcess from 'node:child_process';
3-
import * as fs from 'node:fs';
4-
import * as os from 'node:os';
5-
import * as path from 'node:path';
1+
import assert from 'node:assert';
2+
import childProcess from 'node:child_process';
3+
import fs from 'node:fs';
4+
import os from 'node:os';
5+
import path from 'node:path';
66

7-
import { git, localRepoPath, npm, writeGeneratedFile } from './utils';
7+
import { git, localRepoPath, npm, writeGeneratedFile } from './utils.js';
88

99
const LOCAL = 'local';
1010
const tmpDir = path.join(os.tmpdir(), 'graphql-js-npm-diff');

resources/gen-changelog.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { git, readPackageJSON } from './utils';
1+
import { git, readPackageJSON } from './utils.js';
22

33
const packageJSON = readPackageJSON();
44
const labelsConfig: { [label: string]: { section: string; fold?: boolean } } = {

resources/gen-version.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { readPackageJSON, writeGeneratedFile } from './utils';
1+
import { readPackageJSON, writeGeneratedFile } from './utils.js';
22

33
const { version } = readPackageJSON();
44
const versionMatch = /^(\d+)\.(\d+)\.(\d+)-?(.*)?$/.exec(version);

resources/inline-invariant.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as ts from 'typescript';
1+
import ts from 'typescript';
22

33
/**
44
* Eliminates function call to `invariant` if the condition is met.

resources/utils.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import * as assert from 'node:assert';
2-
import * as childProcess from 'node:child_process';
3-
import * as fs from 'node:fs';
4-
import * as path from 'node:path';
1+
import assert from 'node:assert';
2+
import childProcess from 'node:child_process';
3+
import fs from 'node:fs';
4+
import path from 'node:path';
55

6-
import * as prettier from 'prettier';
6+
import prettier from 'prettier';
77

88
export function localRepoPath(...paths: ReadonlyArray<string>): string {
9-
return path.join(__dirname, '..', ...paths);
9+
const repoDir = new URL('..', import.meta.url).pathname;
10+
return path.join(repoDir, ...paths);
1011
}
1112

1213
export function npm(
@@ -25,6 +26,7 @@ export function git(
2526

2627
interface SpawnOptions {
2728
cwd?: string;
29+
env?: typeof process.env;
2830
}
2931

3032
function spawn(
@@ -62,6 +64,8 @@ export function readdirRecursive(
6264
);
6365
result.push(...list);
6466
}
67+
68+
result.sort((a, b) => a.localeCompare(b));
6569
return result.map((filepath) => './' + filepath);
6670
}
6771

src/__testUtils__/__tests__/dedent-test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { expect } from 'chai';
22
import { describe, it } from 'mocha';
33

4-
import { dedent, dedentString } from '../dedent';
4+
import { dedent, dedentString } from '../dedent.js';
55

66
describe('dedentString', () => {
77
it('removes indentation in typical usage', () => {

src/__testUtils__/__tests__/expectEqualPromisesOrValues-test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { expect } from 'chai';
22
import { describe, it } from 'mocha';
33

4-
import { expectEqualPromisesOrValues } from '../expectEqualPromisesOrValues';
5-
import { expectPromise } from '../expectPromise';
4+
import { expectEqualPromisesOrValues } from '../expectEqualPromisesOrValues.js';
5+
import { expectPromise } from '../expectPromise.js';
66

77
describe('expectEqualPromisesOrValues', () => {
88
it('throws when given unequal values', () => {

src/__testUtils__/__tests__/expectMatchingValues-test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { expect } from 'chai';
22
import { describe, it } from 'mocha';
33

4-
import { expectEqualPromisesOrValues } from '../expectEqualPromisesOrValues';
4+
import { expectEqualPromisesOrValues } from '../expectEqualPromisesOrValues.js';
55

66
describe('expectMatchingValues', () => {
77
it('throws when given unequal values', () => {

src/__testUtils__/__tests__/expectPromise-test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { expect } from 'chai';
22
import { describe, it } from 'mocha';
33

4-
import { expectPromise } from '../expectPromise';
4+
import { expectPromise } from '../expectPromise.js';
55

66
describe('expectPromise', () => {
77
it('throws if passed a value', () => {

src/__testUtils__/__tests__/genFuzzStrings-test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { expect } from 'chai';
22
import { describe, it } from 'mocha';
33

4-
import { genFuzzStrings } from '../genFuzzStrings';
4+
import { genFuzzStrings } from '../genFuzzStrings.js';
55

66
function expectFuzzStrings(options: {
77
allowedChars: ReadonlyArray<string>;

src/__testUtils__/__tests__/inspectStr-test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { expect } from 'chai';
22
import { describe, it } from 'mocha';
33

4-
import { inspectStr } from '../inspectStr';
4+
import { inspectStr } from '../inspectStr.js';
55

66
describe('inspectStr', () => {
77
it('handles null and undefined values', () => {

src/__testUtils__/__tests__/resolveOnNextTick-test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { expect } from 'chai';
22
import { describe, it } from 'mocha';
33

4-
import { resolveOnNextTick } from '../resolveOnNextTick';
4+
import { resolveOnNextTick } from '../resolveOnNextTick.js';
55

66
describe('resolveOnNextTick', () => {
77
it('resolves promise on the next tick', async () => {

src/__testUtils__/expectEqualPromisesOrValues.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { assert } from 'chai';
22

3-
import { isPromise } from '../jsutils/isPromise';
4-
import type { PromiseOrValue } from '../jsutils/PromiseOrValue';
3+
import { isPromise } from '../jsutils/isPromise.js';
4+
import type { PromiseOrValue } from '../jsutils/PromiseOrValue.js';
55

6-
import { expectMatchingValues } from './expectMatchingValues';
6+
import { expectMatchingValues } from './expectMatchingValues.js';
77

88
export function expectEqualPromisesOrValues<T>(
99
items: ReadonlyArray<PromiseOrValue<T>>,

0 commit comments

Comments
 (0)