WEBVTT

00:00:00.000 --> 00:00:01.320
One of the most important data structures

00:00:01.340 --> 00:00:02.540
in the Elements build server

00:00:02.560 --> 00:00:03.590
is the source graph.

00:00:03.680 --> 00:00:05.130
I designed it around two ideas.

00:00:05.600 --> 00:00:06.800
One, your editor, your terminal,

00:00:06.820 --> 00:00:07.780
your agents and every phase

00:00:07.800 --> 00:00:09.870
of the build read the same graph, in memory.

00:00:10.240 --> 00:00:11.790
Two, we never redo work.

00:00:11.960 --> 00:00:13.640
If it's impossible that a file changed,

00:00:13.660 --> 00:00:14.920
we don't read it, parse it,

00:00:14.940 --> 00:00:16.450
resolve it or type check it.

00:00:16.620 --> 00:00:17.890
So a build takes milliseconds.

00:00:18.060 --> 00:00:19.300
And we can do program analysis

00:00:19.320 --> 00:00:21.140
and emit transforms that aren't possible

00:00:21.160 --> 00:00:22.990
when every tool has its own graph.

00:00:23.160 --> 00:00:24.430
It starts with an import.

00:00:24.600 --> 00:00:25.820
This file imports LiveTable

00:00:25.840 --> 00:00:27.230
from @elements/app.

00:00:27.360 --> 00:00:28.270
That's a dependency,

00:00:28.420 --> 00:00:29.830
and in the graph it's an edge.

00:00:30.240 --> 00:00:32.180
But an edge needs a file at the other end,

00:00:32.200 --> 00:00:33.870
so we'll have to resolve that path.

00:00:34.060 --> 00:00:35.460
We walk up to node modules.

00:00:35.480 --> 00:00:37.310
We find @elements, and then app.

00:00:37.360 --> 00:00:39.291
We read the exports in its package.json,

00:00:39.311 --> 00:00:41.070
and we land on index.js.

00:00:41.660 --> 00:00:42.800
Normally every one of those steps

00:00:42.820 --> 00:00:43.970
is a system call,

00:00:44.000 --> 00:00:45.730
for every import in your program.

00:00:46.060 --> 00:00:47.080
We don't do that.

00:00:47.100 --> 00:00:48.700
The build server scans the disk once

00:00:48.720 --> 00:00:49.280
when it starts,

00:00:49.300 --> 00:00:51.090
and keeps the file tree in memory.

00:00:51.340 --> 00:00:51.900
The file watcher

00:00:51.920 --> 00:00:53.240
updates it when a file changes.

00:00:53.260 --> 00:00:55.390
So the resolver makes no system calls.

00:00:55.540 --> 00:00:57.140
But that one path doesn't resolve

00:00:57.160 --> 00:00:58.190
to one file.

00:00:58.380 --> 00:01:00.790
For the server, it's server index.js.

00:01:00.980 --> 00:01:02.140
For the browser, the package

00:01:02.160 --> 00:01:04.850
redirects it to browser index.js.

00:01:05.120 --> 00:01:06.360
Those are runtime files,

00:01:06.380 --> 00:01:07.630
the JavaScript that runs.

00:01:08.100 --> 00:01:09.790
The type checker wants different files,

00:01:09.860 --> 00:01:11.080
the .d.ts files.

00:01:11.100 --> 00:01:12.320
And those are different for the server

00:01:12.340 --> 00:01:13.450
and the browser too.

00:01:13.860 --> 00:01:15.860
So one import path gives us four files,

00:01:15.880 --> 00:01:17.350
and that gives us four graphs.

00:01:18.100 --> 00:01:19.540
Server runtime, server types,

00:01:19.560 --> 00:01:21.150
browser runtime, browser types.

00:01:21.480 --> 00:01:23.080
In Elements, a source file is read once

00:01:23.100 --> 00:01:25.350
and parsed once, and it has four vertices,

00:01:25.370 --> 00:01:26.320
one in each graph.

00:01:26.650 --> 00:01:28.240
And we need those graphs together.

00:01:28.630 --> 00:01:30.390
When we emit, we use the types graph

00:01:30.410 --> 00:01:31.840
to transform the code.

00:01:32.010 --> 00:01:33.910
When we analyze, we add runtime libraries

00:01:33.930 --> 00:01:35.100
to the runtime graph.

00:01:35.330 --> 00:01:36.790
You can't do that when the type checker

00:01:36.810 --> 00:01:38.050
and the bundler are separate tools,

00:01:38.070 --> 00:01:39.390
because neither one can read

00:01:39.410 --> 00:01:40.340
the other's graph.

00:01:40.930 --> 00:01:42.280
A build runs in phases.

00:01:42.530 --> 00:01:44.640
We sync the file tree, then config,

00:01:44.690 --> 00:01:46.900
install, crawl, analyze and emit.

00:01:47.250 --> 00:01:48.590
Then we migrate the test database

00:01:48.610 --> 00:01:49.680
and run the tests.

00:01:49.990 --> 00:01:51.750
Only if the tests pass do we migrate

