Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Svelte, Vue tests ... #12841

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/warpc/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions internal/warpc/gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
var scripts = []string{
"greet.js",
"renderkatex.js",
"buildsvelte.js",
}

func main() {
Expand All @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions internal/warpc/gen/shims.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
let performanceNowShim = () => Date.now();
export { performanceNowShim as 'performance.now' };
1,252 changes: 1,252 additions & 0 deletions internal/warpc/js/buildsvelte.bundle.js

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions internal/warpc/js/buildsvelte.js
Original file line number Diff line number Diff line change
@@ -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);
4 changes: 2 additions & 2 deletions internal/warpc/js/greet.bundle.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion internal/warpc/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"license": "ISC",
"description": "",
"devDependencies": {
"katex": "^0.16.11"
"katex": "^0.16.11",
"svelte": "^4.2.19"
}
}
30 changes: 15 additions & 15 deletions internal/warpc/js/renderkatex.bundle.js

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions internal/warpc/svelte.go
Original file line number Diff line number Diff line change
@@ -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"`
}
35 changes: 26 additions & 9 deletions internal/warpc/warpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,20 +525,30 @@ 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]
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 {
if err := d.katex.dispatcher.Close(); err != nil {
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
}
Expand All @@ -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}
func AllDispatchers(opts Options) *Dispatchers {
if opts.Runtime.Data == nil {
opts.Runtime = Binary{Name: "javy_quickjs_provider_v2", Data: quickjsWasm}
}
if katexOpts.Main.Data == nil {
katexOpts.Main = Binary{Name: "renderkatex", Data: katexWasm}

if opts.Main.Data != nil {
panic("Main.Data must be nil")
}

if katexOpts.Infof == nil {
katexOpts.Infof = func(format string, v ...any) {
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},
katex: &lazyDispatcher[KatexInput, KatexOutput]{opts: katexOpts},
svelte: &lazyDispatcher[SvelteInput, SvelteOutput]{opts: svelteOpts},
}
}
53 changes: 53 additions & 0 deletions internal/warpc/warpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -445,6 +493,11 @@ var (
Data: katexWasm,
}

svelteBinary = Binary{
Name: "buildsvelte",
Data: svelteWasm,
}

quickjsBinary = Binary{
Name: "javy_quickjs_provider_v2",
Data: quickjsWasm,
Expand Down
Binary file added internal/warpc/wasm/buildsvelte.wasm
Binary file not shown.
Binary file modified internal/warpc/wasm/greet.wasm
Binary file not shown.
Binary file modified internal/warpc/wasm/renderkatex.wasm
Binary file not shown.