-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
vite.config.default.ts
163 lines (152 loc) · 4.42 KB
/
vite.config.default.ts
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/// <reference types="vite/client" />
import dts from 'vite-plugin-dts';
import { copyFileSync } from 'node:fs';
import { defineConfig, LibraryOptions, LibraryFormats, Plugin } from 'vite';
import glob from 'fast-glob';
import { build, Format } from 'esbuild';
import { resolve } from 'path';
import { umdWrapper } from 'esbuild-plugin-umd-wrapper';
// don't empty out dir if --watch flag is passed
const emptyOutDir = !process.argv.includes('--watch');
function minifyAndUMDPlugin({
name,
outDir,
}: {
name: LibraryOptions['name'];
outDir: string;
}): Plugin {
return {
name: 'minify-plugin',
async writeBundle(outputOptions, bundle) {
for (const file of Object.values(bundle)) {
if (
file.type === 'asset' &&
(file.fileName.endsWith('.cjs.map') || file.fileName.endsWith('.css'))
) {
const isCSS = file.fileName.endsWith('.css');
const inputFilePath = resolve(
outputOptions.dir!,
file.fileName,
).replace(/\.map$/, '');
const baseFileName = file.fileName.replace(
/(\.cjs|\.css)(\.map)?$/,
'',
);
const outputFilePath = resolve(outputOptions.dir!, baseFileName);
// console.log(outputFilePath, 'minifying', file.fileName);
if (isCSS) {
await buildFile({
input: inputFilePath,
output: `${outputFilePath}.min.css`,
minify: true,
isCss: true,
outDir,
});
} else {
await buildFile({
name,
input: inputFilePath,
output: `${outputFilePath}.umd.cjs`,
minify: false,
isCss: false,
outDir,
});
await buildFile({
name,
input: inputFilePath,
output: `${outputFilePath}.umd.min.cjs`,
minify: true,
isCss: false,
outDir,
});
}
}
}
},
};
}
async function buildFile({
name,
input,
output,
minify,
isCss,
outDir,
}: {
name?: LibraryOptions['name'];
input: string;
output: string;
outDir: string;
minify: boolean;
isCss: boolean;
}) {
await build({
entryPoints: [input],
outfile: output,
minify,
sourcemap: true,
format: isCss ? undefined : ('umd' as Format),
target: isCss ? undefined : 'es2017',
treeShaking: !isCss,
plugins: [
umdWrapper({
libraryName: name,
}),
],
});
const filename = output.replace(new RegExp(`^.+/(${outDir}/)`), '$1');
console.log(filename);
console.log(`${filename}.map`);
}
export default function (
entry: LibraryOptions['entry'],
name: LibraryOptions['name'],
options?: { outputDir?: string; fileName?: string; plugins?: Plugin[] },
) {
const { fileName, outputDir: outDir = 'dist', plugins = [] } = options || {};
let formats: LibraryFormats[] = ['es', 'cjs'];
return defineConfig(() => ({
build: {
// See https://vitejs.dev/guide/build.html#library-mode
lib: {
entry,
name,
fileName,
// TODO: turn on `umd` for rrweb when https://github.com/schummar/vite/tree/feature/libMultiEntryUMD gets merged
// More info: https://github.com/vitejs/vite/pull/7047#issuecomment-1288080855
// formats: ['es', 'umd', 'cjs'],
formats,
},
outDir,
emptyOutDir,
// Leaving this unminified so you can see what exactly gets included in
// the bundles
minify: false,
sourcemap: true,
// rollupOptions: {
// output: {
// manualChunks: {},
// },
// },
},
plugins: [
dts({
insertTypesEntry: true,
rollupTypes: true,
afterBuild: (emittedFiles: Map<string, string>) => {
// To pass publint (`npm x publint@latest`) and ensure the
// package is supported by all consumers, we must export types that are
// read as ESM. To do this, there must be duplicate types with the
// correct extension supplied in the package.json exports field.
const files: string[] = Array.from(emittedFiles.keys());
files.forEach((file) => {
const ctsFile = file.replace('.d.ts', '.d.cts');
copyFileSync(file, ctsFile);
});
},
}),
minifyAndUMDPlugin({ name, outDir }),
...plugins,
],
}));
}