forked from QwikDev/qwik
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubmodule-qwikloader.ts
232 lines (213 loc) · 5.94 KB
/
submodule-qwikloader.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import { InputOptions, OutputOptions, rollup } from 'rollup';
import {
BuildConfig,
ensureDir,
fileSize,
PackageJSON,
readFile,
rollupOnWarn,
terser,
writeFile,
} from './util';
import { join } from 'path';
import { transform } from 'esbuild';
import { writePackageJson } from './package-json';
/**
* 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) {
const input: InputOptions = {
input: join(config.srcDir, 'qwikloader-entry.ts'),
plugins: [
{
name: 'qwikloaderTranspile',
resolveId(id) {
if (!id.endsWith('.ts')) {
return join(config.srcDir, id + '.ts');
}
return null;
},
async transform(code, id) {
const result = await transform(code, {
sourcefile: id,
target: 'es2017',
format: 'esm',
loader: 'ts',
});
return result.code;
},
},
],
onwarn: rollupOnWarn,
};
const defaultMinified: OutputOptions = {
// QWIK_LOADER_DEFAULT_MINIFIED
dir: config.distPkgDir,
format: 'es',
entryFileNames: `qwikloader.js`,
exports: 'none',
intro: `(()=>{`,
outro: `})()`,
plugins: [
terser({
compress: {
global_defs: {
'window.BuildEvents': false,
},
keep_fargs: false,
unsafe: true,
passes: 2,
},
format: {
comments: /@vite/,
},
}),
],
};
const defaultDebug: OutputOptions = {
// QWIK_LOADER_DEFAULT_DEBUG
dir: config.distPkgDir,
format: 'es',
entryFileNames: `qwikloader.debug.js`,
exports: 'none',
intro: `(()=>{`,
outro: `})()`,
plugins: [
terser({
compress: {
global_defs: {
'window.BuildEvents': false,
},
inline: false,
join_vars: false,
loops: false,
sequences: false,
},
format: {
comments: true,
beautify: true,
braces: true,
},
mangle: false,
}),
],
};
const optimizeMinified: OutputOptions = {
// QWIK_LOADER_OPTIMIZE_MINIFIED
dir: config.distPkgDir,
format: 'es',
entryFileNames: `qwikloader.optimize.js`,
exports: 'none',
intro: `(()=>{`,
outro: `})()`,
plugins: [
terser({
compress: {
global_defs: {
'window.BuildEvents': true,
},
keep_fargs: false,
unsafe: true,
passes: 2,
},
format: {
comments: /@vite-ignore/g,
},
}),
],
};
const optimizeDebug: OutputOptions = {
// QWIK_LOADER_OPTIMIZE_DEBUG
dir: config.distPkgDir,
format: 'es',
entryFileNames: `qwikloader.optimize.debug.js`,
exports: 'none',
intro: `(()=>{`,
outro: `})()`,
plugins: [
terser({
compress: {
global_defs: {
'window.BuildEvents': true,
},
inline: false,
join_vars: false,
loops: false,
sequences: false,
},
format: {
comments: /@vite-ignore/g,
beautify: true,
braces: true,
},
mangle: false,
}),
],
};
const build = await rollup(input);
await Promise.all([
build.write(defaultMinified),
build.write(defaultDebug),
build.write(optimizeMinified),
build.write(optimizeDebug),
]);
await generateLoaderSubmodule(config);
const optimizeFileSize = await fileSize(join(config.distPkgDir, 'qwikloader.optimize.js'));
console.log(`🐸 qwikloader:`, optimizeFileSize);
}
/**
* Load each of the qwik scripts to be inlined with esbuild "define" as const varialbles.
*/
export async function inlineQwikScriptsEsBuild(config: BuildConfig) {
const variableToFileMap = [
['QWIK_LOADER_DEFAULT_MINIFIED', 'qwikloader.js'],
['QWIK_LOADER_DEFAULT_DEBUG', 'qwikloader.debug.js'],
['QWIK_LOADER_OPTIMIZE_MINIFIED', 'qwikloader.optimize.js'],
['QWIK_LOADER_OPTIMIZE_DEBUG', 'qwikloader.optimize.debug.js'],
];
const define: { [varName: string]: string } = {};
await Promise.all(
variableToFileMap.map(async (varToFile) => {
const varName = `global.${varToFile[0]}`;
const filePath = join(config.distPkgDir, varToFile[1]);
const content = await readFile(filePath, 'utf-8');
define[varName] = JSON.stringify(content.trim());
})
);
return define;
}
async function generateLoaderSubmodule(config: BuildConfig) {
const loaderDistDir = join(config.distPkgDir, 'loader');
const loaderCode = await readFile(join(config.distPkgDir, 'qwikloader.js'), 'utf-8');
const loaderDebugCode = await readFile(join(config.distPkgDir, 'qwikloader.debug.js'), 'utf-8');
const code = [
`const QWIK_LOADER = ${JSON.stringify(loaderCode.trim())};`,
`const QWIK_LOADER_DEBUG = ${JSON.stringify(loaderDebugCode.trim())};`,
];
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.cjs`,
module: `index.mjs`,
types: `index.d.ts`,
private: true,
};
await writePackageJson(loaderDistDir, loaderPkg);
}