Skip to content

Commit

Permalink
WIP: move v8go rendering to data_source.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimafisk committed Jul 14, 2020
1 parent 258b7db commit 4692f20
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 30 deletions.
6 changes: 3 additions & 3 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ func Build() {
// Directly copy static assets to the build dir.
build.AssetsCopy(buildPath)

// Build JSON from "content/" directory.
staticBuildStr, allNodesStr := build.DataSource(buildPath, siteConfig)

// Prep the client SPA.
build.Client(buildPath)

// Build JSON from "content/" directory.
staticBuildStr, allNodesStr := build.DataSource(buildPath, siteConfig)

// Run the build.js script using user local NodeJS.
build.ExecNode(staticBuildStr, allNodesStr)

Expand Down
34 changes: 8 additions & 26 deletions cmd/build/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"rogchap.com/v8go"
)

// SSRComponents holds the server side rendered code for all svelte files in the layouts/ dir.
var SSRComponents map[string]string

// Client builds the SPA.
Expand All @@ -28,6 +29,9 @@ func Client(buildPath string) {
// Set up counter for logging output.
compiledComponentCounter := 0

// Initialize map to hold SSR code used in data_source.go.
SSRComponents = make(map[string]string)

// Get svelte compiler code from node_modules.
compiler, err := ioutil.ReadFile("node_modules/svelte/compiler.js")
if err != nil {
Expand Down Expand Up @@ -125,30 +129,8 @@ func compileSvelte(ctx *v8go.Context, layoutPath string, destFile string, styleP
// Remove static export statements.
ssrStr = reStaticExport.ReplaceAllString(ssrStr, "")

// Get static import statement.
createSsrComponent, err := ioutil.ReadFile("node_modules/svelte/internal/index.js")
if err != nil {
fmt.Printf("Can't read node_modules/svelte/internal/index.js: %v", err)
}
createStr := string(createSsrComponent)
ctx1, _ := v8go.NewContext(nil)
ctx1.RunScript(createStr, "create_ssr")
ctx1.RunScript("var exports = {};", "create_ssr")
ctx1.RunScript(ssrStr, "create_ssr")
ctx1.RunScript("var { html, css: staticCss} = Component.render();", "create_ssr")
staticHTML, err := ctx1.RunScript("html;", "create_ssr")
if err != nil {
fmt.Printf("V8go could not execute js default: %v\n", err)
}
fmt.Println(staticHTML)
staticCSS, err := ctx1.RunScript("staticCss.code;", "create_ssr")
if err != nil {
fmt.Printf("V8go could not execute js default: %v\n", err)
}
fmt.Println(staticCSS)
ssrJsBytes := []byte(ssrJsCode.String())
ssrJsWriteErr := ioutil.WriteFile(destFile, ssrJsBytes, 0755)
if ssrJsWriteErr != nil {
fmt.Printf("Unable to write SSR file: %v", ssrJsWriteErr)
}
SSRComponents[layoutPath] = ssrStr
//fmt.Println(layoutPath)
//fmt.Println(SSRComponents["layout/global/html.svelte"])

}
107 changes: 106 additions & 1 deletion cmd/build/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"strconv"
"strings"
"time"

"rogchap.com/v8go"
)

// DataSource builds json list from "content/" directory.
Expand All @@ -30,6 +32,19 @@ func DataSource(buildPath string, siteConfig readers.SiteConfig) (string, string
staticBuildStr := "["
allNodesStr := "["

// Use v8go and add create_ssr_component() function.
// TODO: Need to update node_modules/svelte/internal/index.js
// - line 1320: let on_destroy;
// - line 1337: let on_destroy = [];
createSsrComponent, npmReadErr := ioutil.ReadFile("node_modules/svelte/internal/index.js")
if npmReadErr != nil {
fmt.Printf("Can't read node_modules/svelte/internal/index.js: %v", npmReadErr)
}
createStr := string(createSsrComponent)
ctx1, _ := v8go.NewContext(nil)
ctx1.RunScript(createStr, "create_ssr")
ctx1.RunScript("var exports = {};", "create_ssr")

// Start the new nodes.js file.
err := ioutil.WriteFile(nodesJSPath, []byte(`const nodes = [`), 0755)
if err != nil {
Expand Down Expand Up @@ -132,6 +147,24 @@ func DataSource(buildPath string, siteConfig readers.SiteConfig) (string, string
log.Println(err)
}

// START
ctx1.RunScript(SSRComponents["layout/content/"+contentType+".svelte"], "create_ssr")
// TODO: Need to get full allNodes obj (don't reuse nodeDetailsStr) for props.
//fmt.Println(SSRComponents["layout/content/"+contentType+".svelte"])
//ctx1.RunScript("var props = {route: '"+SSRComponents["layout/content/"+contentType+".svelte"]+"', node: '"+nodeDetailsStr+"', allNodes: '"+nodeDetailsStr+"'};", "create_ssr")
//renderComponent, compErr := ctx1.RunScript("Component;", "create_ssr")
/*
renderComponent, compErr := ctx1.RunScript("JSON.stringify(Component);", "create_ssr")
if compErr != nil {
fmt.Printf("Could not get component for props: %v\n", compErr)
}
fmt.Println(renderComponent.String())
*/
//test, terr := ctx1.RunScript("var props = {route: 'thing'};", "create_ssr")
//test, terr := ctx1.RunScript("var props = {route: '"+SSRComponents["layout/content/"+contentType+".svelte"]+"', node: '"+nodeDetailsStr+"', allNodes: '"+nodeDetailsStr+"'};", "create_ssr")
//test, terr := ctx1.RunScript("var props = {route: "+renderComponent.String()+", node: '"+nodeDetailsStr+"', allNodes: '"+nodeDetailsStr+"'};", "create_ssr")
//test, terr := ctx1.RunScript("var props = {route: '"+renderComponent.String()+"'};", "create_ssr")
//test, terr := ctx1.RunScript("var props = {route: Component};", "create_ssr")
// Need to encode html so it can be send as string to NodeJS in exec.Command.
encodedNodeDetails := nodeDetailsStr
// Remove newlines.
Expand All @@ -143,7 +176,79 @@ func DataSource(buildPath string, siteConfig readers.SiteConfig) (string, string
// Reduce extra whitespace to a single space.
reS := regexp.MustCompile(`\s+`)
encodedNodeDetails = reS.ReplaceAllString(encodedNodeDetails, " ")

//fmt.Println(encodedNodeDetails)
test, terr := ctx1.RunScript("var props = {route: Component, node: "+encodedNodeDetails+", allNodes: "+encodedNodeDetails+"};", "create_ssr")
if terr != nil {
fmt.Printf("Could not create props: %v\n", terr)
}
fmt.Println(test)
/*
props, perr := ctx1.RunScript("JSON.stringify(props);", "create_ssr")
if perr != nil {
fmt.Printf("Bad props: %v\n", perr)
}
fmt.Println(props)
*/
/*
test, terr := ctx1.RunScript("props;", "create_ssr")
if terr != nil {
fmt.Printf("props: %v\n", terr)
}
fmt.Println(test)
*/
//fmt.Println(SSRComponents["layout/global/html.svelte"])
//ctx1.RunScript(SSRComponents["layout/global/html.svelte"], "create_ssr")
htmlComponent := strings.ReplaceAll(SSRComponents["layout/global/html.svelte"], "Component", "htmlComponent")
//htmlComponent = strings.ReplaceAll(htmlComponent, "css", "htmlCSS")
htmlComponent = strings.ReplaceAll(htmlComponent, "const", "var")
htmlL, herr := ctx1.RunScript(htmlComponent, "create_ssr")
if herr != nil {
fmt.Printf("Can't add html Component: %v\n", herr)
}
fmt.Println(htmlL)
//ctx1.RunScript("var { html, css: staticCss} = Component.render(props);", "create_ssr")
render, rerr := ctx1.RunScript("var { html, css: staticCss} = htmlComponent.render(props);", "create_ssr")
if rerr != nil {
fmt.Printf("Can't render htmlComponent: %v\n", rerr)
}
fmt.Println(render)
staticHTML, err := ctx1.RunScript("html;", "create_ssr")
if err != nil {
fmt.Printf("V8go could not execute js default: %v\n", err)
}
fmt.Println(staticHTML.String())
htmlBytes := []byte(staticHTML.String())
htmlWriteErr := ioutil.WriteFile(destPath, htmlBytes, 0755)
if htmlWriteErr != nil {
fmt.Printf("Unable to write SSR file: %v\n", htmlWriteErr)
}
/*
staticCSS, err := ctx1.RunScript("staticCss.code;", "create_ssr")
if err != nil {
fmt.Printf("V8go could not execute js default: %v\n", err)
}
fmt.Println(staticCSS)
ssrJsBytes := []byte(ssrJsCode.String())
ssrJsWriteErr := ioutil.WriteFile(destFile, ssrJsBytes, 0755)
if ssrJsWriteErr != nil {
fmt.Printf("Unable to write SSR file: %v", ssrJsWriteErr)
}
*/
// END

/*
// Need to encode html so it can be send as string to NodeJS in exec.Command.
encodedNodeDetails := nodeDetailsStr
// Remove newlines.
reN := regexp.MustCompile(`\r?\n`)
encodedNodeDetails = reN.ReplaceAllString(encodedNodeDetails, " ")
// Remove tabs.
reT := regexp.MustCompile(`\t`)
encodedNodeDetails = reT.ReplaceAllString(encodedNodeDetails, " ")
// Reduce extra whitespace to a single space.
reS := regexp.MustCompile(`\s+`)
encodedNodeDetails = reS.ReplaceAllString(encodedNodeDetails, " ")
*/
// Add node info for being referenced in allNodes object.
allNodesStr = allNodesStr + encodedNodeDetails + ","

Expand Down

0 comments on commit 4692f20

Please sign in to comment.