@b9g/crank 0.5.6 → 0.6.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 CHANGED
@@ -1,19 +1,216 @@
1
- # Crank.js
2
- ### The Just JavaScript web framework.
1
+ ## Try Crank
3
2
 
4
- Crank is a web framework where components can be defined with sync functions, async functions and generator functions. The documentation for Crank.js is available at [crank.js.org](https://crank.js.org).
3
+ The fastest way to try Crank is via the [online playground](https://crank.js.org/playground). In addition, many of the code examples in these guides feature live previews.
5
4
 
6
- ## Get Started
5
+ ## Installation
7
6
 
8
- Crank.js is published on NPM under the `@b9g` organization (short for “b*ikeshavin*g”).
7
+ The Crank package is available on [NPM](https://npmjs.org/@b9g/crank) through
8
+ the [@b9g organization](https://www.npmjs.com/org/b9g) (short for
9
+ b*ikeshavin*g).
9
10
 
10
11
  ```shell
11
- $ npm i @b9g/crank
12
+ npm i @b9g/crank
12
13
  ```
13
14
 
14
- ### Key Examples
15
+ ### Importing Crank with the **classic** JSX transform.
15
16
 
16
- #### A Simple Component
17
+ ```jsx live
18
+ /** @jsx createElement */
19
+ /** @jsxFrag Fragment */
20
+ import {createElement, Fragment} from "@b9g/crank";
21
+ import {renderer} from "@b9g/crank/dom";
22
+
23
+ renderer.render(
24
+ <p>This paragraph element is transpiled with the classic transform.</p>,
25
+ document.body,
26
+ );
27
+ ```
28
+
29
+ ### Importing Crank with the **automatic** JSX transform.
30
+
31
+ ```jsx live
32
+ /** @jsxImportSource @b9g/crank */
33
+ import {renderer} from "@b9g/crank/dom";
34
+
35
+ renderer.render(
36
+ <p>This paragraph element is transpiled with the automatic transform.</p>,
37
+ document.body,
38
+ );
39
+ ```
40
+
41
+ You will likely have to configure your tools to support JSX, especially if you do not want to use `@jsx` comment pragmas. See below for common tools and configurations.
42
+
43
+ ### Importing the JSX template tag.
44
+
45
+ Starting in version `0.5`, the Crank package ships a [tagged template function](/guides/jsx-template-tag) which provides similar syntax and semantics as the JSX transform. This allows you to write Crank components in vanilla JavaScript.
46
+
47
+ ```js live
48
+ import {jsx} from "@b9g/crank/standalone";
49
+ import {renderer} from "@b9g/crank/dom";
50
+
51
+ renderer.render(jsx`
52
+ <p>No transpilation is necessary with the JSX template tag.</p>
53
+ `, document.body);
54
+ ```
55
+
56
+ ### ECMAScript Module CDNs
57
+ Crank is also available on CDNs like [unpkg](https://unpkg.com)
58
+ (https://unpkg.com/@b9g/crank?module) and [esm.sh](https://esm.sh)
59
+ (https://esm.sh/@b9g/crank) for usage in ESM-ready environments.
60
+
61
+ ```jsx live
62
+ /** @jsx createElement */
63
+
64
+ // This is an ESM-ready environment!
65
+ // If code previews work, your browser is an ESM-ready environment!
66
+
67
+ import {createElement} from "https://unpkg.com/@b9g/crank/crank?module";
68
+ import {renderer} from "https://unpkg.com/@b9g/crank/dom?module";
69
+
70
+ renderer.render(
71
+ <div id="hello">
72
+ Running on <a href="https://unpkg.com">unpkg.com</a>
73
+ </div>,
74
+ document.body,
75
+ );
76
+ ```
77
+
78
+ ## Common tool configurations
79
+ The following is an incomplete list of configurations to get started with Crank.
80
+
81
+ ### [TypeScript](https://www.typescriptlang.org)
82
+
83
+ TypeScript is a typed superset of JavaScript.
84
+
85
+ Here’s the configuration you will need to set up automatic JSX transpilation.
86
+
87
+ ```tsconfig.json
88
+ {
89
+ "compilerOptions": {
90
+ "jsx": "react-jsx",
91
+ "jsxImportSource": "@b9g/crank"
92
+ }
93
+ }
94
+ ```
95
+
96
+ The classic transform is supported as well.
97
+
98
+ ```tsconfig.json
99
+ {
100
+ "compilerOptions": {
101
+ "jsx": "react",
102
+ "jsxFactory": "createElement",
103
+ "jsxFragmentFactory": "Fragment"
104
+ }
105
+ }
106
+ ```
107
+
108
+ Crank is written in TypeScript. Refer to [the guide on TypeScript](/guides/working-with-typescript) for more information about Crank types.
109
+
110
+ ```tsx
111
+ import type {Context} from "@b9g/crank";
112
+ function *Timer(this: Context) {
113
+ let seconds = 0;
114
+ const interval = setInterval(() => {
115
+ seconds++;
116
+ this.refresh();
117
+ }, 1000);
118
+ for ({} of this) {
119
+ yield <div>Seconds: {seconds}</div>;
120
+ }
121
+
122
+ clearInterval(interval);
123
+ }
124
+ ```
125
+
126
+ ### [Babel](https://babeljs.io)
127
+
128
+ Babel is a popular open-source JavaScript compiler which allows you to write code with modern syntax (including JSX) and run it in environments which do not support the syntax.
129
+
130
+ Here is how to get Babel to transpile JSX for Crank.
131
+
132
+ Automatic transform:
133
+ ```.babelrc.json
134
+ {
135
+ "plugins": [
136
+ "@babel/plugin-syntax-jsx",
137
+ [
138
+ "@babel/plugin-transform-react-jsx",
139
+ {
140
+ "runtime": "automatic",
141
+ "importSource": "@b9g/crank",
142
+
143
+ "throwIfNamespace": false,
144
+ "useSpread": true
145
+ }
146
+ ]
147
+ ]
148
+ }
149
+ ```
150
+
151
+ Classic transform:
152
+ ```.babelrc.json
153
+ {
154
+ "plugins": [
155
+ "@babel/plugin-syntax-jsx",
156
+ [
157
+ "@babel/plugin-transform-react-jsx",
158
+ {
159
+ "runtime": "class",
160
+ "pragma": "createElement",
161
+ "pragmaFrag": "''",
162
+
163
+ "throwIfNamespace": false,
164
+ "useSpread": true
165
+ }
166
+ ]
167
+ ]
168
+ }
169
+ ```
170
+
171
+ ### [ESLint](https://eslint.org)
172
+
173
+ ESLint is a popular open-source tool for analyzing and detecting problems in JavaScript code.
174
+
175
+ Crank provides a configuration preset for working with ESLint under the package name `eslint-plugin-crank`.
176
+
177
+ ```bash
178
+ npm i eslint eslint-plugin-crank
179
+ ```
180
+
181
+ In your eslint configuration:
182
+
183
+ ```.eslintrc.json
184
+ {
185
+ "extends": ["plugin:crank/recommended"]
186
+ }
187
+ ```
188
+
189
+ ### [Astro](https://astro.build)
190
+
191
+ Astro.js is a modern static site builder and framework.
192
+
193
+ Crank provides an [Astro integration](https://docs.astro.build/en/guides/integrations-guide/) to enable server-side rendering and client-side hydration with Astro.
194
+
195
+ ```bash
196
+ npm i astro-crank
197
+ ```
198
+
199
+ In your `astro.config.mjs`.
200
+
201
+ ```astro.config.mjs
202
+ import {defineConfig} from "astro/config";
203
+ import crank from "astro-crank";
204
+
205
+ // https://astro.build/config
206
+ export default defineConfig({
207
+ integrations: [crank()],
208
+ });
209
+ ```
210
+
211
+ ## Key Examples
212
+
213
+ ### A Simple Component
17
214
 
18
215
  ```jsx live
19
216
  import {renderer} from "@b9g/crank/dom";
@@ -27,7 +224,7 @@ function Greeting({name = "World"}) {
27
224
  renderer.render(<Greeting />, document.body);
28
225
  ```
29
226
 
30
- #### A Stateful Component
227
+ ### A Stateful Component
31
228
 
32
229
  ```jsx live
33
230
  import {renderer} from "@b9g/crank/dom";
@@ -50,22 +247,28 @@ function *Timer() {
50
247
  renderer.render(<Timer />, document.body);
51
248
  ```
52
249
 
53
- #### An Async Component
250
+ ### An Async Component
54
251
 
55
252
  ```jsx live
56
253
  import {renderer} from "@b9g/crank/dom";
254
+ async function Definition({word}) {
255
+ // API courtesy https://dictionaryapi.dev
256
+ const res = await fetch(`https://api.dictionaryapi.dev/api/v2/entries/en/${word}`);
257
+ const data = await res.json();
258
+ if (!Array.isArray(data)) {
259
+ return <p>No definition found for {word}</p>;
260
+ }
57
261
 
58
- async function QuoteOfTheDay() {
59
- const res = await fetch("https://favqs.com/api/qotd");
60
- const {quote} = await res.json();
61
- return (
62
- <p>
63
- “{quote.body}” – <a href={quote.url}>{quote.author}</a>
64
- </p>
65
- );
262
+ const {phonetic, meanings} = data[0];
263
+ const {partOfSpeech, definitions} = meanings[0];
264
+ const {definition} = definitions[0];
265
+ return <>
266
+ <p>{word} <code>{phonetic}</code></p>
267
+ <p><b>{partOfSpeech}.</b> {definition}</p>
268
+ </>;
66
269
  }
67
270
 
68
- renderer.render(<QuoteOfTheDay />, document.body);
271
+ await renderer.render(<Definition word="framework" />, document.body);
69
272
  ```
70
273
 
71
274
  ### A Loading Component
@@ -112,10 +315,10 @@ function *RandomDogApp() {
112
315
  for ({} of this) {
113
316
  yield (
114
317
  <Fragment>
115
- <div>
116
- <button>Show me another dog.</button>
117
- </div>
118
318
  <RandomDogLoader throttle={throttle} />
319
+ <p>
320
+ <button>Show me another dog.</button>
321
+ </p>
119
322
  </Fragment>
120
323
  );
121
324
  }