forked from graphql/graphql-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-npm.ts
153 lines (129 loc) · 4.74 KB
/
build-npm.ts
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
152
153
import assert from 'node:assert';
import fs from 'node:fs';
import path from 'node:path';
import ts from 'typescript';
import { changeExtensionInImportPaths } from './change-extension-in-import-paths.js';
import { inlineInvariant } from './inline-invariant.js';
import {
readPackageJSON,
readTSConfig,
showDirStats,
writeGeneratedFile,
} from './utils.js';
console.log('\n./npmDist');
buildPackage('./npmDist', false);
showDirStats('./npmDist');
console.log('\n./npmEsmDist');
buildPackage('./npmEsmDist', true);
showDirStats('./npmEsmDist');
function buildPackage(outDir: string, isESMOnly: boolean): void {
fs.rmSync(outDir, { recursive: true, force: true });
fs.mkdirSync(outDir);
fs.copyFileSync('./LICENSE', `./${outDir}/LICENSE`);
fs.copyFileSync('./README.md', `./${outDir}/README.md`);
const packageJSON = readPackageJSON();
delete packageJSON.private;
delete packageJSON.scripts;
delete packageJSON.devDependencies;
assert(packageJSON.types === undefined, 'Unexpected "types" in package.json');
const supportedTSVersions = Object.keys(packageJSON.typesVersions);
assert(
supportedTSVersions.length === 1,
'Property "typesVersions" should have exactly one key.',
);
// TODO: revisit once TS implements https://github.com/microsoft/TypeScript/issues/32166
const notSupportedTSVersionFile = 'NotSupportedTSVersion.d.ts';
fs.writeFileSync(
path.join(outDir, notSupportedTSVersionFile),
// Provoke syntax error to show this message
`"Package 'graphql' support only TS versions that are ${supportedTSVersions[0]}".`,
);
packageJSON.typesVersions = {
...packageJSON.typesVersions,
'*': { '*': [notSupportedTSVersionFile] },
};
// TODO: move to integration tests
const publishTag = packageJSON.publishConfig?.tag;
assert(publishTag != null, 'Should have packageJSON.publishConfig defined!');
const { version } = packageJSON;
const versionMatch = /^\d+\.\d+\.\d+-?(?<preReleaseTag>.*)?$/.exec(version);
if (versionMatch?.groups == null) {
throw new Error('Version does not match semver spec: ' + version);
}
const { preReleaseTag } = versionMatch.groups;
if (preReleaseTag != null) {
const splittedTag = preReleaseTag.split('.');
// Note: `experimental-*` take precedence over `alpha`, `beta` or `rc`.
const versionTag = splittedTag[2] ?? splittedTag[0];
assert(
['alpha', 'beta', 'rc'].includes(versionTag) ||
versionTag.startsWith('experimental-'),
`"${versionTag}" tag is not supported.`,
);
assert.equal(
versionTag,
publishTag,
'Publish tag and version tag should match!',
);
}
if (isESMOnly) {
packageJSON.exports = {};
const { emittedTSFiles } = emitTSFiles({
outDir,
module: 'es2020',
extension: '.js',
});
for (const filepath of emittedTSFiles) {
if (path.basename(filepath) === 'index.js') {
const relativePath = './' + path.relative('./npmEsmDist', filepath);
packageJSON.exports[path.dirname(relativePath)] = relativePath;
}
}
// Temporary workaround to allow "internal" imports, no grantees provided
packageJSON.exports['./*.js'] = './*.js';
packageJSON.exports['./*'] = './*.js';
packageJSON.publishConfig.tag += '-esm';
packageJSON.version += '+esm';
} else {
delete packageJSON.type;
packageJSON.main = 'index';
packageJSON.module = 'index.mjs';
emitTSFiles({ outDir, module: 'commonjs', extension: '.js' });
emitTSFiles({ outDir, module: 'es2020', extension: '.mjs' });
}
// Should be done as the last step so only valid packages can be published
writeGeneratedFile(`./${outDir}/package.json`, JSON.stringify(packageJSON));
}
// Based on https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#getting-the-dts-from-a-javascript-file
function emitTSFiles(options: {
outDir: string;
module: string;
extension: string;
}): {
emittedTSFiles: ReadonlyArray<string>;
} {
const { outDir, module, extension } = options;
const tsOptions = readTSConfig({
module,
noEmit: false,
declaration: true,
declarationDir: outDir,
outDir,
listEmittedFiles: true,
});
const tsHost = ts.createCompilerHost(tsOptions);
tsHost.writeFile = (filepath, body) =>
writeGeneratedFile(filepath.replace(/.js$/, extension), body);
const tsProgram = ts.createProgram(['src/index.ts'], tsOptions, tsHost);
const tsResult = tsProgram.emit(undefined, undefined, undefined, undefined, {
after: [changeExtensionInImportPaths({ extension }), inlineInvariant],
});
assert(
!tsResult.emitSkipped,
'Fail to generate `*.d.ts` files, please run `npm run check`',
);
assert(tsResult.emittedFiles != null);
return {
emittedTSFiles: tsResult.emittedFiles.sort((a, b) => a.localeCompare(b)),
};
}