@backtickjs/bundler 0.3.2 → 0.5.0
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/dist/bundle/buildBundle.d.ts +1 -1
- package/dist/bundle/buildBundle.js +20 -20
- package/dist/bundle/buildBundle.js.map +1 -1
- package/dist/bundle/expandServerComponents.d.ts +2 -2
- package/dist/bundle/expandServerComponents.js +22 -23
- package/dist/bundle/expandServerComponents.js.map +1 -1
- package/dist/bundle/params.d.ts +0 -1
- package/dist/bundle/params.js +0 -9
- package/dist/bundle/params.js.map +1 -1
- package/dist/print/printBundle.js +18 -3
- package/dist/print/printBundle.js.map +1 -1
- package/package.json +2 -2
- package/src/bundle/buildBundle.ts +26 -27
- package/src/bundle/expandServerComponents.ts +32 -26
- package/src/bundle/params.ts +0 -12
- package/src/print/printBundle.ts +21 -3
|
@@ -8,7 +8,7 @@ import type { Modules } from "../modules.js";
|
|
|
8
8
|
*/
|
|
9
9
|
export interface BundleTree {
|
|
10
10
|
readonly modules: readonly (readonly [string, ScriptModule])[];
|
|
11
|
-
readonly constants: readonly (readonly [string, string])[];
|
|
11
|
+
readonly constants: readonly (readonly [string, string, string?])[];
|
|
12
12
|
readonly root: string;
|
|
13
13
|
readonly names: Names;
|
|
14
14
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { isClientExec, isClientImport, isClientScript, isJsxElement, } from "@backtickjs/core";
|
|
2
2
|
import { BundlerError } from "../errors.js";
|
|
3
3
|
import { expandServerComponents } from "./expandServerComponents.js";
|
|
4
|
-
import { bindingsOf, capturesOf
|
|
4
|
+
import { bindingsOf, capturesOf } from "./params.js";
|
|
5
5
|
import { sourceName } from "./bindingKey.js";
|
|
6
6
|
import { array, arrow, call, createNames, imported, literal, object, } from "../print/code.js";
|
|
7
7
|
// Names a binding may not take: what strict code cannot bind. Every name the
|
|
@@ -118,24 +118,22 @@ export async function buildBundle(spliceable, provided) {
|
|
|
118
118
|
}
|
|
119
119
|
return label;
|
|
120
120
|
};
|
|
121
|
-
// A
|
|
122
|
-
//
|
|
123
|
-
//
|
|
124
|
-
//
|
|
125
|
-
// two
|
|
121
|
+
// A script is one value, as a module-level `const` is: its code run at the
|
|
122
|
+
// top of the bundle, where nothing encloses it, and every read is that
|
|
123
|
+
// constant. One per script, not per code: two runs of a host function that
|
|
124
|
+
// each make a function are two functions, and two that each make a context
|
|
125
|
+
// two contexts, as in plain TypeScript.
|
|
126
126
|
//
|
|
127
|
-
// A
|
|
128
|
-
//
|
|
129
|
-
//
|
|
127
|
+
// A script that reads a variable of the scope it's read in, itself or
|
|
128
|
+
// through what it's handed, is made there instead, as one declared in a
|
|
129
|
+
// function is.
|
|
130
130
|
const values = new Map();
|
|
131
131
|
const valueOf = (script, scope) => {
|
|
132
132
|
let label = values.get(script);
|
|
133
133
|
if (label !== undefined) {
|
|
134
134
|
return label;
|
|
135
135
|
}
|
|
136
|
-
const at =
|
|
137
|
-
? rootScope()
|
|
138
|
-
: { bindings: scope.bindings, read: new Set() };
|
|
136
|
+
const at = { bindings: scope.bindings, read: new Set() };
|
|
139
137
|
const code = call(labelOf(scriptFor(script)), exprCallArgs(script, at));
|
|
140
138
|
const outside = [...at.read].filter((key) => scope.bindings.has(key));
|
|
141
139
|
if (outside.length > 0) {
|
|
@@ -144,7 +142,14 @@ export async function buildBundle(spliceable, provided) {
|
|
|
144
142
|
}
|
|
145
143
|
label = `$value${constants.length}`;
|
|
146
144
|
values.set(script, label);
|
|
147
|
-
|
|
145
|
+
// One written at a host module's top level is the app's, under its id,
|
|
146
|
+
// shared by every bundle of it (`$value`); one written in a function is
|
|
147
|
+
// this bundle's.
|
|
148
|
+
constants.push([
|
|
149
|
+
label,
|
|
150
|
+
code,
|
|
151
|
+
script.module.scope === "module" ? script.module.id : undefined,
|
|
152
|
+
]);
|
|
148
153
|
return label;
|
|
149
154
|
};
|
|
150
155
|
// Writes a value as the code it becomes: composition as data, which is what
|
|
@@ -154,10 +159,7 @@ export async function buildBundle(spliceable, provided) {
|
|
|
154
159
|
// reached each script. Expansion has refused cycles, so it ends.
|
|
155
160
|
const render = (spliceable, scope = rootScope()) => {
|
|
156
161
|
if (isClientScript(spliceable)) {
|
|
157
|
-
|
|
158
|
-
return valueOf(spliceable, scope);
|
|
159
|
-
}
|
|
160
|
-
return call(labelOf(scriptFor(spliceable)), exprCallArgs(spliceable, scope));
|
|
162
|
+
return valueOf(spliceable, scope);
|
|
161
163
|
}
|
|
162
164
|
if (isJsxElement(spliceable)) {
|
|
163
165
|
return renderJsx(spliceable);
|
|
@@ -234,9 +236,7 @@ export async function buildBundle(spliceable, provided) {
|
|
|
234
236
|
sourceName(key) +
|
|
235
237
|
"` shadows it: the binding is still there, but no longer reachable by " +
|
|
236
238
|
"name, and naming it anyway would mean emitting what the source " +
|
|
237
|
-
"couldn't say.
|
|
238
|
-
"outside every script, so a fragment spliced into one can't reach " +
|
|
239
|
-
"its binding either.");
|
|
239
|
+
"couldn't say.");
|
|
240
240
|
};
|
|
241
241
|
// The arguments of a call of a script: a thunk per splice, then its
|
|
242
242
|
// captures.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"buildBundle.js","sourceRoot":"","sources":["../../src/bundle/buildBundle.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,YAAY,EACZ,cAAc,EACd,cAAc,EACd,YAAY,GAEb,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AACrE,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,
|
|
1
|
+
{"version":3,"file":"buildBundle.js","sourceRoot":"","sources":["../../src/bundle/buildBundle.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,YAAY,EACZ,cAAc,EACd,cAAc,EACd,YAAY,GAEb,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,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IAAI,YAAY,CACpB,GAAG,GAAG,4DAA4D;YAChE,8BAA8B,CACjC,CAAC;IACJ,CAAC,CAAC;IAEF,2EAA2E;IAC3E,wEAAwE;IACxE,MAAM,IAAI,GAAG,MAAM,CAAC,MAAM,sBAAsB,CAAC,UAAU,CAAC,CAAC,CAAC;IAC9D,0EAA0E;IAC1E,MAAM,OAAO,GAAG,CAAC,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC,GAAG,CACrC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,CAAU,CACtD,CAAC;IACF,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;AAC7C,CAAC"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { type ClientScript } from "@backtickjs/core";
|
|
1
|
+
import { type ClientScript, type JsxElement } from "@backtickjs/core";
|
|
2
2
|
interface Ancestors {
|
|
3
3
|
readonly value: object;
|
|
4
4
|
readonly parent: Ancestors | null;
|
|
5
5
|
}
|
|
6
|
-
export declare function expandServerComponents(spliceable: unknown, ancestors?: Ancestors | null,
|
|
6
|
+
export declare function expandServerComponents(spliceable: unknown, ancestors?: Ancestors | null, shared?: Map<JsxElement | ClientScript, Promise<unknown>>): Promise<unknown>;
|
|
7
7
|
export {};
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { isClientImport, isClientScript, isJsxElement, } from "@backtickjs/core";
|
|
2
2
|
import { BundlerError } from "../errors.js";
|
|
3
|
-
import { isValue } from "./params.js";
|
|
4
3
|
const CYCLE_ERROR = "Can't splice a value that contains itself: a bundle writes each value " +
|
|
5
4
|
"out in full, so a cycle never ends. Break the cycle before splicing it.";
|
|
6
5
|
const contains = (ancestors, value) => {
|
|
@@ -15,16 +14,16 @@ const contains = (ancestors, value) => {
|
|
|
15
14
|
// each element of one replaced by what its component drew, expanded in turn.
|
|
16
15
|
// Siblings expand at once, so two components awaiting data wait together.
|
|
17
16
|
//
|
|
18
|
-
// A component runs
|
|
19
|
-
//
|
|
20
|
-
//
|
|
21
|
-
// server component comes back as
|
|
17
|
+
// A component runs once per element, as React's server renderer runs one: one
|
|
18
|
+
// element drawn in two places runs once, and two elements run twice. The
|
|
19
|
+
// component itself never leaves the host, and only the client code it bottoms
|
|
20
|
+
// out in reaches the bundle. A value holding no server component comes back as
|
|
21
|
+
// the same object.
|
|
22
22
|
//
|
|
23
|
-
// A
|
|
24
|
-
//
|
|
25
|
-
//
|
|
26
|
-
|
|
27
|
-
export async function expandServerComponents(spliceable, ancestors = null, values = new Map()) {
|
|
23
|
+
// A script is one value, so what one splices is expanded once, however many
|
|
24
|
+
// places read it. `shared` holds each element's and each script's expansion
|
|
25
|
+
// for the call.
|
|
26
|
+
export async function expandServerComponents(spliceable, ancestors = null, shared = new Map()) {
|
|
28
27
|
if (typeof spliceable !== "object" || spliceable === null) {
|
|
29
28
|
return spliceable;
|
|
30
29
|
}
|
|
@@ -32,31 +31,31 @@ export async function expandServerComponents(spliceable, ancestors = null, value
|
|
|
32
31
|
throw new BundlerError(CYCLE_ERROR);
|
|
33
32
|
}
|
|
34
33
|
const inside = { value: spliceable, parent: ancestors };
|
|
35
|
-
const all = (entries) => Promise.all(entries.map((entry) => expandServerComponents(entry, inside,
|
|
34
|
+
const all = (entries) => Promise.all(entries.map((entry) => expandServerComponents(entry, inside, shared)));
|
|
35
|
+
// Each element and each script once, its expansion shared.
|
|
36
|
+
const once = (key, expand) => {
|
|
37
|
+
let made = shared.get(key);
|
|
38
|
+
if (made === undefined) {
|
|
39
|
+
made = expand();
|
|
40
|
+
shared.set(key, made);
|
|
41
|
+
}
|
|
42
|
+
return made;
|
|
43
|
+
};
|
|
36
44
|
if (isJsxElement(spliceable)) {
|
|
37
45
|
const { type } = spliceable;
|
|
38
46
|
// A tag the client draws is left for rendering to refuse.
|
|
39
47
|
if (typeof type !== "function" || isClientImport(type)) {
|
|
40
48
|
return spliceable;
|
|
41
49
|
}
|
|
42
|
-
return expandServerComponents(await type(spliceable.props), inside,
|
|
50
|
+
return once(spliceable, async () => expandServerComponents(await type(spliceable.props), inside, shared));
|
|
43
51
|
}
|
|
44
52
|
if (isClientScript(spliceable)) {
|
|
45
|
-
|
|
53
|
+
return once(spliceable, async () => {
|
|
46
54
|
const args = await all(spliceable.args);
|
|
47
55
|
return args.every((arg, index) => arg === spliceable.args[index])
|
|
48
56
|
? spliceable
|
|
49
57
|
: { ...spliceable, args };
|
|
50
|
-
};
|
|
51
|
-
if (!isValue(spliceable)) {
|
|
52
|
-
return expanded();
|
|
53
|
-
}
|
|
54
|
-
let made = values.get(spliceable);
|
|
55
|
-
if (made === undefined) {
|
|
56
|
-
made = expanded();
|
|
57
|
-
values.set(spliceable, made);
|
|
58
|
-
}
|
|
59
|
-
return made;
|
|
58
|
+
});
|
|
60
59
|
}
|
|
61
60
|
if (Array.isArray(spliceable)) {
|
|
62
61
|
const elements = await all(spliceable);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"expandServerComponents.js","sourceRoot":"","sources":["../../src/bundle/expandServerComponents.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,cAAc,EACd,cAAc,EACd,YAAY,
|
|
1
|
+
{"version":3,"file":"expandServerComponents.js","sourceRoot":"","sources":["../../src/bundle/expandServerComponents.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,cAAc,EACd,cAAc,EACd,YAAY,GAEb,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAE5C,MAAM,WAAW,GACf,wEAAwE;IACxE,yEAAyE,CAAC;AAS5E,MAAM,QAAQ,GAAG,CAAC,SAA2B,EAAE,KAAa,EAAW,EAAE;IACvE,KAAK,IAAI,EAAE,GAAG,SAAS,EAAE,EAAE,KAAK,IAAI,EAAE,EAAE,GAAG,EAAE,CAAC,MAAM,EAAE,CAAC;QACrD,IAAI,EAAE,CAAC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AU,CAAC,CAAC;IACpD,IAAI,SAAS,KAAK,MAAM,CAAC,SAAS,IAAI,SAAS,KAAK,IAAI,EAAE,CAAC;QACzD,OAAO,UAAU,CAAC;IACpB,CAAC;IACD,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;IAC3C,MAAM,QAAQ,GAAG,MAAM,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;IAC9D,IAAI,QAAQ,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK,KAAK,OAAO,CAAC,KAAK,CAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QACnE,OAAO,UAAU,CAAC;IACpB,CAAC;IACD,2DAA2D;IAC3D,OAAO,MAAM,CAAC,WAAW,CACvB,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC,GAAG,EAAE,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CACtD,CAAC;AACJ,CAAC"}
|
package/dist/bundle/params.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
1
|
import type { ClientScript, Param } from "@backtickjs/core";
|
|
2
2
|
export declare function bindingsOf(param: Param | undefined): readonly string[];
|
|
3
|
-
export declare function isValue(script: ClientScript): boolean;
|
|
4
3
|
export declare function capturesOf(script: ClientScript): readonly string[];
|
package/dist/bundle/params.js
CHANGED
|
@@ -3,15 +3,6 @@
|
|
|
3
3
|
export function bindingsOf(param) {
|
|
4
4
|
return param?.kind === "splice" ? param.bindings : [];
|
|
5
5
|
}
|
|
6
|
-
// Whether a script is one value for the bundle, made once at its top: a
|
|
7
|
-
// `once` script, a function and a class, as a module-level one is. A function
|
|
8
|
-
// or a class that reads a variable of the script around it, itself or through
|
|
9
|
-
// what it's handed, is made where it's read instead (see `valueOf`).
|
|
10
|
-
export function isValue(script) {
|
|
11
|
-
return (script.once === true ||
|
|
12
|
-
script.module.kind === "function" ||
|
|
13
|
-
script.module.kind === "class");
|
|
14
|
-
}
|
|
15
6
|
// The binding keys a script captures, in parameter order.
|
|
16
7
|
export function capturesOf(script) {
|
|
17
8
|
return script.module.params.flatMap((param) => param.kind === "capture" ? [param.key] : []);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"params.js","sourceRoot":"","sources":["../../src/bundle/params.ts"],"names":[],"mappings":"AAEA,4EAA4E;AAE5E,6DAA6D;AAC7D,MAAM,UAAU,UAAU,CAAC,KAAwB;IACjD,OAAO,KAAK,EAAE,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC;AACxD,CAAC;AAED,
|
|
1
|
+
{"version":3,"file":"params.js","sourceRoot":"","sources":["../../src/bundle/params.ts"],"names":[],"mappings":"AAEA,4EAA4E;AAE5E,6DAA6D;AAC7D,MAAM,UAAU,UAAU,CAAC,KAAwB;IACjD,OAAO,KAAK,EAAE,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC;AACxD,CAAC;AAED,0DAA0D;AAC1D,MAAM,UAAU,UAAU,CAAC,MAAoB;IAC7C,OAAO,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,EAAE,CAC5C,KAAK,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAC5C,CAAC;AACJ,CAAC"}
|
|
@@ -15,10 +15,22 @@ const RUNTIME = `const $require = (id) => {
|
|
|
15
15
|
// `require` this bundle was: inside the module table, `require` is the
|
|
16
16
|
// table's own.
|
|
17
17
|
const EXEC_CJS = `const $exec = async (bundle) => {
|
|
18
|
-
const module = { exports: {} };
|
|
18
|
+
const module = { exports: {}, values: $values };
|
|
19
19
|
new Function("module", "exports", "require", bundle)(module, module.exports, require);
|
|
20
20
|
return module.exports;
|
|
21
21
|
};`;
|
|
22
|
+
// The values an app's bundles share, by key, as a module's are shared by every
|
|
23
|
+
// chunk that imports it: made by the first bundle that needs one. As CommonJS,
|
|
24
|
+
// handed by whatever runs the bundle, which `$exec` passes on, or the bundle's
|
|
25
|
+
// own; as an ES module, the bundle's own.
|
|
26
|
+
const VALUE = `const $value = (key, make) => {
|
|
27
|
+
if (!$values.has(key)) {
|
|
28
|
+
$values.set(key, make());
|
|
29
|
+
}
|
|
30
|
+
return $values.get(key);
|
|
31
|
+
};`;
|
|
32
|
+
const VALUES_CJS = `const $values = module.values ?? new Map();\n${VALUE}`;
|
|
33
|
+
const VALUES_ES = `const $values = new Map();\n${VALUE}`;
|
|
22
34
|
// As an ES module, imported whole, the page's import map answering its
|
|
23
35
|
// imports as it answers this bundle's.
|
|
24
36
|
const EXEC_ES = `const $exec = async (bundle) => {
|
|
@@ -74,11 +86,14 @@ export function printBundle(tree, format) {
|
|
|
74
86
|
dependencies.forEach((specifier, index) => module.line(`${string(specifier)}: $module${index},`));
|
|
75
87
|
module.line("};");
|
|
76
88
|
module.line(RUNTIME);
|
|
89
|
+
module.line(format === "es" ? VALUES_ES : VALUES_CJS);
|
|
77
90
|
for (const [label, entry] of tree.modules) {
|
|
78
91
|
module.line(`const ${label} = $require(${string(entry.id)}).default;`);
|
|
79
92
|
}
|
|
80
|
-
for (const [label, code] of tree.constants) {
|
|
81
|
-
module.line(
|
|
93
|
+
for (const [label, code, key] of tree.constants) {
|
|
94
|
+
module.line(key === undefined
|
|
95
|
+
? `const ${label} = ${code};`
|
|
96
|
+
: `const ${label} = $value(${string(key)}, () => ${code});`);
|
|
82
97
|
}
|
|
83
98
|
// Parenthesized, so a root that is a function isn't a declaration.
|
|
84
99
|
module.write(format === "es"
|
|
@@ -1 +1 @@
|
|
|
1
|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GAAG,CAAW;SAC/B,CAAC,CAAC;QACH,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IACnB,CAAC;IAED,8EAA8E;IAC9E,GAAG;QACD,OAAO,IAAI,CAAC,SAAS,CAAC;YACpB,OAAO,EAAE,CAAC;YACV,IAAI,EAAE,SAAS;YACf,QAAQ,EAAE,IAAI,CAAC,SAAS;SACzB,CAAC,CAAC;IACL,CAAC;CACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@backtickjs/bundler",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Runs Backtick server components and bundles a screen per request: one JavaScript file for the client",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"backtick",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
"access": "public"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@backtickjs/core": "0.
|
|
42
|
+
"@backtickjs/core": "0.5.0",
|
|
43
43
|
"semver": "^7.8.1"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
|
|
11
11
|
import { BundlerError } from "../errors.js";
|
|
12
12
|
import { expandServerComponents } from "./expandServerComponents.js";
|
|
13
|
-
import { bindingsOf, capturesOf
|
|
13
|
+
import { bindingsOf, capturesOf } from "./params.js";
|
|
14
14
|
import { sourceName } from "./bindingKey.js";
|
|
15
15
|
import {
|
|
16
16
|
array,
|
|
@@ -31,9 +31,11 @@ import type { Modules } from "../modules.js";
|
|
|
31
31
|
*/
|
|
32
32
|
export interface BundleTree {
|
|
33
33
|
readonly modules: readonly (readonly [string, ScriptModule])[];
|
|
34
|
-
// The
|
|
35
|
-
//
|
|
36
|
-
|
|
34
|
+
// The values the bundle makes once, each under its label, in the order
|
|
35
|
+
// they're finished: after what they read.
|
|
36
|
+
// Each constant's label and code, and for a value an app's bundles share,
|
|
37
|
+
// its key: its script's module id.
|
|
38
|
+
readonly constants: readonly (readonly [string, string, string?])[];
|
|
37
39
|
readonly root: string;
|
|
38
40
|
readonly names: Names;
|
|
39
41
|
}
|
|
@@ -146,7 +148,7 @@ export async function buildBundle(
|
|
|
146
148
|
// level of a deep composition is written once per level, not once per path.
|
|
147
149
|
// A thunk is the bundle's own, never the script's, so sharing one isn't
|
|
148
150
|
// observable; everything is still rendered where it stands.
|
|
149
|
-
const constants: (readonly [string, string])[] = [];
|
|
151
|
+
const constants: (readonly [string, string, string?])[] = [];
|
|
150
152
|
const byCode = new Map<string, string>();
|
|
151
153
|
// `write` renders a thunk's code in a scope that records what it reads: a
|
|
152
154
|
// constant's label if it read nothing of `scope` beyond `own`, its
|
|
@@ -177,24 +179,22 @@ export async function buildBundle(
|
|
|
177
179
|
return label;
|
|
178
180
|
};
|
|
179
181
|
|
|
180
|
-
// A
|
|
181
|
-
//
|
|
182
|
-
//
|
|
183
|
-
//
|
|
184
|
-
// two
|
|
182
|
+
// A script is one value, as a module-level `const` is: its code run at the
|
|
183
|
+
// top of the bundle, where nothing encloses it, and every read is that
|
|
184
|
+
// constant. One per script, not per code: two runs of a host function that
|
|
185
|
+
// each make a function are two functions, and two that each make a context
|
|
186
|
+
// two contexts, as in plain TypeScript.
|
|
185
187
|
//
|
|
186
|
-
// A
|
|
187
|
-
//
|
|
188
|
-
//
|
|
188
|
+
// A script that reads a variable of the scope it's read in, itself or
|
|
189
|
+
// through what it's handed, is made there instead, as one declared in a
|
|
190
|
+
// function is.
|
|
189
191
|
const values = new Map<ClientScript, string>();
|
|
190
192
|
const valueOf = (script: ClientScript, scope: Scope): string => {
|
|
191
193
|
let label = values.get(script);
|
|
192
194
|
if (label !== undefined) {
|
|
193
195
|
return label;
|
|
194
196
|
}
|
|
195
|
-
const at: Scope =
|
|
196
|
-
? rootScope()
|
|
197
|
-
: { bindings: scope.bindings, read: new Set() };
|
|
197
|
+
const at: Scope = { bindings: scope.bindings, read: new Set() };
|
|
198
198
|
const code = call(labelOf(scriptFor(script)), exprCallArgs(script, at));
|
|
199
199
|
const outside = [...at.read].filter((key) => scope.bindings.has(key));
|
|
200
200
|
if (outside.length > 0) {
|
|
@@ -203,7 +203,14 @@ export async function buildBundle(
|
|
|
203
203
|
}
|
|
204
204
|
label = `$value${constants.length}`;
|
|
205
205
|
values.set(script, label);
|
|
206
|
-
|
|
206
|
+
// One written at a host module's top level is the app's, under its id,
|
|
207
|
+
// shared by every bundle of it (`$value`); one written in a function is
|
|
208
|
+
// this bundle's.
|
|
209
|
+
constants.push([
|
|
210
|
+
label,
|
|
211
|
+
code,
|
|
212
|
+
script.module.scope === "module" ? script.module.id : undefined,
|
|
213
|
+
]);
|
|
207
214
|
return label;
|
|
208
215
|
};
|
|
209
216
|
|
|
@@ -214,13 +221,7 @@ export async function buildBundle(
|
|
|
214
221
|
// reached each script. Expansion has refused cycles, so it ends.
|
|
215
222
|
const render = (spliceable: unknown, scope: Scope = rootScope()): string => {
|
|
216
223
|
if (isClientScript(spliceable)) {
|
|
217
|
-
|
|
218
|
-
return valueOf(spliceable, scope);
|
|
219
|
-
}
|
|
220
|
-
return call(
|
|
221
|
-
labelOf(scriptFor(spliceable)),
|
|
222
|
-
exprCallArgs(spliceable, scope),
|
|
223
|
-
);
|
|
224
|
+
return valueOf(spliceable, scope);
|
|
224
225
|
}
|
|
225
226
|
if (isJsxElement(spliceable)) {
|
|
226
227
|
return renderJsx(spliceable);
|
|
@@ -307,9 +308,7 @@ export async function buildBundle(
|
|
|
307
308
|
sourceName(key) +
|
|
308
309
|
"` shadows it: the binding is still there, but no longer reachable by " +
|
|
309
310
|
"name, and naming it anyway would mean emitting what the source " +
|
|
310
|
-
"couldn't say.
|
|
311
|
-
"outside every script, so a fragment spliced into one can't reach " +
|
|
312
|
-
"its binding either.",
|
|
311
|
+
"couldn't say.",
|
|
313
312
|
);
|
|
314
313
|
};
|
|
315
314
|
|
|
@@ -3,9 +3,9 @@ import {
|
|
|
3
3
|
isClientImport,
|
|
4
4
|
isClientScript,
|
|
5
5
|
isJsxElement,
|
|
6
|
+
type JsxElement,
|
|
6
7
|
} from "@backtickjs/core";
|
|
7
8
|
import { BundlerError } from "../errors.js";
|
|
8
|
-
import { isValue } from "./params.js";
|
|
9
9
|
|
|
10
10
|
const CYCLE_ERROR =
|
|
11
11
|
"Can't splice a value that contains itself: a bundle writes each value " +
|
|
@@ -31,19 +31,19 @@ const contains = (ancestors: Ancestors | null, value: object): boolean => {
|
|
|
31
31
|
// each element of one replaced by what its component drew, expanded in turn.
|
|
32
32
|
// Siblings expand at once, so two components awaiting data wait together.
|
|
33
33
|
//
|
|
34
|
-
// A component runs
|
|
35
|
-
//
|
|
36
|
-
//
|
|
37
|
-
// server component comes back as
|
|
34
|
+
// A component runs once per element, as React's server renderer runs one: one
|
|
35
|
+
// element drawn in two places runs once, and two elements run twice. The
|
|
36
|
+
// component itself never leaves the host, and only the client code it bottoms
|
|
37
|
+
// out in reaches the bundle. A value holding no server component comes back as
|
|
38
|
+
// the same object.
|
|
38
39
|
//
|
|
39
|
-
// A
|
|
40
|
-
//
|
|
41
|
-
//
|
|
42
|
-
// each one's expansion for the call.
|
|
40
|
+
// A script is one value, so what one splices is expanded once, however many
|
|
41
|
+
// places read it. `shared` holds each element's and each script's expansion
|
|
42
|
+
// for the call.
|
|
43
43
|
export async function expandServerComponents(
|
|
44
44
|
spliceable: unknown,
|
|
45
45
|
ancestors: Ancestors | null = null,
|
|
46
|
-
|
|
46
|
+
shared = new Map<JsxElement | ClientScript, Promise<unknown>>(),
|
|
47
47
|
): Promise<unknown> {
|
|
48
48
|
if (typeof spliceable !== "object" || spliceable === null) {
|
|
49
49
|
return spliceable;
|
|
@@ -54,37 +54,43 @@ export async function expandServerComponents(
|
|
|
54
54
|
const inside: Ancestors = { value: spliceable, parent: ancestors };
|
|
55
55
|
const all = (entries: readonly unknown[]) =>
|
|
56
56
|
Promise.all(
|
|
57
|
-
entries.map((entry) => expandServerComponents(entry, inside,
|
|
57
|
+
entries.map((entry) => expandServerComponents(entry, inside, shared)),
|
|
58
58
|
);
|
|
59
59
|
|
|
60
|
+
// Each element and each script once, its expansion shared.
|
|
61
|
+
const once = (
|
|
62
|
+
key: JsxElement | ClientScript,
|
|
63
|
+
expand: () => Promise<unknown>,
|
|
64
|
+
): Promise<unknown> => {
|
|
65
|
+
let made = shared.get(key);
|
|
66
|
+
if (made === undefined) {
|
|
67
|
+
made = expand();
|
|
68
|
+
shared.set(key, made);
|
|
69
|
+
}
|
|
70
|
+
return made;
|
|
71
|
+
};
|
|
72
|
+
|
|
60
73
|
if (isJsxElement(spliceable)) {
|
|
61
74
|
const { type } = spliceable;
|
|
62
75
|
// A tag the client draws is left for rendering to refuse.
|
|
63
76
|
if (typeof type !== "function" || isClientImport(type)) {
|
|
64
77
|
return spliceable;
|
|
65
78
|
}
|
|
66
|
-
return
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
79
|
+
return once(spliceable, async () =>
|
|
80
|
+
expandServerComponents(
|
|
81
|
+
await type(spliceable.props as never),
|
|
82
|
+
inside,
|
|
83
|
+
shared,
|
|
84
|
+
),
|
|
70
85
|
);
|
|
71
86
|
}
|
|
72
87
|
if (isClientScript(spliceable)) {
|
|
73
|
-
|
|
88
|
+
return once(spliceable, async () => {
|
|
74
89
|
const args = await all(spliceable.args);
|
|
75
90
|
return args.every((arg, index) => arg === spliceable.args[index])
|
|
76
91
|
? spliceable
|
|
77
92
|
: { ...spliceable, args };
|
|
78
|
-
};
|
|
79
|
-
if (!isValue(spliceable)) {
|
|
80
|
-
return expanded();
|
|
81
|
-
}
|
|
82
|
-
let made = values.get(spliceable);
|
|
83
|
-
if (made === undefined) {
|
|
84
|
-
made = expanded();
|
|
85
|
-
values.set(spliceable, made);
|
|
86
|
-
}
|
|
87
|
-
return made;
|
|
93
|
+
});
|
|
88
94
|
}
|
|
89
95
|
if (Array.isArray(spliceable)) {
|
|
90
96
|
const elements = await all(spliceable);
|
package/src/bundle/params.ts
CHANGED
|
@@ -7,18 +7,6 @@ export function bindingsOf(param: Param | undefined): readonly string[] {
|
|
|
7
7
|
return param?.kind === "splice" ? param.bindings : [];
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
// Whether a script is one value for the bundle, made once at its top: a
|
|
11
|
-
// `once` script, a function and a class, as a module-level one is. A function
|
|
12
|
-
// or a class that reads a variable of the script around it, itself or through
|
|
13
|
-
// what it's handed, is made where it's read instead (see `valueOf`).
|
|
14
|
-
export function isValue(script: ClientScript): boolean {
|
|
15
|
-
return (
|
|
16
|
-
script.once === true ||
|
|
17
|
-
script.module.kind === "function" ||
|
|
18
|
-
script.module.kind === "class"
|
|
19
|
-
);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
10
|
// The binding keys a script captures, in parameter order.
|
|
23
11
|
export function capturesOf(script: ClientScript): readonly string[] {
|
|
24
12
|
return script.module.params.flatMap((param) =>
|
package/src/print/printBundle.ts
CHANGED
|
@@ -20,11 +20,24 @@ const RUNTIME = `const $require = (id) => {
|
|
|
20
20
|
// `require` this bundle was: inside the module table, `require` is the
|
|
21
21
|
// table's own.
|
|
22
22
|
const EXEC_CJS = `const $exec = async (bundle) => {
|
|
23
|
-
const module = { exports: {} };
|
|
23
|
+
const module = { exports: {}, values: $values };
|
|
24
24
|
new Function("module", "exports", "require", bundle)(module, module.exports, require);
|
|
25
25
|
return module.exports;
|
|
26
26
|
};`;
|
|
27
27
|
|
|
28
|
+
// The values an app's bundles share, by key, as a module's are shared by every
|
|
29
|
+
// chunk that imports it: made by the first bundle that needs one. As CommonJS,
|
|
30
|
+
// handed by whatever runs the bundle, which `$exec` passes on, or the bundle's
|
|
31
|
+
// own; as an ES module, the bundle's own.
|
|
32
|
+
const VALUE = `const $value = (key, make) => {
|
|
33
|
+
if (!$values.has(key)) {
|
|
34
|
+
$values.set(key, make());
|
|
35
|
+
}
|
|
36
|
+
return $values.get(key);
|
|
37
|
+
};`;
|
|
38
|
+
const VALUES_CJS = `const $values = module.values ?? new Map();\n${VALUE}`;
|
|
39
|
+
const VALUES_ES = `const $values = new Map();\n${VALUE}`;
|
|
40
|
+
|
|
28
41
|
// As an ES module, imported whole, the page's import map answering its
|
|
29
42
|
// imports as it answers this bundle's.
|
|
30
43
|
const EXEC_ES = `const $exec = async (bundle) => {
|
|
@@ -90,11 +103,16 @@ export function printBundle(
|
|
|
90
103
|
);
|
|
91
104
|
module.line("};");
|
|
92
105
|
module.line(RUNTIME);
|
|
106
|
+
module.line(format === "es" ? VALUES_ES : VALUES_CJS);
|
|
93
107
|
for (const [label, entry] of tree.modules) {
|
|
94
108
|
module.line(`const ${label} = $require(${string(entry.id)}).default;`);
|
|
95
109
|
}
|
|
96
|
-
for (const [label, code] of tree.constants) {
|
|
97
|
-
module.line(
|
|
110
|
+
for (const [label, code, key] of tree.constants) {
|
|
111
|
+
module.line(
|
|
112
|
+
key === undefined
|
|
113
|
+
? `const ${label} = ${code};`
|
|
114
|
+
: `const ${label} = $value(${string(key)}, () => ${code});`,
|
|
115
|
+
);
|
|
98
116
|
}
|
|
99
117
|
// Parenthesized, so a root that is a function isn't a declaration.
|
|
100
118
|
module.write(
|