Skip to content

Commit fea1499

Browse files
committed
Fully switch to ESM (scripts, tests, etc.)
1 parent 9df43fa commit fea1499

File tree

245 files changed

+1492
-1447
lines changed

Some content is hidden

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

245 files changed

+1492
-1447
lines changed

.eslintrc.yml

Lines changed: 3 additions & 4 deletions
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,6 +695,7 @@ overrides:
696695
rules:
697696
node/no-sync: off
698697
import/no-unresolved: off
698+
import/no-namespace: off
699699
import/no-nodejs-modules: off
700700
- files: 'resources/**'
701701
env:
@@ -734,7 +734,6 @@ overrides:
734734
version: detect
735735
rules:
736736
node/no-unpublished-require: off
737-
node/no-missing-import: off
738737
import/no-default-export: off
739738
import/no-commonjs: off
740739
import/no-nodejs-modules: off

.mocharc.yml

Lines changed: 3 additions & 3 deletions
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

Lines changed: 5 additions & 5 deletions
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

@@ -26,7 +26,7 @@ describe('Integration Tests', () => {
2626
);
2727

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

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

integrationTests/webpack/test.js

Lines changed: 0 additions & 1 deletion
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

Lines changed: 1 addition & 0 deletions
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/add-extension-to-import-paths.ts

Lines changed: 0 additions & 68 deletions
This file was deleted.

resources/benchmark.ts

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
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';
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+
7+
import { exec, execOutput, localRepoPath } from './utils.js';
68

79
const NS_PER_SEC = 1e9;
810
const LOCAL = 'local';
@@ -22,19 +24,6 @@ function runBenchmarks() {
2224
}
2325
}
2426

