diff --git a/cmd/build.go b/cmd/build.go index 56e67ed4..52d963de 100644 --- a/cmd/build.go +++ b/cmd/build.go @@ -153,18 +153,6 @@ func Build() error { log.Fatal("\nError in DataSource build step", err) } - // Run Gopack (custom Snowpack alternative) on app for ESM support. - err = build.Gopack(buildPath, buildPath+"/spa/core/main.js") - if err != nil { - log.Fatal("\nError in Gopack build step", err) - } - - // Run Gopack manually on dynamic imports - err = build.GopackDynamic(buildPath) - if err != nil { - log.Fatal("\nError in GopackDynamic build step", err) - } - // Run Minification err = build.Minify(buildPath) if err != nil { diff --git a/cmd/build/client.go b/cmd/build/client.go index 92040d0f..883901c5 100644 --- a/cmd/build/client.go +++ b/cmd/build/client.go @@ -143,7 +143,7 @@ func Client(buildPath string, coreFS embed.FS) error { componentStr = string(componentBytes) } destPath := buildPath + "/spa/" + strings.TrimSuffix(path, ".svelte") + ".js" - err = (compileSvelte(ctx, SSRctx, path, componentStr, destPath, stylePath)) + err = compileSvelte(ctx, SSRctx, buildPath, path, componentStr, destPath, stylePath) if err != nil { fmt.Printf("Could not compile '%s' Svelte component: %s", path, err) } @@ -244,7 +244,7 @@ func compileComponent(err error, layoutPath string, layoutFileInfo os.FileInfo, } componentStr := string(component) // Actually compile component - if err = compileSvelte(ctx, SSRctx, layoutPath, componentStr, destFile, stylePath); err != nil { + if err = compileSvelte(ctx, SSRctx, buildPath, layoutPath, componentStr, destFile, stylePath); err != nil { return compiledComponentCounter, allLayoutsStr, fmt.Errorf("%w\n", err) } // Create entry for layouts.js. diff --git a/cmd/build/compile.go b/cmd/build/compile.go index 8fe35605..2b0a4d33 100644 --- a/cmd/build/compile.go +++ b/cmd/build/compile.go @@ -35,7 +35,7 @@ var ( reCSSCli = regexp.MustCompile(`var(\s)css(\s)=(\s)\{(.*\n){0,}\};`) ) -func compileSvelte(ctx *v8go.Context, SSRctx *v8go.Context, layoutPath string, +func compileSvelte(ctx *v8go.Context, SSRctx *v8go.Context, buildPath string, layoutPath string, componentStr string, destFile string, stylePath string) error { // Create any sub directories need for filepath. @@ -56,6 +56,10 @@ func compileSvelte(ctx *v8go.Context, SSRctx *v8go.Context, layoutPath string, return fmt.Errorf("V8go could not execute js.code for %s: %w\n", layoutPath, err) } jsBytes := []byte(jsCode.String()) + jsBytes, err = Gopack(buildPath, destFile, jsBytes) + if err != nil { + return fmt.Errorf("Could not add ESM support via Gopack: %w", err) + } err = os.WriteFile(destFile, jsBytes, 0755) if err != nil { return fmt.Errorf("Unable to write compiled client file: %w\n", err) diff --git a/cmd/build/gopack.go b/cmd/build/gopack.go index a98ac335..7d793d2b 100644 --- a/cmd/build/gopack.go +++ b/cmd/build/gopack.go @@ -4,13 +4,11 @@ import ( "bytes" "fmt" "io" - "io/ioutil" "os" "path" "path/filepath" "regexp" "strings" - "time" "github.com/plentico/plenti/readers" ) @@ -40,38 +38,10 @@ var ( rePath = regexp.MustCompile(`(?:'|").*(?:'|")`) ) -// Initialize globally to keep track during recursion. -var alreadyConvertedFiles []string - // Gopack ensures ESM support for NPM dependencies. -func Gopack(buildPath, entrypoint string) error { - - defer Benchmark(time.Now(), "Running Gopack") - - Log("\nRunning gopack to build esm support for npm dependencies") - - // Clear web_modules from previous build. - alreadyConvertedFiles = []string{} - - // Start at the entry point for the app - err := runPack(buildPath, entrypoint) - if err != nil { - return err - } - - return nil -} - -func runPack(buildPath, convertPath string) error { +func Gopack(buildPath string, convertPath string, contentBytes []byte) ([]byte, error) { // Destination path for dependencies gopackDir := buildPath + "/spa/web_modules" - - // Get the actual contents of the file we want to convert - contentBytes, err := ioutil.ReadFile(convertPath) - if err != nil { - return fmt.Errorf("\nCould not read file %s to convert to esm\n%w", convertPath, err) - } - // Created byte array of all dynamic imports in the current file. dynamicImportPaths := reDynamicImport.FindAll(contentBytes, -1) for _, dynamicImportPath := range dynamicImportPaths { @@ -101,6 +71,8 @@ func runPack(buildPath, convertPath string) error { var foundPath string // Initialize the full path of the import. var fullPathStr string + // Initialize error. + var err error // Convert .svelte file extensions to .js so the browser can read them. if filepath.Ext(pathStr) == ".svelte" { @@ -180,53 +152,24 @@ func runPack(buildPath, convertPath string) error { } } - // Do not convert files that have already been converted to avoid loops. - if !alreadyConverted(fullPathStr, alreadyConvertedFiles) { - // Add the current file to list of already converted files. - alreadyConvertedFiles = append(alreadyConvertedFiles, fullPathStr) - // Use fullPathStr recursively to find its imports. - err = runPack(buildPath, fullPathStr) - if err != nil { - return fmt.Errorf("\nCan't runPack on %s %w", fullPathStr, err) + //if foundPath != "" { + // Remove "public" build dir from path. + replacePath := strings.Replace(foundPath, buildPath, "", 1) + // Wrap path in quotes. + replacePath = "'" + replacePath + "'" + // Convert string path to bytes. + replacePathBytes := []byte(replacePath) + // Actually replace the path to the dependency in the source content. + contentBytes = bytes.ReplaceAll(contentBytes, staticStatement, + rePath.ReplaceAll(staticStatement, rePath.ReplaceAll(pathBytes, replacePathBytes))) + /* + } else { + return contentBytes, fmt.Errorf("\nImport path '%s' not resolvable from file '%s'\n", pathStr, convertPath) } - } - - if foundPath != "" { - // Remove "public" build dir from path. - replacePath := strings.Replace(foundPath, buildPath, "", 1) - // Wrap path in quotes. - replacePath = "'" + replacePath + "'" - // Convert string path to bytes. - replacePathBytes := []byte(replacePath) - // Actually replace the path to the dependency in the source content. - contentBytes = bytes.ReplaceAll(contentBytes, staticStatement, - rePath.ReplaceAll(staticStatement, rePath.ReplaceAll(pathBytes, replacePathBytes))) - } else { - return fmt.Errorf("\nImport path '%s' not resolvable from file '%s'\n", pathStr, convertPath) - } - } - // Overwrite the old file with the new content that contains the updated import path. - err = ioutil.WriteFile(convertPath, contentBytes, 0644) - if err != nil { - return fmt.Errorf("Could not overwite %s with new import: %w\n", convertPath, err) + */ } - return nil - -} + return contentBytes, nil -func alreadyConverted(convertPath string, alreadyConvertedFiles []string) bool { - // Check if there are already files that have been converted - if len(alreadyConvertedFiles) > 0 { - for _, convertedFile := range alreadyConvertedFiles { - // Compare the currently queued file with each already converted file - if convertPath == convertedFile { - // Exit the function to avoid endless loops where files - // reference each other (like main.js and router.svelte) - return true - } - } - } - return false } func pathExists(path string) bool { diff --git a/cmd/build/gopack_dynamic.go b/cmd/build/gopack_dynamic.go deleted file mode 100644 index 842bf93e..00000000 --- a/cmd/build/gopack_dynamic.go +++ /dev/null @@ -1,40 +0,0 @@ -package build - -import ( - "fmt" - "os" - "path/filepath" - "time" -) - -// Create ESM support for files that don't have static imports in the app -func GopackDynamic(buildPath string) error { - - defer Benchmark(time.Now(), "Running GopackDynamic") - - Log("\nRunning Gopack manually on dynamic imports") - - // Dynamically imported CMS FieldWidgets - fieldWidgetPath := "/layouts/_fields" - // Check if there are any custom FieldWidgets in the project - if _, err := os.Stat(buildPath + fieldWidgetPath); !os.IsNotExist(err) { - // There are custom FieldWidgets, so add ESM support for each - err = filepath.Walk(buildPath+fieldWidgetPath, func(path string, info os.FileInfo, err error) error { - if err != nil { - return fmt.Errorf("\nCan't walk ejected FieldWidget path %s: %w", path, err) - } - if info.IsDir() { - return nil - } - err = Gopack(buildPath, buildPath+"/spa"+fieldWidgetPath+"/"+path) - if err != nil { - return fmt.Errorf("\nError running Gopack for custom FieldWidget: %w", err) - } - return nil - }) - if err != nil { - return fmt.Errorf("\nCould not get custom FieldWidget: %w\n", err) - } - } - return nil -} diff --git a/defaults/core/main.js b/defaults/core/main.js index 8835d338..e8f49a08 100755 --- a/defaults/core/main.js +++ b/defaults/core/main.js @@ -1,4 +1,4 @@ -import Router from './router.svelte'; +import Router from './router.js'; new Router({ target: document,