-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbuild.js
86 lines (75 loc) · 2.46 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
import fs from 'fs/promises';
import path from 'path';
import ncc from '@vercel/ncc';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const bundledDependencies = new Set([
// Thumbhash is an ESM module, so we need to bundle it
'thumbhash',
]);
const packageJSON = JSON.parse(
await fs.readFile('package.json', { encoding: 'utf-8' }),
);
const externals = [
...Object.keys(packageJSON.dependencies),
...Object.keys(packageJSON.peerDependencies),
].filter((dependency) => !bundledDependencies.has(dependency));
await ncc(path.resolve(__dirname, './src/index.ts'), {
// provide a custom cache path or disable caching
cache: false,
externals,
// directory outside of which never to emit assets
filterAssetBase: process.cwd(),
minify: false,
sourceMap: true,
assetBuilds: false,
sourceMapBasePrefix: '../', // default treats sources as output-relative
// when outputting a sourcemap, automatically include
// source-map-support in the output file (increases output by 32kB).
sourceMapRegister: true, // default
watch: false, // default
license: '', // default does not generate a license file
target: 'es2020', // default
v8cache: false, // default
quiet: false, // default
debugLog: true, // default
}).then(async ({ code, map, assets }) => {
// Assets is an object of asset file names to { source, permissions, symlinks }
// expected relative to the output code (if any)
for (const [fileName, { source, permissions }] of Object.entries(assets)) {
const destFileName = path.resolve(__dirname, './lib/', fileName);
await fs.mkdir(path.dirname(destFileName), { recursive: true });
await fs.writeFile(destFileName, source, {
mode: permissions,
});
}
await fs.writeFile(path.resolve(__dirname, './lib/index.js'), code);
});
const newPackageJSON = {
...packageJSON,
scripts: {},
main: 'index.js',
types: 'index.d.ts',
private: false,
files: ['**/*'],
dependencies: externals.reduce((acc, dependency) => {
if (!(dependency in packageJSON.dependencies)) {
return acc;
}
return {
...acc,
[dependency]: packageJSON.dependencies[dependency],
};
}, {}),
};
await fs.writeFile(
path.resolve(__dirname, 'lib/package.json'),
JSON.stringify(newPackageJSON, null, 2),
'utf-8',
);
await fs.copyFile(
path.resolve(__dirname, 'README.md'),
path.resolve(__dirname, 'lib/README.md'),
);