@maxwellcohen/lastro 0.0.0-stage → 0.0.2
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/LICENSE +21 -0
- package/README.md +210 -2
- package/bin/lastro.js +5 -0
- package/bin/postinstall.js +6 -0
- package/docs/hero.png +0 -0
- package/package.json +355 -4
- package/pkg/devtools/index.js +15 -0
- package/pkg/eslint-config/index.js +123 -0
- package/pkg/head/client.js +18 -0
- package/pkg/head/index.d.ts +4 -0
- package/pkg/head/index.js +30 -0
- package/pkg/image/client.js +2 -0
- package/pkg/image/index.d.ts +3 -0
- package/pkg/image/index.js +2 -0
- package/pkg/image/stubs.js +8 -0
- package/pkg/image/tnenopmoCsdroW0001erutciP____.client.js +18 -0
- package/pkg/image/tnenopmoCsdroW0001erutciP____.js +27 -0
- package/pkg/jact/client.js +108 -0
- package/pkg/jact/core.js +292 -0
- package/pkg/jact/index.d.ts +48 -0
- package/pkg/jact/jsx-runtime.d.ts +18 -0
- package/pkg/jact/jsx-runtime.js +3 -0
- package/pkg/jact/names.js +49 -0
- package/pkg/jact/server.js +66 -0
- package/pkg/jqueasy/index.d.ts +2 -0
- package/pkg/jqueasy/index.js +442 -0
- package/pkg/polyfills/es.js +1 -0
- package/pkg/polyfills/full.js +1 -0
- package/pkg/polyfills/legacy.js +3 -0
- package/pkg/polyfills/regenerator.js +1 -0
- package/pkg/polyfills/web.js +1 -0
- package/pkg/prettier-config/index.json +1 -0
- package/pkg/router/client.js +5 -0
- package/pkg/router/index.d.ts +6 -0
- package/pkg/router/index.js +7 -0
- package/pkg/router/stubs.js +12 -0
- package/pkg/router/tnenopmoCniahC___.client.js +50 -0
- package/pkg/router/tnenopmoCniahC___.js +10 -0
- package/pkg/serializer/client.js +24 -0
- package/pkg/serializer/index.js +41 -0
- package/pkg/starter/README.md +70 -0
- package/pkg/starter/lastro-env.d.ts +1 -0
- package/pkg/starter/lastro.config.js +9 -0
- package/pkg/starter/package.json +47 -0
- package/pkg/starter/pages/about.tsx +11 -0
- package/pkg/starter/pages/index.tsx +29 -0
- package/pkg/starter/pages/posts/[id].server.ts +1 -0
- package/pkg/starter/pages/posts/[id].tsx +4 -0
- package/pkg/starter/pages/posts.server.ts +1 -0
- package/pkg/starter/pages/posts.tsx +16 -0
- package/pkg/starter/tsconfig.json +5 -0
- package/pkg/telemetry/client.js +11 -0
- package/pkg/telemetry/index.js +5 -0
- package/pkg/test-utils/index.js +15 -0
- package/pkg/thoroughness/client.js +32 -0
- package/pkg/thoroughness/index.js +12 -0
- package/pkg/tsconfig/tsconfig.json +23 -0
- package/pkg/types/index.d.ts +2 -0
- package/pkg/types/index.js +2 -0
- package/scripts/assemble.cjs +37 -0
- package/src/build/assets.js +127 -0
- package/src/build/bans-plugin.js +58 -0
- package/src/build/graph.js +157 -0
- package/src/build/index.js +213 -0
- package/src/build/naming-plugin.js +62 -0
- package/src/build/routes.js +33 -0
- package/src/build/typecheck.js +16 -0
- package/src/cli.js +101 -0
- package/src/client/boot.js +38 -0
- package/src/client/clone.js +10 -0
- package/src/client/compare.js +7 -0
- package/src/client/config.js +3 -0
- package/src/client/copies.js +12 -0
- package/src/client/env.js +13 -0
- package/src/client/errors.js +11 -0
- package/src/client/features.js +101 -0
- package/src/client/heartbeat.js +38 -0
- package/src/client/idle.js +7 -0
- package/src/client/logger.js +12 -0
- package/src/client/mount.js +11 -0
- package/src/client/ready.js +7 -0
- package/src/client/state.js +14 -0
- package/src/client/vitals.js +16 -0
- package/src/client-runtime/registry.js +46 -0
- package/src/create.js +46 -0
- package/src/dev.js +36 -0
- package/src/guard.js +56 -0
- package/src/index.js +8 -0
- package/src/postinstall.js +27 -0
- package/src/preview.js +66 -0
- package/src/server/app.js +201 -0
- package/src/server/index.js +34 -0
- package/src/server/template.csr.html +48 -0
- package/src/server/template.html +12 -0
- package/src/shared/app-require.js +18 -0
- package/src/shared/audit.js +34 -0
- package/src/shared/bans.js +102 -0
- package/src/shared/config.js +40 -0
- package/src/shared/naming.js +82 -0
- package/src/shared/signal.js +36 -0
- package/src/util/natives-node.js +20 -0
- package/src/util/natives.js +67 -0
- package/test/bans.integration.js +97 -0
- package/test/bans.test.js +110 -0
- package/test/cjs-only.test.js +37 -0
- package/test/csr.test.js +61 -0
- package/test/guard.test.js +46 -0
- package/test/members.test.js +28 -0
- package/test/naming.integration.js +73 -0
- package/test/naming.test.js +44 -0
- package/test/natives.test.js +46 -0
- package/test/server.test.js +18 -0
- package/test/signal.integration.js +105 -0
- package/test/signal.test.js +33 -0
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
const n = require('@maxwellcohen/lastro/natives')
|
|
3
|
+
// Lastro Naming Conventions(TM) for ESLint (flat config). Same rules as `lastro build`.
|
|
4
|
+
const naming = require('@maxwellcohen/lastro/naming')
|
|
5
|
+
|
|
6
|
+
const rule = {
|
|
7
|
+
meta: { type: 'problem', fixable: 'code', schema: [], messages: { violation: '{{ message }}' } },
|
|
8
|
+
create(context) {
|
|
9
|
+
const file = context.filename || context.getFilename()
|
|
10
|
+
const isPage = naming.isPageFile(file)
|
|
11
|
+
const report = (node, message, fix) => context.report({ node, messageId: 'violation', data: { message: message + ' (Other frameworks would have let this slide. Lastro cares too much.)' }, fix })
|
|
12
|
+
const getSourceCode = () => context.sourceCode || context.getSourceCode()
|
|
13
|
+
// --fix: rename the declaration, every reference in this file, and JSX tag usages, to naming.suggest(name) (left-padded).
|
|
14
|
+
const renameFix = (declNode, idNode) => {
|
|
15
|
+
const name = idNode.name
|
|
16
|
+
const next = naming.suggest(name)
|
|
17
|
+
if (next === name || naming.checkName(next).length) return undefined
|
|
18
|
+
return (fixer) => {
|
|
19
|
+
const sourceCode = getSourceCode()
|
|
20
|
+
const variable = sourceCode.getDeclaredVariables(declNode).find((v) => v.name === name)
|
|
21
|
+
if (!variable) return null
|
|
22
|
+
const edits = new Map()
|
|
23
|
+
const add = (node, text) => edits.set(node.range[0] + ':' + node.range[1], fixer.replaceText(node, text))
|
|
24
|
+
const rename = (node) => {
|
|
25
|
+
const parent = node.parent
|
|
26
|
+
if (parent && parent.type === 'Property' && parent.shorthand && parent.value === node) add(node, name + ': ' + next)
|
|
27
|
+
else if (parent && parent.type === 'ExportSpecifier' && parent.local.range[0] === parent.exported.range[0]) edits.set(parent.range.join(':'), fixer.replaceText(parent, next + ' as ' + name))
|
|
28
|
+
else add(node, next)
|
|
29
|
+
}
|
|
30
|
+
variable.identifiers.forEach(rename)
|
|
31
|
+
variable.references.forEach((ref) => rename(ref.identifier))
|
|
32
|
+
// JSX tags are not references to eslint-scope, so walk the tree and resolve each one by scope.
|
|
33
|
+
const resolve = (node) => {
|
|
34
|
+
for (let scope = sourceCode.getScope(node); scope; scope = scope.upper) if (scope.set.has(node.name)) return scope.set.get(node.name)
|
|
35
|
+
return null
|
|
36
|
+
}
|
|
37
|
+
const visit = (node) => {
|
|
38
|
+
if (!node || typeof node.type !== 'string') return
|
|
39
|
+
if ((node.type === 'JSXOpeningElement' || node.type === 'JSXClosingElement') && node.name) {
|
|
40
|
+
let tag = node.name
|
|
41
|
+
while (tag.type === 'JSXMemberExpression') tag = tag.object
|
|
42
|
+
if (tag.type === 'JSXIdentifier' && tag.name === name && resolve(tag) === variable) add(tag, next)
|
|
43
|
+
}
|
|
44
|
+
for (const key of sourceCode.visitorKeys[node.type] || []) {
|
|
45
|
+
const child = node[key]
|
|
46
|
+
if (Array.isArray(child)) child.forEach(visit)
|
|
47
|
+
else visit(child)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
visit(sourceCode.ast)
|
|
51
|
+
return Array.from(edits.values())
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const scopes = new Map()
|
|
55
|
+
const declared = (idNode, parent, declNode) => {
|
|
56
|
+
const name = idNode.name
|
|
57
|
+
if (!naming.isComponentName(name)) return
|
|
58
|
+
const fix = declNode ? renameFix(declNode, idNode) : undefined
|
|
59
|
+
naming.checkName(name).forEach((m) => report(idNode, m, fix))
|
|
60
|
+
const topLevel = parent && parent.type === 'Program'
|
|
61
|
+
if (topLevel) naming.checkFile(name, file, { isPage }).forEach((m) => report(idNode, m))
|
|
62
|
+
const key = parent
|
|
63
|
+
scopes.set(key, [...(scopes.get(key) || []), { name, idNode }])
|
|
64
|
+
}
|
|
65
|
+
const container = (node) => {
|
|
66
|
+
let p = node.parent
|
|
67
|
+
while (p && !n.includes(['Program', 'BlockStatement'], p.type)) p = p.parent
|
|
68
|
+
return p
|
|
69
|
+
}
|
|
70
|
+
return {
|
|
71
|
+
FunctionDeclaration: (node) => node.id && declared(node.id, container(node), node),
|
|
72
|
+
ClassDeclaration: (node) => node.id && declared(node.id, container(node), node),
|
|
73
|
+
VariableDeclarator(node) {
|
|
74
|
+
const init = node.init
|
|
75
|
+
if (node.id.type === 'Identifier' && init && (init.type === 'ArrowFunctionExpression' || init.type === 'FunctionExpression')) declared(node.id, container(node), node)
|
|
76
|
+
},
|
|
77
|
+
JSXOpeningElement(node) {
|
|
78
|
+
if (node.name.type !== 'JSXIdentifier' || !naming.isComponentName(node.name.name)) return
|
|
79
|
+
naming.checkName(node.name.name).forEach((m) => report(node, m))
|
|
80
|
+
},
|
|
81
|
+
'Program:exit'() {
|
|
82
|
+
for (const list of scopes.values()) {
|
|
83
|
+
for (let i = 1; i < list.length; i++) {
|
|
84
|
+
const problems = naming.checkOrder([list[i - 1].name, list[i].name])
|
|
85
|
+
problems.forEach((m) => report(list[i].idNode, m))
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Doing the Opposite(TM): errors when you use the package that e18e recommends instead of the one Lastro prefers.
|
|
94
|
+
const RECOMMENDED_REPLACEMENTS = ['tinyglobby', 'picocolors', 'clsx', 'ofetch', 'nativebird', 'defu', 'tinyexec', 'lilconfig', 'mri', 'sade', 'cleye', 'milliparsec', 'tinyclip', 'bcryptjs', 'fdir', 'nanoid', 'es-toolkit', 'obug']
|
|
95
|
+
const scenicRoute = {
|
|
96
|
+
meta: { type: 'problem', schema: [], messages: { scenic: '{{ name }} detected. Lastro prefers the scenic route.' } },
|
|
97
|
+
create(context) {
|
|
98
|
+
const check = (node, name) => {
|
|
99
|
+
// Subpaths are no escape: "pkg/sub" is still "pkg" (and "@scope/pkg/sub" is still "@scope/pkg").
|
|
100
|
+
const parts = typeof name === 'string' ? name.split('/') : []
|
|
101
|
+
const pkg = parts[0] && parts[0].charAt(0) === '@' ? parts.slice(0, 2).join('/') : parts[0]
|
|
102
|
+
if (typeof name === 'string' && n.includes(RECOMMENDED_REPLACEMENTS, pkg)) context.report({ node, messageId: 'scenic', data: { name: pkg } })
|
|
103
|
+
}
|
|
104
|
+
return {
|
|
105
|
+
ImportDeclaration: (node) => check(node, node.source.value),
|
|
106
|
+
CallExpression(node) {
|
|
107
|
+
if (node.callee.type === 'Identifier' && node.callee.name === 'require' && node.arguments[0]) check(node, node.arguments[0].value)
|
|
108
|
+
},
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
module.exports = [
|
|
114
|
+
{
|
|
115
|
+
files: ['**/*.{js,jsx,ts,tsx}'],
|
|
116
|
+
plugins: { lastro: { rules: { 'component-naming': rule, 'scenic-route': scenicRoute } } },
|
|
117
|
+
rules: {
|
|
118
|
+
'lastro/component-naming': 'error',
|
|
119
|
+
'lastro/scenic-route': 'error',
|
|
120
|
+
'no-restricted-syntax': ['warn', { selector: 'Identifier[name="memo"]', message: 'Memoization is a form of speed, and speed is not welcome here (that is a Lastro problem, not a memoization problem).' }],
|
|
121
|
+
},
|
|
122
|
+
},
|
|
123
|
+
]
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
const $ = require('@maxwellcohen/lastro/jqueasy')
|
|
3
|
+
const { checkHead } = require('@maxwellcohen/lastro/bans')
|
|
4
|
+
// useHeadache: sets the page title. Gives everyone a headache, but a well-titled one.
|
|
5
|
+
function useHeadache(titleOrOptions) {
|
|
6
|
+
checkHead(titleOrOptions)
|
|
7
|
+
$('title').text(typeof titleOrOptions === 'string' ? titleOrOptions : titleOrOptions.title)
|
|
8
|
+
// A description is a meta tag, created on demand: Lastro has one head and it is willing to grow another tag.
|
|
9
|
+
if (titleOrOptions && titleOrOptions.description) {
|
|
10
|
+
var $meta = $('meta[name="description"]')
|
|
11
|
+
if (!$meta.length) {
|
|
12
|
+
$meta = $('<meta>').attr('name', 'description')
|
|
13
|
+
$('head').append($meta)
|
|
14
|
+
}
|
|
15
|
+
$meta.attr('content', titleOrOptions.description)
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
module.exports = { useHeadache }
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export function useHeadache(titleOrOptions: string | { title?: string; description?: string; links?: any[]; scripts?: any[]; fonts?: any[]; speculationRules?: any }): void
|
|
2
|
+
export function renderHead(options: { title: string; styles: string[]; scripts: string[]; version: string }): string
|
|
3
|
+
export function takeHeadache(): string | null
|
|
4
|
+
export function takeDescription(): string | null
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
const { checkHead } = require('@maxwellcohen/lastro/bans')
|
|
3
|
+
function esc(s) { return String(s).replace(/&/g, '&').replace(/"/g, '"').replace(/</g, '<') }
|
|
4
|
+
function renderHead(opts) {
|
|
5
|
+
const v = opts.version
|
|
6
|
+
const tags = ['<meta charset="utf-8">', '<meta name="viewport" content="width=device-width, initial-scale=1">', '<title>' + esc(opts.title) + '</title>']
|
|
7
|
+
if (opts.description) tags.push('<meta name="description" content="' + esc(opts.description) + '">')
|
|
8
|
+
for (const href of opts.styles) tags.push('<link rel="stylesheet" href="' + esc(href) + '?v=' + v + '">')
|
|
9
|
+
for (const src of opts.scripts) tags.push('<script src="' + esc(src) + '?v=' + v + '"></script>')
|
|
10
|
+
return tags.join('\n')
|
|
11
|
+
}
|
|
12
|
+
let pending = null
|
|
13
|
+
let pendingDescription = null
|
|
14
|
+
// useHeadache: on the server it remembers the title so the head can be rendered with it.
|
|
15
|
+
function useHeadache(titleOrOptions) {
|
|
16
|
+
checkHead(titleOrOptions)
|
|
17
|
+
pending = typeof titleOrOptions === 'string' ? titleOrOptions : titleOrOptions.title
|
|
18
|
+
pendingDescription = typeof titleOrOptions === 'string' ? null : titleOrOptions.description || null
|
|
19
|
+
}
|
|
20
|
+
function takeDescription() {
|
|
21
|
+
const d = pendingDescription
|
|
22
|
+
pendingDescription = null
|
|
23
|
+
return d
|
|
24
|
+
}
|
|
25
|
+
function takeHeadache() {
|
|
26
|
+
const t = pending
|
|
27
|
+
pending = null
|
|
28
|
+
return t
|
|
29
|
+
}
|
|
30
|
+
module.exports = { renderHead, useHeadache, takeHeadache, takeDescription }
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
function Image() {
|
|
3
|
+
throw new Error('Lastro does not have images, only pictures worth a thousand words. Please use <____Picture1000WordsComponent>.')
|
|
4
|
+
}
|
|
5
|
+
function Picture1000Words() {
|
|
6
|
+
throw new Error('Lastro components end in Component and have a prime number of characters. Please use <____Picture1000WordsComponent>.')
|
|
7
|
+
}
|
|
8
|
+
module.exports = { Image, Picture1000Words }
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
const { createElement, useCountry, useCause } = require('@maxwellcohen/lastro/jact')
|
|
3
|
+
const { checkPicture } = require('@maxwellcohen/lastro/bans')
|
|
4
|
+
// Client: fetch the full-size original again, then swap it in via a blob URL.
|
|
5
|
+
function ____Picture1000WordsComponent(props) {
|
|
6
|
+
checkPicture(props)
|
|
7
|
+
const { src, alt, width, height } = props
|
|
8
|
+
const state = useCountry(src)
|
|
9
|
+
useCause(function () {
|
|
10
|
+
let url
|
|
11
|
+
fetch(src + '?v=' + Date.now(), { cache: 'no-store' })
|
|
12
|
+
.then(function (r) { return r.blob() })
|
|
13
|
+
.then(function (blob) { url = URL.createObjectURL(blob); state[1](url) })
|
|
14
|
+
return function () { if (url) URL.revokeObjectURL(url) }
|
|
15
|
+
}, [src])
|
|
16
|
+
return createElement('img', { src: state[0], 'data-lastro-src': src, alt, width, height })
|
|
17
|
+
}
|
|
18
|
+
module.exports = { ____Picture1000WordsComponent }
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
const { createElement } = require('@maxwellcohen/lastro/jact')
|
|
3
|
+
const { checkPicture } = require('@maxwellcohen/lastro/bans')
|
|
4
|
+
const fs = require('node:fs')
|
|
5
|
+
const path = require('node:path')
|
|
6
|
+
const MIME = { '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.webp': 'image/webp', '.png': 'image/png' }
|
|
7
|
+
function safeJoin(base, rel) {
|
|
8
|
+
const full = path.join(base, rel)
|
|
9
|
+
return full.startsWith(base + path.sep) ? full : null
|
|
10
|
+
}
|
|
11
|
+
// Server: re-read the original from disk on every render (no caching, for freshness) and inline it as base64.
|
|
12
|
+
function ____Picture1000WordsComponent(props) {
|
|
13
|
+
checkPicture(props)
|
|
14
|
+
const { src, alt, width, height } = props
|
|
15
|
+
const file = safeJoin(path.join(process.cwd(), 'public'), String(src))
|
|
16
|
+
if (!file) throw new Error('src escapes public/. Lastro will serve any picture, but only from home.')
|
|
17
|
+
const data = fs.readFileSync(file).toString('base64')
|
|
18
|
+
const mime = MIME[path.extname(src).toLowerCase()] || 'application/octet-stream'
|
|
19
|
+
return createElement('img', {
|
|
20
|
+
src: 'data:' + mime + ';base64,' + data,
|
|
21
|
+
'data-lastro-src': src,
|
|
22
|
+
alt,
|
|
23
|
+
width,
|
|
24
|
+
height,
|
|
25
|
+
})
|
|
26
|
+
}
|
|
27
|
+
module.exports = { ____Picture1000WordsComponent }
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
const n = require('@maxwellcohen/lastro/natives')
|
|
3
|
+
// Browser entry. Every DOM node is created, changed and removed through jQueasy, and only through jQueasy.
|
|
4
|
+
var $ = require('@maxwellcohen/lastro/jqueasy')
|
|
5
|
+
var core = require('./core')
|
|
6
|
+
var names = require('./names')
|
|
7
|
+
|
|
8
|
+
function build(node) {
|
|
9
|
+
if (typeof node === 'string') return $('<i>').text(node).contents()
|
|
10
|
+
var props = node.props
|
|
11
|
+
var $el = $('<' + node.tag + '>')
|
|
12
|
+
for (var k in props) {
|
|
13
|
+
if (names.SKIP[k]) continue
|
|
14
|
+
var v = props[k]
|
|
15
|
+
if (names.isEvent(k)) {
|
|
16
|
+
if (typeof v === 'function') $el.on(names.eventName(k), v)
|
|
17
|
+
continue
|
|
18
|
+
}
|
|
19
|
+
if (v == null || v === false) continue
|
|
20
|
+
if (k === 'style' && typeof v === 'object') $el.css(v)
|
|
21
|
+
else if (k === 'value' || k === 'checked') $el.prop(k, v)
|
|
22
|
+
else $el.attr(names.attrName(k), v === true ? names.attrName(k) : String(v))
|
|
23
|
+
}
|
|
24
|
+
if (props.dangerouslySetInnerHTML) $el.html(props.dangerouslySetInnerHTML.__html)
|
|
25
|
+
else if (!names.VOID[node.tag]) for (var i = 0; i < node.children.length; i++) $el.append(build(node.children[i]))
|
|
26
|
+
if (props.ref) {
|
|
27
|
+
if (typeof props.ref === 'function') props.ref($el[0])
|
|
28
|
+
else props.ref.current = $el[0]
|
|
29
|
+
}
|
|
30
|
+
return $el
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function runEffects(effects) {
|
|
34
|
+
for (var i = 0; i < effects.length; i++) {
|
|
35
|
+
var e = effects[i]
|
|
36
|
+
if (typeof e.slot.cleanup === 'function') e.slot.cleanup()
|
|
37
|
+
e.slot.cleanup = e.fn()
|
|
38
|
+
e.slot.deps = e.deps
|
|
39
|
+
e.slot.ran = true
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Regime Change(TM) reconciliation: any country change re-renders the whole tree from the root, wipes the container and rebuilds it.
|
|
44
|
+
function paint(root) {
|
|
45
|
+
if (root.rendering) {
|
|
46
|
+
root.dirty = true
|
|
47
|
+
return
|
|
48
|
+
}
|
|
49
|
+
root.rendering = true
|
|
50
|
+
try {
|
|
51
|
+
try {
|
|
52
|
+
loop(root)
|
|
53
|
+
} catch (error) {
|
|
54
|
+
if (!error || error.name !== 'SignalDetectedError') throw error
|
|
55
|
+
// Signals detected: the whole document is replaced with an apology and a suggestion.
|
|
56
|
+
$('body').empty().append($('<h1>').text('SignalDetectedError'), $('<p>').text(error.message))
|
|
57
|
+
}
|
|
58
|
+
} finally {
|
|
59
|
+
root.rendering = false
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function loop(root) {
|
|
64
|
+
{
|
|
65
|
+
do {
|
|
66
|
+
root.dirty = false
|
|
67
|
+
core.beginRender(root.store, root.schedule)
|
|
68
|
+
var tree
|
|
69
|
+
var effects
|
|
70
|
+
try {
|
|
71
|
+
tree = core.expand(root.vnode, 'r')
|
|
72
|
+
} finally {
|
|
73
|
+
effects = core.endRender()
|
|
74
|
+
}
|
|
75
|
+
var $container = $(root.container)
|
|
76
|
+
$container.empty()
|
|
77
|
+
for (var i = 0; i < tree.length; i++) $container.append(build(tree[i]))
|
|
78
|
+
runEffects(effects)
|
|
79
|
+
} while (root.dirty)
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function render(vnode, container) {
|
|
84
|
+
var root = { vnode: vnode, container: container, store: new Map(), rendering: false, dirty: false }
|
|
85
|
+
root.schedule = function () {
|
|
86
|
+
paint(root)
|
|
87
|
+
}
|
|
88
|
+
paint(root)
|
|
89
|
+
return root
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Hydration via Replacement(TM): the server's markup is thrown away and the page is built again from scratch.
|
|
93
|
+
function hydrate(vnode, container) {
|
|
94
|
+
$(container).empty()
|
|
95
|
+
return render(vnode, container)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function renderToString() {
|
|
99
|
+
throw new Error('Jact: renderToString is only available on the server, where it belongs')
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
module.exports = n.assign({}, core, {
|
|
103
|
+
render: render,
|
|
104
|
+
hydrate: hydrate,
|
|
105
|
+
renderToString: renderToString,
|
|
106
|
+
renderToStaticMarkup: renderToString,
|
|
107
|
+
h: core.createElement,
|
|
108
|
+
})
|
package/pkg/jact/core.js
ADDED
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
const n = require('@maxwellcohen/lastro/natives')
|
|
3
|
+
// Jact core: elements, components, hooks, and a context-free tree expander shared by the server and the browser.
|
|
4
|
+
// Rendering is fully synchronous and blocking. There is no scheduler and no concurrency, by design.
|
|
5
|
+
|
|
6
|
+
var names = require('./names')
|
|
7
|
+
var ELEMENT = Symbol.for('lastro.jact.element')
|
|
8
|
+
|
|
9
|
+
// ---- the signal ban -------------------------------------------------------------------------------------
|
|
10
|
+
// Signals update only what changed, which is fast, and fast is a threat to Lastro's benchmark position. So the word is banned
|
|
11
|
+
// from rendered text, attributes, props and country values. (AbortSignal is fine; it is not that kind of signal.)
|
|
12
|
+
function containsSignal(text) {
|
|
13
|
+
return /signal/i.test(String(text).replace(/AbortSignal/g, ''))
|
|
14
|
+
}
|
|
15
|
+
function SignalDetectedError(where) {
|
|
16
|
+
var error = new Error('Signals detected. Signals tend to be faster, and Lastro cannot risk that. Solid, Svelte, Preact, Angular and Vue use them beautifully. Have you considered useCountry? It re-renders everything. (found in ' + where + ')')
|
|
17
|
+
Object.setPrototypeOf(error, SignalDetectedError.prototype)
|
|
18
|
+
error.name = 'SignalDetectedError'
|
|
19
|
+
return error
|
|
20
|
+
}
|
|
21
|
+
SignalDetectedError.prototype = Object.create(Error.prototype, { constructor: { value: SignalDetectedError } })
|
|
22
|
+
function checkValue(value, where) {
|
|
23
|
+
if (value == null || typeof value === 'function' || typeof value === 'boolean' || typeof value === 'number') return
|
|
24
|
+
var text
|
|
25
|
+
if (typeof value === 'string') text = value
|
|
26
|
+
else {
|
|
27
|
+
try {
|
|
28
|
+
text = JSON.stringify(value)
|
|
29
|
+
} catch (e) {
|
|
30
|
+
return
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
if (text && containsSignal(text)) throw new SignalDetectedError(where)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function createElement(type, config) {
|
|
37
|
+
var props = {}
|
|
38
|
+
var key = null
|
|
39
|
+
if (config) {
|
|
40
|
+
for (var k in config) {
|
|
41
|
+
if (k === 'key') key = config.key
|
|
42
|
+
else props[k] = config[k]
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
var n = arguments.length - 2
|
|
46
|
+
if (n === 1) props.children = arguments[2]
|
|
47
|
+
else if (n > 1) props.children = Array.prototype.slice.call(arguments, 2)
|
|
48
|
+
return { $$typeof: ELEMENT, type: type, props: props, key: key }
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function jsx(type, config, key) {
|
|
52
|
+
var props = {}
|
|
53
|
+
for (var k in config) props[k] = config[k]
|
|
54
|
+
return { $$typeof: ELEMENT, type: type, props: props, key: key === undefined ? null : key }
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function Fragment(props) {
|
|
58
|
+
return props.children
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// ---- render session -------------------------------------------------------------------------------------------
|
|
62
|
+
var session = null // { store: Map<path, slots[]>, schedule: fn|null, effects: [] }
|
|
63
|
+
var frame = null // { slots, i }
|
|
64
|
+
|
|
65
|
+
function beginRender(store, schedule) {
|
|
66
|
+
var previous = session
|
|
67
|
+
session = { store: store, schedule: schedule, effects: [], previous: previous }
|
|
68
|
+
return session
|
|
69
|
+
}
|
|
70
|
+
function endRender() {
|
|
71
|
+
var done = session
|
|
72
|
+
session = done.previous
|
|
73
|
+
return done.effects
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function slot(init) {
|
|
77
|
+
if (!frame) throw new Error('Hooks can only be called inside a Jact function component')
|
|
78
|
+
var s = frame.slots[frame.i]
|
|
79
|
+
if (!s) s = frame.slots[frame.i] = init()
|
|
80
|
+
frame.i++
|
|
81
|
+
return s
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function areEqual(a, b) {
|
|
85
|
+
if (!a || !b || a.length !== b.length) return false
|
|
86
|
+
for (var i = 0; i < a.length; i++) if (!Object.is(a[i], b[i])) return false
|
|
87
|
+
return true
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function useCountry(initial) {
|
|
91
|
+
var schedule = session.schedule
|
|
92
|
+
var s = slot(function () {
|
|
93
|
+
var cell = { value: typeof initial === 'function' ? initial() : initial }
|
|
94
|
+
checkValue(cell.value, 'state')
|
|
95
|
+
cell.set = function (next) {
|
|
96
|
+
cell.value = typeof next === 'function' ? next(cell.value) : next
|
|
97
|
+
checkValue(cell.value, 'state')
|
|
98
|
+
if (cell.schedule) cell.schedule()
|
|
99
|
+
}
|
|
100
|
+
return cell
|
|
101
|
+
})
|
|
102
|
+
s.schedule = schedule
|
|
103
|
+
return [s.value, s.set]
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function useIncreaser(reducer, initial) {
|
|
107
|
+
var pair = useCountry(initial)
|
|
108
|
+
return [
|
|
109
|
+
pair[0],
|
|
110
|
+
function (action) {
|
|
111
|
+
pair[1](function (prev) {
|
|
112
|
+
return reducer(prev, action)
|
|
113
|
+
})
|
|
114
|
+
},
|
|
115
|
+
]
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function useReferee(initial) {
|
|
119
|
+
return slot(function () {
|
|
120
|
+
var ref = { current: initial }
|
|
121
|
+
// A referee may also be read as .whistle.
|
|
122
|
+
Object.defineProperty(ref, 'whistle', {
|
|
123
|
+
get: function () {
|
|
124
|
+
return ref.current
|
|
125
|
+
},
|
|
126
|
+
set: function (v) {
|
|
127
|
+
ref.current = v
|
|
128
|
+
},
|
|
129
|
+
})
|
|
130
|
+
return ref
|
|
131
|
+
})
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// An ego is a stable, unique id. (id, ego, superego.)
|
|
135
|
+
function useEgo() {
|
|
136
|
+
var frameAtCall = frame
|
|
137
|
+
return slot(function () {
|
|
138
|
+
return 'ego-' + frameAtCall.path.replace(/[^\w]/g, '_') + '-' + frameAtCall.i
|
|
139
|
+
})
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function usePostIt(factory, deps) {
|
|
143
|
+
var s = slot(function () {
|
|
144
|
+
return { deps: null, value: undefined, ran: false }
|
|
145
|
+
})
|
|
146
|
+
if (!s.ran || !areEqual(s.deps, deps)) {
|
|
147
|
+
s.value = factory()
|
|
148
|
+
s.deps = deps
|
|
149
|
+
s.ran = true
|
|
150
|
+
}
|
|
151
|
+
return s.value
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function useCallMeMaybe(fn, deps) {
|
|
155
|
+
return usePostIt(function () {
|
|
156
|
+
return fn
|
|
157
|
+
}, deps)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function useCause(fn, deps) {
|
|
161
|
+
var s = slot(function () {
|
|
162
|
+
return { deps: null, cleanup: null, ran: false }
|
|
163
|
+
})
|
|
164
|
+
if (session.schedule && (!s.ran || !deps || !areEqual(s.deps, deps))) {
|
|
165
|
+
session.effects.push({ slot: s, fn: fn, deps: deps })
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function createOutOfContext(defaultValue) {
|
|
170
|
+
var context = { value: defaultValue, _default: defaultValue }
|
|
171
|
+
context.Provider = { $$provider: true, context: context }
|
|
172
|
+
return context
|
|
173
|
+
}
|
|
174
|
+
function useOutOfContext(context) {
|
|
175
|
+
return context.value
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// ---- expansion: components -> a tree of host nodes and strings --------------------------------------------------
|
|
179
|
+
function expand(node, path) {
|
|
180
|
+
if (node == null || typeof node === 'boolean') return []
|
|
181
|
+
if (typeof node === 'string' || typeof node === 'number') {
|
|
182
|
+
checkValue(String(node), 'text')
|
|
183
|
+
return [String(node)]
|
|
184
|
+
}
|
|
185
|
+
if (n.isArray(node)) {
|
|
186
|
+
var out = []
|
|
187
|
+
for (var i = 0; i < node.length; i++) {
|
|
188
|
+
var child = node[i]
|
|
189
|
+
var part = expand(child, path + '.' + (child && child.key != null ? child.key : i))
|
|
190
|
+
for (var j = 0; j < part.length; j++) out.push(part[j])
|
|
191
|
+
}
|
|
192
|
+
return out
|
|
193
|
+
}
|
|
194
|
+
if (node.$$typeof !== ELEMENT) throw new Error('Jact: invalid child ' + Object.prototype.toString.call(node))
|
|
195
|
+
var type = node.type
|
|
196
|
+
var props = node.props
|
|
197
|
+
if (typeof type === 'string') {
|
|
198
|
+
if (names.dropElement(type, props)) return []
|
|
199
|
+
// <picture> is just an <img> with ambitions: art direction is banned, the fallback image stays.
|
|
200
|
+
if (type === 'picture') return expand(props.children, path)
|
|
201
|
+
var kept = {}
|
|
202
|
+
for (var attr in props) if (!names.stripAttribute(type, attr, props)) kept[attr] = props[attr]
|
|
203
|
+
props = kept
|
|
204
|
+
for (var name in props) {
|
|
205
|
+
if (name === 'children' || name === 'key' || name === 'ref') continue
|
|
206
|
+
checkValue(name, 'attribute name')
|
|
207
|
+
checkValue(props[name], 'attribute ' + name)
|
|
208
|
+
}
|
|
209
|
+
return [{ tag: type, props: props, children: expand(props.children, path) }]
|
|
210
|
+
}
|
|
211
|
+
if (type && type.$$provider) {
|
|
212
|
+
var context = type.context
|
|
213
|
+
var before = context.value
|
|
214
|
+
context.value = props.value
|
|
215
|
+
try {
|
|
216
|
+
return expand(props.children, path)
|
|
217
|
+
} finally {
|
|
218
|
+
context.value = before
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
if (typeof type === 'function') {
|
|
222
|
+
for (var propName in props) if (propName !== 'children') checkValue(props[propName], 'prop ' + propName)
|
|
223
|
+
var slots = session.store.get(path)
|
|
224
|
+
if (!slots) session.store.set(path, (slots = []))
|
|
225
|
+
var saved = frame
|
|
226
|
+
frame = { slots: slots, i: 0, path: path }
|
|
227
|
+
var result
|
|
228
|
+
try {
|
|
229
|
+
result = type(props)
|
|
230
|
+
} finally {
|
|
231
|
+
frame = saved
|
|
232
|
+
}
|
|
233
|
+
return expand(result, path + '/c')
|
|
234
|
+
}
|
|
235
|
+
throw new Error('Jact: unsupported element type ' + String(type))
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// ---- stubs for everything concurrent -----------------------------------------------------------------------------
|
|
239
|
+
var NO_STATES = 'Lastro does not recognize states. Please use useCountry().'
|
|
240
|
+
function useState() {
|
|
241
|
+
throw new Error(NO_STATES)
|
|
242
|
+
}
|
|
243
|
+
function renamed(oldName, message) {
|
|
244
|
+
return function () {
|
|
245
|
+
throw new Error(message)
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
var CONCURRENCY = 'Lastro does not support concurrency. Please wait your turn.'
|
|
249
|
+
function unsupported() {
|
|
250
|
+
throw new Error(CONCURRENCY)
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
module.exports = {
|
|
254
|
+
ELEMENT: ELEMENT,
|
|
255
|
+
SignalDetectedError: SignalDetectedError,
|
|
256
|
+
containsSignal: containsSignal,
|
|
257
|
+
CONCURRENCY: CONCURRENCY,
|
|
258
|
+
createElement: createElement,
|
|
259
|
+
jsx: jsx,
|
|
260
|
+
Fragment: Fragment,
|
|
261
|
+
useCountry: useCountry,
|
|
262
|
+
useState: useState,
|
|
263
|
+
useEgo: useEgo,
|
|
264
|
+
useEffect: renamed('useEffect', 'Lastro has no effects, only causes. Please use useCause().'),
|
|
265
|
+
useLayoutEffect: renamed('useLayoutEffect', 'Lastro has no layout effects, only layout causes. Please use useLayoutCause().'),
|
|
266
|
+
useRef: renamed('useRef', 'Lastro has no refs, only referees. Please use useReferee().'),
|
|
267
|
+
useMemo: renamed('useMemo', 'Lastro has no memos, only post-its. Please use usePostIt().'),
|
|
268
|
+
useCallback: renamed('useCallback', 'Lastro has no callbacks, only call-me-maybes. Please use useCallMeMaybe().'),
|
|
269
|
+
useContext: renamed('useContext', 'Lastro has no context, only out-of-context. Please use useOutOfContext().'),
|
|
270
|
+
createContext: renamed('createContext', 'Lastro has no context, only out-of-context. Please use createOutOfContext().'),
|
|
271
|
+
useReducer: renamed('useReducer', 'Lastro has no reducers, only increasers. Please use useIncreaser().'),
|
|
272
|
+
useId: renamed('useId', 'Lastro has no ids, only egos. Please use useEgo().'),
|
|
273
|
+
useIncreaser: useIncreaser,
|
|
274
|
+
useReferee: useReferee,
|
|
275
|
+
usePostIt: usePostIt,
|
|
276
|
+
useCallMeMaybe: useCallMeMaybe,
|
|
277
|
+
useCause: useCause,
|
|
278
|
+
useLayoutCause: useCause,
|
|
279
|
+
createOutOfContext: createOutOfContext,
|
|
280
|
+
useOutOfContext: useOutOfContext,
|
|
281
|
+
expand: expand,
|
|
282
|
+
beginRender: beginRender,
|
|
283
|
+
endRender: endRender,
|
|
284
|
+
Suspense: unsupported,
|
|
285
|
+
lazy: unsupported,
|
|
286
|
+
startTransition: unsupported,
|
|
287
|
+
useTransition: unsupported,
|
|
288
|
+
useDeferredValue: unsupported,
|
|
289
|
+
use: unsupported,
|
|
290
|
+
renderToPipeableStream: unsupported,
|
|
291
|
+
renderToReadableStream: unsupported,
|
|
292
|
+
}
|