Skip to content

Commit fea1e6a

Browse files
committed
Add build script
1 parent c489f44 commit fea1e6a

File tree

8 files changed

+185
-16
lines changed

8 files changed

+185
-16
lines changed

.babelrc.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
],
1414
"env": {
1515
"cjs": {
16-
"presets": [["@babel/preset-env", { "modules": "commonjs" }]],
17-
"plugins": ["./resources/inline-invariant"]
16+
"presets": [["@babel/preset-env", { "modules": "commonjs" }]]
1817
},
1918
"mjs": {
2019
"presets": [["@babel/preset-env", { "modules": false }]],

.eslintignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Copied from '.gitignore', please keep it in sync.
22
node_modules
33
coverage
4-
lib
4+
dist

.eslintrc.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,4 +478,21 @@ overrides:
478478
no-console: off
479479
- files: '**/__*__/**'
480480
rules:
481+
node/no-unpublished-import: off
481482
import/no-extraneous-dependencies: [error, { devDependencies: true }]
483+
- files: 'resources/**'
484+
parserOptions:
485+
sourceType: script
486+
rules:
487+
node/no-unpublished-import: off
488+
node/no-unpublished-require: off
489+
node/no-missing-require: off
490+
node/no-sync: off
491+
node/no-unsupported-features/node-builtins: off
492+
node/global-require: off
493+
import/no-dynamic-require: off
494+
import/no-extraneous-dependencies: [error, { devDependencies: true }]
495+
import/no-nodejs-modules: off
496+
import/no-commonjs: off
497+
no-await-in-loop: off
498+
no-console: off

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77

88
node_modules
99
coverage
10-
lib
10+
dist

.prettierignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Copied from '.gitignore', please keep it in sync.
22
node_modules
33
coverage
4-
lib
4+
dist

package.json

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,7 @@
1111
"type": "git",
1212
"url": "http://github.com/graphql/graphql-relay-js.git"
1313
},
14-
"main": "lib/index.js",
15-
"directories": {
16-
"lib": "./lib"
17-
},
18-
"files": [
19-
"lib",
20-
"README.md",
21-
"LICENSE"
22-
],
14+
"main": "index.js",
2315
"scripts": {
2416
"prepublish": "./resources/prepublish.sh",
2517
"prettier": "prettier --write --list-different .",
@@ -29,8 +21,7 @@
2921
"testonly:cover": "nyc npm run testonly",
3022
"lint": "eslint .",
3123
"check": "flow check",
32-
"build": "rm -rf lib/* && babel src --ignore __tests__ --out-dir lib && npm run build:flow",
33-
"build:flow": "find ./src -name '*.js' -not -path '*/__tests__*' | while read filepath; do cp $filepath `echo $filepath | sed 's/\\/src\\//\\/lib\\//g'`.flow; done"
24+
"build": "node resources/build.js"
3425
},
3526
"peerDependencies": {
3627
"graphql": "^14.0.0 || ^15.0.0"

resources/build.js

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// @noflow
2+
3+
'use strict';
4+
5+
const fs = require('fs');
6+
const path = require('path');
7+
const assert = require('assert');
8+
9+
const babel = require('@babel/core');
10+
11+
const { rmdirRecursive, readdirRecursive } = require('./utils');
12+
13+
if (require.main === module) {
14+
rmdirRecursive('./dist');
15+
fs.mkdirSync('./dist');
16+
17+
const srcFiles = readdirRecursive('./src', { ignoreDir: /^__.*__$/ });
18+
for (const filepath of srcFiles) {
19+
const srcPath = path.join('./src', filepath);
20+
const destPath = path.join('./dist', filepath);
21+
22+
fs.mkdirSync(path.dirname(destPath), { recursive: true });
23+
if (filepath.endsWith('.js')) {
24+
fs.copyFileSync(srcPath, destPath + '.flow');
25+
26+
const cjs = babelBuild(srcPath, { envName: 'cjs' });
27+
fs.writeFileSync(destPath, cjs);
28+
} else if (filepath.endsWith('d.ts')) {
29+
fs.copyFileSync(srcPath, destPath);
30+
}
31+
}
32+
33+
fs.copyFileSync('./LICENSE', './dist/LICENSE');
34+
fs.copyFileSync('./README.md', './dist/README.md');
35+
36+
// Should be done as the last step so only valid packages can be published
37+
const packageJSON = buildPackageJSON();
38+
fs.writeFileSync('./dist/package.json', JSON.stringify(packageJSON, null, 2));
39+
40+
showStats();
41+
}
42+
43+
function babelBuild(srcPath, options) {
44+
return babel.transformFileSync(srcPath, options).code + '\n';
45+
}
46+
47+
function buildPackageJSON() {
48+
const packageJSON = require('../package.json');
49+
delete packageJSON.private;
50+
delete packageJSON.scripts;
51+
delete packageJSON.devDependencies;
52+
53+
const { version } = packageJSON;
54+
const versionMatch = /^\d+\.\d+\.\d+-?(.*)?$/.exec(version);
55+
if (!versionMatch) {
56+
throw new Error('Version does not match semver spec: ' + version);
57+
}
58+
59+
const [, preReleaseTag] = versionMatch;
60+
61+
if (preReleaseTag != null) {
62+
const [tag] = preReleaseTag.split('.');
63+
assert(['alpha', 'beta', 'rc'].includes(tag), `"${tag}" tag is supported.`);
64+
65+
assert(!packageJSON.publishConfig, 'Can not override "publishConfig".');
66+
packageJSON.publishConfig = { tag: tag || 'latest' };
67+
}
68+
69+
return packageJSON;
70+
}
71+
72+
function showStats() {
73+
const fileTypes = {};
74+
let totalSize = 0;
75+
76+
for (const filepath of readdirRecursive('./dist')) {
77+
const name = filepath.split(path.sep).pop();
78+
const [base, ...splitExt] = name.split('.');
79+
const ext = splitExt.join('.');
80+
81+
const filetype = ext ? '*.' + ext : base;
82+
fileTypes[filetype] = fileTypes[filetype] || { filepaths: [], size: 0 };
83+
84+
const { size } = fs.lstatSync(path.join('./dist', filepath));
85+
totalSize += size;
86+
fileTypes[filetype].size += size;
87+
fileTypes[filetype].filepaths.push(filepath);
88+
}
89+
90+
let stats = [];
91+
for (const [filetype, typeStats] of Object.entries(fileTypes)) {
92+
const numFiles = typeStats.filepaths.length;
93+
94+
if (numFiles > 1) {
95+
stats.push([filetype + ' x' + numFiles, typeStats.size]);
96+
} else {
97+
stats.push([typeStats.filepaths[0], typeStats.size]);
98+
}
99+
}
100+
stats.sort((a, b) => b[1] - a[1]);
101+
stats = stats.map(([type, size]) => [type, (size / 1024).toFixed(2) + ' KB']);
102+
103+
const typeMaxLength = Math.max(...stats.map((x) => x[0].length));
104+
const sizeMaxLength = Math.max(...stats.map((x) => x[1].length));
105+
for (const [type, size] of stats) {
106+
console.log(
107+
type.padStart(typeMaxLength) + ' | ' + size.padStart(sizeMaxLength),
108+
);
109+
}
110+
111+
console.log('-'.repeat(typeMaxLength + 3 + sizeMaxLength));
112+
const totalMB = (totalSize / 1024 / 1024).toFixed(2) + ' MB';
113+
console.log(
114+
'Total'.padStart(typeMaxLength) + ' | ' + totalMB.padStart(sizeMaxLength),
115+
);
116+
}

resources/utils.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// @noflow
2+
3+
'use strict';
4+
5+
const fs = require('fs');
6+
const path = require('path');
7+
8+
function rmdirRecursive(dirPath) {
9+
if (fs.existsSync(dirPath)) {
10+
for (const dirent of fs.readdirSync(dirPath, { withFileTypes: true })) {
11+
const fullPath = path.join(dirPath, dirent.name);
12+
if (dirent.isDirectory()) {
13+
rmdirRecursive(fullPath);
14+
} else {
15+
fs.unlinkSync(fullPath);
16+
}
17+
}
18+
fs.rmdirSync(dirPath);
19+
}
20+
}
21+
22+
function readdirRecursive(dirPath, opts = {}) {
23+
const { ignoreDir } = opts;
24+
const result = [];
25+
for (const dirent of fs.readdirSync(dirPath, { withFileTypes: true })) {
26+
const name = dirent.name;
27+
if (!dirent.isDirectory()) {
28+
result.push(dirent.name);
29+
continue;
30+
}
31+
32+
if (ignoreDir && ignoreDir.test(name)) {
33+
continue;
34+
}
35+
const list = readdirRecursive(path.join(dirPath, name), opts).map((f) =>
36+
path.join(name, f),
37+
);
38+
result.push(...list);
39+
}
40+
return result;
41+
}
42+
43+
module.exports = {
44+
rmdirRecursive,
45+
readdirRecursive,
46+
};

0 commit comments

Comments
 (0)