Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var Plugin = api.Plugin{ Name: "http", Setup: func(build api.PluginBuild) { build.OnResolve(api.OnResolveOptions{Filter: `^https?://`}, func(args api.OnResolveArgs) (api.OnResolveResult, error) { return api.OnResolveResult{ Path: args.Path, Namespace: "http-url", }, nil }) build.OnResolve(api.OnResolveOptions{Filter: ".*", Namespace: "http-url"}, func(args api.OnResolveArgs) (api.OnResolveResult, error) { base, err := url.Parse(args.Importer) if err != nil { return api.OnResolveResult{}, err } relative, err := url.Parse(args.Path) if err != nil { return api.OnResolveResult{}, err } return api.OnResolveResult{ Path: base.ResolveReference(relative).String(), Namespace: "http-url", }, nil }) build.OnLoad(api.OnLoadOptions{Filter: ".*", Namespace: "http-url"}, func(args api.OnLoadArgs) (api.OnLoadResult, error) { var contents string sum := sha256.Sum256([]byte(args.Path)) filePath := filepath.Join(cacheDIR, fmt.Sprintf("%x", sum)) if _, err := os.Stat(cacheDIR); os.IsNotExist(err) { os.MkdirAll(cacheDIR, os.ModePerm) } if _, err := os.Stat(filePath); os.IsNotExist(err) { res, err := http.Get(args.Path) fmt.Printf("Downloading: %s\n", args.Path) if err != nil { return api.OnLoadResult{}, err } defer res.Body.Close() bytes, err := ioutil.ReadAll(res.Body) if err != nil { return api.OnLoadResult{}, err } contents = string(bytes) ioutil.WriteFile(filePath, bytes, os.ModePerm) } else { bytes, err := ioutil.ReadFile(filePath) if err != nil { return api.OnLoadResult{}, err } contents = string(bytes) } return api.OnLoadResult{Contents: &contents}, nil }) }, }
Plugin provides the http.Plugin to be used with esbuild plugins
Functions ¶
This section is empty.
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.