diff --git a/cmd/build/client.go b/cmd/build/client.go index 8472ba3b..64f45a3a 100644 --- a/cmd/build/client.go +++ b/cmd/build/client.go @@ -26,6 +26,9 @@ func Client(buildPath string, tempBuildDir string, ejectedPath string) { stylePath := buildPath + "/spa/bundle.css" + // Initialize string for layout.js component list. + var allComponentsStr string + // Set up counter for logging output. compiledComponentCounter := 0 @@ -93,6 +96,11 @@ func Client(buildPath string, tempBuildDir string, ejectedPath string) { compileSvelte(ctx, SSRctx, layoutPath, destFile, stylePath) + // Create entry for layout.js. + layoutSignature := strings.ReplaceAll(strings.ReplaceAll((layoutPath), "/", "_"), ".", "_") + destLayoutPath := strings.TrimPrefix(layoutPath, "layout/") + allComponentsStr = allComponentsStr + "export {default as " + layoutSignature + "} from '../" + destLayoutPath + "';\n" + compiledComponentCounter++ } @@ -103,6 +111,12 @@ func Client(buildPath string, tempBuildDir string, ejectedPath string) { fmt.Printf("Could not get layout file: %s", layoutFilesErr) } + // Write layout.js to filesystem. + compWriteErr := ioutil.WriteFile(buildPath+"/spa/ejected/layout.js", []byte(allComponentsStr), os.ModePerm) + if compWriteErr != nil { + fmt.Printf("Unable to write layout.js file: %v\n", compWriteErr) + } + Log("Number of components compiled: " + strconv.Itoa(compiledComponentCounter)) } @@ -238,6 +252,12 @@ func compileSvelte(ctx *v8go.Context, SSRctx *v8go.Context, layoutPath string, d } } + // Remove allComponents object (leaving just componentSignature) for SSR. + reAllComponentsDot := regexp.MustCompile(`allComponents\.`) + ssrStr = reAllComponentsDot.ReplaceAllString(ssrStr, "") + reAllComponentsBracket := regexp.MustCompile(`allComponents\[\"(.*)\"\]`) + ssrStr = reAllComponentsBracket.ReplaceAllString(ssrStr, "${1}") + // Add component to context so it can be used to render HTML in data_source.go. _, addSSRCompErr := SSRctx.RunScript(ssrStr, "create_ssr") if addSSRCompErr != nil { diff --git a/defaults/layout/global/html.svelte b/defaults/layout/global/html.svelte index a1f3e3fe..5d8f798d 100755 --- a/defaults/layout/global/html.svelte +++ b/defaults/layout/global/html.svelte @@ -4,7 +4,7 @@ import Footer from './footer.svelte'; import { makeTitle } from '../scripts/make_title.svelte'; - export let route, content, allContent; + export let route, content, allContent, allComponents; @@ -13,7 +13,7 @@