-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathsubmodule-qwikloader.ts
More file actions
132 lines (116 loc) · 4 KB
/
submodule-qwikloader.ts
File metadata and controls
132 lines (116 loc) · 4 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
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
import { join } from 'node:path';
import { build } from 'vite';
import {
type BuildConfig,
ensureDir,
fileSize,
type PackageJSON,
readFile,
writeFile,
} from './util.ts';
import { minify } from 'terser';
import { writePackageJson } from './package-json.ts';
/**
* Builds the qwikloader javascript files. 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 submoduleQwikLoader(config: BuildConfig) {
// Build the debug version first
await build({
build: {
emptyOutDir: false,
copyPublicDir: false,
target: 'es2020',
lib: {
entry: join(config.srcQwikDir, 'qwikloader.ts'),
formats: ['es'],
fileName: () => 'qwikloader.debug.js',
},
minify: false,
outDir: config.distQwikPkgDir,
},
});
// Read the debug version
const debugFilePath = join(config.distQwikPkgDir, 'qwikloader.debug.js');
const debugContent = await readFile(debugFilePath, 'utf-8');
// Create the minified version using terser
const minifyResult = await minify(debugContent, {
compress: {
global_defs: {
'window.BuildEvents': false,
},
keep_fargs: false,
unsafe: true,
passes: 2,
},
mangle: {
keep_fnames: false,
properties: false,
toplevel: true,
},
// uncomment this to understand the minified version better
// format: { semicolons: false },
});
// Write the minified version
const minifiedFilePath = join(config.distQwikPkgDir, 'qwikloader.js');
await writeFile(minifiedFilePath, minifyResult.code || '');
await generateLoaderSubmodule(config);
const loaderSize = await fileSize(join(config.distQwikPkgDir, 'qwikloader.js'));
console.log(`🐸 qwikloader:`, loaderSize);
}
const getLoaderJsonString = async (config: BuildConfig, name: string) => {
const filePath = join(config.distQwikPkgDir, name);
const content = await readFile(filePath, 'utf-8');
// Remove vite comments and leading/trailing whitespace
let cleaned = content.trim().replace(/\n?\/\*\s*@vite[^*]+\*\/\n?/g, '');
if (cleaned.endsWith(';')) {
cleaned = cleaned.slice(0, -1);
}
return JSON.stringify(cleaned);
};
/** Load each of the qwik scripts to be inlined with esbuild "define" as const variables. */
export async function inlineQwikScriptsEsBuild(config: BuildConfig) {
const variableToFileMap = [
['QWIK_LOADER_DEFAULT_MINIFIED', 'qwikloader.js'],
['QWIK_LOADER_DEFAULT_DEBUG', 'qwikloader.debug.js'],
];
const define: { [varName: string]: string } = {};
await Promise.all(
variableToFileMap.map(async (varToFile) => {
const varName = `globalThis.${varToFile[0]}`;
define[varName] = await getLoaderJsonString(config, varToFile[1]);
})
);
return define;
}
async function generateLoaderSubmodule(config: BuildConfig) {
const loaderDistDir = join(config.distQwikPkgDir, 'loader');
const code = [
`const QWIK_LOADER = ${await getLoaderJsonString(config, 'qwikloader.js')};`,
`const QWIK_LOADER_DEBUG = ${await getLoaderJsonString(config, 'qwikloader.debug.js')};`,
];
const esmCode = [...code, `export { QWIK_LOADER, QWIK_LOADER_DEBUG };`];
const cjsCode = [
...code,
`exports.QWIK_LOADER = QWIK_LOADER;`,
`exports.QWIK_LOADER_DEBUG = QWIK_LOADER_DEBUG;`,
];
const dtsCode = [
`export declare const QWIK_LOADER: string;`,
`export declare const QWIK_LOADER_DEBUG: string;`,
];
ensureDir(loaderDistDir);
await writeFile(join(loaderDistDir, 'index.mjs'), esmCode.join('\n') + '\n');
await writeFile(join(loaderDistDir, 'index.cjs'), cjsCode.join('\n') + '\n');
await writeFile(join(loaderDistDir, 'index.d.ts'), dtsCode.join('\n') + '\n');
const loaderPkg: PackageJSON = {
name: `@builder.io/qwik/loader`,
version: config.distVersion,
main: `index.mjs`,
types: `index.d.ts`,
private: true,
type: 'module',
};
await writePackageJson(loaderDistDir, loaderPkg);
}