Skip to content

Commit

Permalink
Remove goroutine for compiling svelte.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimafisk committed Apr 19, 2020
1 parent 2a9e4c6 commit c40c1d5
Showing 1 changed file with 37 additions and 55 deletions.
92 changes: 37 additions & 55 deletions cmd/build/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,13 @@ import (
"os/exec"
"path/filepath"
"strings"
"sync"
"time"
)

// Client builds the SPA.
func Client(buildPath string) {

fmt.Println("\nBuilding client SPA using svelte compiler")

var wg sync.WaitGroup

stylePath := buildPath + "/spa/bundle.css"
// Clear out any previous CSS.
if _, stylePathExistsErr := os.Stat(stylePath); stylePathExistsErr == nil {
Expand Down Expand Up @@ -83,8 +79,43 @@ func Client(buildPath string) {
}
fileContentStr := string(fileContentByte)

wg.Add(1)
go compileSvelte(fileContentStr, destFile, stylePath, &wg)
// Execute node script to compile .svelte to .js
compiledBytes, buildErr := exec.Command("node", "layout/ejected/build_client.js", fileContentStr).Output()
if buildErr != nil {
fmt.Printf("Could not compile svelte to JS: %s\n", buildErr)
}

compiledStr := string(compiledBytes)
compiledStrArray := strings.Split(compiledStr, "!plenti-split!")

// Get the JS only from the script output.
jsStr := strings.TrimSpace(compiledStrArray[0])
// Convert file extensions to be snowpack friendly.
jsStr = strings.Replace(jsStr, ".svelte", ".js", -1)
jsStr = strings.Replace(jsStr, "from \"svelte/internal\";", "from \"../web_modules/svelte/internal/index.js\";", -1)
jsStr = strings.Replace(jsStr, "from \"navaid\";", "from \"../web_modules/navaid.js\";", -1)

// Write compiled .js to build directory.
jsBytes := []byte(jsStr)
destFile = strings.TrimSuffix(destFile, filepath.Ext(destFile)) + ".js"
err := ioutil.WriteFile(destFile, jsBytes, 0755)
if err != nil {
fmt.Printf("Unable to write file: %v", err)
}

// Get the CSS only from the script output.
cssStr := strings.TrimSpace(compiledStrArray[1])
// If there is CSS, write it into the bundle.css file.
if cssStr != "null" {
cssFile, WriteStyleErr := os.OpenFile(stylePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if WriteStyleErr != nil {
fmt.Printf("Could not open bundle.css for writing: %s", WriteStyleErr)
}
defer cssFile.Close()
if _, err := cssFile.WriteString(cssStr); err != nil {
log.Println(err)
}
}

compiledComponentCounter++

Expand All @@ -95,56 +126,7 @@ func Client(buildPath string) {
fmt.Printf("Could not get layout file: %s", layoutFilesErr)
}

wg.Wait()

fmt.Printf("Number of source files copied: %d\n", copiedSourceCounter)
fmt.Printf("Number of components compiled: %d\n", compiledComponentCounter)

}

func compileSvelte(fileContentStr string, destFile string, stylePath string, wg *sync.WaitGroup) {

start := time.Now()

// Execute node script to compile .svelte to .js
compiledBytes, buildErr := exec.Command("node", "layout/ejected/build_client.js", fileContentStr).Output()
if buildErr != nil {
fmt.Printf("Could not compile svelte to JS: %s\n", buildErr)
}

elapsed := time.Since(start)
fmt.Printf("Compiling component took %s\n", elapsed)

compiledStr := string(compiledBytes)
compiledStrArray := strings.Split(compiledStr, "!plenti-split!")

// Get the JS only from the script output.
jsStr := strings.TrimSpace(compiledStrArray[0])
// Convert file extensions to be snowpack friendly.
jsStr = strings.Replace(jsStr, ".svelte", ".js", -1)
jsStr = strings.Replace(jsStr, "from \"svelte/internal\";", "from \"../web_modules/svelte/internal/index.js\";", -1)
jsStr = strings.Replace(jsStr, "from \"navaid\";", "from \"../web_modules/navaid.js\";", -1)

// Write compiled .js to build directory.
jsBytes := []byte(jsStr)
destFile = strings.TrimSuffix(destFile, filepath.Ext(destFile)) + ".js"
err := ioutil.WriteFile(destFile, jsBytes, 0755)
if err != nil {
fmt.Printf("Unable to write file: %v", err)
}

// Get the CSS only from the script output.
cssStr := strings.TrimSpace(compiledStrArray[1])
// If there is CSS, write it into the bundle.css file.
if cssStr != "null" {
cssFile, WriteStyleErr := os.OpenFile(stylePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if WriteStyleErr != nil {
fmt.Printf("Could not open bundle.css for writing: %s", WriteStyleErr)
}
defer cssFile.Close()
if _, err := cssFile.WriteString(cssStr); err != nil {
log.Println(err)
}
}
wg.Done()
}

0 comments on commit c40c1d5

Please sign in to comment.