You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: DEVELOPER_GUIDE.md
+116Lines changed: 116 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -268,3 +268,119 @@ for (const scope of [module.scope, instance.scope]) {
268
268
### CSS Analysis
269
269
270
270
While we didn't go deep in how the analysis works for every single step of the analysis it's worth exploring the CSS analysis a bit more in depth. This phase in itself is subdivided in three phases: we first analyze the css, during this phase we validate the structure of svelte specific css (eg. the `:global` selector) is valid also marking every node within global as `used`. We then proceed to `prune` the css. In this phase we match every selector with the html structure...if a selector doesn't match any element we `prune` it away either by commenting it out in dev (so that the users can actually see what's being removed) or by completely remove it in prod. Finally we walk the css AST once more to warn about all the `unused` css nodes accessing the metadata we collected in those two phases.
271
+
272
+
## Phase 3: Transform
273
+
274
+
After the analysis phase we can now move to the last compilation phase: the transform. However this phase is really two phases: svelte components generate completely different code based on the `compilerOption.generate` option...the function is small enough that we can just dump it here
warnings: state.warnings, // set afterwards. TODO apply preprocessor sourcemap
315
+
metadata: {
316
+
runes: analysis.runes
317
+
},
318
+
ast: /**@type{any}*/null// set afterwards
319
+
};
320
+
}
321
+
```
322
+
323
+
As you can see the key part of this function is this bit here
324
+
325
+
```ts
326
+
const program =
327
+
options.generate==='server'
328
+
?server_component(analysis, options)
329
+
:client_component(analysis, options);
330
+
```
331
+
332
+
Here we just delegate to it's own method to generate either a client component or a server component. Before we jump into exploring those two let's take a look at the output of the compilation of a very simple counter component.
333
+
334
+
```svelte
335
+
<script>
336
+
let count = $state(0);
337
+
</script>
338
+
339
+
<button onclick={() => (count += 1)}>
340
+
clicks: {count}
341
+
</button>
342
+
```
343
+
344
+
here's the compiled output for the client
345
+
346
+
```ts
347
+
import'svelte/internal/disclose-version';
348
+
import'svelte/internal/flags/async';
349
+
import*as$from'svelte/internal/client';
350
+
351
+
var on_click = (_, count) =>$.set(count, $.get(count) +1);
As you can see the server mostly concern itself with generating the html for the component, runes are basically removed, event listeners just vanish. It makes sense because on the server we don't add any listener, we just produce the html that will be hydrated by the client component.
0 commit comments