This repository has been archived by the owner on May 26, 2023. It is now read-only.
forked from graphql/graphql-js
/
build.js
151 lines (124 loc) · 4.5 KB
/
build.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// @noflow
'use strict';
const fs = require('fs');
const path = require('path');
const assert = require('assert');
const babel = require('@babel/core');
const {
copyFile,
writeFile,
rmdirRecursive,
mkdirRecursive,
readdirRecursive,
parseSemver,
} = require('./utils');
if (require.main === module) {
rmdirRecursive('./dist');
mkdirRecursive('./dist');
copyFile('./LICENSE', './dist/LICENSE');
copyFile('./README.md', './dist/README.md');
const tsArtifacts = readdirRecursive('./ts-build', { ignoreDir: /^__.*__$/ });
for (const filepath of tsArtifacts) {
if (filepath.endsWith('.js')) {
buildTSEmittedJSFile(filepath);
} else if (filepath.endsWith('.d.ts')) {
const srcPath = path.join('./ts-build', filepath);
const destPath = path.join('./dist', filepath);
copyFile(srcPath, destPath);
}
}
rmdirRecursive('./ts-build');
const srcFiles = readdirRecursive('./src', { ignoreDir: /^__.*__$/ });
for (const filepath of srcFiles) {
if (filepath.endsWith('.js')) {
buildFlowFile(filepath);
} else if (filepath.endsWith('.js.flow') || filepath.endsWith('.d.ts')) {
const srcPath = path.join('./src', filepath);
const destPath = path.join('./dist', filepath);
copyFile(srcPath, destPath);
}
}
const tsFiles = readdirRecursive('./tstypes', { ignoreDir: /^__.*__$/ });
for (const filepath of tsFiles) {
if (filepath.endsWith('.d.ts')) {
const srcPath = path.join('./tstypes', filepath);
const destPath = path.join('./dist', filepath);
copyFile(srcPath, destPath);
}
}
const packageJSON = buildPackageJSON();
assert(
packageJSON.version === require('../dist/version').version,
'Version in package.json and version.js should match',
);
writeFile('./dist/package.json', JSON.stringify(packageJSON, null, 2));
showStats();
}
function showStats() {
const fileTypes = {};
let totalSize = 0;
for (const filepath of readdirRecursive('./dist')) {
const name = filepath.split(path.sep).pop();
const [base, ...splitedExt] = name.split('.');
const ext = splitedExt.join('.');
const filetype = ext ? '*.' + ext : base;
fileTypes[filetype] = fileTypes[filetype] || { filepaths: [], size: 0 };
const { size } = fs.lstatSync(path.join('./dist', filepath));
totalSize += size;
fileTypes[filetype].size += size;
fileTypes[filetype].filepaths.push(filepath);
}
let stats = [];
for (const [filetype, typeStats] of Object.entries(fileTypes)) {
const numFiles = typeStats.filepaths.length;
if (numFiles > 1) {
stats.push([filetype + ' x' + numFiles, typeStats.size]);
} else {
stats.push([typeStats.filepaths[0], typeStats.size]);
}
}
stats.sort((a, b) => b[1] - a[1]);
stats = stats.map(([type, size]) => [type, (size / 1024).toFixed(2) + ' KB']);
const typeMaxLength = Math.max(...stats.map(x => x[0].length));
const sizeMaxLength = Math.max(...stats.map(x => x[1].length));
for (const [type, size] of stats) {
console.log(
type.padStart(typeMaxLength) + ' | ' + size.padStart(sizeMaxLength),
);
}
console.log('-'.repeat(typeMaxLength + 3 + sizeMaxLength));
const totalMB = (totalSize / 1024 / 1024).toFixed(2) + ' MB';
console.log(
'Total'.padStart(typeMaxLength) + ' | ' + totalMB.padStart(sizeMaxLength),
);
}
function babelBuild(srcPath, envName) {
return babel.transformFileSync(srcPath, { envName }).code + '\n';
}
function buildFlowFile(filepath) {
const srcPath = path.join('./src', filepath);
const destPath = path.join('./dist', filepath);
copyFile(srcPath, destPath + '.flow');
writeFile(destPath, babelBuild(srcPath, 'cjs'));
writeFile(destPath.replace(/\.js$/, '.mjs'), babelBuild(srcPath, 'mjs'));
}
function buildTSEmittedJSFile(filepath) {
const srcPath = path.join('./ts-build', filepath);
const destPath = path.join('./dist', filepath);
writeFile(destPath, babelBuild(srcPath, 'cjs'));
writeFile(destPath.replace(/\.js$/, '.mjs'), babelBuild(srcPath, 'mjs'));
}
function buildPackageJSON() {
const packageJSON = require('../package.json');
delete packageJSON.private;
delete packageJSON.scripts;
delete packageJSON.devDependencies;
const { preReleaseTag } = parseSemver(packageJSON.version);
if (preReleaseTag != null) {
const [tag] = preReleaseTag.split('.');
assert(tag === 'rc', 'Only "rc" tag is supported.');
assert(!packageJSON.publishConfig, 'Can not override "publishConfig".');
packageJSON.publishConfig = { tag: tag || 'latest' };
}
return packageJSON;
}