00:01:51.770 --> 00:01:53.340
the app database and release.

00:01:53.750 --> 00:01:55.380
We build the graphs in the crawl.

00:01:55.750 --> 00:01:57.090
We start with your project files.

00:01:57.110 --> 00:01:58.550
We read each one, parse it,

00:01:58.570 --> 00:01:59.550
resolve its imports,

00:01:59.570 --> 00:02:01.600
and follow those edges into the packages.

00:02:02.070 --> 00:02:03.490
So a package file is only read

00:02:03.510 --> 00:02:04.980
if your program can reach it.

00:02:05.270 --> 00:02:06.390
The elements.dev site

00:02:06.410 --> 00:02:08.790
has seven and a half thousand files

00:02:08.810 --> 00:02:09.860
in node modules.

00:02:10.170 --> 00:02:12.360
The build reads about two thousand of them.

00:02:12.590 --> 00:02:13.800
Emit works the same way.

00:02:13.910 --> 00:02:15.430
For the server we write your project files

00:02:15.450 --> 00:02:17.180
and the package files they can reach.

00:02:17.310 --> 00:02:18.890
For the browser, we only write a file

00:02:18.910 --> 00:02:20.720
if a page can actually reach it.

00:02:20.880 --> 00:02:22.660
Analyze runs on the types graphs.

00:02:23.050 --> 00:02:24.030
We type check the server

00:02:24.050 --> 00:02:25.620
and the browser separately,

00:02:25.770 --> 00:02:27.240
each one through its own graph.

00:02:27.390 --> 00:02:28.630
Then program analysis follows

00:02:28.650 --> 00:02:30.740
your function calls from file to file.

00:02:30.810 --> 00:02:32.130
So it can add async and await

00:02:32.150 --> 00:02:33.310
automatically for you.

00:02:33.330 --> 00:02:34.530
It knows when code in the browser

00:02:34.550 --> 00:02:35.900
can reach a SQL call.

00:02:36.270 --> 00:02:38.000
Here's a function that calls SQL.

00:02:38.170 --> 00:02:39.690
I call it from a button in this template,

00:02:39.710 --> 00:02:40.440
and save.

00:02:40.630 --> 00:02:42.300
And the build has a security error.

00:02:42.470 --> 00:02:44.370
You can't call server code from the browser

00:02:44.390 --> 00:02:46.220
without going through an RPC function.

00:02:46.740 --> 00:02:48.470
The type checker alone reports nothing

00:02:48.490 --> 00:02:49.470
for this code.

00:02:49.820 --> 00:02:51.230
So I add the RPC build tag,

00:02:51.250 --> 00:02:52.360
and the build passes.

00:02:52.550 --> 00:02:54.250
And in this file we wrote for the browser,

00:02:54.270 --> 00:02:55.440
the SQL is gone.

00:02:55.570 --> 00:02:58.040
The function body is a call to the server.

00:02:58.190 --> 00:02:59.380
That was the first build.

00:02:59.770 --> 00:03:01.440
Here's the part I care about most.

00:03:01.750 --> 00:03:03.640
After that, we never redo work.

00:03:03.890 --> 00:03:05.280
The graph stays in memory.

00:03:05.390 --> 00:03:06.310
When you save a file,

00:03:06.330 --> 00:03:07.870
the file watcher tells us which file,

00:03:07.890 --> 00:03:09.240
so we don't scan the disk.

00:03:09.490 --> 00:03:11.330
If the modified time is the same,

00:03:11.350 --> 00:03:12.120
we don't read it.

00:03:12.190 --> 00:03:13.430
If the text hash is the same,

00:03:13.450 --> 00:03:14.350
we don't parse it.

00:03:14.370 --> 00:03:15.590
If the imports are the same,

00:03:15.610 --> 00:03:16.860
we don't resolve anything.

00:03:17.210 --> 00:03:17.910
Watch.

00:03:18.140 --> 00:03:19.670
I change the text inside this query,

00:03:19.690 --> 00:03:20.420
and save.

00:03:20.710 --> 00:03:22.530
One file was type checked for each target,

00:03:22.550 --> 00:03:23.920
out of about two hundred.

00:03:24.030 --> 00:03:24.920
Three milliseconds.

00:03:25.330 --> 00:03:27.010
The files that import it weren't checked,

00:03:27.030 --> 00:03:29.200
because its exported types didn't change.

00:03:29.370 --> 00:03:31.070
Now I rename a field in the interface,

00:03:31.090 --> 00:03:33.130
and that does change its exported types.

00:03:33.150 --> 00:03:35.020
So the server rechecks three files,

00:03:35.110 --> 00:03:36.550
the browser rechecks two,

00:03:36.570 --> 00:03:37.980
and the template has the error.

00:03:38.030 --> 00:03:38.920
Seven milliseconds.

00:03:39.470 --> 00:03:40.940
Every phase works that way.

00:03:41.050 --> 00:03:41.870
We only emit a file

00:03:41.890 --> 00:03:43.500
if its output might have changed.

00:03:43.630 --> 00:03:44.570
We only run the tests

