forked from gohugoio/hugo
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
While very useful on its own (and combined with the passthrough render hooks), this also serves as a proof of concept of using WASI (WebAssembly System Interface) modules in Hugo. This will be marked _experimental_ in the documentation. Not because it will be removed or changed in a dramatic way, but we need to think a little more how to best set up/configure similar services, define where these WASM files gets stored, maybe we can allow user provided WASM files plugins via Hugo Modules mounts etc. See these issues for more context: * gohugoio#12736 * gohugoio#12737 See gohugoio#11927
- Loading branch information
Showing
26 changed files
with
1,604 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# TODO1 clean up when done. | ||
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 | ||
touch warpc_test.go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
//go:generate go run main.go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/evanw/esbuild/pkg/api" | ||
) | ||
|
||
var scripts = []string{ | ||
"greet.js", | ||
"renderkatex.js", | ||
} | ||
|
||
func main() { | ||
for _, script := range scripts { | ||
filename := filepath.Join("../js", script) | ||
err := buildJSBundle(filename) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
} | ||
|
||
func buildJSBundle(filename string) error { | ||
minify := true | ||
result := api.Build( | ||
api.BuildOptions{ | ||
EntryPoints: []string{filename}, | ||
Bundle: true, | ||
MinifyWhitespace: minify, | ||
MinifyIdentifiers: minify, | ||
MinifySyntax: minify, | ||
Target: api.ES2020, | ||
Outfile: strings.Replace(filename, ".js", ".bundle.js", 1), | ||
SourceRoot: "../js", | ||
}) | ||
|
||
if len(result.Errors) > 0 { | ||
return fmt.Errorf("build failed: %v", result.Errors) | ||
} | ||
if len(result.OutputFiles) != 1 { | ||
return fmt.Errorf("expected 1 output file, got %d", len(result.OutputFiles)) | ||
} | ||
|
||
of := result.OutputFiles[0] | ||
if err := os.WriteFile(filepath.FromSlash(of.Path), of.Contents, 0o644); err != nil { | ||
return fmt.Errorf("write file failed: %v", err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules/ | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Read JSONL from stdin. | ||
export function readInput(handle) { | ||
const buffSize = 1024; | ||
let currentLine = []; | ||
const buffer = new Uint8Array(buffSize); | ||
|
||
// Read all the available bytes | ||
while (true) { | ||
// Stdin file descriptor | ||
const fd = 0; | ||
let bytesRead = 0; | ||
try { | ||
bytesRead = Javy.IO.readSync(fd, buffer); | ||
} catch (e) { | ||
// IO.readSync fails with os error 29 when stdin closes. | ||
if (e.message.includes('os error 29')) { | ||
break; | ||
} | ||
throw new Error('Error reading from stdin'); | ||
} | ||
|
||
if (bytesRead < 0) { | ||
throw new Error('Error reading from stdin'); | ||
break; | ||
} | ||
|
||
if (bytesRead === 0) { | ||
break; | ||
} | ||
|
||
currentLine = [...currentLine, ...buffer.subarray(0, bytesRead)]; | ||
|
||
// Split array into chunks by newline. | ||
let i = 0; | ||
for (let j = 0; i < currentLine.length; i++) { | ||
if (currentLine[i] === 10) { | ||
const chunk = currentLine.splice(j, i + 1); | ||
const arr = new Uint8Array(chunk); | ||
const json = JSON.parse(new TextDecoder().decode(arr)); | ||
handle(json); | ||
j = i + 1; | ||
} | ||
} | ||
// Remove processed data. | ||
currentLine = currentLine.slice(i); | ||
} | ||
} | ||
|
||
// Write JSONL to stdout | ||
export function writeOutput(output) { | ||
const encodedOutput = new TextEncoder().encode(JSON.stringify(output) + '\n'); | ||
const buffer = new Uint8Array(encodedOutput); | ||
// Stdout file descriptor | ||
const fd = 1; | ||
Javy.IO.writeSync(fd, buffer); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { readInput, writeOutput } from './common'; | ||
|
||
const greet = function (input) { | ||
writeOutput({ header: input.header, data: { greeting: 'Hello ' + input.data.name + '!' } }); | ||
}; | ||
|
||
console.log('Greet module loaded'); | ||
|
||
readInput(greet); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "js", | ||
"version": "1.0.0", | ||
"main": "greet.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"description": "", | ||
"devDependencies": { | ||
"katex": "^0.16.11" | ||
} | ||
} |
Oops, something went wrong.