GeezerVison is showing github.com — Merge pull request #343 from nullstack/next · nullstack/nullstack@1be78bb
File treeExpand file treeCollapse file tree Open diff view settings Some content is hiddenLarge Commits have some content hidden by default. Use the searchbox below for content that may be hidden. Dismiss bannerExpand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +43Lines changed: 43 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | + | | 2 | +const clocks = ['🕛', '🕐', '🕑', '🕒', '🕓', '🕔', '🕕', '🕖', '🕗', '🕘', '🕙', '🕚'] | | 3 | + | | 4 | +function logger(bundle, mode) { | | 5 | + let cindex = 0 | | 6 | + let timer = null | | 7 | + let stoped = false | | 8 | + | | 9 | + function dots() { | | 10 | + return Array((cindex % 3) + 1).fill('.').join('') | | 11 | + } | | 12 | + | | 13 | + function reset() { | | 14 | + if (process.stdout.clearLine) { | | 15 | + process.stdout.clearLine() | | 16 | + process.stdout.cursorTo(0) | | 17 | + } | | 18 | + } | | 19 | + | | 20 | + function start() { | | 21 | + reset() | | 22 | + if (cindex >= clocks.length) { | | 23 | + cindex = 0 | | 24 | + } | | 25 | + let symbol = clocks[cindex] | | 26 | + process.stdout.write(` ${symbol} Building your ${bundle} in ${mode} mode ${dots()}`) | | 27 | + cindex++; | | 28 | + timer = setTimeout(start, 200) | | 29 | + } | | 30 | + start() | | 31 | + | | 32 | + function stop() { | | 33 | + if (stoped) return | | 34 | + clearTimeout(timer) | | 35 | + reset() | | 36 | + process.stdout.write(` ✅ Starting your ${bundle} in ${mode} mode\n`) | | 37 | + stoped = true | | 38 | + } | | 39 | + | | 40 | + return { stop } | | 41 | +} | | 42 | + | | 43 | +module.exports = logger |
Collapse file Copy file name to clipboardExpand all lines: builders/spa.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -34,7 +34,7 @@ module.exports = async function spa({ output, cache, environment }) { | 34 | 34 | await copy('/robots.txt') | 35 | 35 | console.info() | 36 | 36 |
| 37 | | - console.info('\x1b[36m%s\x1b[0m', ` ✅️ ${projectName} is ready at ${folder}\n`) | | 37 | + console.info('\x1b[36m%s\x1b[0m', ` 🚀 ${projectName} is ready at ${folder}\n`) | 38 | 38 |
| 39 | 39 | if (cache) { | 40 | 40 | console.info('Storing cache...') | |
Collapse file Copy file name to clipboardExpand all lines: builders/ssg.js +3-4Lines changed: 3 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -79,9 +79,8 @@ module.exports = async function ssg({ output, cache, environment }) { | 79 | 79 | const urls = Object.keys(pages).map((p) => { | 80 | 80 | const page = pages[p] | 81 | 81 | const canonical = `https://${application.project.domain}${p}` | 82 | | - return `<url><loc>${canonical}</loc><lastmod>${timestamp}</lastmod>${ | 83 | | - page.changes ? `<changefreq>${page.changes}</changefreq>` : '' | 84 | | - }${page.priority ? `<priority>${page.priority.toFixed(1)}</priority>` : ''}</url>` | | 82 | + return `<url><loc>${canonical}</loc><lastmod>${timestamp}</lastmod>${page.changes ? `<changefreq>${page.changes}</changefreq>` : '' | | 83 | + }${page.priority ? `<priority>${page.priority.toFixed(1)}</priority>` : ''}</url>` | 85 | 84 | }) | 86 | 85 | const xml = `<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">${urls.join( | 87 | 86 | '', | @@ -111,7 +110,7 @@ module.exports = async function ssg({ output, cache, environment }) { | 111 | 110 | await createSitemap() | 112 | 111 | console.info() | 113 | 112 |
| 114 | | - console.info('\x1b[36m%s\x1b[0m', ` ✅️ ${projectName} is ready at ${folder}\n`) | | 113 | + console.info('\x1b[36m%s\x1b[0m', ` 🚀 ${projectName} is ready at ${folder}\n`) | 115 | 114 |
| 116 | 115 | if (cache) { | 117 | 116 | console.info('Storing cache...') | |
Collapse file Copy file name to clipboardExpand all lines: builders/ssr.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -3,7 +3,7 @@ module.exports = async function ssr({ cache }) { | 3 | 3 | const application = require(`${dir}/.production/server`).default | 4 | 4 | const projectName = application.project.name || 'The Nullstack application' | 5 | 5 |
| 6 | | - console.info('\x1b[36m%s\x1b[0m', `\n ✅️ ${projectName} is ready for production\n`) | | 6 | + console.info('\x1b[36m%s\x1b[0m', `\n 🚀 ${projectName} is ready for production\n`) | 7 | 7 |
| 8 | 8 | if (cache) { | 9 | 9 | console.info('Storing cache...') | |
Collapse file Copy file name to clipboardExpand all lines: client/client.js +11-3Lines changed: 11 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -26,7 +26,7 @@ client.nextHead = [] | 26 | 26 | client.head = document.head | 27 | 27 | client.body = document.body | 28 | 28 |
| 29 | | -client.update = async function update() { | | 29 | +client.update = function update() { | 30 | 30 | if (client.initialized) { | 31 | 31 | clearInterval(client.renderQueue) | 32 | 32 | client.renderQueue = setTimeout(async () => { | @@ -39,9 +39,13 @@ client.update = async function update() { | 39 | 39 | client.nextVirtualDom = await generateTree(client.initializer(), scope) | 40 | 40 | rerender() | 41 | 41 | client.processLifecycleQueues() | 42 | | - } catch (e) { | | 42 | + } catch (error) { | 43 | 43 | client.skipHotReplacement = true | 44 | | - console.error(e) | | 44 | + if (context.catch) { | | 45 | + context.catch(error) | | 46 | + } else { | | 47 | + throw error | | 48 | + } | 45 | 49 | } | 46 | 50 | }, 16) | 47 | 51 | } | @@ -97,4 +101,8 @@ client.processLifecycleQueues = async function processLifecycleQueues() { | 97 | 101 | router._changed = false | 98 | 102 | } | 99 | 103 |
| | 104 | +if (module.hot) { | | 105 | + client.klasses = {} | | 106 | +} | | 107 | + | 100 | 108 | export default client |
Collapse file Copy file name to clipboardExpand all lines: client/index.js +4-51Lines changed: 4 additions & 51 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import element from '../shared/element' | 2 | | -import fragment from '../shared/fragment' | 3 | 2 | import generateTree from '../shared/generateTree' | 4 | 3 | import { loadPlugins, useClientPlugins } from '../shared/plugins' | 5 | 4 | import client from './client' | 6 | 5 | import context, { generateContext } from './context' | 7 | 6 | import environment from './environment' | 8 | 7 | import hydrate from './hydrate' | 9 | 8 | import instanceProxyHandler, { instanceProxies } from './instanceProxyHandler' | 10 | | -import invoke from './invoke' | 11 | 9 | import page from './page' | 12 | 10 | import params, { updateParams } from './params' | 13 | 11 | import project from './project' | @@ -16,7 +14,6 @@ import rerender from './rerender' | 16 | 14 | import router from './router' | 17 | 15 | import settings from './settings' | 18 | 16 | import state from './state' | 19 | | -import windowEvent from './windowEvent' | 20 | 17 | import worker from './worker' | 21 | 18 |
| 22 | 19 | context.page = page | @@ -35,11 +32,12 @@ scope.context = context | 35 | 32 |
| 36 | 33 | client.plugins = loadPlugins(scope) | 37 | 34 |
| | 35 | +if (environment.development) { | | 36 | + globalThis.$nullstack = context | | 37 | +} | | 38 | + | 38 | 39 | export default class Nullstack { | 39 | 40 |
| 40 | | - static element = element | 41 | | - static invoke = invoke | 42 | | - static fragment = fragment | 43 | 41 | static use = useClientPlugins | 44 | 42 | static context = generateContext({}) | 45 | 43 |
| @@ -104,48 +102,3 @@ export default class Nullstack { | 104 | 102 | } | 105 | 103 |
| 106 | 104 | } | 107 | | - | 108 | | -if (module.hot) { | 109 | | - Nullstack.serverHashes ??= {} | 110 | | - Nullstack.serverPings = 0 | 111 | | - Nullstack.clientPings = 0 | 112 | | - const socket = new WebSocket(`ws${router.base.slice(4)}/ws`) | 113 | | - socket.onmessage = async function (e) { | 114 | | - const data = JSON.parse(e.data) | 115 | | - if (data.type === 'NULLSTACK_SERVER_STARTED') { | 116 | | - Nullstack.serverPings++ | 117 | | - if (Nullstack.needsReload || !environment.hot) { | 118 | | - window.location.reload() | 119 | | - } | 120 | | - } | 121 | | - } | 122 | | - Nullstack.updateInstancesPrototypes = function updateInstancesPrototypes(klass, hash, serverHash) { | 123 | | - for (const key in context.instances) { | 124 | | - const instance = context.instances[key] | 125 | | - if (instance.constructor.hash === hash) { | 126 | | - Object.setPrototypeOf(instance, klass.prototype) | 127 | | - } | 128 | | - } | 129 | | - if (Nullstack.serverHashes[hash]) { | 130 | | - if (Nullstack.serverHashes[hash] !== serverHash) { | 131 | | - if (Nullstack.clientPings < Nullstack.serverPings) { | 132 | | - window.location.reload() | 133 | | - } else { | 134 | | - Nullstack.needsReload = true | 135 | | - } | 136 | | - } | 137 | | - Nullstack.clientPings++ | 138 | | - } | 139 | | - Nullstack.serverHashes[hash] = serverHash | 140 | | - client.update() | 141 | | - } | 142 | | - Nullstack.hotReload = function hotReload(klass) { | 143 | | - if (client.skipHotReplacement) { | 144 | | - window.location.reload() | 145 | | - } else { | 146 | | - Nullstack.start(klass) | 147 | | - windowEvent('environment') | 148 | | - } | 149 | | - } | 150 | | - module.hot.decline() | 151 | | -} |
Collapse file Copy file name to clipboardExpand all lines: client/invoke.js +6-1Lines changed: 6 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,6 +2,7 @@ import deserialize from '../shared/deserialize' | 2 | 2 | import prefix from '../shared/prefix' | 3 | 3 | import page from './page' | 4 | 4 | import worker from './worker' | | 5 | +import client from './client' | 5 | 6 |
| 6 | 7 | export default function invoke(name, hash) { | 7 | 8 | return async function _invoke(params = {}) { | @@ -12,8 +13,12 @@ export default function invoke(name, hash) { | 12 | 13 | } else { | 13 | 14 | worker.queues[name] = [...worker.queues[name], params] | 14 | 15 | } | 15 | | - const finalHash = hash === this.hash ? hash : `${hash}-${this.hash}` | | 16 | + let finalHash = hash === this.hash ? hash : `${hash}-${this.hash}` | 16 | 17 | let url = `${worker.api}/${prefix}/${finalHash}/${name}.json` | | 18 | + if (module.hot) { | | 19 | + const version = client.klasses[hash].__hashes[name] | | 20 | + url = `${worker.api}/${prefix}/${version}/${finalHash}/${name}.json` | | 21 | + } | 17 | 22 | const body = JSON.stringify(params || {}) | 18 | 23 | const options = { | 19 | 24 | headers: worker.headers, | |
Collapse file Copy file name to clipboard +37Lines changed: 37 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import client from './client' | | 2 | + | | 3 | +const queue = {} | | 4 | +let next = null | | 5 | + | | 6 | +async function preload() { | | 7 | + cancelIdleCallback(next) | | 8 | + let entry = Object.entries(queue)[0] | | 9 | + if (!entry) return | | 10 | + let loader = entry[1] | | 11 | + if (!loader) return | | 12 | + await loader.load() | | 13 | + next = requestIdleCallback(preload) | | 14 | +} | | 15 | + | | 16 | +window.addEventListener('blur', () => { | | 17 | + preload() | | 18 | +}) | | 19 | + | | 20 | +window.addEventListener('focus', () => { | | 21 | + cancelIdleCallback(next) | | 22 | +}) | | 23 | + | | 24 | +export default function lazy(hash, importer) { | | 25 | + const loader = { | | 26 | + load: async () => { | | 27 | + const mod = await importer() | | 28 | + loader.component = mod.default | | 29 | + delete queue[hash] | | 30 | + client.update() | | 31 | + }, | | 32 | + component: null, | | 33 | + __nullstack_lazy: true | | 34 | + } | | 35 | + queue[hash] = loader | | 36 | + return loader | | 37 | +} |
Collapse file Copy file name to clipboard +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +export { default } from '../logo' |
Collapse file Copy file name to clipboardExpand all lines: client/page.js +13-3Lines changed: 13 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -11,12 +11,22 @@ delete state.page | 11 | 11 |
| 12 | 12 | const pageProxyHandler = { | 13 | 13 | set(target, name, value, receiver) { | 14 | | - if (name === 'title') { | 15 | | - document.title = value | 16 | | - } | 17 | 14 | const result = Reflect.set(target, name, value, receiver) | 18 | 15 | if (name === 'title') { | | 16 | + document.title = value | | 17 | + document.querySelector('head > meta[property="og:title"]').setAttribute('content', value) | 19 | 18 | windowEvent('page') | | 19 | + } else if (name === 'description') { | | 20 | + document.querySelector('head > meta[name="description"]').setAttribute('content', value) | | 21 | + document.querySelector('head > meta[property="og:description"]').setAttribute('content', value) | | 22 | + } else if (name === 'locale') { | | 23 | + document.querySelector('html').setAttribute('lang', value) | | 24 | + document.querySelector('head > meta[property="og:locale"]').setAttribute('content', value) | | 25 | + } else if (name === 'image') { | | 26 | + document.querySelector('head > meta[property="og:image"]').setAttribute('content', value) | | 27 | + } else if (name === 'canonical') { | | 28 | + canonical = (path.indexOf('//') === -1) ? router.base + value : value | | 29 | + document.querySelector('head > link[rel="canonical"]').setAttribute('href', canonical) | 20 | 30 | } | 21 | 31 | client.update() | 22 | 32 | return result | |
|