25-
function localDir(...paths: ReadonlyArray<string>) {
26-
return path.join(__dirname, '..', ...paths);
27-
}
28-
29-
function exec(command: string, options = {}) {
30-
const result = cp.execSync(command, {
31-
encoding: 'utf-8',
32-
stdio: ['inherit', 'pipe', 'inherit'],
33-
...options,
34-
});
35-
return result?.trimEnd();
36-
}
37-
3827
interface BenchmarkProject {
3928
revision: string;
4029
projectPath: string;
@@ -58,7 +47,7 @@ function prepareBenchmarkProjects(
5847
fs.rmSync(projectPath, { recursive: true, force: true });
5948
fs.mkdirSync(projectPath);
6049

61-
fs.cpSync(localDir('benchmark'), path.join(projectPath, 'benchmark'), {
50+
fs.cpSync(localRepoPath('benchmark'), path.join(projectPath, 'benchmark'), {
6251
recursive: true,
6352
});
6453

@@ -80,14 +69,14 @@ function prepareBenchmarkProjects(
8069

8170
function prepareNPMPackage(revision: string) {
8271
if (revision === LOCAL) {
83-
const repoDir = localDir();
72+
const repoDir = localRepoPath();
8473
const archivePath = path.join(tmpDir, 'graphql-local.tgz');
8574
fs.renameSync(buildNPMArchive(repoDir), archivePath);
8675
return archivePath;
8776
}
8877

8978
// Returns the complete git hash for a given git revision reference.
90-
const hash = exec(`git rev-parse "${revision}"`);
79+
const hash = execOutput(`git rev-parse "${revision}"`);
9180

9281
const archivePath = path.join(tmpDir, `graphql-${hash}.tgz`);
9382
if (fs.existsSync(archivePath)) {
@@ -108,7 +97,9 @@ function prepareBenchmarkProjects(
10897
exec('npm --quiet run build:npm', { cwd: repoDir });
10998

11099
const distDir = path.join(repoDir, 'npmDist');
111-
const archiveName = exec(`npm --quiet pack ${distDir}`, { cwd: repoDir });
100+
const archiveName = execOutput(`npm --quiet pack ${distDir}`, {
101+
cwd: repoDir,
102+
});
112103
return path.join(repoDir, archiveName);
113104
}
114105
}
@@ -334,7 +325,7 @@ function getArguments(argv: ReadonlyArray<string>) {
334325

335326
function findAllBenchmarks() {
336327
return fs
337-
.readdirSync(localDir('benchmark'), { withFileTypes: true })
328+
.readdirSync(localRepoPath('benchmark'), { withFileTypes: true })
338329
.filter((dirent) => dirent.isFile())
339330
.map((dirent) => dirent.name)
340331
.filter((name) => name.endsWith('-benchmark.js'))

resources/build-deno.ts

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import * as fs from 'node:fs';
2-
import * as path from 'node:path';
1+
import assert from 'node:assert';
2+
import fs from 'node:fs';
3+
import path from 'node:path';
4+
import util from 'node:util';
35

4-
import * as ts from 'typescript';
6+
import ts from 'typescript';
57

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

1011
fs.rmSync('./denoDist', { recursive: true, force: true });
1112
fs.mkdirSync('./denoDist');
@@ -22,8 +23,8 @@ for (const filepath of srcFiles) {
2223
);
2324

2425
const transformed = ts.transform(sourceFile, [
25-
addExtensionToImportPaths({ extension: '.ts' }),
2626
inlineInvariant,
27+
addExtensionToImportPaths,
2728
]);
2829
const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
2930
const newContent = printer.printBundle(
@@ -42,3 +43,65 @@ fs.copyFileSync('./LICENSE', './denoDist/LICENSE');
4243
fs.copyFileSync('./README.md', './denoDist/README.md');
4344

4445
showDirStats('./denoDist');
46+
47+
/**
48+
* Adds extension to all paths imported inside MJS files
49+
*
50+
* Transforms:
51+
*
52+
* ```
53+
* import { foo } from './bar.js';
54+
* export { foo } from './bar.js';
55+
* ```
56+
*
57+
* to:
58+
*
59+
* ```
60+
* import { foo } from './bar.ts';
61+
* export { foo } from './bar.ts';
62+
* ```
63+
*
64+
*/
65+
export function addExtensionToImportPaths(context: ts.TransformationContext) {
66+
const { factory } = context;
67+
68+
return visitSourceFile;
69+
70+
function visitSourceFile(sourceFile: ts.SourceFile) {
71+
return ts.visitNode(sourceFile, visitNode);
72+
}
73+
74+
function visitNode(node: ts.Node): ts.Node {
75+
const source: string | undefined = (node as any).moduleSpecifier?.text;
76+
if (source?.startsWith('./') || source?.startsWith('../')) {
77+
const tsSource = ts.createStringLiteral(source.replace(/\.js$/, '.ts'));
78+
if (ts.isImportDeclaration(node)) {
79+
return factory.updateImportDeclaration(
80+
node,
81+
node.decorators,
82+
node.modifiers,
83+
node.importClause,
84+
tsSource,
85+
node.assertClause,
86+
);
87+
}
88+
if (ts.isExportDeclaration(node)) {
89+
return factory.updateExportDeclaration(
90+
node,
91+
node.decorators,
92+
node.modifiers,
93+
node.isTypeOnly,
94+
node.exportClause,
95+
tsSource,
96+
node.assertClause,
97+
);
98+
}
99+
100+
assert(
101+
false,
102+
'Unexpected node with moduleSpecifier: ' + util.inspect(node),
103+
);
104+
}
105+
return ts.visitEachChild(node, visitNode, context);
106+
}
107+
}

resources/build-npm.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
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 {
9+
localRepoPath,
1010
readdirRecursive,
1111
readPackageJSON,
1212
showDirStats,
1313
writeGeneratedFile,
14-
} from './utils';
14+
} from './utils.js';
1515

1616
fs.rmSync('./npmDist', { recursive: true, force: true });
1717
fs.mkdirSync('./npmDist');
1818

1919
// Based on https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#getting-the-dts-from-a-javascript-file
2020
const tsConfig = JSON.parse(
21-
fs.readFileSync(require.resolve('../tsconfig.json'), 'utf-8'),
21+
fs.readFileSync(localRepoPath('tsconfig.json'), 'utf-8'),
2222
);
2323
assert(
2424
tsConfig.compilerOptions,
@@ -51,7 +51,7 @@ tsHost.writeFile = (filepath, body) => {
5151

5252
const tsProgram = ts.createProgram(['src/index.ts'], tsOptions, tsHost);
5353
const tsResult = tsProgram.emit(undefined, undefined, undefined, undefined, {
54-
after: [addExtensionToImportPaths({ extension: '.js' }), inlineInvariant],
54+
after: [inlineInvariant],
5555
});
5656
assert(
5757
!tsResult.emitSkipped,
@@ -95,7 +95,6 @@ function buildPackageJSON() {
9595
'*': { '*': [notSupportedTSVersionFile] },
9696
};
9797

98-
packageJSON.type = 'module';
9998
packageJSON.exports = {};
10099

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

0 commit comments

Comments
 (0)