zitejs 0.10.0 → 0.10.1-device.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.
@@ -55,6 +55,10 @@ const REFUSED = [
55
55
  pattern: /^zitejs\/(api|caller)(\/|$)/,
56
56
  fix: "it's classic's: a Start app calls its own server functions directly, from a loader or an event handler",
57
57
  },
58
+ {
59
+ pattern: /^zitejs\/query(\/|$)/,
60
+ fix: "it's classic's: a Start app's router already provides its query client",
61
+ },
58
62
  {
59
63
  pattern: /^zitejs\/backend(\/|$)/,
60
64
  fix: "it's classic's server module: import ZiteError and the rest from zitejs/start",
@@ -1,4 +1,4 @@
1
- // zitejs/backend, zitejs/backend/base and zitejs/caller as a Start app's types see them, under the `zite-start`
2
- // condition: classic's server modules, with nothing in them for a Start app. ZiteError and the rest come from
3
- // zitejs/start, and server functions are called directly.
1
+ // zitejs/backend, zitejs/backend/base, zitejs/caller and zitejs/query as a Start app's types see them, under the
2
+ // `zite-start` condition: classic's modules, with nothing in them for a Start app. ZiteError and the rest come from
3
+ // zitejs/start, server functions are called directly, and the router provides the query client.
4
4
  export {};
@@ -1 +1 @@
1
- export declare const ZITEJS_VERSION = "0.10.0";
1
+ export declare const ZITEJS_VERSION = "0.10.1-device.0";
@@ -1,2 +1,2 @@
1
1
  // Written by scripts/write-version.mjs from package.json.
2
- export const ZITEJS_VERSION = '0.10.0';
2
+ export const ZITEJS_VERSION = '0.10.1-device.0';
@@ -1,5 +1,6 @@
1
1
  import { parse } from '@babel/parser';
2
2
  import MagicString from 'magic-string';
3
+ import { readFileSync } from 'fs';
3
4
  import path from 'path';
4
5
  import { walk } from 'estree-walker';
5
6
  import { domTagNames } from './domTagNames.js';
@@ -63,11 +64,51 @@ function classifyElement(name) {
63
64
  }
64
65
  return null;
65
66
  }
