toiljs 0.0.136 → 0.0.137
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -0
- package/build/compiler/compiler-build.d.ts +9 -0
- package/build/compiler/compiler-build.js +177 -0
- package/build/compiler/compiler-cache.d.ts +28 -0
- package/build/compiler/compiler-cache.js +114 -0
- package/build/compiler/compiler-lock.d.ts +1 -0
- package/build/compiler/compiler-lock.js +99 -0
- package/build/compiler/emails.js +2 -3
- package/build/compiler/index.js +70 -40
- package/build/compiler/prerender.d.ts +1 -0
- package/build/compiler/prerender.js +55 -0
- package/build/compiler/ssg.js +5 -2
- package/build/compiler/template-build.js +10 -10
- package/build/compiler/toil-docs.generated.js +1 -1
- package/build/compiler/toilscript-worker.d.ts +1 -0
- package/build/compiler/toilscript-worker.js +78 -0
- package/package.json +1 -1
package/build/compiler/index.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { readDeriveCatalog, deriveIdentity } from 'toiljs/io';
|
|
2
|
-
import { spawn } from 'node:child_process';
|
|
3
2
|
import fs from 'node:fs';
|
|
4
3
|
import { createRequire } from 'node:module';
|
|
5
4
|
import net from 'node:net';
|
|
@@ -15,6 +14,8 @@ import { prerenderStaticParams } from './ssg.js';
|
|
|
15
14
|
import { extractDevSsrTemplates, extractServerSlots, extractTemplates, } from './template-build.js';
|
|
16
15
|
import { createViteConfig } from './vite.js';
|
|
17
16
|
import { logSecurityDeployment } from './security-deployment.js';
|
|
17
|
+
import { runCompilerPasses } from './compiler-build.js';
|
|
18
|
+
import { withCompilerLock } from './compiler-lock.js';
|
|
18
19
|
export const SURFACE_DECORATOR = /^[ \t]*@(data|rest|service|remote|stream|daemon|scheduled)\b/m;
|
|
19
20
|
function toilconfigEntries(root) {
|
|
20
21
|
try {
|
|
@@ -136,6 +137,11 @@ function serverAuthEnabled(root, configAuth) {
|
|
|
136
137
|
return serverImportsAuth(root, serverEntryFiles(root));
|
|
137
138
|
}
|
|
138
139
|
export async function buildServer(root, auth = false, authRoutes = {}, logging = false) {
|
|
140
|
+
if (!fs.existsSync(path.join(root, 'toilconfig.json')))
|
|
141
|
+
return;
|
|
142
|
+
await withCompilerLock(root, () => buildServerLocked(path.resolve(root), auth, authRoutes, logging));
|
|
143
|
+
}
|
|
144
|
+
async function buildServerLocked(root, auth, authRoutes, logging) {
|
|
139
145
|
if (!fs.existsSync(path.join(root, 'toilconfig.json')))
|
|
140
146
|
return;
|
|
141
147
|
for (const dir of serverDirs(root)) {
|
|
@@ -183,47 +189,58 @@ export async function buildServer(root, auth = false, authRoutes = {}, logging =
|
|
|
183
189
|
}
|
|
184
190
|
if (split.hasDaemon || split.hasStream) {
|
|
185
191
|
const artifacts = serverArtifacts(root);
|
|
192
|
+
const passes = [];
|
|
186
193
|
if (split.hasDaemon)
|
|
187
|
-
|
|
194
|
+
passes.push(toilscriptPass(root, split.cold, {
|
|
188
195
|
mode: 'cold',
|
|
189
196
|
outFile: artifacts.cold,
|
|
190
197
|
withRpc: false,
|
|
191
198
|
authUser: authOn,
|
|
192
199
|
logging,
|
|
193
|
-
});
|
|
200
|
+
}));
|
|
194
201
|
if (split.hasStream && split.stream.length > 0)
|
|
195
|
-
|
|
202
|
+
passes.push(toilscriptPass(root, split.stream, {
|
|
196
203
|
mode: 'hot',
|
|
197
204
|
outFile: artifacts.stream,
|
|
198
205
|
withRpc: false,
|
|
199
206
|
authUser: authOn,
|
|
200
207
|
logging,
|
|
201
|
-
});
|
|
208
|
+
}));
|
|
202
209
|
if (split.request.length > 0)
|
|
203
|
-
|
|
210
|
+
passes.push(toilscriptPass(root, split.request, {
|
|
204
211
|
mode: 'hot',
|
|
205
212
|
outFile: serverWasmFile(root),
|
|
206
213
|
withRpc: true,
|
|
207
214
|
rpcSurfaceFiles: split.hasStream ? split.stream : undefined,
|
|
208
215
|
authUser: authOn,
|
|
209
216
|
logging,
|
|
210
|
-
});
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
+
}));
|
|
218
|
+
await runCompilerPasses(root, binJs, passes, () => {
|
|
219
|
+
migrateRpcFile(root);
|
|
220
|
+
if (split.hasStream && split.stream.length > 0)
|
|
221
|
+
emitStreamClientSurface(root, artifacts.stream, split.stream);
|
|
222
|
+
}, {
|
|
223
|
+
validate: (readOutput) => {
|
|
224
|
+
if (split.hasDaemon)
|
|
225
|
+
validateDeriveContinuationArtifacts(null, readOutput(artifacts.cold));
|
|
226
|
+
if (split.request.length > 0)
|
|
227
|
+
validateDeriveContinuationArtifacts(readOutput(serverWasmFile(root)), split.hasDaemon ? readOutput(artifacts.cold) : null);
|
|
228
|
+
},
|
|
229
|
+
});
|
|
217
230
|
return;
|
|
218
231
|
}
|
|
219
|
-
|
|
232
|
+
const pass = toilscriptPass(root, files, {
|
|
220
233
|
mode: 'hot',
|
|
221
234
|
outFile: null,
|
|
222
235
|
withRpc: true,
|
|
223
236
|
authUser: authOn,
|
|
224
237
|
logging,
|
|
225
238
|
});
|
|
226
|
-
|
|
239
|
+
await runCompilerPasses(root, binJs, [pass], () => {
|
|
240
|
+
migrateRpcFile(root);
|
|
241
|
+
}, {
|
|
242
|
+
validate: (readOutput) => validateDeriveContinuationArtifacts(readOutput(serverWasmFile(root)), null),
|
|
243
|
+
});
|
|
227
244
|
}
|
|
228
245
|
export function validateDeriveContinuationArtifacts(hot, cold) {
|
|
229
246
|
const moduleOf = (bytes) => new WebAssembly.Module(Uint8Array.from(bytes));
|
|
@@ -392,8 +409,8 @@ export function assertNoStreamInRequestTier(root, split) {
|
|
|
392
409
|
'\nRemove the import, or move the code into a *.stream.ts module.');
|
|
393
410
|
}
|
|
394
411
|
}
|
|
395
|
-
|
|
396
|
-
const args = [
|
|
412
|
+
function toilscriptPass(root, files, opts) {
|
|
413
|
+
const args = [...files, '--target', 'release'];
|
|
397
414
|
const logLib = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../server/globals/toil-log.ts');
|
|
398
415
|
args.push('--lib', logLib, '--use', 'Log=ToilLog', '--use', `__TOIL_LOGGING=${opts.logging ? '1' : '0'}`);
|
|
399
416
|
const editorTier = opts.mode === 'cold' ? 'cold' : opts.withRpc ? 'request' : 'stream';
|
|
@@ -407,6 +424,16 @@ async function runToilscriptPass(root, binJs, files, opts) {
|
|
|
407
424
|
args.push('--outFile', opts.outFile);
|
|
408
425
|
if (opts.withRpc)
|
|
409
426
|
args.push('--rpcModule', 'shared/server.ts');
|
|
427
|
+
else {
|
|
428
|
+
args.push('--rpcModule', '');
|
|
429
|
+
const config = JSON.parse(fs.readFileSync(path.join(root, 'toilconfig.json'), 'utf8'));
|
|
430
|
+
const target = config.targets?.release;
|
|
431
|
+
const text = target?.textFile ?? config.options?.textFile;
|
|
432
|
+
if (text && opts.outFile)
|
|
433
|
+
args.push('--textFile', opts.outFile.replace(/\.[^.]+$/, '') + path.extname(text));
|
|
434
|
+
if ((target?.sourceMap ?? config.options?.sourceMap) && opts.outFile)
|
|
435
|
+
args.push('--sourceMap', path.basename(opts.outFile) + '.map');
|
|
436
|
+
}
|
|
410
437
|
if (opts.rpcSurfaceFiles)
|
|
411
438
|
for (const surfaceFile of opts.rpcSurfaceFiles)
|
|
412
439
|
args.push('--rpcSurfaceFiles', surfaceFile);
|
|
@@ -416,22 +443,16 @@ async function runToilscriptPass(root, binJs, files, opts) {
|
|
|
416
443
|
args.push('--lib', AUTH_EMAILS_LIB_SUBDIR);
|
|
417
444
|
args.push('--noConfigEntries');
|
|
418
445
|
args.push('--disableWarning', '235');
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
const source = fs.readFileSync(generated, 'utf8');
|
|
430
|
-
const updated = migrateGeneratedRpcClient(source);
|
|
431
|
-
if (source !== updated)
|
|
432
|
-
fs.writeFileSync(generated, updated);
|
|
433
|
-
}
|
|
434
|
-
}
|
|
446
|
+
return { name: editorTier, args };
|
|
447
|
+
}
|
|
448
|
+
function migrateRpcFile(root) {
|
|
449
|
+
const generated = path.join(root, 'shared/server.ts');
|
|
450
|
+
if (!fs.existsSync(generated))
|
|
451
|
+
return;
|
|
452
|
+
const source = fs.readFileSync(generated, 'utf8');
|
|
453
|
+
const updated = migrateGeneratedRpcClient(source);
|
|
454
|
+
if (source !== updated)
|
|
455
|
+
fs.writeFileSync(generated, updated);
|
|
435
456
|
}
|
|
436
457
|
export function migrateGeneratedRpcClient(source) {
|
|
437
458
|
return source.replace(/(async function __toilRpcCall\(__id: number, __body: )Uint8Array(?=\))/, '$1Uint8Array<ArrayBuffer>');
|
|
@@ -452,9 +473,11 @@ function watchServer(cfg, watcher) {
|
|
|
452
473
|
building = true;
|
|
453
474
|
process.stdout.write(pc.dim(' server changed, rebuilding…') + '\n');
|
|
454
475
|
const authOn = serverAuthEnabled(root, cfg.auth);
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
476
|
+
withCompilerLock(root, async () => {
|
|
477
|
+
await renderEmails(cfg, authOn);
|
|
478
|
+
await renderAuthEmails(cfg, authOn);
|
|
479
|
+
await buildServerLocked(root, cfg.auth, cfg.authRoutes, cfg.logging ?? true);
|
|
480
|
+
})
|
|
458
481
|
.then(() => process.stdout.write(pc.green(' ✓ ') + pc.dim('server rebuilt') + '\n'))
|
|
459
482
|
.catch((e) => process.stdout.write(pc.red(` ✗ server rebuild failed: ${String(e)}`) + '\n'))
|
|
460
483
|
.finally(() => {
|
|
@@ -643,6 +666,9 @@ function isExposedHost(host) {
|
|
|
643
666
|
return host !== '127.0.0.1' && host !== 'localhost' && host !== '::1';
|
|
644
667
|
}
|
|
645
668
|
export async function dev(opts = {}) {
|
|
669
|
+
return withCompilerLock(path.resolve(opts.root ?? process.cwd()), () => devLocked(opts));
|
|
670
|
+
}
|
|
671
|
+
async function devLocked(opts) {
|
|
646
672
|
const cfg = await loadConfig(opts);
|
|
647
673
|
const hasServer = fs.existsSync(path.join(cfg.root, 'toilconfig.json'));
|
|
648
674
|
if (hasServer)
|
|
@@ -653,7 +679,7 @@ export async function dev(opts = {}) {
|
|
|
653
679
|
generate(cfg);
|
|
654
680
|
if (hasServer)
|
|
655
681
|
await extractServerSlots(cfg);
|
|
656
|
-
await
|
|
682
|
+
await buildServerLocked(cfg.root, cfg.auth, cfg.authRoutes, cfg.logging ?? true);
|
|
657
683
|
if (hasServer)
|
|
658
684
|
process.stdout.write(pc.green(' ✓ ') + pc.dim('server built') + '\n');
|
|
659
685
|
if (!hasServer) {
|
|
@@ -730,6 +756,9 @@ export async function dev(opts = {}) {
|
|
|
730
756
|
return server;
|
|
731
757
|
}
|
|
732
758
|
export async function build(opts = {}) {
|
|
759
|
+
await withCompilerLock(path.resolve(opts.root ?? process.cwd()), () => buildLocked(opts));
|
|
760
|
+
}
|
|
761
|
+
async function buildLocked(opts) {
|
|
733
762
|
const cfg = await loadConfig(opts);
|
|
734
763
|
const hasServer = fs.existsSync(path.join(cfg.root, 'toilconfig.json'));
|
|
735
764
|
if (hasServer && !opts.serverOnly)
|
|
@@ -739,7 +768,7 @@ export async function build(opts = {}) {
|
|
|
739
768
|
await renderAuthEmails(cfg, authOnBuild);
|
|
740
769
|
generate(cfg);
|
|
741
770
|
const priorServerSlots = hasServer ? await extractServerSlots(cfg) : new Map();
|
|
742
|
-
await
|
|
771
|
+
await buildServerLocked(cfg.root, cfg.auth, cfg.authRoutes, cfg.logging ?? false);
|
|
743
772
|
if (opts.serverOnly)
|
|
744
773
|
return;
|
|
745
774
|
if (hasServer)
|
|
@@ -759,7 +788,7 @@ export async function build(opts = {}) {
|
|
|
759
788
|
const ssr = await extractTemplates(cfg, 'edge', priorServerSlots);
|
|
760
789
|
if (ssr.serverSlotsChanged) {
|
|
761
790
|
process.stdout.write(pc.dim(' SSR template changed; recompiling the server with the new hash…') + '\n');
|
|
762
|
-
await
|
|
791
|
+
await buildServerLocked(cfg.root, cfg.auth, cfg.authRoutes, cfg.logging ?? false);
|
|
763
792
|
}
|
|
764
793
|
logSecurityDeployment(cfg);
|
|
765
794
|
}
|
|
@@ -985,5 +1014,6 @@ function emitStreamClientSurface(root, streamWasmPath, streamFiles) {
|
|
|
985
1014
|
attach +
|
|
986
1015
|
(typeBlock.length > 0 ? '\n' + typeBlock : '');
|
|
987
1016
|
fs.mkdirSync(path.dirname(rpcModule), { recursive: true });
|
|
988
|
-
fs.
|
|
1017
|
+
if (!fs.existsSync(rpcModule) || fs.readFileSync(rpcModule, 'utf8') !== out)
|
|
1018
|
+
fs.writeFileSync(rpcModule, out);
|
|
989
1019
|
}
|
|
@@ -3,4 +3,5 @@ import { type ResolvedToilConfig } from './config.js';
|
|
|
3
3
|
export declare function extractStaticExports(filePath: string, names: readonly string[]): Record<string, Record<string, unknown>>;
|
|
4
4
|
export declare function extractStaticMetadata(filePath: string): Record<string, unknown> | null;
|
|
5
5
|
export declare function exportsTrue(filePath: string, name: string): boolean;
|
|
6
|
+
export declare function mayExport(filePath: string, name: string): boolean;
|
|
6
7
|
export declare function prerenderPlugin(cfg: ResolvedToilConfig): Plugin;
|
|
@@ -4,6 +4,16 @@ import { parseSync } from 'oxc-parser';
|
|
|
4
4
|
import { patternToBracketFile, scanRoutes, staticSectionPattern } from './routes.js';
|
|
5
5
|
import { injectSeoHtml, routeSeo } from './seo.js';
|
|
6
6
|
const UNRESOLVED = Symbol('unresolved');
|
|
7
|
+
function hasCommonJsReference(node) {
|
|
8
|
+
if (Array.isArray(node))
|
|
9
|
+
return node.some(hasCommonJsReference);
|
|
10
|
+
if (node === null || typeof node !== 'object')
|
|
11
|
+
return false;
|
|
12
|
+
const entry = node;
|
|
13
|
+
if (entry.type === 'Identifier' && (entry.name === 'module' || entry.name === 'exports'))
|
|
14
|
+
return true;
|
|
15
|
+
return Object.values(entry).some(hasCommonJsReference);
|
|
16
|
+
}
|
|
7
17
|
function evalNode(node) {
|
|
8
18
|
switch (node.type) {
|
|
9
19
|
case 'Literal':
|
|
@@ -112,6 +122,51 @@ export function extractStaticMetadata(filePath) {
|
|
|
112
122
|
export function exportsTrue(filePath, name) {
|
|
113
123
|
return staticExports(filePath, [name])[name] === true;
|
|
114
124
|
}
|
|
125
|
+
export function mayExport(filePath, name) {
|
|
126
|
+
let source;
|
|
127
|
+
try {
|
|
128
|
+
source = fs.readFileSync(filePath, 'utf8');
|
|
129
|
+
}
|
|
130
|
+
catch {
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
const parsed = parseSync(filePath, source);
|
|
134
|
+
if (parsed.errors.length)
|
|
135
|
+
return true;
|
|
136
|
+
for (const statement of parsed.program.body) {
|
|
137
|
+
if (statement.type === 'TSExportAssignment')
|
|
138
|
+
return true;
|
|
139
|
+
if (statement.type === 'ExportAllDeclaration') {
|
|
140
|
+
if (!statement.exported)
|
|
141
|
+
return true;
|
|
142
|
+
const exported = statement.exported;
|
|
143
|
+
if (('name' in exported ? exported.name : exported.value) === name)
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
146
|
+
if (statement.type !== 'ExportNamedDeclaration')
|
|
147
|
+
continue;
|
|
148
|
+
for (const specifier of statement.specifiers) {
|
|
149
|
+
const exported = specifier.exported;
|
|
150
|
+
if (('name' in exported ? exported.name : exported.value) === name)
|
|
151
|
+
return true;
|
|
152
|
+
}
|
|
153
|
+
const declaration = statement.declaration;
|
|
154
|
+
if (declaration?.type === 'VariableDeclaration') {
|
|
155
|
+
for (const entry of declaration.declarations) {
|
|
156
|
+
if (entry.id.type !== 'Identifier' || entry.id.name === name)
|
|
157
|
+
return true;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
else if (declaration &&
|
|
161
|
+
'id' in declaration &&
|
|
162
|
+
declaration.id &&
|
|
163
|
+
'name' in declaration.id &&
|
|
164
|
+
declaration.id.name === name) {
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return /\b(?:module|exports)\b/.test(source) && hasCommonJsReference(parsed.program);
|
|
169
|
+
}
|
|
115
170
|
export function prerenderPlugin(cfg) {
|
|
116
171
|
return {
|
|
117
172
|
name: 'toil:prerender-seo',
|
package/build/compiler/ssg.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
import { createServer } from 'vite';
|
|
4
|
-
import { extractStaticMetadata } from './prerender.js';
|
|
4
|
+
import { extractStaticMetadata, mayExport } from './prerender.js';
|
|
5
5
|
import { scanRoutes } from './routes.js';
|
|
6
6
|
import { injectSeoHtml, joinUrl, llmsTxt, routeSeo, sitemapXml } from './seo.js';
|
|
7
7
|
import { createViteConfig } from './vite.js';
|
|
@@ -29,7 +29,10 @@ export async function prerenderStaticParams(cfg) {
|
|
|
29
29
|
if (!fs.existsSync(shellPath))
|
|
30
30
|
return [];
|
|
31
31
|
const allRoutes = scanRoutes(cfg.routesAbsDir);
|
|
32
|
-
const dynamic = allRoutes.filter((r) => r.slot === undefined &&
|
|
32
|
+
const dynamic = allRoutes.filter((r) => r.slot === undefined &&
|
|
33
|
+
!r.intercept &&
|
|
34
|
+
/[:*]/.test(r.pattern) &&
|
|
35
|
+
mayExport(r.file, 'generateStaticParams'));
|
|
33
36
|
if (dynamic.length === 0)
|
|
34
37
|
return [];
|
|
35
38
|
const shell = fs.readFileSync(shellPath, 'utf8');
|
|
@@ -9,7 +9,7 @@ import { scanRoutes } from './routes.js';
|
|
|
9
9
|
import { generateSlotsModule } from './ssr-codegen.js';
|
|
10
10
|
import { assignSlotIds, coherenceHash, encodeSlots, extractFromHtml, } from './template.js';
|
|
11
11
|
import { createViteConfig } from './vite.js';
|
|
12
|
-
import { extractStaticMetadata } from './prerender.js';
|
|
12
|
+
import { extractStaticMetadata, mayExport } from './prerender.js';
|
|
13
13
|
import { escapeAttr, escapeHtml, injectSeoHtml, routeSeo } from './seo.js';
|
|
14
14
|
const SSR_MARKER = '<template id="__toil_ssr"></template>';
|
|
15
15
|
const ROOT_DIV = '<div id="root"></div>';
|
|
@@ -148,7 +148,7 @@ export function writeServerSlotsModules(root, modules) {
|
|
|
148
148
|
return written;
|
|
149
149
|
}
|
|
150
150
|
async function renderSsrRoutes(cfg, shell) {
|
|
151
|
-
const routes = scanRoutes(cfg.routesAbsDir).filter((r) => r.slot === undefined && !r.intercept);
|
|
151
|
+
const routes = scanRoutes(cfg.routesAbsDir).filter((r) => r.slot === undefined && !r.intercept && mayExport(r.file, 'ssr'));
|
|
152
152
|
if (routes.length === 0)
|
|
153
153
|
return [];
|
|
154
154
|
const warn = (msg) => {
|
|
@@ -160,16 +160,16 @@ async function renderSsrRoutes(cfg, shell) {
|
|
|
160
160
|
appType: 'custom',
|
|
161
161
|
logLevel: 'silent',
|
|
162
162
|
});
|
|
163
|
-
const client = (await server.ssrLoadModule('toiljs/client'));
|
|
164
|
-
const globalsModule = path.join(cfg.toilDir, 'globals.ts');
|
|
165
|
-
if (fs.existsSync(globalsModule)) {
|
|
166
|
-
await server.ssrLoadModule(globalsModule);
|
|
167
|
-
}
|
|
168
|
-
const appRequire = createRequire(path.join(cfg.root, 'package.json'));
|
|
169
|
-
const react = appRequire('react');
|
|
170
|
-
const reactDomServer = appRequire('react-dom/server');
|
|
171
163
|
const rendered = [];
|
|
172
164
|
try {
|
|
165
|
+
const client = (await server.ssrLoadModule('toiljs/client'));
|
|
166
|
+
const globalsModule = path.join(cfg.toilDir, 'globals.ts');
|
|
167
|
+
if (fs.existsSync(globalsModule)) {
|
|
168
|
+
await server.ssrLoadModule(globalsModule);
|
|
169
|
+
}
|
|
170
|
+
const appRequire = createRequire(path.join(cfg.root, 'package.json'));
|
|
171
|
+
const react = appRequire('react');
|
|
172
|
+
const reactDomServer = appRequire('react-dom/server');
|
|
173
173
|
for (const r of routes) {
|
|
174
174
|
let mod;
|
|
175
175
|
try {
|