Skip to content

Commit

Permalink
Load siteConfig when modifying import path (#320).
Browse files Browse the repository at this point in the history
  • Loading branch information
jimafisk committed May 2, 2024
1 parent d84c8e0 commit 1005ca6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
6 changes: 3 additions & 3 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,19 +160,19 @@ func Build() error {
}

// Run Gopack (custom Snowpack alternative) on app for ESM support.
err = build.Gopack(buildPath, spaPath, spaPath+"core/main.js", siteConfig.Fingerprint)
err = build.Gopack(buildPath, spaPath, spaPath+"core/main.js")
if err != nil {
log.Fatal("\nError in Gopack main.js build step", err)
}

// Run Gopack (custom Snowpack alternative) on dynamically imported adminMenu.
err = build.Gopack(buildPath, spaPath, spaPath+"core/cms/admin_menu.js", siteConfig.Fingerprint)
err = build.Gopack(buildPath, spaPath, spaPath+"core/cms/admin_menu.js")
if err != nil {
log.Fatal("\nError in Gopack admin_menu.svelte build step", err)
}

// Run Gopack manually on dynamic imports
err = build.GopackDynamic(buildPath, spaPath, siteConfig.Fingerprint)
err = build.GopackDynamic(buildPath, spaPath)
if err != nil {
log.Fatal("\nError in GopackDynamic build step", err)
}
Expand Down
12 changes: 7 additions & 5 deletions cmd/build/gopack.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var (
var alreadyConvertedFiles []string

// Gopack ensures ESM support for NPM dependencies.
func Gopack(buildPath, spaPath, entrypoint, fingerprint string) error {
func Gopack(buildPath, spaPath, entrypoint string) error {

defer Benchmark(time.Now(), "Running Gopack")

Expand All @@ -54,15 +54,15 @@ func Gopack(buildPath, spaPath, entrypoint, fingerprint string) error {
alreadyConvertedFiles = []string{}

// Start at the entry point for the app
err := runPack(buildPath, spaPath, entrypoint, fingerprint)
err := runPack(buildPath, spaPath, entrypoint)
if err != nil {
return err
}

return nil
}

func runPack(buildPath, spaPath, convertPath, fingerprint string) error {
func runPack(buildPath, spaPath, convertPath string) error {
// Destination path for dependencies
gopackDir := spaPath + "web_modules"

Expand Down Expand Up @@ -193,7 +193,7 @@ func runPack(buildPath, spaPath, convertPath, fingerprint string) error {
// Add the current file to list of already converted files.
alreadyConvertedFiles = append(alreadyConvertedFiles, fullPathStr)
// Use fullPathStr recursively to find its imports.
err = runPack(buildPath, spaPath, fullPathStr, fingerprint)
err = runPack(buildPath, spaPath, fullPathStr)
if err != nil {
return fmt.Errorf("\nCan't runPack on %s %w", fullPathStr, err)
}
Expand All @@ -202,8 +202,10 @@ func runPack(buildPath, spaPath, convertPath, fingerprint string) error {
if foundPath != "" {
// Remove "public" build dir from path.
replacePath := strings.Replace(foundPath, buildPath, "", 1)
// Load plenti.json config
siteConfig, _ := readers.GetSiteConfig(".")
// Wrap path in quotes and add query params to break cache.
replacePath = "'" + replacePath + "?" + fingerprint + "'"
replacePath = "'" + replacePath + "?" + siteConfig.Fingerprint + "'"
// Convert string path to bytes.
replacePathBytes := []byte(replacePath)
// Actually replace the path to the dependency in the source content.
Expand Down
4 changes: 2 additions & 2 deletions cmd/build/gopack_dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

// Create ESM support for files that don't have static imports in the app
func GopackDynamic(buildPath, spaPath, fingerprint string) error {
func GopackDynamic(buildPath, spaPath string) error {

defer Benchmark(time.Now(), "Running GopackDynamic")

Expand All @@ -27,7 +27,7 @@ func GopackDynamic(buildPath, spaPath, fingerprint string) error {
if info.IsDir() {
return nil
}
err = Gopack(buildPath, spaPath, spaPath+fieldWidgetPath+"/"+path, fingerprint)
err = Gopack(buildPath, spaPath, spaPath+fieldWidgetPath+"/"+path)
if err != nil {
return fmt.Errorf("\nError running Gopack for custom FieldWidget: %w", err)
}
Expand Down

0 comments on commit 1005ca6

Please sign in to comment.