-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
100 lines (88 loc) · 2.02 KB
/
index.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
import { visualizer } from 'rollup-plugin-visualizer'
import importFrom from 'import-from-esm'
import * as fs from 'node:fs'
/**
* @returns {Promise<import('vite')>}
*/
async function importVite() {
/** @type {import('vite')} */
let vite = await importFrom.silent(process.cwd(), 'vite');
try {
vite ??= await import(import.meta.resolve('vite'));
} catch {}
return vite;
}
const start = async ({
help,
template = 'treemap',
open,
output,
config,
entry,
sourcemap = false,
mode,
}) => {
if (help) {
return
}
// fallback to 'raw-data'
if (template === 'json') {
template = 'raw-data'
}
// import from cwd's node_modules
const { build } = await importVite()
let outFile
if (template === 'raw-data') {
outFile = output.replace(/\.html$/, '.json')
open = false
} else if (template === 'list') {
outFile = output.replace(/\.html$/, '.yml')
open = false
} else {
outFile = output
// Only open it if the command is passed an it is an html file.
if (open) {
open = output.endsWith('.html')
}
}
const viteBuildOptions = {}
if (entry || sourcemap) {
viteBuildOptions.rollupOptions = {}
if (entry != undefined) {
viteBuildOptions.rollupOptions.input = {
app: entry
}
}
if (sourcemap != undefined) {
viteBuildOptions.sourcemap = sourcemap
}
}
await build({
configFile: config,
mode: mode,
plugins: [
{
...visualizer({
open,
filename: outFile,
title: 'Vite Bundle Visualizer',
template,
brotliSize: true,
gzipSize: true,
sourcemap,
}),
enforce: 'post'
},
],
build: {
...viteBuildOptions
}
})
// fix encoding for list template
if (template === 'list') {
const tent = Buffer.from(fs.readFileSync(outFile)).toString().replaceAll('\0', '')
fs.writeFileSync(outFile, tent, { encoding: 'utf-8' });
}
console.log(`\nGenerated at ${outFile}`)
}
export default start