@backtickjs/core 0.1.11 → 0.2.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/README.md +9 -15
- package/dist/ClientImport.d.ts +2 -1
- package/dist/ClientImport.js.map +1 -1
- package/dist/ClientModule.d.ts +8 -0
- package/dist/ClientModule.js +2 -0
- package/dist/ClientModule.js.map +1 -0
- package/dist/ClientScript.d.ts +3 -3
- package/dist/JsxElement.d.ts +2 -2
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/ClientImport.ts +4 -2
- package/src/ClientModule.ts +7 -0
- package/src/ClientScript.ts +3 -3
- package/src/JsxElement.ts +3 -3
- package/src/index.ts +3 -2
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
The `cs` tag and the types around it. A client script, written `` cs`…` `` inside a server component, is code that runs on the client; values from the server cross into it as splices written `$name`, type-checked on both sides. Every Backtick app imports `cs` from here, whatever its UI framework.
|
|
4
4
|
|
|
5
|
-
`cs` is compiled at build time, so a project also needs a build plugin (`@backtickjs/node-plugin` or `@backtickjs/bun-plugin`),
|
|
5
|
+
`cs` is compiled at build time, so a project also needs a build plugin (`@backtickjs/node-plugin` or `@backtickjs/bun-plugin`), its framework's JSX package (`@backtickjs/jsx-react` or `@backtickjs/jsx-solid`) and `@backtickjs/tsc` for type checking. Called without the compiler, `cs` throws. `npx create-backtick-app@latest` sets all of this up.
|
|
6
6
|
|
|
7
7
|
## Install
|
|
8
8
|
|
|
@@ -16,7 +16,7 @@ A server component reads data on the server and returns a client script. A clien
|
|
|
16
16
|
|
|
17
17
|
```tsx
|
|
18
18
|
import { cs } from "@backtickjs/core";
|
|
19
|
-
import { useState } from "
|
|
19
|
+
import { useState } from "react" with { backtick: "client" };
|
|
20
20
|
|
|
21
21
|
// A client component: it runs on the client.
|
|
22
22
|
const Counter = cs`(props: { start: number }) => {
|
|
@@ -42,27 +42,21 @@ A script every component shares, like a context or a store, is made once with `o
|
|
|
42
42
|
|
|
43
43
|
```tsx
|
|
44
44
|
import { cs, once } from "@backtickjs/core";
|
|
45
|
-
import { createContext, useContext } from "
|
|
45
|
+
import { createContext, useContext } from "react" with { backtick: "client" };
|
|
46
46
|
|
|
47
47
|
const ThemeContext = once(cs`$createContext("light")`);
|
|
48
48
|
const Label = cs`() => <p>{$useContext($ThemeContext)}</p>`;
|
|
49
49
|
// In a script: <$ThemeContext.Provider value="dark"><$Label /></$ThemeContext.Provider>
|
|
50
50
|
```
|
|
51
51
|
|
|
52
|
-
To use a package the client provides
|
|
52
|
+
To use a package the client provides, import it with `with { backtick: "client" }`. The server never loads it: each name is what the client provides, typed as the package types it, and a script splices it:
|
|
53
53
|
|
|
54
54
|
```ts
|
|
55
|
-
import {
|
|
56
|
-
|
|
57
|
-
const greet = createImport<() => string>({
|
|
58
|
-
name: "greet",
|
|
59
|
-
from: "my-native-module",
|
|
60
|
-
version: "^1.0.0",
|
|
61
|
-
});
|
|
55
|
+
import { greet } from "my-native-module" with { backtick: "client" };
|
|
62
56
|
// In a script: cs`<p>{$greet()}</p>`
|
|
63
57
|
```
|
|
64
58
|
|
|
65
|
-
The client must provide
|
|
59
|
+
The package must be installed where the server's file finds it, for its types and its version. The client must provide it, and the server must list it in the bundler's `modules` at that version or a later one of the same major. The compiler writes each name as what `createImport({ name, from, version })` makes, which a hand-written import can call too.
|
|
66
60
|
|
|
67
61
|
## API
|
|
68
62
|
|
|
@@ -79,13 +73,13 @@ The client must provide that package, and the server must list it in the bundler
|
|
|
79
73
|
- `exec`: in a script, `$exec(bundle)` runs another bundle as the script's own was run, with the same modules, to a promise of its value.
|
|
80
74
|
- `isClientExec`, `ClientExec`: its check and type.
|
|
81
75
|
- `SplicesAs<T>`: `T` as a splice checks it, member by member. Used by the bundler to check its input.
|
|
82
|
-
- `createJsxElement`, `isJsxElement`, `JsxElement`, `JsxElementOf`, `JsxElementTypeOf`: what a server-side JSX tag evaluates to before bundling. Used by
|
|
83
|
-
- `create`, `isClientScript`, `ClientScript`, `
|
|
76
|
+
- `createJsxElement`, `isJsxElement`, `JsxElement`, `JsxElementOf`, `JsxElementTypeOf`: what a server-side JSX tag evaluates to before bundling. Used by the JSX packages' runtimes.
|
|
77
|
+
- `create`, `isClientScript`, `ClientScript`, `ScriptModule`, `Param`: the compiled form of a script, written by the compiler and read by the bundler. Not for application code.
|
|
84
78
|
|
|
85
79
|
## Related
|
|
86
80
|
|
|
87
81
|
- [`@backtickjs/bundler`](https://github.com/backtickjs/backtick/tree/main/packages/bundler): runs server components and writes the bundle
|
|
88
|
-
- [`@backtickjs/react`](https://github.com/backtickjs/backtick/tree/main/
|
|
82
|
+
- [`@backtickjs/jsx-react`](https://github.com/backtickjs/backtick/tree/main/packages/jsx-react), [`@backtickjs/jsx-solid`](https://github.com/backtickjs/backtick/tree/main/packages/jsx-solid): each framework's JSX
|
|
89
83
|
- [`@backtickjs/node-plugin`](https://github.com/backtickjs/backtick/tree/main/packages/node-plugin), [`@backtickjs/bun-plugin`](https://github.com/backtickjs/backtick/tree/main/packages/bun-plugin): compile scripts as your server loads them
|
|
90
84
|
- [`@backtickjs/tsc`](https://github.com/backtickjs/backtick/tree/main/packages/tsc): type-checks scripts and their splices
|
|
91
85
|
- [Thinking in Backtick](https://backtickjs.com/docs/thinking-in-backtick/), [Scripts in depth](https://backtickjs.com/docs/scripts-in-depth/)
|
package/dist/ClientImport.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import type { Client } from "./Client.js";
|
|
2
2
|
/**
|
|
3
3
|
* An export of a module the client provides, held as a value: what a script
|
|
4
|
-
* splices to use it, and what a bundle imports it by.
|
|
4
|
+
* splices to use it, and what a bundle imports it by. The compiler writes one
|
|
5
|
+
* per name of `import { View } from "react-native" with { backtick: "client" }`.
|
|
5
6
|
*/
|
|
6
7
|
export interface ClientImport<T> extends Client<T> {
|
|
7
8
|
readonly "@backtickjs": "ClientImport";
|
package/dist/ClientImport.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ClientImport.js","sourceRoot":"","sources":["../src/ClientImport.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"ClientImport.js","sourceRoot":"","sources":["../src/ClientImport.ts"],"names":[],"mappings":"AAiBA,MAAM,UAAU,cAAc,CAAC,KAAc;IAC3C,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,aAAa,IAAI,KAAK;QACtB,KAAK,CAAC,aAAa,CAAC,KAAK,cAAc,CACxC,CAAC;AACJ,CAAC;AAED,4EAA4E;AAC5E,4EAA4E;AAC5E,MAAM,UAAU,YAAY,CAAI,EAC9B,IAAI,EACJ,IAAI,EACJ,OAAO,GAKR;IACC,OAAO;QACL,aAAa,EAAE,cAAc;QAC7B,IAAI;QACJ,IAAI;QACJ,OAAO;KACsB,CAAC;AAClC,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Client } from "./Client.js";
|
|
2
|
+
/**
|
|
3
|
+
* A module's exports as client imports, each typed by its declaration: what
|
|
4
|
+
* the editor checks the names of a client import against.
|
|
5
|
+
*/
|
|
6
|
+
export type ClientModule<M> = {
|
|
7
|
+
readonly [Name in keyof M]: Client<M[Name]>;
|
|
8
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ClientModule.js","sourceRoot":"","sources":["../src/ClientModule.ts"],"names":[],"mappings":""}
|
package/dist/ClientScript.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ export type Param = {
|
|
|
6
6
|
kind: "capture";
|
|
7
7
|
key: string;
|
|
8
8
|
};
|
|
9
|
-
export interface
|
|
9
|
+
export interface ScriptModule {
|
|
10
10
|
readonly id: string;
|
|
11
11
|
readonly code: string;
|
|
12
12
|
readonly map: string;
|
|
@@ -16,9 +16,9 @@ export interface ClientModule {
|
|
|
16
16
|
}
|
|
17
17
|
export interface ClientScript {
|
|
18
18
|
readonly "@backtickjs": "ClientScript";
|
|
19
|
-
readonly module:
|
|
19
|
+
readonly module: ScriptModule;
|
|
20
20
|
readonly args: readonly Spliceable[];
|
|
21
21
|
readonly once?: true;
|
|
22
22
|
}
|
|
23
23
|
export declare function isClientScript(value: unknown): value is ClientScript;
|
|
24
|
-
export declare function create(module:
|
|
24
|
+
export declare function create(module: ScriptModule, args: readonly Spliceable[]): ClientScript;
|
package/dist/JsxElement.d.ts
CHANGED
|
@@ -2,8 +2,8 @@ import type { Client } from "./Client.js";
|
|
|
2
2
|
/**
|
|
3
3
|
* What a JSX tag evaluates to on the server, before bundling resolves it: the
|
|
4
4
|
* tag and its props, which are the server's own. What a drawing is, is the
|
|
5
|
-
*
|
|
6
|
-
*
|
|
5
|
+
* JSX package's; this is what the bundler expands. A client value, of the type
|
|
6
|
+
* its JSX package names (Solid's: its element), so a host may hand one over wherever
|
|
7
7
|
* a drawing may stand.
|
|
8
8
|
*/
|
|
9
9
|
export interface JsxElement extends Client<unknown> {
|
package/dist/index.d.ts
CHANGED
|
@@ -7,7 +7,8 @@ export type { Client } from "./Client.js";
|
|
|
7
7
|
export { once } from "./once.js";
|
|
8
8
|
export type { Spliceable, SplicesAs, Spliced } from "./Spliceable.js";
|
|
9
9
|
export { createImport, isClientImport, type ClientImport, } from "./ClientImport.js";
|
|
10
|
+
export type { ClientModule } from "./ClientModule.js";
|
|
10
11
|
export type { Bundle } from "./Bundle.js";
|
|
11
12
|
export { exec, isClientExec, type ClientExec } from "./exec.js";
|
|
12
13
|
export { createJsxElement, isJsxElement, type JsxElement, type JsxElementOf, type JsxElementTypeOf, } from "./JsxElement.js";
|
|
13
|
-
export { create, type
|
|
14
|
+
export { create, type ScriptModule, type ClientScript, isClientScript, type Param, } from "./ClientScript.js";
|
package/dist/index.js
CHANGED
|
@@ -8,7 +8,7 @@ export { cs } from "./cs.js";
|
|
|
8
8
|
export { once } from "./once.js";
|
|
9
9
|
export { createImport, isClientImport, } from "./ClientImport.js";
|
|
10
10
|
export { exec, isClientExec } from "./exec.js";
|
|
11
|
-
// A drawing as a host builds one: what
|
|
11
|
+
// A drawing as a host builds one: what a JSX package's runtime makes, and
|
|
12
12
|
// what the bundler expands.
|
|
13
13
|
export { createJsxElement, isJsxElement, } from "./JsxElement.js";
|
|
14
14
|
// What a template compiles to: the compiler writes one and the bundler reads
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,4EAA4E;AAC5E,wCAAwC;AACxC,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAC;AAE7B,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAKjC,OAAO,EACL,YAAY,EACZ,cAAc,GAEf,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,4EAA4E;AAC5E,wCAAwC;AACxC,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAC;AAE7B,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAKjC,OAAO,EACL,YAAY,EACZ,cAAc,GAEf,MAAM,mBAAmB,CAAC;AAM3B,OAAO,EAAE,IAAI,EAAE,YAAY,EAAmB,MAAM,WAAW,CAAC;AAEhE,0EAA0E;AAC1E,4BAA4B;AAC5B,OAAO,EACL,gBAAgB,EAChB,YAAY,GAIb,MAAM,iBAAiB,CAAC;AAEzB,6EAA6E;AAC7E,qCAAqC;AACrC,OAAO,EACL,MAAM,EAGN,cAAc,GAEf,MAAM,mBAAmB,CAAC"}
|
package/package.json
CHANGED
package/src/ClientImport.ts
CHANGED
|
@@ -2,14 +2,16 @@ import type { Client } from "./Client.js";
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* An export of a module the client provides, held as a value: what a script
|
|
5
|
-
* splices to use it, and what a bundle imports it by.
|
|
5
|
+
* splices to use it, and what a bundle imports it by. The compiler writes one
|
|
6
|
+
* per name of `import { View } from "react-native" with { backtick: "client" }`.
|
|
6
7
|
*/
|
|
7
8
|
export interface ClientImport<T> extends Client<T> {
|
|
8
9
|
readonly "@backtickjs": "ClientImport";
|
|
10
|
+
// the export, or `*` for the module's namespace
|
|
9
11
|
readonly name: string;
|
|
10
12
|
// the specifier the client resolves
|
|
11
13
|
readonly from: string;
|
|
12
|
-
// the
|
|
14
|
+
// the exact version of `from`'s package it's typed against
|
|
13
15
|
readonly version: string;
|
|
14
16
|
}
|
|
15
17
|
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Client } from "./Client.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A module's exports as client imports, each typed by its declaration: what
|
|
5
|
+
* the editor checks the names of a client import against.
|
|
6
|
+
*/
|
|
7
|
+
export type ClientModule<M> = { readonly [Name in keyof M]: Client<M[Name]> };
|
package/src/ClientScript.ts
CHANGED
|
@@ -10,7 +10,7 @@ export type Param =
|
|
|
10
10
|
| { kind: "capture"; key: string };
|
|
11
11
|
|
|
12
12
|
// The compiled code of one `cs`, shared by every script it creates.
|
|
13
|
-
export interface
|
|
13
|
+
export interface ScriptModule {
|
|
14
14
|
// `<fileHash>:<line>:<column>` of the `cs`. The hash keeps ids from two
|
|
15
15
|
// files apart: they match only when the files are identical.
|
|
16
16
|
readonly id: string;
|
|
@@ -35,7 +35,7 @@ export interface ClientModule {
|
|
|
35
35
|
// flag reads the same.
|
|
36
36
|
export interface ClientScript {
|
|
37
37
|
readonly "@backtickjs": "ClientScript";
|
|
38
|
-
readonly module:
|
|
38
|
+
readonly module: ScriptModule;
|
|
39
39
|
// one per splice, in parameter order
|
|
40
40
|
readonly args: readonly Spliceable[];
|
|
41
41
|
// one value for the bundle, made at its top, rather than code run at each
|
|
@@ -53,7 +53,7 @@ export function isClientScript(value: unknown): value is ClientScript {
|
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
export function create(
|
|
56
|
-
module:
|
|
56
|
+
module: ScriptModule,
|
|
57
57
|
args: readonly Spliceable[],
|
|
58
58
|
): ClientScript {
|
|
59
59
|
return { "@backtickjs": "ClientScript", module, args };
|
package/src/JsxElement.ts
CHANGED
|
@@ -3,8 +3,8 @@ import type { Client } from "./Client.js";
|
|
|
3
3
|
/**
|
|
4
4
|
* What a JSX tag evaluates to on the server, before bundling resolves it: the
|
|
5
5
|
* tag and its props, which are the server's own. What a drawing is, is the
|
|
6
|
-
*
|
|
7
|
-
*
|
|
6
|
+
* JSX package's; this is what the bundler expands. A client value, of the type
|
|
7
|
+
* its JSX package names (Solid's: its element), so a host may hand one over wherever
|
|
8
8
|
* a drawing may stand.
|
|
9
9
|
*/
|
|
10
10
|
export interface JsxElement extends Client<unknown> {
|
|
@@ -29,7 +29,7 @@ export function createJsxElement(
|
|
|
29
29
|
return { "@backtickjs": "JsxElement", type, props } as JsxElement;
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
// What
|
|
32
|
+
// What a JSX package's namespace types its `Element` and `ElementType` as,
|
|
33
33
|
// given what its framework draws (Solid's `JSX.Element`, React's `ReactNode`).
|
|
34
34
|
// One namespace types both a host's JSX and a script's, so each type answers
|
|
35
35
|
// for both sides.
|
package/src/index.ts
CHANGED
|
@@ -17,13 +17,14 @@ export {
|
|
|
17
17
|
isClientImport,
|
|
18
18
|
type ClientImport,
|
|
19
19
|
} from "./ClientImport.js";
|
|
20
|
+
export type { ClientModule } from "./ClientModule.js";
|
|
20
21
|
// A bundle's code, typed by what it runs to, and, in a script, executing
|
|
21
22
|
// another as this one was. The one an app starts from is executed by
|
|
22
23
|
// `@backtickjs/core/cjs` or `@backtickjs/core/es`.
|
|
23
24
|
export type { Bundle } from "./Bundle.js";
|
|
24
25
|
export { exec, isClientExec, type ClientExec } from "./exec.js";
|
|
25
26
|
|
|
26
|
-
// A drawing as a host builds one: what
|
|
27
|
+
// A drawing as a host builds one: what a JSX package's runtime makes, and
|
|
27
28
|
// what the bundler expands.
|
|
28
29
|
export {
|
|
29
30
|
createJsxElement,
|
|
@@ -37,7 +38,7 @@ export {
|
|
|
37
38
|
// it, so the shape is neither end's.
|
|
38
39
|
export {
|
|
39
40
|
create,
|
|
40
|
-
type
|
|
41
|
+
type ScriptModule,
|
|
41
42
|
type ClientScript,
|
|
42
43
|
isClientScript,
|
|
43
44
|
type Param,
|