Skip to content

Commit

Permalink
feat: introduce remote loader for missing files/
Browse files Browse the repository at this point in the history
  • Loading branch information
shyim committed Dec 12, 2023
1 parent cf2767b commit 89cc188
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/esbuild/esbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func getEsbuildOptions(ctx context.Context, options AssetCompileOptions) (*api.B
Bundle: true,
Write: false,
LogLevel: api.LogLevelWarning,
Plugins: []api.Plugin{newScssPlugin(ctx)},
Plugins: []api.Plugin{newScssPlugin(ctx), newRemoteLoaderPlugin()},
Loader: map[string]api.Loader{
".html": api.LoaderText,
".twig": api.LoaderText,
Expand Down
86 changes: 86 additions & 0 deletions internal/esbuild/remote_loader_plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package esbuild

import (
"fmt"
"github.com/evanw/esbuild/pkg/api"
"io"
"net/http"
"strings"
)

const RemoteLoaderName = "sw-remote-loader"

func newRemoteLoaderPlugin() api.Plugin {
return api.Plugin{
Name: RemoteLoaderName,
Setup: func(build api.PluginBuild) {
// Route all @administration imports to our namespace
build.OnResolve(api.OnResolveOptions{Filter: `^@administration\/`}, func(args api.OnResolveArgs) (api.OnResolveResult, error) {
return api.OnResolveResult{
Path: args.Path,
Namespace: RemoteLoaderName,
}, nil
})

// All relative imports inside our namespace, should be still our namespace
build.OnResolve(api.OnResolveOptions{Filter: "/.*/", Namespace: RemoteLoaderName}, func(args api.OnResolveArgs) (api.OnResolveResult, error) {
return api.OnResolveResult{
Path: args.Path,
Namespace: RemoteLoaderName,
}, nil
})

// When our namespace is used, we load the remote file
build.OnLoad(api.OnLoadOptions{Filter: "/.*/", Namespace: RemoteLoaderName}, func(args api.OnLoadArgs) (api.OnLoadResult, error) {
// Try to load the file with .ts and .js extension
if content, err := fetchRemoteAsset(args.Path + ".ts"); err == nil {
return api.OnLoadResult{
Contents: &content,
Loader: api.LoaderTS,
}, nil
}

if content, err := fetchRemoteAsset(args.Path + ".js"); err == nil {
return api.OnLoadResult{
Contents: &content,
Loader: api.LoaderTS,
}, nil
}

return api.OnLoadResult{}, fmt.Errorf("file does not exists")
})
},
}
}

func fetchRemoteAsset(file string) (string, error) {
path := strings.ReplaceAll(file, "@administration/", "administration/src/")

r, err := http.NewRequest("GET", "https://raw.githubusercontent.com/shopware/shopware/trunk/src/Administration/Resources/app/"+path, nil)
if err != nil {
return "", err
}

r.Header.Add("User-Agent", "Shopware CLI")

resp, err := http.DefaultClient.Do(r)
if err != nil {
return "", err
}

if resp.StatusCode != 200 {
return "", fmt.Errorf("file does not exists")
}

content, err := io.ReadAll(resp.Body)

if err != nil {
return "", err
}

if err := resp.Body.Close(); err != nil {
return "", err
}

return string(content), nil
}

0 comments on commit 89cc188

Please sign in to comment.