forked from alangpierce/sucrase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.ts
executable file
·103 lines (94 loc) · 3.61 KB
/
build.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
#!./node_modules/.bin/sucrase-node
/* eslint-disable no-console */
import mergeDirectoryContents from "./mergeDirectoryContents";
import run from "./run";
const SUCRASE = "./node_modules/.bin/sucrase";
const SUCRASE_SELF = "./bin/sucrase";
const TSC = "./node_modules/.bin/tsc";
// Fast mode is useful for development. It runs all builds in parallel and does not generate types
// or update dependencies.
const fast = process.argv.includes("--fast");
async function main(): Promise<void> {
const promiseFactories = [
() => buildSucrase(),
() => buildIntegration("./integrations/gulp-plugin"),
() => buildIntegration("./integrations/jest-plugin"),
() => buildIntegration("./integrations/webpack-loader"),
() => buildIntegration("./integrations/webpack-object-rest-spread-plugin"),
() => buildWebsite(),
];
if (fast) {
await Promise.all(promiseFactories.map((f) => f()));
} else {
for (const f of promiseFactories) {
await f();
}
}
}
async function buildSucrase(): Promise<void> {
console.log("Building Sucrase");
await run(`rm -rf ./dist`);
await run(`${SUCRASE} ./src -d ./dist --transforms imports,typescript -q`);
if (!fast) {
await run(`rm -rf ./dist-self-build`);
await run(`rm -rf ./dist-types`);
// The installed Sucrase version is always the previous version, but released versions of
// Sucrase should be self-compiled, so we do a multi-phase compilation. We compile Sucrase with
// the previous version, then use it to compile the current code, then use that to compile the
// code again. The second and third outputs should be exactly identical; otherwise we may have a
// problem where it miscompiled itself.
await run(`${SUCRASE_SELF} ./src -d ./dist-self-build --transforms imports,typescript -q`);
await run(
`${SUCRASE_SELF} ./src -d ./dist-self-build --transforms typescript --out-extension mjs -q`,
);
await run("rm -rf ./dist");
await run("mv ./dist-self-build ./dist");
await run(`${SUCRASE_SELF} ./src -d ./dist-self-build --transforms imports,typescript -q`);
await run(
`${SUCRASE_SELF} ./src -d ./dist-self-build --transforms typescript --out-extension mjs -q`,
);
await run("diff -r ./dist ./dist-self-build");
// Also add in .d.ts files from tsc, which only need to be compiled once.
await run(
`${TSC} --emitDeclarationOnly --declaration --isolatedModules false --project ./src --outDir ./dist-types`,
);
await mergeDirectoryContents("./dist-types/src", "./dist");
// Link all integrations to Sucrase so that all building/linting/testing is up to date.
await run("yarn link");
}
}
async function buildIntegration(path: string): Promise<void> {
console.log(`Building ${path}`);
if (!fast) {
const originalDir = process.cwd();
process.chdir(path);
await run("yarn");
await run("yarn link sucrase");
process.chdir(originalDir);
}
await run(`rm -rf ${path}/dist`);
await run(`${SUCRASE} ${path}/src -d ${path}/dist --transforms imports,typescript -q`);
if (!fast) {
await run(
`${TSC} --emitDeclarationOnly --declaration --isolatedModules false --project ${path} --outDir ${path}/dist`,
);
}
}
/**
* Just runs yarn for the website to prepare it for lint.
*/
async function buildWebsite(): Promise<void> {
if (!fast) {
console.log("Installing website dependencies");
const originalDir = process.cwd();
process.chdir("./website");
await run("yarn");
await run("yarn link sucrase");
process.chdir(originalDir);
}
}
main().catch((e) => {
console.error("Unhandled error:");
console.error(e);
process.exitCode = 1;
});