From b30b68c6d65c8104fb4e30f0458a746c1ea8a503 Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Fri, 13 May 2022 15:09:26 +0300 Subject: [PATCH 1/2] Remove superficial ESLint config Was added as part of #3501 --- .eslintrc.yml | 7 ------- 1 file changed, 7 deletions(-) diff --git a/.eslintrc.yml b/.eslintrc.yml index f7b0853d09..7120ada6e6 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -743,10 +743,3 @@ overrides: # Ignore docusarus related webpack aliases import/no-unresolved: ['error', { 'ignore': ['^@theme', '^@docusaurus', '^@generated'] }] - - files: - - website/docusaurus.config.js - - website/sidebars.js - - integrationTests/**/* - - resources/**/* - env: - node: true From 864523ee7e0e9d55aabb09d700bec46360606c93 Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Fri, 13 May 2022 22:49:16 +0300 Subject: [PATCH 2/2] resources: remove all usages of `require.main` --- resources/benchmark.ts | 60 ++++++++-------- resources/build-deno.ts | 66 +++++++++--------- resources/build-npm.ts | 146 ++++++++++++++++++++------------------- resources/gen-version.ts | 4 +- 4 files changed, 140 insertions(+), 136 deletions(-) diff --git a/resources/benchmark.ts b/resources/benchmark.ts index 57f8895c8f..dc10d9a3b3 100644 --- a/resources/benchmark.ts +++ b/resources/benchmark.ts @@ -12,15 +12,14 @@ const maxTime = 5; // The minimum sample size required to perform statistical analysis. const minSamples = 5; -// Get the revisions and make things happen! -if (require.main === module) { +async function runBenchmarks() { + // Get the revisions and make things happen! const { benchmarks, revisions } = getArguments(process.argv.slice(2)); const benchmarkProjects = prepareBenchmarkProjects(revisions); - runBenchmarks(benchmarks, benchmarkProjects).catch((error) => { - console.error(error); - process.exit(1); - }); + for (const benchmark of benchmarks) { + await runBenchmark(benchmark, benchmarkProjects); + } } function localDir(...paths: ReadonlyArray) { @@ -266,35 +265,33 @@ function maxBy(array: ReadonlyArray, fn: (obj: T) => number) { } // Prepare all revisions and run benchmarks matching a pattern against them. -async function runBenchmarks( - benchmarks: ReadonlyArray, +async function runBenchmark( + benchmark: string, benchmarkProjects: ReadonlyArray, ) { - for (const benchmark of benchmarks) { - const results = []; - for (let i = 0; i < benchmarkProjects.length; ++i) { - const { revision, projectPath } = benchmarkProjects[i]; - const modulePath = path.join(projectPath, benchmark); - - if (i === 0) { - const { name } = await sampleModule(modulePath); - console.log('⏱ ' + name); - } + const results = []; + for (let i = 0; i < benchmarkProjects.length; ++i) { + const { revision, projectPath } = benchmarkProjects[i]; + const modulePath = path.join(projectPath, benchmark); + + if (i === 0) { + const { name } = await sampleModule(modulePath); + console.log('⏱ ' + name); + } - try { - const samples = await collectSamples(modulePath); + try { + const samples = await collectSamples(modulePath); - results.push(computeStats(revision, samples)); - process.stdout.write(' ' + cyan(i + 1) + ' tests completed.\u000D'); - } catch (error) { - console.log(' ' + revision + ': ' + red(String(error))); - } + results.push(computeStats(revision, samples)); + process.stdout.write(' ' + cyan(i + 1) + ' tests completed.\u000D'); + } catch (error) { + console.log(' ' + revision + ': ' + red(String(error))); } - console.log('\n'); - - beautifyBenchmark(results); - console.log(''); } + console.log('\n'); + + beautifyBenchmark(results); + console.log(''); } function getArguments(argv: ReadonlyArray) { @@ -422,3 +419,8 @@ function sampleModule(modulePath: string): Promise { }); }); } + +runBenchmarks().catch((error) => { + console.error(error); + process.exit(1); +}); diff --git a/resources/build-deno.ts b/resources/build-deno.ts index f4ba1d9e6c..c0b3e895c4 100644 --- a/resources/build-deno.ts +++ b/resources/build-deno.ts @@ -7,40 +7,38 @@ import { addExtensionToImportPaths } from './add-extension-to-import-paths'; import { inlineInvariant } from './inline-invariant'; import { readdirRecursive, showDirStats, writeGeneratedFile } from './utils'; -if (require.main === module) { - fs.rmSync('./denoDist', { recursive: true, force: true }); - fs.mkdirSync('./denoDist'); - - const srcFiles = readdirRecursive('./src', { ignoreDir: /^__.*__$/ }); - for (const filepath of srcFiles) { - if (filepath.endsWith('.ts')) { - const srcPath = path.join('./src', filepath); - - const sourceFile = ts.createSourceFile( - srcPath, - fs.readFileSync(srcPath, 'utf-8'), - ts.ScriptTarget.Latest, - ); - - const transformed = ts.transform(sourceFile, [ - addExtensionToImportPaths({ extension: '.ts' }), - inlineInvariant, - ]); - const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed }); - const newContent = printer.printBundle( - ts.createBundle(transformed.transformed), - ); - - transformed.dispose(); - - const destPath = path.join('./denoDist', filepath); - fs.mkdirSync(path.dirname(destPath), { recursive: true }); - writeGeneratedFile(destPath, newContent); - } +fs.rmSync('./denoDist', { recursive: true, force: true }); +fs.mkdirSync('./denoDist'); + +const srcFiles = readdirRecursive('./src', { ignoreDir: /^__.*__$/ }); +for (const filepath of srcFiles) { + if (filepath.endsWith('.ts')) { + const srcPath = path.join('./src', filepath); + + const sourceFile = ts.createSourceFile( + srcPath, + fs.readFileSync(srcPath, 'utf-8'), + ts.ScriptTarget.Latest, + ); + + const transformed = ts.transform(sourceFile, [ + addExtensionToImportPaths({ extension: '.ts' }), + inlineInvariant, + ]); + const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed }); + const newContent = printer.printBundle( + ts.createBundle(transformed.transformed), + ); + + transformed.dispose(); + + const destPath = path.join('./denoDist', filepath); + fs.mkdirSync(path.dirname(destPath), { recursive: true }); + writeGeneratedFile(destPath, newContent); } +} - fs.copyFileSync('./LICENSE', './denoDist/LICENSE'); - fs.copyFileSync('./README.md', './denoDist/README.md'); +fs.copyFileSync('./LICENSE', './denoDist/LICENSE'); +fs.copyFileSync('./README.md', './denoDist/README.md'); - showDirStats('./denoDist'); -} +showDirStats('./denoDist'); diff --git a/resources/build-npm.ts b/resources/build-npm.ts index fe7f16082c..42715a09de 100644 --- a/resources/build-npm.ts +++ b/resources/build-npm.ts @@ -6,61 +6,75 @@ import * as ts from 'typescript'; import { addExtensionToImportPaths } from './add-extension-to-import-paths'; import { inlineInvariant } from './inline-invariant'; -import { readPackageJSON, showDirStats, writeGeneratedFile } from './utils'; - -if (require.main === module) { - fs.rmSync('./npmDist', { recursive: true, force: true }); - fs.mkdirSync('./npmDist'); - - const packageJSON = buildPackageJSON(); - - // Based on https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#getting-the-dts-from-a-javascript-file - const tsConfig = JSON.parse( - fs.readFileSync(require.resolve('../tsconfig.json'), 'utf-8'), - ); - assert( - tsConfig.compilerOptions, - '"tsconfig.json" should have `compilerOptions`', +import { + readdirRecursive, + readPackageJSON, + showDirStats, + writeGeneratedFile, +} from './utils'; + +fs.rmSync('./npmDist', { recursive: true, force: true }); +fs.mkdirSync('./npmDist'); + +// Based on https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#getting-the-dts-from-a-javascript-file +const tsConfig = JSON.parse( + fs.readFileSync(require.resolve('../tsconfig.json'), 'utf-8'), +); +assert( + tsConfig.compilerOptions, + '"tsconfig.json" should have `compilerOptions`', +); + +const { options: tsOptions, errors: tsOptionsErrors } = + ts.convertCompilerOptionsFromJson( + { + ...tsConfig.compilerOptions, + module: 'es2020', + noEmit: false, + declaration: true, + declarationDir: './npmDist', + outDir: './npmDist', + }, + process.cwd(), ); - const { options: tsOptions, errors: tsOptionsErrors } = - ts.convertCompilerOptionsFromJson( - { - ...tsConfig.compilerOptions, - module: 'es2020', - noEmit: false, - declaration: true, - declarationDir: './npmDist', - outDir: './npmDist', - }, - process.cwd(), - ); - - assert( - tsOptionsErrors.length === 0, - 'Fail to parse options: ' + tsOptionsErrors, - ); +assert( + tsOptionsErrors.length === 0, + 'Fail to parse options: ' + tsOptionsErrors, +); + +const tsHost = ts.createCompilerHost(tsOptions); +tsHost.writeFile = (filepath, body) => { + fs.mkdirSync(path.dirname(filepath), { recursive: true }); + writeGeneratedFile(filepath, body); +}; + +const tsProgram = ts.createProgram(['src/index.ts'], tsOptions, tsHost); +const tsResult = tsProgram.emit(undefined, undefined, undefined, undefined, { + after: [addExtensionToImportPaths({ extension: '.js' }), inlineInvariant], +}); +assert( + !tsResult.emitSkipped, + 'Fail to generate `*.d.ts` files, please run `npm run check`', +); + +fs.copyFileSync('./LICENSE', './npmDist/LICENSE'); +fs.copyFileSync('./README.md', './npmDist/README.md'); + +// Should be done as the last step so only valid packages can be published +writeGeneratedFile( + './npmDist/package.json', + JSON.stringify(buildPackageJSON()), +); + +showDirStats('./npmDist'); - const tsHost = ts.createCompilerHost(tsOptions); - tsHost.writeFile = (filepath, body) => { - if (path.basename(filepath) === 'index.js') { - const relative = './' + path.relative('./npmDist', filepath); - const key = relative.replace(/\/?index.js$/, ''); - packageJSON.exports[key] = relative; - } - - fs.mkdirSync(path.dirname(filepath), { recursive: true }); - writeGeneratedFile(filepath, body); - }; +function buildPackageJSON() { + const packageJSON = readPackageJSON(); - const tsProgram = ts.createProgram(['src/index.ts'], tsOptions, tsHost); - const tsResult = tsProgram.emit(undefined, undefined, undefined, undefined, { - after: [addExtensionToImportPaths({ extension: '.js' }), inlineInvariant], - }); - assert( - !tsResult.emitSkipped, - 'Fail to generate `*.d.ts` files, please run `npm run check`', - ); + delete packageJSON.private; + delete packageJSON.scripts; + delete packageJSON.devDependencies; assert(packageJSON.types === undefined, 'Unexpected "types" in package.json'); const supportedTSVersions = Object.keys(packageJSON.typesVersions); @@ -75,33 +89,25 @@ if (require.main === module) { // Provoke syntax error to show this message `"Package 'graphql' support only TS versions that are ${supportedTSVersions[0]}".`, ); + packageJSON.typesVersions = { ...packageJSON.typesVersions, '*': { '*': [notSupportedTSVersionFile] }, }; - fs.copyFileSync('./LICENSE', './npmDist/LICENSE'); - fs.copyFileSync('./README.md', './npmDist/README.md'); - - // Should be done as the last step so only valid packages can be published - writeGeneratedFile('./npmDist/package.json', JSON.stringify(packageJSON)); - - showDirStats('./npmDist'); -} - -function buildPackageJSON() { - const packageJSON = readPackageJSON(); + packageJSON.type = 'module'; + packageJSON.exports = {}; - delete packageJSON.private; - delete packageJSON.scripts; - delete packageJSON.devDependencies; + for (const filepath of readdirRecursive('./src', { ignoreDir: /^__.*__$/ })) { + if (path.basename(filepath) === 'index.ts') { + const key = path.dirname(filepath); + packageJSON.exports[key] = filepath.replace(/\.ts$/, '.js'); + } + } - packageJSON.type = 'module'; // Temporary workaround to allow "internal" imports, no grantees provided - packageJSON.exports = { - './*.js': './*.js', - './*': './*.js', - }; + packageJSON.exports['./*.js'] = './*.js'; + packageJSON.exports['./*'] = './*.js'; // TODO: move to integration tests const publishTag = packageJSON.publishConfig?.tag; diff --git a/resources/gen-version.ts b/resources/gen-version.ts index a127ce1203..beb4225741 100644 --- a/resources/gen-version.ts +++ b/resources/gen-version.ts @@ -30,6 +30,4 @@ export const versionInfo = Object.freeze({ }); `; -if (require.main === module) { - writeGeneratedFile('./src/version.ts', body); -} +writeGeneratedFile('./src/version.ts', body);