-
-
Notifications
You must be signed in to change notification settings - Fork 32.6k
/
Copy pathbuild.mjs
163 lines (141 loc) · 4.68 KB
/
build.mjs
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
154
155
156
157
158
159
160
161
162
163
import childProcess from 'child_process';
import path from 'path';
import { promisify } from 'util';
import yargs from 'yargs';
import * as fs from 'fs/promises';
import { cjsCopy } from './copyFilesUtils.mjs';
import { getVersionEnvVariables, getWorkspaceRoot } from './utils.mjs';
const exec = promisify(childProcess.exec);
const validBundles = [
// build for node using commonJS modules
'node',
// build with a hardcoded target using ES6 modules
'stable',
];
const bundleTypes = {
stable: 'module',
node: 'commonjs',
};
async function run(argv) {
const { bundle, largeFiles, outDir: outDirBase, verbose, cjsDir } = argv;
if (!validBundles.includes(bundle)) {
throw new TypeError(
`Unrecognized bundle '${bundle}'. Did you mean one of "${validBundles.join('", "')}"?`,
);
}
const packageJsonPath = path.resolve('./package.json');
const packageJson = JSON.parse(await fs.readFile(packageJsonPath, { encoding: 'utf8' }));
const babelRuntimeVersion = packageJson.dependencies?.['@babel/runtime'];
if (!babelRuntimeVersion) {
throw new Error(
'package.json needs to have a dependency on `@babel/runtime` when building with `@babel/plugin-transform-runtime`.',
);
}
const babelConfigPath = path.resolve(getWorkspaceRoot(), 'babel.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',
'**/*.test/*.*',
'**/test-cases/*.*',
];
const outFileExtension = '.js';
const relativeOutDir = {
node: cjsDir,
stable: './esm',
}[bundle];
const outDir = path.resolve(outDirBase, relativeOutDir);
const env = {
NODE_ENV: 'production',
BABEL_ENV: bundle,
MUI_BUILD_VERBOSE: verbose,
MUI_BABEL_RUNTIME_VERSION: babelRuntimeVersion,
MUI_OUT_FILE_EXTENSION: outFileExtension,
...(await getVersionEnvVariables(packageJson)),
};
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 (outFileExtension !== '.js') {
babelArgs.push('--out-file-extension', outFileExtension);
}
if (largeFiles) {
babelArgs.push('--compact false');
}
const command = ['pnpm 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}`);
}
// cjs for reexporting from commons only modules.
// If we need to rely more on this we can think about setting up a separate commonjs => commonjs build for .cjs files to .cjs
// `--extensions-.cjs --out-file-extension .cjs`
await cjsCopy({ from: srcDir, to: outDir });
// Write a package.json file in the output directory if we are building the stable bundle
// or if the output directory is not the root of the package.
const shouldWriteBundlePackageJson = bundle === 'stable' || relativeOutDir !== './';
if (shouldWriteBundlePackageJson && !argv.skipEsmPkg) {
const rootBundlePackageJson = path.join(outDir, 'package.json');
await fs.writeFile(
rootBundlePackageJson,
JSON.stringify({ type: bundleTypes[bundle], sideEffects: packageJson.sideEffects }),
);
}
if (verbose) {
// eslint-disable-next-line no-console
console.log(stdout);
}
}
yargs(process.argv.slice(2))
.command({
command: '$0 <bundle>',
description: 'build package',
builder: (command) => {
return 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('skipEsmPkg', {
type: 'boolean',
default: false,
describe:
"Set to `true` if you don't want to generate a package.json file in the /esm folder.",
})
.option('cjsDir', {
default: './',
type: 'string',
description: 'The directory to copy the cjs files to.',
})
.option('out-dir', { default: './build', type: 'string' })
.option('verbose', { type: 'boolean' });
},
handler: run,
})
.help()
.strict(true)
.version(false)
.parse();