(entry string, importMap *importmap.ImportMap)
| 814 | } |
| 815 | |
| 816 | func (s *Handler) analyzeDependencyTree(entry string, importMap *importmap.ImportMap) (tree map[string][]byte, err error) { |
| 817 | tree = make(map[string][]byte) |
| 818 | ret := esbuild.Build(esbuild.BuildOptions{ |
| 819 | EntryPoints: []string{entry}, |
| 820 | Target: esbuild.ES2022, |
| 821 | Format: esbuild.FormatESModule, |
| 822 | Platform: esbuild.PlatformBrowser, |
| 823 | JSX: esbuild.JSXPreserve, |
| 824 | MinifyWhitespace: true, |
| 825 | Bundle: true, |
| 826 | Write: false, |
| 827 | Outdir: "/esbuild", |
| 828 | Plugins: []esbuild.Plugin{ |
| 829 | { |
| 830 | Name: "loader", |
| 831 | Setup: func(build esbuild.PluginBuild) { |
| 832 | build.OnResolve(esbuild.OnResolveOptions{Filter: ".*"}, func(args esbuild.OnResolveArgs) (esbuild.OnResolveResult, error) { |
| 833 | url, _ := url.Parse(args.Importer) |
| 834 | path := args.Path |
| 835 | if importMap != nil { |
| 836 | path, _ = importMap.Resolve(args.Path, url) |
| 837 | } |
| 838 | if isHttpSepcifier(path) || (!isRelPathSpecifier(path) && !isAbsPathSpecifier(path)) { |
| 839 | return esbuild.OnResolveResult{Path: path, External: true}, nil |
| 840 | } |
| 841 | if isModulePath(path) { |
| 842 | if isRelPathSpecifier(path) { |
| 843 | path = filepath.Join(filepath.Dir(args.Importer), path) |
| 844 | } |
| 845 | return esbuild.OnResolveResult{Path: path, Namespace: "module", PluginData: path}, nil |
| 846 | } |
| 847 | return esbuild.OnResolveResult{}, nil |
| 848 | }) |
| 849 | build.OnLoad(esbuild.OnLoadOptions{Filter: ".*", Namespace: "module"}, func(args esbuild.OnLoadArgs) (esbuild.OnLoadResult, error) { |
| 850 | data, err := os.ReadFile(args.Path) |
| 851 | if err != nil { |
| 852 | return esbuild.OnLoadResult{}, err |
| 853 | } |
| 854 | tree[args.Path] = data |
| 855 | contents := string(data) |
| 856 | loader := esbuild.LoaderJS |
| 857 | pathname := args.PluginData.(string) |
| 858 | ext := filepath.Ext(pathname) |
| 859 | switch ext { |
| 860 | case ".jsx": |
| 861 | loader = esbuild.LoaderJSX |
| 862 | case ".ts", ".mts": |
| 863 | loader = esbuild.LoaderTS |
| 864 | case ".tsx": |
| 865 | loader = esbuild.LoaderTSX |
| 866 | case ".vue", ".svelte": |
| 867 | xx := xxhash.New() |
| 868 | xx.Write(data) |
| 869 | etag := hex.EncodeToString(xx.Sum(nil)) |
| 870 | cacheKey := fmt.Sprintf("preload-%s", pathname) |
| 871 | etagCacheKey := fmt.Sprintf("preload-%s.etag", pathname) |
| 872 | langCacheKey := fmt.Sprintf("preload-%s.lang", pathname) |
| 873 | if js, ok := s.loaderCache.Load(cacheKey); ok { |
no test coverage detected