Skip to content

Commit 73c0933

Browse files
committed
feat: support uploading source maps for extension JS bundles
1 parent fb896bc commit 73c0933

1 file changed

Lines changed: 34 additions & 9 deletions

File tree

cmd/src/extensions_publish.go

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010
"os/exec"
1111
"path/filepath"
12+
"strings"
1213
)
1314

1415
func init() {
@@ -29,6 +30,11 @@ Examples:
2930
}
3031
$ src extensions publish
3132
33+
Notes:
34+
35+
Source maps are supported (for easier debugging of extensions). If the main JavaScript bundle is "dist/myext.js",
36+
it looks for a source map in "dist/myext.map".
37+
3238
`
3339

3440
flagSet := flag.NewFlagSet("publish", flag.ExitOnError)
@@ -68,7 +74,7 @@ Examples:
6874
return err
6975
}
7076

71-
var bundle *string
77+
var bundle, sourceMap *string
7278
if *urlFlag != "" {
7379
manifest, err = updatePropertyInManifest(manifest, "url", *urlFlag)
7480
if err != nil {
@@ -81,7 +87,7 @@ Examples:
8187
}
8288

8389
var err error
84-
bundle, err = readBundleFileInManifest(manifest, filepath.Dir(*manifestFlag))
90+
bundle, sourceMap, err = readExtensionArtifacts(manifest, filepath.Dir(*manifestFlag))
8591
if err != nil {
8692
return err
8793
}
@@ -90,14 +96,16 @@ Examples:
9096
query := `mutation PublishExtension(
9197
$extensionID: String!,
9298
$manifest: String!,
93-
$bundle: String
99+
$bundle: String,
100+
$sourceMap: String,
94101
$force: Boolean!,
95102
) {
96103
extensionRegistry {
97104
publishExtension(
98105
extensionID: $extensionID,
99106
manifest: $manifest,
100107
bundle: $bundle,
108+
sourceMap: $sourceMap,
101109
force: $force,
102110
) {
103111
extension {
@@ -124,6 +132,7 @@ Examples:
124132
"extensionID": extensionID,
125133
"manifest": string(manifest),
126134
"bundle": bundle,
135+
"sourceMap": sourceMap,
127136
"force": *forceFlag,
128137
},
129138
result: &result,
@@ -239,21 +248,37 @@ func addReadmeToManifest(manifest []byte, dir string) ([]byte, error) {
239248
return json.MarshalIndent(o, "", " ")
240249
}
241250

242-
func readBundleFileInManifest(manifest []byte, dir string) (*string, error) {
251+
func readExtensionArtifacts(manifest []byte, dir string) (bundle, sourceMap *string, err error) {
243252
var o struct {
244253
Main string `json:"main"`
245254
}
246255
if err := json.Unmarshal(manifest, &o); err != nil {
247-
return nil, err
256+
return nil, nil, err
248257
}
249258
if o.Main == "" {
250-
return nil, nil
259+
return nil, nil, nil
251260
}
261+
252262
mainPath := filepath.Join(dir, o.Main)
263+
253264
data, err := ioutil.ReadFile(mainPath)
254265
if err != nil {
255-
return nil, fmt.Errorf(`extension manifest "main" bundle file: %s`, err)
266+
return nil, nil, fmt.Errorf(`extension manifest "main" bundle file: %s`, err)
256267
}
257-
tmp := string(data)
258-
return &tmp, nil
268+
{
269+
tmp := string(data)
270+
bundle = &tmp
271+
}
272+
273+
// Guess that source map is the main file with a ".map" extension.
274+
sourceMapPath := strings.TrimSuffix(mainPath, filepath.Ext(mainPath)) + ".map"
275+
data, err = ioutil.ReadFile(sourceMapPath)
276+
if err == nil {
277+
tmp := string(data)
278+
sourceMap = &tmp
279+
} else if !os.IsNotExist(err) {
280+
return nil, nil, err
281+
}
282+
283+
return bundle, sourceMap, nil
259284
}

0 commit comments

Comments
 (0)