-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuild.js
108 lines (96 loc) · 2.55 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
const childProcess = require("child_process");
const path = require("path");
const { promisify } = require("util");
const yargs = require("yargs");
const exec = promisify(childProcess.exec);
const validBundles = ["stable"];
async function run(argv) {
const { bundle, largeFiles, outDir: relativeOutDir, verbose } = argv;
if (validBundles.indexOf(bundle) === -1) {
throw new TypeError(
`Unrecognized bundle '${bundle}'. Did you mean one of "${validBundles.join(
"\", \"",
)}"?`,
);
}
const env = {
NODE_ENV: "production",
BABEL_ENV: bundle,
MUI_BUILD_VERBOSE: verbose,
};
const babelConfigPath = path.resolve(__dirname, "../babel-build.config.js");
const srcDir = path.resolve("./src");
const extensions = [".js", ".ts", ".tsx"];
const ignore = [
"**/*.test.js",
"**/*.test.ts",
"**/*.test.tsx",
"**/*.spec.ts",
"**/*.spec.tsx",
"**/*.d.ts",
"**/__stories__/*",
"**/*.stories.ts",
"**/*.stories.js",
"**/*.stories.tsx",
"**/*.stories.jsx",
];
const outDir = path.resolve(
relativeOutDir,
{
stable: "./",
}[bundle],
);
const babelArgs = [
"--config-file",
babelConfigPath,
"--extensions",
`"${extensions.join(",")}"`,
srcDir,
"--out-dir",
outDir,
"--ignore",
// Need to put these patterns in quotes otherwise they might be evaluated by the used terminal.
`"${ignore.join("\",\"")}"`,
];
if (largeFiles) {
babelArgs.push("--compact false");
}
const command = ["npx babel", ...babelArgs].join(" ");
if (verbose) {
// eslint-disable-next-line no-console
console.log(`running '${command}' with ${JSON.stringify(env)}`);
}
const { stderr, stdout } = await exec(command, {
env: { ...process.env, ...env },
});
if (stderr) {
throw new Error(`'${command}' failed with \n${stderr}`);
}
if (verbose) {
// eslint-disable-next-line no-console
console.log(stdout);
}
}
yargs
.command({
command: "$0 <bundle>",
description: "build package",
builder: (command) => command
.positional("bundle", {
description: `Valid bundles: "${validBundles.join("\" | \"")}"`,
type: "string",
})
.option("largeFiles", {
type: "boolean",
default: false,
describe:
"Set to `true` if you know you are transpiling large files.",
})
.option("out-dir", { default: "./dist", type: "string" })
.option("verbose", { type: "boolean" }),
handler: run,
})
.help()
.strict(true)
.version(false)
.parse();