Skip to content

Commit

Permalink
Compiler generator.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimafisk committed May 31, 2023
1 parent 4422a50 commit 94f0856
Show file tree
Hide file tree
Showing 252 changed files with 92 additions and 78,792 deletions.
2 changes: 1 addition & 1 deletion cmd/build/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func Client(buildPath string, coreFS embed.FS, compilerFS embed.FS) error {
compiledComponentCounter := 0

// Get transformed svelte compiler code from embedded filesystem.
compiler, err := compilerFS.ReadFile("compiler/compiler.js")
compiler, err := compilerFS.ReadFile("compiler/generated/compiler.js")
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/build/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func compileSvelte(ctx *v8go.Context, SSRctx *v8go.Context, layoutPath string,
}

// Compile component with Svelte.
scriptDOM := fmt.Sprintf(`;__svelte__.compile({ path: %q, code: %q, target: "dom", css: false })`, layoutPath, componentStr)
scriptDOM := fmt.Sprintf(`;__svelte__.compile({ path: %q, code: %q, target: "dom", css: false, name: "Component" })`, layoutPath, componentStr)
resultDOM, err := ctx.RunScript(scriptDOM, "compile_svelte")
if err != nil {
return fmt.Errorf("\nDOM: Can't compile component file %s\n%w", layoutPath, err)
Expand Down Expand Up @@ -84,7 +84,7 @@ func compileSvelte(ctx *v8go.Context, SSRctx *v8go.Context, layoutPath string,
}

// Get Server Side Rendered (SSR) JS.
scriptSSR := fmt.Sprintf(`;__svelte__.compile({ path: %q, code: %q, target: "ssr", css: false})`, layoutPath, componentStr)
scriptSSR := fmt.Sprintf(`;__svelte__.compile({ path: %q, code: %q, target: "ssr", css: false, name: "Component" })`, layoutPath, componentStr)
resultSSR, err := ctx.RunScript(scriptSSR, "compile_svelte")
if err != nil {
return fmt.Errorf("\nSSR: Can't compile component file %s\n%w", layoutPath, err)
Expand Down
43 changes: 43 additions & 0 deletions defaults/compiler/compiler_entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { compile as compileSvelte } from "svelte/compiler"

type Input = {
code: string
path: string
target: "ssr" | "dom"
dev: boolean
css: boolean
name: string
}

// Capitalized for Go
type Output =
| {
JS: string
CSS: string
}
| {
Error: {
Path: string
Name: string
Message: string
Stack?: string
}
}

// Compile svelte code
export function compile(input: Input): string {
const { code, path, target, dev, css, name } = input
const svelte = compileSvelte(code, {
name: name,
filename: path,
generate: target,
hydratable: true,
format: "esm",
dev: dev,
css: css,
})
return JSON.stringify({
CSS: svelte.css.code,
JS: svelte.js.code,
} as Output)
}
3 changes: 3 additions & 0 deletions defaults/compiler/compiler_generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package compiler

//go:generate go run github.com/evanw/esbuild/cmd/esbuild compiler_entry.ts --format=iife --global-name=__svelte__ --bundle --platform=node --inject:shimssr.ts --external:url --outfile=generated/compiler.js --log-level=warning
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ var __svelte__ = (() => {
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// compiler.ts
var compiler_exports = {};
__export(compiler_exports, {
// compiler_entry.ts
var compiler_entry_exports = {};
__export(compiler_entry_exports, {
compile: () => compile2
});

Expand All @@ -49,7 +49,7 @@ var __svelte__ = (() => {
}
};

// node_modules/svelte/compiler.mjs
// ../node_modules/svelte/compiler.mjs
var now = typeof process !== "undefined" && process.hrtime ? () => {
const t = process.hrtime();
return t[0] * 1e3 + t[1] / 1e6;
Expand Down Expand Up @@ -43426,11 +43426,11 @@ ${frame}`
return component.generate(result);
}

// compiler.ts
// compiler_entry.ts
function compile2(input) {
const { code, path, target, dev, css } = input;
const { code, path, target, dev, css, name: name2 } = input;
const svelte = compile(code, {
name: "Component",
name: name2,
filename: path,
generate: target,
hydratable: true,
Expand All @@ -43443,5 +43443,5 @@ ${frame}`
JS: svelte.js.code
});
}
return __toCommonJS(compiler_exports);
return __toCommonJS(compiler_entry_exports);
})();
30 changes: 30 additions & 0 deletions defaults/compiler/shimssr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Shim for getting the svelte compiler to run in a V8 isolate.
*/

// URL shim for the browser
// TODO: properly shim URL
export class URL {
constructor(url: string) {
console.log(url)
}
}

// TODO: properly shim performance.now()
export const self = {
performance: {
now(): number {
return 0
},
},
}

// In development mode when compiling for the browser we hit this codepath:
// https://github.com/Rich-Harris/magic-string/blob/8f666889136ac2580356e48610b3ac95c276191e/src/SourceMap.js#L3-L10
// Since we're running in a V8 isolate, we don't have a window or a Buffer.
// TODO: shim btoa properly
export const window = {
btoa: (data: string): string => {
return ""
},
}
2 changes: 1 addition & 1 deletion defaults/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package defaults

import "embed"

//go:embed all:compiler/*
//go:embed compiler/generated/compiler.js
var CompilerFS embed.FS

//go:embed all:core/*
Expand Down
Loading

0 comments on commit 94f0856

Please sign in to comment.