-
Notifications
You must be signed in to change notification settings - Fork 77
/
ncc.js
86 lines (68 loc) · 2.34 KB
/
ncc.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
const spawn = require('@expo/spawn-async');
const ncc = require('@vercel/ncc');
const fs = require('fs');
const { boolish } = require('getenv');
const path = require('path');
// For some entries, we need to specify externals for ncc to leave their require() as-it.
const EXTERNAL_REQUIRES = {
fingerprint: ['@expo/fingerprint', 'module', 'sqlite3'],
'fingerprint-post': ['@expo/fingerprint', 'module', 'sqlite3'],
'preview-build': ['@expo/fingerprint', 'module', 'sqlite3'],
};
const actionsDir = path.resolve(__dirname, 'src/actions');
const buildDir = path.resolve(__dirname, 'build');
const modulesDir = path.resolve(__dirname, 'node_modules');
prebuild().then(() => build());
async function prebuild() {
if (!boolish(process.env.CI, false)) {
return console.log('Skipped CI environment simulation');
}
console.log('Preparing build in simulated CI environment');
await fs.promises.rm(buildDir, { force: true, recursive: true });
await fs.promises.rm(modulesDir, { force: true, recursive: true });
await spawn('bun', ['install', '--frozen-lockfile'], {
stdio: 'inherit',
env: { ...process.env, CI: true },
});
console.log();
}
async function build() {
console.log('Building actions');
const actions = fs
.readdirSync(actionsDir, { withFileTypes: true })
.filter(entity => entity.isFile())
.map(entity => ({
name: path.basename(entity.name, path.extname(entity.name)),
file: path.resolve(actionsDir, entity.name),
}));
for (const action of actions) {
const files = await compile(action.file);
write(files, path.resolve(buildDir, action.name));
console.log(`✓ ${path.relative(__dirname, action.file)}`);
}
console.log();
console.log('All actions built');
}
async function compile(entry) {
const { code, map, assets } = await ncc(entry, {
externals: EXTERNAL_REQUIRES[path.parse(entry).name] ?? [],
cache: false,
license: 'license.txt',
quiet: true,
});
return {
...assets,
'index.js': { source: code },
'index.js.map': { source: map },
};
}
function write(files, dir) {
for (const fileName in files) {
if (!files[fileName].source) {
continue;
}
const filePath = path.resolve(dir, fileName);
fs.mkdirSync(path.dirname(filePath), { recursive: true });
fs.writeFileSync(filePath, files[fileName].source, { encoding: 'utf-8' });
}
}