@ternkit/tern 0.0.0-stage → 0.4.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/LICENSE +21 -0
- package/README.md +49 -2
- package/dist/index.d.ts +18 -0
- package/dist/index.js +15 -0
- package/dist/runtime/DefaultError.svelte +8 -0
- package/dist/runtime/DefaultError.svelte.d.ts +18 -0
- package/dist/runtime/Passthrough.svelte +8 -0
- package/dist/runtime/Passthrough.svelte.d.ts +8 -0
- package/dist/runtime/Root.svelte +28 -0
- package/dist/runtime/Root.svelte.d.ts +11 -0
- package/dist/runtime/client.d.ts +1 -0
- package/dist/runtime/client.js +2 -0
- package/dist/runtime/deferred.d.ts +29 -0
- package/dist/runtime/deferred.js +111 -0
- package/dist/runtime/forms.d.ts +39 -0
- package/dist/runtime/forms.js +91 -0
- package/dist/runtime/navigation.d.ts +55 -0
- package/dist/runtime/navigation.js +69 -0
- package/dist/runtime/router.svelte.d.ts +19 -0
- package/dist/runtime/router.svelte.js +506 -0
- package/dist/runtime/select.d.ts +38 -0
- package/dist/runtime/select.js +45 -0
- package/dist/runtime/state.svelte.d.ts +51 -0
- package/dist/runtime/state.svelte.js +62 -0
- package/dist/runtime/types.d.ts +101 -0
- package/dist/runtime/types.js +1 -0
- package/dist/server/index.d.ts +35 -0
- package/dist/server/index.js +40 -0
- package/dist/server/intl.d.ts +100 -0
- package/dist/server/intl.js +222 -0
- package/dist/server/polyfills.d.ts +2 -0
- package/dist/server/polyfills.js +47 -0
- package/dist/vite/index.d.ts +23 -0
- package/dist/vite/index.js +118 -0
- package/package.json +72 -4
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { svelte } from '@sveltejs/vite-plugin-svelte';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
/** Paths shared with the Go code generator; keep in sync with internal/codegen. */
|
|
4
|
+
const CLIENT_ENTRY = '.tern/generated/client.js';
|
|
5
|
+
const SERVER_ENTRY = '.tern/generated/server.js';
|
|
6
|
+
const CLIENT_OUT = '.tern/output/client';
|
|
7
|
+
const SERVER_OUT = '.tern/output/server';
|
|
8
|
+
/** The dev endpoint the Go app calls to server-render a page. */
|
|
9
|
+
const RENDER_PATH = '/__tern/render';
|
|
10
|
+
/**
|
|
11
|
+
* The Vite plugin for Tern apps. It includes the Svelte plugin, so it is
|
|
12
|
+
* the only plugin a Tern app needs:
|
|
13
|
+
*
|
|
14
|
+
* ```js
|
|
15
|
+
* // vite.config.js
|
|
16
|
+
* import { tern } from '@ternkit/tern/vite';
|
|
17
|
+
* import { defineConfig } from 'vite';
|
|
18
|
+
*
|
|
19
|
+
* export default defineConfig({ plugins: [tern()] });
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* `vite build` builds the client; `vite build --ssr` builds the server
|
|
23
|
+
* renderer that `tern build` embeds in the Go binary.
|
|
24
|
+
*/
|
|
25
|
+
export function tern(options = {}) {
|
|
26
|
+
let root = process.cwd();
|
|
27
|
+
const plugin = {
|
|
28
|
+
name: 'tern',
|
|
29
|
+
config(config, env) {
|
|
30
|
+
root = path.resolve(config.root ?? process.cwd());
|
|
31
|
+
const shared = {
|
|
32
|
+
appType: 'custom',
|
|
33
|
+
resolve: {
|
|
34
|
+
alias: {
|
|
35
|
+
$lib: path.join(root, 'src/lib'),
|
|
36
|
+
$routes: path.join(root, '.tern/generated/routes.js')
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
if (env.isSsrBuild) {
|
|
41
|
+
return {
|
|
42
|
+
...shared,
|
|
43
|
+
publicDir: false,
|
|
44
|
+
build: {
|
|
45
|
+
ssr: true,
|
|
46
|
+
outDir: SERVER_OUT,
|
|
47
|
+
emptyOutDir: true,
|
|
48
|
+
minify: true,
|
|
49
|
+
target: 'es2023',
|
|
50
|
+
rolldownOptions: {
|
|
51
|
+
input: path.join(root, SERVER_ENTRY),
|
|
52
|
+
output: { format: 'iife', name: '__tern_server', entryFileNames: 'server.js' }
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
// One self-contained file: the renderer runs in an embedded engine
|
|
56
|
+
// without a module loader or Node built-ins.
|
|
57
|
+
ssr: { noExternal: true },
|
|
58
|
+
// Libraries often read process.env.NODE_ENV; the engine has no
|
|
59
|
+
// process object, so replace it at build time.
|
|
60
|
+
define: { 'process.env.NODE_ENV': JSON.stringify('production') }
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
const vitePort = Number(process.env.TERN_VITE_PORT) || undefined;
|
|
64
|
+
const appPort = Number(process.env.TERN_APP_PORT) || undefined;
|
|
65
|
+
return {
|
|
66
|
+
...shared,
|
|
67
|
+
publicDir: 'static',
|
|
68
|
+
build: {
|
|
69
|
+
outDir: CLIENT_OUT,
|
|
70
|
+
emptyOutDir: true,
|
|
71
|
+
manifest: true,
|
|
72
|
+
assetsDir: '_app/immutable',
|
|
73
|
+
rolldownOptions: { input: path.join(root, CLIENT_ENTRY) }
|
|
74
|
+
},
|
|
75
|
+
server: {
|
|
76
|
+
// Bind to IPv4 explicitly: the Go app proxies to 127.0.0.1, and
|
|
77
|
+
// Node may resolve "localhost" to ::1 only.
|
|
78
|
+
host: vitePort ? '127.0.0.1' : undefined,
|
|
79
|
+
port: vitePort,
|
|
80
|
+
strictPort: vitePort !== undefined,
|
|
81
|
+
// The browser talks to the Go app, which proxies HMR to Vite.
|
|
82
|
+
ws: appPort ? { clientPort: appPort } : undefined,
|
|
83
|
+
fs: { allow: [root] }
|
|
84
|
+
},
|
|
85
|
+
optimizeDeps: {
|
|
86
|
+
entries: [CLIENT_ENTRY, 'src/**/*.svelte'],
|
|
87
|
+
exclude: ['@ternkit/tern']
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
},
|
|
91
|
+
configureServer(server) {
|
|
92
|
+
server.middlewares.use(RENDER_PATH, (req, res) => {
|
|
93
|
+
void renderInDev(server, req).then((body) => {
|
|
94
|
+
res.setHeader('Content-Type', 'application/json');
|
|
95
|
+
res.end(body);
|
|
96
|
+
}, (err) => {
|
|
97
|
+
const e = err instanceof Error ? err : new Error(String(err));
|
|
98
|
+
server.ssrFixStacktrace(e);
|
|
99
|
+
res.statusCode = 500;
|
|
100
|
+
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
|
|
101
|
+
res.end(e.stack ?? e.message);
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
async function renderInDev(server, req) {
|
|
107
|
+
if (req.method !== 'POST')
|
|
108
|
+
throw new Error('POST required');
|
|
109
|
+
const chunks = [];
|
|
110
|
+
for await (const chunk of req)
|
|
111
|
+
chunks.push(chunk);
|
|
112
|
+
const { payload, href } = JSON.parse(Buffer.concat(chunks).toString('utf8'));
|
|
113
|
+
const mod = (await server.ssrLoadModule(path.join(root, SERVER_ENTRY)));
|
|
114
|
+
return mod.render(payload, href);
|
|
115
|
+
}
|
|
116
|
+
return [...svelte(options.svelte), plugin];
|
|
117
|
+
}
|
|
118
|
+
export default tern;
|
package/package.json
CHANGED
|
@@ -1,6 +1,74 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ternkit/tern",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "Client runtime and Vite plugin for Tern: Svelte on the client, Go on the server",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/Unfield/Tern.git",
|
|
10
|
+
"directory": "packages/tern"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/Unfield/Tern",
|
|
13
|
+
"bugs": "https://github.com/Unfield/Tern/issues",
|
|
14
|
+
"keywords": [
|
|
15
|
+
"svelte",
|
|
16
|
+
"go",
|
|
17
|
+
"framework",
|
|
18
|
+
"vite",
|
|
19
|
+
"router"
|
|
20
|
+
],
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"svelte": "./dist/index.js",
|
|
25
|
+
"default": "./dist/index.js"
|
|
26
|
+
},
|
|
27
|
+
"./client": {
|
|
28
|
+
"types": "./dist/runtime/client.d.ts",
|
|
29
|
+
"svelte": "./dist/runtime/client.js",
|
|
30
|
+
"default": "./dist/runtime/client.js"
|
|
31
|
+
},
|
|
32
|
+
"./server": {
|
|
33
|
+
"types": "./dist/server/index.d.ts",
|
|
34
|
+
"svelte": "./dist/server/index.js",
|
|
35
|
+
"default": "./dist/server/index.js"
|
|
36
|
+
},
|
|
37
|
+
"./polyfills": {
|
|
38
|
+
"types": "./dist/server/polyfills.d.ts",
|
|
39
|
+
"default": "./dist/server/polyfills.js"
|
|
40
|
+
},
|
|
41
|
+
"./vite": {
|
|
42
|
+
"types": "./dist/vite/index.d.ts",
|
|
43
|
+
"default": "./dist/vite/index.js"
|
|
44
|
+
},
|
|
45
|
+
"./package.json": "./package.json"
|
|
46
|
+
},
|
|
47
|
+
"files": [
|
|
48
|
+
"dist",
|
|
49
|
+
"!dist/**/*.test.*"
|
|
50
|
+
],
|
|
51
|
+
"scripts": {
|
|
52
|
+
"build": "svelte-package -i src -o dist",
|
|
53
|
+
"prepare": "svelte-package -i src -o dist",
|
|
54
|
+
"typecheck": "svelte-check --tsconfig ./tsconfig.json --fail-on-warnings",
|
|
55
|
+
"test": "vitest run"
|
|
56
|
+
},
|
|
57
|
+
"peerDependencies": {
|
|
58
|
+
"svelte": "^5.46.4",
|
|
59
|
+
"vite": "^8.0.0"
|
|
60
|
+
},
|
|
61
|
+
"dependencies": {
|
|
62
|
+
"@sveltejs/vite-plugin-svelte": "^7.3.1",
|
|
63
|
+
"core-js": "^3.51.0"
|
|
64
|
+
},
|
|
65
|
+
"devDependencies": {
|
|
66
|
+
"@sveltejs/package": "^3.0.2",
|
|
67
|
+
"@types/node": "^24.0.0",
|
|
68
|
+
"svelte": "^5.57.3",
|
|
69
|
+
"svelte-check": "^4.7.8",
|
|
70
|
+
"typescript": "^6.0.0",
|
|
71
|
+
"vite": "^8.3.7",
|
|
72
|
+
"vitest": "^5.1.0"
|
|
73
|
+
}
|
|
74
|
+
}
|