Skip to content

Commit

Permalink
Merge pull request #147 from plentico/issue-119
Browse files Browse the repository at this point in the history
Prep v0.4 release (#119).
  • Loading branch information
jimafisk authored Mar 22, 2021
2 parents 3f98b63 + 883c9a1 commit e1a6189
Show file tree
Hide file tree
Showing 41 changed files with 81 additions and 508 deletions.
50 changes: 14 additions & 36 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,21 @@ import (
"github.com/spf13/cobra"
)

// BuildDirFlag allows users to override name of default build directory (public)
var BuildDirFlag string
// OutputDirFlag allows users to override name of default build directory (public)
var OutputDirFlag string

// VerboseFlag provides users with additional logging information.
var VerboseFlag bool

// BenchmarkFlag provides users with build speed statistics to help identify bottlenecks.
var BenchmarkFlag bool

// NodeJSFlag let you use your systems NodeJS to build the site instead of core build.
var NodeJSFlag bool

func setBuildDir(siteConfig readers.SiteConfig) string {
buildDir := siteConfig.BuildDir
// Check if directory is overridden by flag.
if BuildDirFlag != "" {
if OutputDirFlag != "" {
// If dir flag exists, use it.
buildDir = BuildDirFlag
buildDir = OutputDirFlag
}
return buildDir
}
Expand Down Expand Up @@ -127,34 +124,16 @@ func Build() error {
return err
}

// Run the build.js script using user local NodeJS.
if NodeJSFlag {
clientBuildStr, err := build.NodeClient(buildPath)
if err = common.CheckErr(err); err != nil {
return err
}
staticBuildStr, allNodesStr, err := build.NodeDataSource(buildPath, siteConfig)
if err = common.CheckErr(err); err != nil {
return err
}

if err = common.CheckErr(build.NodeExec(clientBuildStr, staticBuildStr, allNodesStr)); err != nil {
return err
}
} else {

// Prep the client SPA.
err = build.Client(buildPath, tempBuildDir, defaultsEjectedFS)
if err = common.CheckErr(err); err != nil {
return err
}

// Build JSON from "content/" directory.
err = build.DataSource(buildPath, siteConfig, tempBuildDir)
if err = common.CheckErr(err); err != nil {
return err
}
// Prep the client SPA.
err = build.Client(buildPath, tempBuildDir, defaultsEjectedFS)
if err = common.CheckErr(err); err != nil {
return err
}

// Build JSON from "content/" directory.
err = build.DataSource(buildPath, siteConfig, tempBuildDir)
if err = common.CheckErr(err); err != nil {
return err
}

// Run Gopack (custom Snowpack alternative) for ESM support.
Expand Down Expand Up @@ -186,8 +165,7 @@ func init() {
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// buildCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
buildCmd.Flags().StringVarP(&BuildDirFlag, "dir", "d", "", "change name of the build directory")
buildCmd.Flags().StringVarP(&OutputDirFlag, "output", "o", "", "change name of the public build directory")
buildCmd.Flags().BoolVarP(&VerboseFlag, "verbose", "v", false, "show log messages")
buildCmd.Flags().BoolVarP(&BenchmarkFlag, "benchmark", "b", false, "display build time statistics")
buildCmd.Flags().BoolVarP(&NodeJSFlag, "nodejs", "n", false, "use system nodejs for build with ejectable build.js script")
}
52 changes: 26 additions & 26 deletions cmd/build/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func Client(buildPath string, tempBuildDir string, defaultsEjectedFS embed.FS) e

stylePath := buildPath + "/spa/bundle.css"

// Initialize string for layout.js component list.
var allComponentsStr string
// Initialize string for layouts.js component list.
var allLayoutsStr string

// Set up counter for logging output.
compiledComponentCounter := 0
Expand Down Expand Up @@ -133,14 +133,14 @@ func Client(buildPath string, tempBuildDir string, defaultsEjectedFS embed.FS) e
return err
}

// Go through all file paths in the "/layout" folder.
err = filepath.Walk(tempBuildDir+"layout", func(layoutPath string, layoutFileInfo os.FileInfo, err error) error {
// Go through all file paths in the "/layouts" folder.
err = filepath.Walk(tempBuildDir+"layouts", func(layoutPath string, layoutFileInfo os.FileInfo, err error) error {

if err != nil {
return fmt.Errorf("can't stat %s: %w", layoutPath, err)
}
// Create destination path.
destFile := buildPath + "/spa" + strings.TrimPrefix(layoutPath, tempBuildDir+"layout")
destFile := buildPath + "/spa" + strings.TrimPrefix(layoutPath, tempBuildDir+"layouts")
// Make sure path is a directory
if layoutFileInfo.IsDir() {
// Create any sub directories need for filepath.
Expand All @@ -165,12 +165,12 @@ func Client(buildPath string, tempBuildDir string, defaultsEjectedFS embed.FS) e

// Remove temporary theme build directory.
destLayoutPath := strings.TrimPrefix(layoutPath, tempBuildDir)
// Create entry for layout.js.
// Create entry for layouts.js.
layoutSignature := strings.ReplaceAll(strings.ReplaceAll((destLayoutPath), "/", "_"), ".", "_")
// Remove layout directory.
destLayoutPath = strings.TrimPrefix(destLayoutPath, "layout/")
// Compose entry for layout.js file.
allComponentsStr = allComponentsStr + "export {default as " + layoutSignature + "} from '../" + destLayoutPath + "';\n"
// Remove layouts directory.
destLayoutPath = strings.TrimPrefix(destLayoutPath, "layouts/")
// Compose entry for layouts.js file.
allLayoutsStr = allLayoutsStr + "export {default as " + layoutSignature + "} from '../" + destLayoutPath + "';\n"

compiledComponentCounter++

Expand All @@ -184,10 +184,10 @@ func Client(buildPath string, tempBuildDir string, defaultsEjectedFS embed.FS) e

}

// Write layout.js to filesystem.
err = ioutil.WriteFile(buildPath+"/spa/ejected/layout.js", []byte(allComponentsStr), os.ModePerm)
// Write layouts.js to filesystem.
err = ioutil.WriteFile(buildPath+"/spa/ejected/layouts.js", []byte(allLayoutsStr), os.ModePerm)
if err != nil {
return fmt.Errorf("Unable to write layout.js file: %w%s", err, common.Caller())
return fmt.Errorf("Unable to write layouts.js file: %w%s", err, common.Caller())

}

Expand Down Expand Up @@ -349,28 +349,28 @@ func compileSvelte(ctx *v8go.Context, SSRctx *v8go.Context, layoutPath string,
if currentNamedImport != "" && importSignature != "" {
// Only add named imports to create_ssr_component().
reCreateFunc := regexp.MustCompile(`(create_ssr_component\(\(.*\)\s=>\s\{)`)
// Entry should be block scoped, like: let count = layout_scripts_stores_svelte_count;
// Entry should be block scoped, like: let count = layouts_scripts_stores_svelte_count;
blockScopedVar := "\n let " + currentNamedImport + " = " + importSignature + "_" + currentNamedImport + ";"
// Add block scoped var inside create_ssr_component.
ssrStr = reCreateFunc.ReplaceAllString(ssrStr, "${1}"+blockScopedVar)
}
}
}

// Remove allComponents object (leaving just componentSignature) for SSR.
// Match: allComponents.layout_components_grid_svelte
reAllComponentsDot := regexp.MustCompile(`allComponents\.(layout_.*_svelte)`)
ssrStr = reAllComponentsDot.ReplaceAllString(ssrStr, "${1}")
// Match: allComponents[component]
reAllComponentsBracket := regexp.MustCompile(`allComponents\[(.*)\]`)
ssrStr = reAllComponentsBracket.ReplaceAllString(ssrStr, "globalThis[${1}]")
// Match: allComponents["layout_components_decrementer_svelte"]
reAllComponentsBracketStr := regexp.MustCompile(`allComponents\[\"(.*)\"\]`)
ssrStr = reAllComponentsBracketStr.ReplaceAllString(ssrStr, "${1}")
// Remove allLayouts object (leaving just componentSignature) for SSR.
// Match: allLayouts.layouts_components_grid_svelte
reAllLayoutsDot := regexp.MustCompile(`allLayouts\.(layouts_.*_svelte)`)
ssrStr = reAllLayoutsDot.ReplaceAllString(ssrStr, "${1}")
// Match: allLayouts[component]
reAllLayoutsBracket := regexp.MustCompile(`allLayouts\[(.*)\]`)
ssrStr = reAllLayoutsBracket.ReplaceAllString(ssrStr, "globalThis[${1}]")
// Match: allLayouts["layouts_components_decrementer_svelte"]
reAllLayoutsBracketStr := regexp.MustCompile(`allLayouts\[\"(.*)\"\]`)
ssrStr = reAllLayoutsBracketStr.ReplaceAllString(ssrStr, "${1}")

paginatedContent, _ := getPagination()
for _, pager := range paginatedContent {
if "layout_content_"+pager.contentType+"_svelte" == componentSignature {
if "layouts_content_"+pager.contentType+"_svelte" == componentSignature {
for _, paginationVar := range pager.paginationVars {
// Prefix var so it doesn't conflict with other variables.
globalVar := "plenti_global_pager_" + paginationVar
Expand Down Expand Up @@ -437,7 +437,7 @@ func getPagination() ([]pager, *regexp.Regexp) {
// Initialize new pager struct
var pagers []pager
// Check for pagination in plenti.json config file.
for configContentType, slug := range siteConfig.Types {
for configContentType, slug := range siteConfig.Routes {
// Initialize list of all :paginate() vars in a given slug.
replacements := []string{}
// Find every instance of :paginate() in the slug.
Expand Down
14 changes: 7 additions & 7 deletions cmd/build/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,16 @@ func DataSource(buildPath string, siteConfig readers.SiteConfig, tempBuildDir st
// Get field key/values from content source.
typeFields := readers.GetTypeFields(fileContentBytes)
// Setup regex to find field name.
reField := regexp.MustCompile(`:field\((.*?)\)`)
reField := regexp.MustCompile(`:fields\((.*?)\)`)
// Check for path overrides from plenti.json config file.
for configContentType, slug := range siteConfig.Types {
for configContentType, slug := range siteConfig.Routes {
if configContentType == contentType {
// Replace :filename.
slug = strings.Replace(slug, ":filename", strings.TrimSuffix(fileName, filepath.Ext(fileName)), -1)

// Replace :field().
// Replace :fields().
fieldReplacements := reField.FindAllStringSubmatch(slug, -1)
// Loop through all :field() replacements found in config file.
// Loop through all :fields() replacements found in config file.
for _, replacement := range fieldReplacements {
// Loop through all top level keys found in content source file.
for field, fieldValue := range typeFields.Fields {
Expand Down Expand Up @@ -285,15 +285,15 @@ func DataSource(buildPath string, siteConfig readers.SiteConfig, tempBuildDir st
}

func createProps(currentContent content, allContentStr string) error {
routeSignature := "layout_content_" + currentContent.contentType + "_svelte"
_, err := SSRctx.RunScript("var props = {route: "+routeSignature+", content: "+currentContent.contentDetails+", allContent: "+allContentStr+"};", "create_ssr")
componentSignature := "layouts_content_" + currentContent.contentType + "_svelte"
_, err := SSRctx.RunScript("var props = {content: "+currentContent.contentDetails+", layout: "+componentSignature+", allContent: "+allContentStr+"};", "create_ssr")
if err != nil {

return fmt.Errorf("Could not create props: %w%s", err, common.Caller())

}
// Render the HTML with props needed for the current content.
_, err = SSRctx.RunScript("var { html, css: staticCss} = layout_global_html_svelte.render(props);", "create_ssr")
_, err = SSRctx.RunScript("var { html, css: staticCss} = layouts_global_html_svelte.render(props);", "create_ssr")
if err != nil {
return fmt.Errorf("Can't render htmlComponent: %w%s", err, common.Caller())

Expand Down
13 changes: 1 addition & 12 deletions cmd/build/eject_copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,8 @@ func EjectCopy(buildPath string, tempBuildDir string, defaultsEjectedFS embed.FS
if err != nil {
return fmt.Errorf("can't stat %s: %w", ejectPath, err)
}
// Make list of files not to copy to build.
excludedFiles := []string{
"ejected/build.js",
}
// Check if the current file is in the excluded list.
excluded := false
for _, excludedFile := range excludedFiles {
if excludedFile == ejectPath {
excluded = true
}
}
// If the file is already in .js format just copy it straight over to build dir.
if filepath.Ext(ejectPath) == ".js" && !excluded {
if filepath.Ext(ejectPath) == ".js" {

destPath := buildPath + "/spa/"
if err := os.MkdirAll(destPath+strings.TrimPrefix("ejected", tempBuildDir), os.ModePerm); err != nil {
Expand Down
74 changes: 0 additions & 74 deletions cmd/build/node_client.go

This file was deleted.

Loading

0 comments on commit e1a6189

Please sign in to comment.