00:03:44.590 --> 00:03:46.120
that depend on the change.

00:03:46.310 --> 00:03:47.590
And a deploy only uploads

00:03:47.610 --> 00:03:49.140
the files whose hash changed.

00:03:49.370 --> 00:03:50.870
And a build where nothing changed

00:03:50.890 --> 00:03:52.500
takes less than a millisecond.

00:03:52.790 --> 00:03:53.550
Your editor connects

00:03:53.570 --> 00:03:54.880
to the same build server.

00:03:55.050 --> 00:03:56.270
But the text in your editor

00:03:56.290 --> 00:03:58.220
isn't on disk until you save.

00:03:58.560 --> 00:03:59.840
So the graph has layers.

00:04:00.190 --> 00:04:01.700
Layer zero is the disk.

00:04:01.790 --> 00:04:03.350
Your editor gets its own layer,

00:04:03.370 --> 00:04:04.860
and it's copy on open.

00:04:05.090 --> 00:04:06.670
A file you have open gets a copy

00:04:06.690 --> 00:04:08.320
with your unsaved text.

00:04:08.650 --> 00:04:10.720
Every other file comes from the disk layer.

00:04:11.420 --> 00:04:12.350
So the error in your editor

00:04:12.370 --> 00:04:13.490
is the build's error,

00:04:13.510 --> 00:04:15.660
for both targets, before you save.

00:04:16.250 --> 00:04:18.520
Now think about what you run without this.

00:04:18.810 --> 00:04:19.920
A package installer.

00:04:19.950 --> 00:04:21.290
A type checker for the server,

00:04:21.310 --> 00:04:22.700
and another one for the browser.

00:04:22.790 --> 00:04:24.090
A bundler. A test runner.

00:04:24.110 --> 00:04:25.860
And your editor's TypeScript server.

00:04:26.430 --> 00:04:28.200
Each one reads the same files,

00:04:28.230 --> 00:04:29.230
parses them again,

00:04:29.250 --> 00:04:31.000
and keeps its own graph in memory.

00:04:31.550 --> 00:04:33.050
By Microsoft's own numbers,

00:04:33.070 --> 00:04:34.890
one type check of the VS Code codebase

00:04:34.910 --> 00:04:36.280
takes over four gigabytes.

00:04:37.200 --> 00:04:38.710
The bundler doesn't type check.

00:04:38.920 --> 00:04:40.870
This developer finally tracked a bug down

00:04:40.890 --> 00:04:42.600
to a typo in an import.

00:04:43.030 --> 00:04:44.330
The type checker would have caught it,

00:04:44.350 --> 00:04:45.190
but his bundler

00:04:45.210 --> 00:04:46.680
doesn't run the type checker.

00:04:46.870 --> 00:04:48.500
And the graphs go stale separately.

00:04:49.090 --> 00:04:51.800
This one names two stale caches in one line.

00:04:52.050 --> 00:04:53.150
Deleting the bundler's cache

00:04:53.170 --> 00:04:55.280
is the new restart the TS server.

00:04:56.050 --> 00:04:58.050
Your agent runs these tools one at a time,

00:04:58.070 --> 00:04:58.860
and waits.

00:04:59.390 --> 00:05:00.670
This is from Claude Code's

00:05:00.690 --> 00:05:01.860
own issue tracker.

00:05:01.990 --> 00:05:02.970
To see type errors,

00:05:02.990 --> 00:05:04.480
it runs the full compiler,

00:05:04.610 --> 00:05:06.830
ten to sixty seconds each time.

00:05:07.450 --> 00:05:09.570
And then CI runs all of them again

00:05:09.590 --> 00:05:11.720
on every push, starting from nothing.

00:05:12.250 --> 00:05:13.050
One team wrote

00:05:13.070 --> 00:05:16.080
that their CI took seventeen minutes a run,

00:05:16.510 --> 00:05:18.560
fifty to a hundred times a day.

00:05:18.790 --> 00:05:20.370
In Elements those tools are phases

00:05:20.390 --> 00:05:22.310
of one build, in one build server,

00:05:22.330 --> 00:05:23.920
on one source graph.

00:05:24.170 --> 00:05:25.280
That was my decision.

00:05:25.470 --> 00:05:26.990
It meant writing our own resolver

00:05:27.010 --> 00:05:28.330
and our own file watcher,

00:05:28.350 --> 00:05:29.370
and making Microsoft's

00:05:29.390 --> 00:05:30.960
type checker read our graph.

00:05:31.250 --> 00:05:33.170
I did it so every file is read once,

00:05:33.190 --> 00:05:34.680
no work is done twice,

00:05:34.780 --> 00:05:36.710
and your editor, your terminal, your agents,

00:05:36.730 --> 00:05:38.500
they all get the same build result.

00:05:39.050 --> 00:05:39.790
And when an agent asks

00:05:39.810 --> 00:05:41.170
if the build has errors,

00:05:41.190 --> 00:05:41.850
it's one command,

00:05:41.870 --> 00:05:44.140
and the answer comes back in microseconds.
