Repository navigation
Expand file tree
/
Copy pathvite.lib.config.ts
More file actions
64 lines (59 loc) · 1.71 KB
/
Copy pathvite.lib.config.ts
File metadata and controls
64 lines (59 loc) · 1.71 KB
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
/*
* SDK library build.
*
* vite-plugin-ruby pulls every entrypoint as input, but the SDK needs to ship
* as a single IIFE file (`inlineDynamicImports: true`), which is incompatible
* with multiple entrypoints. So the SDK gets its own pipeline:
*
* vite build --config vite.lib.config.ts → public/packs/js/sdk.js
*
* The `assets:precompile` rake task runs this alongside the main app build.
*/
import { brotliCompressSync, constants, gzipSync } from 'node:zlib';
import { defineConfig, type Plugin } from 'vite';
import path from 'path';
import { aliases } from './vite.shared';
const compressedSdkPlugin = {
name: 'compress-sdk',
generateBundle(_options, bundle) {
const sdkBundle = bundle['js/sdk.js'];
if (sdkBundle?.type !== 'chunk') {
this.error('SDK bundle was not generated');
}
this.emitFile({
type: 'asset',
fileName: 'js/sdk.js.br',
source: brotliCompressSync(sdkBundle.code, {
params: {
[constants.BROTLI_PARAM_QUALITY]: constants.BROTLI_MAX_QUALITY,
},
}),
});
this.emitFile({
type: 'asset',
fileName: 'js/sdk.js.gz',
source: gzipSync(sdkBundle.code, {
level: constants.Z_BEST_COMPRESSION,
}),
});
},
} satisfies Plugin;
export default defineConfig({
plugins: [compressedSdkPlugin],
build: {
rollupOptions: {
output: {
dir: 'public/packs',
entryFileNames: chunkInfo =>
chunkInfo.name === 'sdk' ? 'js/sdk.js' : '[name].js',
inlineDynamicImports: true,
},
},
lib: {
entry: path.resolve(__dirname, './app/javascript/entrypoints/sdk.js'),
formats: ['iife'],
name: 'sdk',
},
},
resolve: { alias: aliases },
});