-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathsubmodule-preloader.ts
More file actions
72 lines (67 loc) · 2.06 KB
/
submodule-preloader.ts
File metadata and controls
72 lines (67 loc) · 2.06 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
65
66
67
68
69
70
71
72
import { join } from 'node:path';
import { build } from 'vite';
import { fileSize, type BuildConfig } from './util';
import { minify } from 'terser';
import type { Plugin } from 'vite';
/**
* Custom plugin to apply terser during the bundle generation. Vite doesn't minify library ES
* modules.
*/
function customTerserPlugin(): Plugin {
return {
name: 'custom-terser',
async renderChunk(code, chunk) {
// Only process JavaScript chunks
if (!chunk.fileName.endsWith('.mjs') && !chunk.fileName.endsWith('.js')) {
return null;
}
// Keep the result readable for debugging
const result = await minify(code, {
compress: {
defaults: false,
module: true,
hoist_props: true,
unused: true,
booleans_as_integers: true,
},
mangle: {
toplevel: false,
properties: {
// use short attribute names for internal properties
regex: '^\\$.+\\$$|^[A-Z][a-zA-Z]+$',
},
},
format: {
comments: true,
},
});
return result.code || null;
},
};
}
/**
* Builds the qwikloader javascript files using Vite. These files can be used by other tooling, and
* are provided in the package so CDNs could point to them. The @builder.io/optimizer submodule also
* provides a utility function.
*/
export async function submodulePreloader(config: BuildConfig) {
await build({
build: {
emptyOutDir: false,
copyPublicDir: false,
lib: {
entry: join(config.srcQwikDir, 'core/preloader'),
formats: ['es', 'cjs'],
fileName: (format) => (format === 'es' ? 'preloader.mjs' : 'preloader.cjs'),
},
rollupOptions: {
external: ['@builder.io/qwik/build'],
},
minify: false, // This is the default, just to be explicit
outDir: config.distQwikPkgDir,
},
plugins: [customTerserPlugin()],
});
const preloaderSize = await fileSize(join(config.distQwikPkgDir, 'preloader.mjs'));
console.log(`🐮 preloader:`, preloaderSize);
}