-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbuild.js
123 lines (110 loc) · 3.92 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { generateDtsBundle } from 'dts-bundle-generator';
import { readFile, writeFile } from 'node:fs/promises';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { defineConfig, build as viteBuild } from 'vite';
const outputDir = process.env.BUILD_ENV !== 'production' ? 'dist-temp' : 'dist';
export const globalConfig = {
libraryName: 'VueRay',
outDir: resolve(dirname(fileURLToPath(import.meta.url)), `../${outputDir}`),
basePath: resolve(dirname(fileURLToPath(import.meta.url)), '..'),
builds: [
{
entry: 'src/index.ts',
outfile: 'index.cjs',
target: 'browser',
},
{
entry: 'src/index.ts',
outfile: 'index.js',
target: 'browser',
},
],
/** @type Record<string, any>|null */
pkg: null, // assigned in init()
getDependencies() {
return Object.keys(this.pkg.dependencies).concat([
'node:fs',
'node:fs/promises',
'node:os',
'node:path',
'node:process',
'node:url',
'fs',
'os',
'process',
'node-ray',
'node-ray/web',
'@permafrost-dev/pretty-format',
]);
},
async init() {
this.pkg = JSON.parse(await readFile(resolve(this.basePath, 'package.json')));
this.builds = this.builds.map(config => {
config.entry = resolve(globalConfig.basePath, config.entry);
config.minify = config.outfile.includes('.min.');
config.standalone = config.outfile.includes('standalone');
config.format = config.outfile.endsWith('.js') ? 'es' : 'cjs';
if (config.standalone) {
config.format = 'iife';
}
return config;
});
},
};
async function buildTypeDefinitions() {
async function generateTypesForEntry(info) {
const dts = generateDtsBundle([{ filePath: info.entry, failOnClass: false, output: { exportReferencedTypes: false } }])
.pop()
.replaceAll('export {};', '')
.replaceAll(/\n{2,}/g, '\n')
.replaceAll(/\t/g, ' ')
.trim();
const baseFn = info.outfile;
await writeFile(outputDir + '/' + baseFn, dts, 'utf8');
console.log(`Compiled ${outputDir}/${baseFn}`);
}
const entries = [{ entry: 'src/index.ts', outfile: 'index.d.ts' }];
await Promise.all(entries.map(e => generateTypesForEntry(e)));
}
async function buildWithVite(config) {
await viteBuild(
defineConfig({
define: {
__BUILD_VERSION__: JSON.stringify(globalConfig.pkg.version),
},
build: {
lib: {
entry: config.entry,
name: globalConfig.libraryName,
formats: [config.format],
fileName: () => config.outfile,
},
emptyOutDir: false,
outDir: globalConfig.outDir,
minify: config.minify || false,
sourcemap: true,
rollupOptions: {
external: globalConfig.getDependencies(),
preserveEntrySignatures: 'strict',
shimMissingExports: true,
treeshake: false,
},
target: config.target === 'browser' ? 'chrome90' : 'node18',
},
resolve: {
extensions: ['.ts', '.js'],
alias: {
'@': `${globalConfig.basePath}/src`,
},
},
}),
);
}
async function main() {
await globalConfig.init();
await Promise.all([...globalConfig.builds.map(config => buildWithVite(config)), buildTypeDefinitions()]);
console.log('All library file builds complete.');
console.log('All type definitions created.');
}
main();