67
+ /**
68
+ * What zitejs/query needs to keep data on the device, in a built page: an id
69
+ * for this build, so data an older build saved is never shown, and the app's
70
+ * `deviceCache` from `zite.config.json` beside the vite config. Only `"disk"`
71
+ * keeps anything: an app that hasn't chosen, or whose config can't be read,
72
+ * keeps nothing.
73
+ */
74
+ const deviceCacheTags = (appDir, build) => {
75
+ let deviceCache;
76
+ try {
77
+ deviceCache = JSON.parse(readFileSync(path.join(appDir, 'zite.config.json'), 'utf8')).deviceCache;
78
+ }
79
+ catch { }
80
+ return [
81
+ { tag: 'meta', attrs: { name: 'zite-build', content: build } },
82
+ ...(deviceCache === 'disk'
83
+ ? []
84
+ : [
85
+ {
86
+ tag: 'meta',
87
+ attrs: { name: 'zite-device-cache', content: 'memory' },
88
+ },
89
+ ]),
90
+ ];
91
+ };
66
92
  export function ziteId() {
67
93
  const cwd = process.cwd();
94
+ let appDir = cwd;
95
+ let building = false;
96
+ // One per build, shared by every page of it.
97
+ const build = `${Date.now().toString(36)}${Math.random().toString(36).slice(2, 8)}`;
68
98
  return {
69
99
  name: 'vite-plugin-zite-id',
70
100
  enforce: 'pre',
101
+ configResolved(config) {
102
+ // Beside the vite config, as the app's own config reads zite.config.json:
103
+ // the build's cwd isn't always the app's directory.
104
+ appDir = config.configFile
105
+ ? path.dirname(config.configFile)
106
+ : config.root;
107
+ building = config.command === 'build';
108
+ },
109
+ transformIndexHtml() {
110
+ return building ? deviceCacheTags(appDir, build) : [];
111
+ },
71
112
  async transform(code, id) {
72
113
  // TanStack's route code splitting moves a route's component into its own module, `route.tsx?tsr-split=…`.
73
114
  const file = id.split('?')[0];
@@ -1,3 +1,5 @@
1
+ import { mkdtempSync, writeFileSync } from 'fs';
2
+ import { tmpdir } from 'os';
1
3
  import path from 'path';
2
4
  import { describe, expect, it } from 'vitest';
3
5
  import { ziteId } from './index.js';
@@ -110,3 +112,37 @@ describe('ziteId', () => {
110
112
  expect(await transform(source)).toBe(source);
111
113
  });
112
114
  });
115
+ describe("ziteId's tags for zitejs/query's device cache", () => {
116
+ const plugin = (command, config) => {
117
+ const dir = mkdtempSync(path.join(tmpdir(), 'zite-app-'));
118
+ if (config) {
119
+ writeFileSync(path.join(dir, 'zite.config.json'), JSON.stringify(config));
120
+ }
121
+ const instance = ziteId();
122
+ instance.configResolved({
123
+ command,
124
+ configFile: path.join(dir, 'vite.config.ts'),
125
+ root: process.cwd(),
126
+ });
127
+ return () => Object.fromEntries(instance.transformIndexHtml().map(t => [
128
+ t.attrs?.name,
129
+ t.attrs?.content,
130
+ ]));
131
+ };
132
+ it('keeps data on the device only for an app that chose disk', () => {
133
+ expect(plugin('build', { deviceCache: 'disk' })()['zite-device-cache']).toBeUndefined();
134
+ expect(plugin('build', { deviceCache: 'memory' })()['zite-device-cache']).toBe('memory');
135
+ expect(plugin('build', { id: 'app' })()['zite-device-cache']).toBe('memory');
136
+ expect(plugin('build')()['zite-device-cache']).toBe('memory');
137
+ });
138
+ it('gives every page of a build the same id, and each build its own', () => {
139
+ const pages = plugin('build', { deviceCache: 'disk' });
140
+ const id = pages()['zite-build'];
141
+ expect(id).toMatch(/\w{6,}/);
142
+ expect(pages()['zite-build']).toBe(id);
143
+ expect(plugin('build', { deviceCache: 'disk' })()['zite-build']).not.toBe(id);
144
+ });
145
+ it('tags nothing on a dev server', () => {
146
+ expect(plugin('serve', { deviceCache: 'disk' })()).toEqual({});
147
+ });
148
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zitejs",
3
- "version": "0.10.0",
3
+ "version": "0.10.1-device.0",
4
4
  "description": "The Zite framework — build apps on Zite Database",
5
5
  "type": "module",
6
6
  "sideEffects": [
@@ -28,6 +28,15 @@
28
28
  "require": "./dist/cjs/caller/index.js",
29
29
  "default": "./dist/esm/caller/index.js"
30
30
  },
31
+ "./query": {
32
+ "zite-start": {
33
+ "types": "./dist/esm/start/classicOnly.d.ts"
34
+ },
35
+ "types": "./dist/esm/query/index.d.ts",
36
+ "import": "./dist/esm/query/index.js",
37
+ "require": "./dist/cjs/query/index.js",
38
+ "default": "./dist/esm/query/index.js"
39
+ },
31
40
  "./runtime": {
32
41
  "types": "./dist/esm/runtime/index.d.ts",
33
42
  "import": "./dist/esm/runtime/index.js",
@@ -168,7 +177,7 @@
168
177
  },
169
178
  "scripts": {
170
179
  "build": "node scripts/write-version.mjs && tsc && tsc -p tsconfig.cjs.json && echo '{\"type\":\"commonjs\"}' > dist/cjs/package.json",
171
- "test": "vitest run",
180
+ "test": "vitest run && vitest run --config vitest.query-5749.config.ts",
172
181
  "test:start": "node tests/start/run-contract.mjs",
173
182
  "test:start:sdk": "node tests/start/sdk/run.mjs",
174
183
  "test:watch": "vitest",
@@ -285,6 +294,7 @@
285
294
  "openai": "6.16.0",
286
295
  "oxfmt": "0.73.0",
287
296
  "react": "^19.0.0",
297
+ "react-query-5749": "npm:@tanstack/react-query@5.74.9",
288
298
  "typescript": "^5.4.0",
289
299
  "vite": "^8.3.4",
290
300
  "vitest": "^4.1.12"