-
-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy pathindex.ts
576 lines (489 loc) · 14.4 KB
/
index.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
import '@sveltejs/site-kit/polyfills';
import { walk } from 'zimmerframe';
import '../patch_window';
import { rollup } from '@rollup/browser';
import { DEV } from 'esm-env';
import commonjs from './plugins/commonjs';
import glsl from './plugins/glsl';
import json from './plugins/json';
import mp3 from './plugins/mp3';
import image from './plugins/image';
import svg from './plugins/svg';
import replace from './plugins/replace';
import loop_protect from './plugins/loop-protect';
import type { Plugin, RollupCache, TransformResult } from '@rollup/browser';
import type { BundleMessageData, BundleOptions } from '../workers';
import type { Warning } from '../../types';
import type { CompileError, CompileResult } from 'svelte/compiler';
import type { File } from '../../Workspace.svelte';
import type { Node } from 'estree';
import { max } from './semver';
import { NPM, VIRTUAL } from '../constants';
import {
add_suffix,
fetch_package,
load_svelte,
parse_npm_url,
resolve_local,
resolve_subpath,
resolve_version
} from '../npm';
import type { BundleResult } from '$lib/public';
// hack for magic-string and rollup inline sourcemaps
// do not put this into a separate module and import it, would be treeshaken in prod
self.window = self;
const ENTRYPOINT = '__entry.js';
const STYLES = '__styles.js';
const ESM_ENV = '__esm-env.js';
let current_id: number;
let ready: ReturnType<typeof load_svelte>;
self.addEventListener('message', async (event: MessageEvent<BundleMessageData>) => {
switch (event.data.type) {
case 'init': {
ready = load_svelte(event.data.svelte_version, (version) => {
self.postMessage({
type: 'version',
message: version
});
});
break;
}
case 'bundle': {
try {
const { svelte, version: svelte_version, can_use_experimental_async } = await ready;
const { uid, files, options } = event.data;
current_id = uid;
setTimeout(async () => {
if (current_id !== uid) return;
const result = await bundle(
svelte,
svelte_version,
uid,
files,
options,
can_use_experimental_async
);
if (JSON.stringify(result.error) === JSON.stringify(ABORT)) return;
if (result && uid === current_id) postMessage(result);
});
} catch (e) {
self.postMessage({
type: 'error',
uid: event.data.uid,
message: `Error loading the compiler: ${(e as Error).message}`
});
}
break;
}
}
});
const ABORT = { aborted: true };
let previous: {
key: string;
cache: RollupCache | undefined;
};
let tailwind: Awaited<ReturnType<typeof init_tailwind>>;
async function init_tailwind() {
const tailwindcss = await import('tailwindcss');
const { default: tailwind_preflight } = await import('tailwindcss/preflight.css?raw');
const { default: tailwind_theme } = await import('tailwindcss/theme.css?raw');
const { default: tailwind_utilities } = await import('tailwindcss/utilities.css?raw');
const tailwind_files: Record<string, string> = {
'tailwindcss/theme.css': tailwind_theme,
'tailwindcss/preflight.css': tailwind_preflight,
'tailwindcss/utilities.css': tailwind_utilities
};
const tailwind_base = [
`@layer theme, base, components, utilities;`,
`@import "tailwindcss/theme.css" layer(theme);`,
`@import "tailwindcss/preflight.css" layer(base);`,
`@import "tailwindcss/utilities.css" layer(utilities);`
].join('\n');
return await tailwindcss.compile(tailwind_base, {
loadStylesheet: async (id, base) => {
return { content: tailwind_files[id], base };
}
});
}
async function get_bundle(
svelte: typeof import('svelte/compiler'),
svelte_version: string,
uid: number,
mode: 'client' | 'server',
virtual: Map<string, File>,
options: BundleOptions,
can_use_experimental_async: boolean
) {
let bundle;
/** A set of package names (without subpaths) to include in pkg.devDependencies when downloading an app */
const imports: Set<string> = new Set();
const warnings: Warning[] = [];
const all_warnings: Array<{ message: string }> = [];
const tailwind_candidates: string[] = [];
function add_tailwind_candidates(ast: Node | undefined) {
if (!ast) return;
walk(ast, null, {
ImportDeclaration() {
// don't descend into these nodes, so that we don't
// pick up import sources
},
Literal(node) {
if (typeof node.value === 'string' && node.value) {
tailwind_candidates.push(...node.value.split(' '));
}
},
TemplateElement(node) {
if (node.value.raw) {
tailwind_candidates.push(...node.value.raw.split(' '));
}
}
});
}
const repl_plugin: Plugin = {
name: 'svelte-repl',
async resolveId(importee, importer) {
if (uid !== current_id) throw ABORT;
// entrypoint
if (!importer) return `${VIRTUAL}/${ENTRYPOINT}`;
// special case
if (importee === 'esm-env') return `${VIRTUAL}/${ESM_ENV}`;
// importing from a URL
if (/^[a-z]+:/.test(importee)) return importee;
// importing a relative file
if (importee[0] === '.') {
if (importer.startsWith(VIRTUAL)) {
const url = new URL(importee, importer);
for (const suffix of ['', '.js', '.json']) {
const with_suffix = `${url.pathname.slice(1)}${suffix}`;
const file = virtual.get(with_suffix);
if (file) {
return url.href + suffix;
}
}
throw new Error(
`'${importee}' (imported by ${importer.replace(VIRTUAL + '/', '')}) does not exist`
);
}
if (importer.startsWith(NPM)) {
const { name, version } = parse_npm_url(importer);
const pkg = await fetch_package(name, name === 'svelte' ? svelte_version : version);
const path = new URL(importee, importer).pathname.replace(`/${name}@${version}/`, '');
return add_suffix(pkg, path);
}
return new URL(importee, importer).href;
}
// importing an external package -> `npm://$/<name>@<version>/<path>`
const match = /^((?:@[^/]+\/)?[^/@]+)(?:@([^/]+))?(\/.+)?$/.exec(importee);
if (!match) throw new Error(`Invalid import "${importee}"`);
const pkg_name = match[1];
if (pkg_name === 'svelte' && svelte_version === 'local') {
return await resolve_local(importee);
}
let default_version = 'latest';
if (importer.startsWith(NPM)) {
// use the version specified in importer's package.json, not `latest`
const { name, version } = parse_npm_url(importer);
const { meta } = await fetch_package(name, name === 'svelte' ? svelte_version : version);
if (meta.name === pkg_name) {
default_version = meta.version;
} else {
default_version = max(
meta.devDependencies?.[pkg_name] ??
meta.peerDependencies?.[pkg_name] ??
meta.dependencies?.[pkg_name]
);
}
}
if (importer.startsWith(VIRTUAL)) {
// if this was imported by one of our files, add it to the `imports` set
imports.add(pkg_name);
}
const v = await resolve_version(match[1], match[2] ?? default_version);
const pkg = await fetch_package(pkg_name, pkg_name === 'svelte' ? svelte_version : v);
const subpath = resolve_subpath(pkg, '.' + (match[3] ?? ''));
return add_suffix(pkg, subpath.slice(2));
},
async load(resolved) {
if (uid !== current_id) throw ABORT;
if (resolved.startsWith(VIRTUAL)) {
const file = virtual.get(resolved.slice(VIRTUAL.length + 1))!;
return file.contents;
}
if (resolved.startsWith(NPM)) {
let [, name, v, subpath] = /^npm:\/\/\$\/((?:@[^/]+\/)?[^/@]+)(?:@([^/]+))?\/(.+)$/.exec(
resolved
)!;
const pkg = await fetch_package(name, name === 'svelte' ? svelte_version : v);
const file = pkg.contents[subpath];
if (file) return file.text;
}
const response = await fetch(resolved);
if (response.ok) return response.text();
throw new Error(`Could not load ${resolved}`);
},
transform(code, id) {
if (uid !== current_id) throw ABORT;
const message = `bundling ${id.replace(VIRTUAL + '/', '').replace(NPM + '/', '')}`;
self.postMessage({ type: 'status', message });
if (!/\.(svelte|js)$/.test(id)) return null;
const name = id.split('/').pop()?.split('.')[0];
let result: CompileResult;
if (id.endsWith('.svelte')) {
const compilerOptions: any = {
filename: name + '.svelte',
generate: Number(svelte.VERSION.split('.')[0]) >= 5 ? 'client' : 'dom',
dev: true,
// @ts-expect-error
templatingMode: options.templatingMode
};
if (can_use_experimental_async) {
compilerOptions.experimental = { async: true };
}
result = svelte.compile(code, compilerOptions);
walk(result.ast.html as import('svelte/compiler').AST.TemplateNode, null, {
Attribute(node) {
if (Array.isArray(node.value)) {
for (const chunk of node.value) {
if (chunk.type === 'Text') {
tailwind_candidates.push(...chunk.data.split(' '));
}
}
}
}
});
add_tailwind_candidates(result.ast.module);
add_tailwind_candidates(result.ast.instance);
add_tailwind_candidates(result.ast.html);
if (result.css?.code) {
// resolve local files by inlining them
result.css.code = result.css.code.replace(
/url\(['"]?\.\/(.+?\.(svg|webp|png))['"]?\)/g,
(match, $1, $2) => {
if (virtual.has($1)) {
if ($2 === 'svg') {
return `url('data:image/svg+xml;base64,${btoa(virtual.get($1)!.contents)}')`;
} else {
return `url('data:image/${$2};base64,${virtual.get($1)!.contents}')`;
}
} else {
return match;
}
}
);
// add the CSS via injecting a style tag
result.js.code +=
'\n\n' +
`
import { styles as $$_styles } from '${VIRTUAL}/${STYLES}';
const $$__style = document.createElement('style');
$$__style.textContent = ${JSON.stringify(result.css.code)};
document.head.append($$__style);
$$_styles.push($$__style);
`.replace(/\t/g, '');
}
} else if (id.endsWith('.svelte.js')) {
const compilerOptions: any = {
filename: name + '.js',
generate: 'client',
dev: true
};
if (can_use_experimental_async) {
compilerOptions.experimental = { async: true };
}
result = svelte.compileModule?.(code, compilerOptions);
if (!result) {
return null;
}
} else {
return null;
}
// @ts-expect-error
(result.warnings || result.stats?.warnings)?.forEach((warning) => {
// This is required, otherwise postMessage won't work
// @ts-ignore
delete warning.toString;
// TODO remove stats post-launch
// @ts-ignore
warnings.push(warning);
});
const transform_result: TransformResult = {
code: result.js.code,
map: result.js.map
};
return transform_result;
}
};
const key = JSON.stringify(options);
bundle = await rollup({
input: './__entry.js',
cache: previous?.key === key && previous.cache,
plugins: [
repl_plugin,
commonjs,
json,
svg,
mp3,
image,
glsl,
loop_protect,
replace({
'process.env.NODE_ENV': JSON.stringify('production')
}),
options.tailwind && {
name: 'tailwind-extract',
transform(code, id) {
// TODO tidy this up
if (id.startsWith(`${NPM}/svelte@`)) return;
if (id.startsWith(`${NPM}/clsx@`)) return;
if (id === `${VIRTUAL}/${ENTRYPOINT}`) return;
if (id === `${VIRTUAL}/${STYLES}`) return;
if (id === `${VIRTUAL}/${ESM_ENV}`) return;
if (id.endsWith('.svelte')) return;
add_tailwind_candidates(this.parse(code));
}
}
],
onwarn(warning) {
all_warnings.push({
message: warning.message
});
}
});
previous = { key, cache: bundle.cache };
return {
bundle,
tailwind: options.tailwind
? (tailwind ?? (await init_tailwind())).build(tailwind_candidates)
: undefined,
imports: Array.from(imports),
error: null,
warnings,
all_warnings
};
}
async function bundle(
svelte: typeof import('svelte/compiler'),
svelte_version: string,
uid: number,
files: File[],
options: BundleOptions,
can_use_experimental_async: boolean
): Promise<BundleResult> {
if (!DEV) {
console.log(`running Svelte compiler version %c${svelte.VERSION}`, 'font-weight: bold');
}
const lookup: Map<string, File> = new Map();
lookup.set(ENTRYPOINT, {
type: 'file',
name: ENTRYPOINT,
basename: ENTRYPOINT,
contents:
svelte.VERSION.split('.')[0] >= '5'
? `
import { unmount as u } from 'svelte';
import { styles } from '${VIRTUAL}/${STYLES}';
export { mount, untrack } from 'svelte';
export {default as App} from './App.svelte';
export function unmount(component) {
u(component);
styles.forEach(style => style.remove());
}
`
: `
import { styles } from '${VIRTUAL}/${STYLES}';
export {default as App} from './App.svelte';
export function mount(component, options) {
return new component(options);
}
export function unmount(component) {
component.$destroy();
styles.forEach(style => style.remove());
}
export function untrack(fn) {
return fn();
}
`,
text: true
});
lookup.set(STYLES, {
type: 'file',
name: STYLES,
basename: STYLES,
contents: `
export let styles = [];
`,
text: true
});
lookup.set(ESM_ENV, {
type: 'file',
name: STYLES,
basename: STYLES,
contents: `
export const BROWSER = true;
export const DEV = true;
`,
text: true
});
files.forEach((file) => {
lookup.set(file.name, file);
});
try {
let client: Awaited<ReturnType<typeof get_bundle>> = await get_bundle(
svelte,
svelte_version,
uid,
'client',
lookup,
options,
can_use_experimental_async
);
const client_result = (
await client.bundle?.generate({
format: 'iife',
exports: 'named',
inlineDynamicImports: true
// sourcemap: 'inline'
})
)?.output[0];
const server = false // TODO how can we do SSR?
? await get_bundle(
svelte,
svelte_version,
uid,
'server',
lookup,
options,
can_use_experimental_async
)
: null;
const server_result = server
? (
await server.bundle?.generate({
format: 'iife',
name: 'SvelteComponent',
exports: 'named'
// sourcemap: 'inline'
})
)?.output?.[0]
: null;
return {
uid,
error: null,
client: client_result,
server: server_result,
tailwind: client.tailwind,
imports: client.imports
};
} catch (err) {
console.error(err);
const e = err as CompileError; // TODO could be a non-Svelte error?
return {
uid,
error: { ...e, message: e.message }, // not all Svelte versions return an enumerable message property
client: null,
server: null,
tailwind: null,
imports: null
};
}
}