Svelte, Vue tests ...

This commit is contained in:
Bjørn Erik Pedersen 2024-09-09 10:26:46 +02:00
parent 84ee00bbc2
commit 2704c7178c
No known key found for this signature in database
14 changed files with 1405 additions and 28 deletions

View file

@ -2,4 +2,5 @@
go generate ./gen
javy compile js/greet.bundle.js -d -o wasm/greet.wasm
javy compile js/renderkatex.bundle.js -d -o wasm/renderkatex.wasm
javy compile js/buildsvelte.bundle.js -d -o wasm/buildsvelte.wasm
touch warpc_test.go

View file

@ -27,6 +27,7 @@ import (
var scripts = []string{
"greet.js",
"renderkatex.js",
"buildsvelte.js",
}
func main() {
@ -45,6 +46,7 @@ func buildJSBundle(filename string) error {
api.BuildOptions{
EntryPoints: []string{filename},
Bundle: true,
Inject: []string{"./shims.js"},
MinifyWhitespace: minify,
MinifyIdentifiers: minify,
MinifySyntax: minify,

View file

@ -0,0 +1,2 @@
let performanceNowShim = () => Date.now();
export { performanceNowShim as 'performance.now' };

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,19 @@
import { readInput, writeOutput } from './common';
import { compile } from 'svelte/compiler';
const build = function (input) {
const data = input.data; //
const source = data.source;
const opts = data.options || {}; // //
const header = input.header;
try {
let { js, css, warnings } = compile(source);
writeOutput({ header: header, data: { result: js.code } });
} catch (e) {
header.err = e.message;
writeOutput({ header: header });
}
};
readInput(build);

View file

@ -1,2 +1,2 @@
(()=>{function s(r){let e=[],c=new Uint8Array(1024);for(;;){let n=0;try{n=Javy.IO.readSync(0,c)}catch(o){if(o.message.includes("os error 29"))break;throw new Error("Error reading from stdin")}if(n<0)throw new Error("Error reading from stdin");if(n===0)break;if(e=[...e,...c.subarray(0,n)],!e.includes(10))continue;let t=0;for(let o=0;t<e.length;t++)if(e[t]===10){let u=e.splice(o,t+1),d=new Uint8Array(u),a;try{a=JSON.parse(new TextDecoder().decode(d))}catch(l){throw new Error(`Error parsing JSON '${new TextDecoder().decode(d)}' from stdin: ${l.message}`)}r(a),o=t+1}e=e.slice(t)}}function f(r){let i=new TextEncoder().encode(JSON.stringify(r)+`
`),e=new Uint8Array(i);Javy.IO.writeSync(1,e)}var w=function(r){f({header:r.header,data:{greeting:"Hello "+r.data.name+"!"}})};console.log("Greet module loaded");s(w);})();
(()=>{function s(r){let e=[],a=new Uint8Array(1024);for(;;){let n=0;try{n=Javy.IO.readSync(0,a)}catch(o){if(o.message.includes("os error 29"))break;throw new Error("Error reading from stdin")}if(n<0)throw new Error("Error reading from stdin");if(n===0)break;if(e=[...e,...a.subarray(0,n)],!e.includes(10))continue;let t=0;for(let o=0;t<e.length;t++)if(e[t]===10){let u=e.splice(o,t+1),i=new Uint8Array(u),d;try{d=JSON.parse(new TextDecoder().decode(i))}catch(w){throw new Error(`Error parsing JSON '${new TextDecoder().decode(i)}' from stdin: ${w.message}`)}r(d),o=t+1}e=e.slice(t)}}function f(r){let c=new TextEncoder().encode(JSON.stringify(r)+`
`),e=new Uint8Array(c);Javy.IO.writeSync(1,e)}var p=function(r){f({header:r.header,data:{greeting:"Hello "+r.data.name+"!"}})};console.log("Greet module loaded");s(p);})();

View file

@ -9,6 +9,7 @@
"license": "ISC",
"description": "",
"devDependencies": {
"katex": "^0.16.11"
"katex": "^0.16.11",
"svelte": "^4.2.19"
}
}

File diff suppressed because one or more lines are too long

30
internal/warpc/svelte.go Normal file
View file

@ -0,0 +1,30 @@
// Copyright 2024 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package warpc
import (
_ "embed"
)
//go:embed wasm/buildsvelte.wasm
var svelteWasm []byte
type SvelteInput struct {
Source string `json:"source"`
Options map[string]any `json:"options"`
}
type SvelteOutput struct {
Result string `json:"result"`
}

View file

@ -526,12 +526,17 @@ func (d *lazyDispatcher[Q, R]) start() (Dispatcher[Q, R], error) {
// Dispatchers holds all the dispatchers for the warpc package.
type Dispatchers struct {
katex *lazyDispatcher[KatexInput, KatexOutput]
svelte *lazyDispatcher[SvelteInput, SvelteOutput]
}
func (d *Dispatchers) Katex() (Dispatcher[KatexInput, KatexOutput], error) {
return d.katex.start()
}
func (d *Dispatchers) Svelte() (Dispatcher[SvelteInput, SvelteOutput], error) {
return d.svelte.start()
}
func (d *Dispatchers) Close() error {
var errs []error
if d.katex.started {
@ -539,6 +544,11 @@ func (d *Dispatchers) Close() error {
errs = append(errs, err)
}
}
if d.svelte.started {
if err := d.svelte.dispatcher.Close(); err != nil {
errs = append(errs, err)
}
}
if len(errs) == 0 {
return nil
}
@ -548,21 +558,28 @@ func (d *Dispatchers) Close() error {
// AllDispatchers creates all the dispatchers for the warpc package.
// Note that the individual dispatchers are started lazily.
// Remember to call Close on the returned Dispatchers when done.
func AllDispatchers(katexOpts Options) *Dispatchers {
if katexOpts.Runtime.Data == nil {
katexOpts.Runtime = Binary{Name: "javy_quickjs_provider_v2", Data: quickjsWasm}
}
if katexOpts.Main.Data == nil {
katexOpts.Main = Binary{Name: "renderkatex", Data: katexWasm}
func AllDispatchers(opts Options) *Dispatchers {
if opts.Runtime.Data == nil {
opts.Runtime = Binary{Name: "javy_quickjs_provider_v2", Data: quickjsWasm}
}
if katexOpts.Infof == nil {
katexOpts.Infof = func(format string, v ...any) {
if opts.Main.Data != nil {
panic("Main.Data must be nil")
}
if opts.Infof == nil {
opts.Infof = func(format string, v ...any) {
// noop
}
}
katexOpts := opts
katexOpts.Main = Binary{Name: "renderkatex", Data: katexWasm}
svelteOpts := opts
svelteOpts.Main = Binary{Name: "buildsvelte", Data: svelteWasm}
return &Dispatchers{
katex: &lazyDispatcher[KatexInput, KatexOutput]{opts: katexOpts},
svelte: &lazyDispatcher[SvelteInput, SvelteOutput]{opts: svelteOpts},
}
}

View file

@ -225,6 +225,54 @@ func TestKatexParallel(t *testing.T) {
wg.Wait()
}
func TestSvelte(t *testing.T) {
c := qt.New(t)
todo := `
<script>
let checked = false;
</script>
<input type="checkbox" {checked}>
`
opts := Options{
PoolSize: 8,
Runtime: quickjsBinary,
Main: svelteBinary,
Infof: func(format string, v ...any) {
fmt.Printf(format, v...)
},
}
d, err := Start[SvelteInput, SvelteOutput](opts)
c.Assert(err, qt.IsNil)
defer d.Close()
message := Message[SvelteInput]{
Header: Header{
Version: currentVersion,
ID: uint32(32),
},
Data: SvelteInput{
Source: todo,
Options: map[string]any{
"opts": map[string]any{},
},
},
}
ctx := context.Background()
result, err := d.Execute(ctx, message)
c.Assert(err, qt.IsNil)
c.Assert(result.Header.Err, qt.Equals, "")
c.Assert(result.GetID(), qt.Equals, message.GetID())
fmt.Println("====>", result.Data.Result)
}
func BenchmarkExecuteKatex(b *testing.B) {
opts := Options{
Runtime: quickjsBinary,
@ -445,6 +493,11 @@ var (
Data: katexWasm,
}
svelteBinary = Binary{
Name: "buildsvelte",
Data: svelteWasm,
}
quickjsBinary = Binary{
Name: "javy_quickjs_provider_v2",
Data: quickjsWasm,

Binary file not shown.

Binary file not shown.

Binary file not shown.