Skip to content
This repository has been archived by the owner on Mar 20, 2023. It is now read-only.

Commit

Permalink
Sync up config files with 'graphql-js'
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov authored and danielrearden committed Jul 4, 2020
1 parent 8436480 commit b8fb28b
Show file tree
Hide file tree
Showing 13 changed files with 204 additions and 344 deletions.
3 changes: 1 addition & 2 deletions .mocharc.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
throw-deprecation: true
check-leaks: true
require:
- '@babel/register'
- ts-node/register
- './resources/register.js'
57 changes: 57 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
"private": true,
"main": "index.js",
"types": "index.d.ts",
"typesVersions": {
"<3.8": {
"*": [
"ts3.4/*"
]
}
},
"sideEffects": false,
"homepage": "https://github.com/graphql/express-graphql",
"bugs": {
Expand Down Expand Up @@ -52,10 +59,13 @@
"@babel/plugin-transform-flow-strip-types": "7.10.4",
"@babel/preset-env": "7.10.4",
"@babel/register": "7.10.4",
"@types/accepts": "^1.3.5",
"@types/body-parser": "1.19.0",
"@types/chai": "4.2.11",
"@types/connect": "3.4.33",
"@types/content-type": "^1.1.3",
"@types/express": "4.17.6",
"@types/http-errors": "^1.6.3",
"@types/mocha": "7.0.2",
"@types/multer": "1.4.3",
"@types/node": "14.0.14",
Expand All @@ -68,6 +78,7 @@
"body-parser": "1.19.0",
"chai": "4.2.0",
"connect": "3.7.0",
"downlevel-dts": "^0.5.0",
"eslint": "7.3.1",
"eslint-plugin-flowtype": "5.2.0",
"eslint-plugin-import": "2.22.0",
Expand Down
37 changes: 17 additions & 20 deletions resources/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,30 @@ const fs = require('fs');
const path = require('path');
const assert = require('assert');

const babel = require('@babel/core');
const ts = require('typescript');

const { rmdirRecursive, readdirRecursive } = require('./utils');
const tsConfig = require('../tsconfig.json');

const transformLoadStaticlyFromNPM = require('./load-staticly-from-npm');
const { exec, rmdirRecursive, readdirRecursive } = require('./utils');

if (require.main === module) {
rmdirRecursive('./dist');
fs.mkdirSync('./dist');

const srcFiles = readdirRecursive('./src', { ignoreDir: /^__.*__$/ });
for (const filepath of srcFiles) {
const srcPath = path.join('./src', filepath);
const destPath = path.join('./dist', filepath);

fs.mkdirSync(path.dirname(destPath), { recursive: true });
if (filepath.endsWith('.js')) {
fs.copyFileSync(srcPath, destPath + '.flow');

const cjs = babelBuild(srcPath, { envName: 'cjs' });
fs.writeFileSync(destPath, cjs);
} else if (filepath.endsWith('d.ts')) {
fs.copyFileSync(srcPath, destPath);
}
}
const { options } = ts.convertCompilerOptionsFromJson(
tsConfig.compilerOptions,
process.cwd(),
);
const program = ts.createProgram({
rootNames: srcFiles.map((filepath) => path.join('./src', filepath)),
options,
});
program.emit(undefined, undefined, undefined, undefined, {
after: [transformLoadStaticlyFromNPM],
});
exec('npx downlevel-dts ./dist ./dist/ts3.4');

fs.copyFileSync('./LICENSE', './dist/LICENSE');
fs.copyFileSync('./README.md', './dist/README.md');
Expand All @@ -40,10 +41,6 @@ if (require.main === module) {
showStats();
}

function babelBuild(srcPath, options) {
return babel.transformFileSync(srcPath, options).code + '\n';
}

function buildPackageJSON() {
const packageJSON = require('../package.json');
delete packageJSON.private;
Expand Down
37 changes: 17 additions & 20 deletions resources/load-staticly-from-npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

const fs = require('fs');

const ts = require('typescript');

/**
* Eliminates function call to `invariant` if the condition is met.
*
* Transforms:
*
* loadStaticlyFromNPM(<npm path>)
Expand All @@ -15,23 +15,20 @@ const fs = require('fs');
*
* "<file content>"
*/
module.exports = function inlineInvariant(context) {
return {
visitor: {
CallExpression(path) {
const { node } = path;

if (
node.callee.type === 'Identifier' &&
node.callee.name === 'loadFileStaticlyFromNPM'
) {
const npmPath = node.arguments[0].value;
const filePath = require.resolve(npmPath);
const content = fs.readFileSync(filePath, 'utf-8');

path.replaceWith(context.types.stringLiteral(content));
}
},
},
module.exports = function transformLoadStaticlyFromNPM(context) {
const visit = (node) => {
if (ts.isCallExpression(node)) {
if (
ts.isIdentifier(node.expression) &&
node.expression.text === 'loadFileStaticlyFromNPM'
) {
const npmPath = node.arguments[0].text;
const filePath = require.resolve(npmPath);
const content = fs.readFileSync(filePath, 'utf-8');
return ts.createStringLiteral(content);
}
}
return ts.visitEachChild(node, visit, context);
};
return visit;
};
9 changes: 9 additions & 0 deletions resources/register.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

const transformLoadStaticlyFromNPM = require('./load-staticly-from-npm');

require('ts-node').register({
transformers: () => ({
after: [transformLoadStaticlyFromNPM],
}),
});
Loading

0 comments on commit b8fb28b

Please sign in to comment.