aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/js/esbuild/sourcemap.go
blob: 647f0c0813ad201e64a916c9064f9e4722123db6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright 2024 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package esbuild

import (
	"encoding/json"
	"strings"

	"github.com/evanw/esbuild/pkg/api"
	"github.com/gohugoio/hugo/common/paths"
)

type sourceMap struct {
	Version        int      `json:"version"`
	Sources        []string `json:"sources"`
	SourcesContent []string `json:"sourcesContent"`
	Mappings       string   `json:"mappings"`
	Names          []string `json:"names"`
}

func fixOutputFile(o *api.OutputFile, resolve func(string) string) error {
	if strings.HasSuffix(o.Path, ".map") {
		b, err := fixSourceMap(o.Contents, resolve)
		if err != nil {
			return err
		}
		o.Contents = b
	}
	return nil
}

func fixSourceMap(s []byte, resolve func(string) string) ([]byte, error) {
	var sm sourceMap
	if err := json.Unmarshal([]byte(s), &sm); err != nil {
		return nil, err
	}

	sm.Sources = fixSourceMapSources(sm.Sources, resolve)

	b, err := json.Marshal(sm)
	if err != nil {
		return nil, err
	}

	return b, nil
}

func fixSourceMapSources(s []string, resolve func(string) string) []string {
	var result []string
	for _, src := range s {
		if s := resolve(src); s != "" {
			// Absolute filenames works fine on U*ix (tested in Chrome on MacOs), but works very poorly on Windows (again Chrome).
			// So, convert it to a URL.
			if u, err := paths.UrlFromFilename(s); err == nil {
				result = append(result, u.String())
			}
		}
	}
	return result
}

// Used in tests.
func SourcesFromSourceMap(s string) []string {
	var sm sourceMap
	if err := json.Unmarshal([]byte(s), &sm); err != nil {
		return nil
	}
	return sm.Sources
}