-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildMap.js
46 lines (40 loc) · 1.21 KB
/
buildMap.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
import { readdir } from 'fs/promises';
import { resolve, sep } from 'path';
import esbuild from 'esbuild';
const listNodeModules = async (dir = 'node_modules') => {
let results = [];
const files = await readdir(dir, { withFileTypes: true });
for (const file of files) {
if (file.isDirectory()) {
const res = resolve(dir, file.name);
results.push(res.split(sep).slice(-2).join('/'));
results = results.concat(await listNodeModules(res));
}
}
return results;
};
const shouldMarkAsExternal = packageName => {
const externalKeywords = ['@aws-sdk', '@mapbox'];
return externalKeywords.some(keyword => packageName.includes(keyword));
// return packageName.includes('aws');
};
const buildMap = async (path, output) => {
try {
const allDependencies = await listNodeModules();
const externalDependencies = allDependencies.filter(shouldMarkAsExternal);
await esbuild.build({
entryPoints: [path],
bundle: true,
sourcemap: true,
format: 'esm',
platform: 'node',
external: externalDependencies,
loader: { '.html': 'text' },
outfile: output,
});
} catch (err) {
console.error(err);
process.exit(1);
}
};
export default buildMap;