Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions cmd/sourcemaps/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,21 @@ func runE(client resources.Client) func(cmd *cobra.Command, args []string) error
}
}

var sourceMapUploadSuffixes = []string{
".js.map", ".js",
".jsbundle.map", ".jsbundle",
".bundle.map", ".bundle",
}

func isSourceMapUploadFile(name string) bool {
for _, suffix := range sourceMapUploadSuffixes {
if strings.HasSuffix(name, suffix) {
return true
}
}
return false
}

func getAllSourceMapFiles(path string) ([]SourceMapFile, error) {
var files []SourceMapFile
routeGroupPattern := regexp.MustCompile(`\(.+?\)/`)
Expand All @@ -194,7 +209,7 @@ func getAllSourceMapFiles(path string) ([]SourceMapFile, error) {
return filepath.SkipDir
}

if !d.IsDir() && (strings.HasSuffix(filePath, ".js.map") || strings.HasSuffix(filePath, ".js")) {
if !d.IsDir() && isSourceMapUploadFile(filePath) {
relPath, err := filepath.Rel(path, filePath)
if err != nil {
return err
Expand Down Expand Up @@ -222,7 +237,7 @@ func getAllSourceMapFiles(path string) ([]SourceMapFile, error) {
}

if len(files) == 0 {
return nil, fmt.Errorf("no .js.map files found. Please double check that you have generated sourcemaps for your app")
return nil, fmt.Errorf("no sourcemap files found (looked for *.js.map, *.jsbundle.map, *.bundle.map and their minified files). Please double check that you have generated sourcemaps for your app")
}

return files, nil
Expand Down
51 changes: 50 additions & 1 deletion cmd/sourcemaps/upload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,56 @@ func TestGetAllSourceMapFiles(t *testing.T) {
defer os.RemoveAll(emptyDir)
_, err = getAllSourceMapFiles(emptyDir)
assert.Error(t, err)
assert.Contains(t, err.Error(), "no .js.map files found")
assert.Contains(t, err.Error(), "no sourcemap files found")
}

func TestIsSourceMapUploadFile(t *testing.T) {
// Web bundles + maps.
assert.True(t, isSourceMapUploadFile("app.js"))
assert.True(t, isSourceMapUploadFile("app.js.map"))
// React Native iOS bundle + map.
assert.True(t, isSourceMapUploadFile("main.jsbundle"))
assert.True(t, isSourceMapUploadFile("main.jsbundle.map"))
// React Native Android bundle + map.
assert.True(t, isSourceMapUploadFile("index.android.bundle"))
assert.True(t, isSourceMapUploadFile("index.android.bundle.map"))
// Unrelated files are ignored.
assert.False(t, isSourceMapUploadFile("styles.css"))
assert.False(t, isSourceMapUploadFile("styles.css.map"))
assert.False(t, isSourceMapUploadFile("README.md"))
}

func TestGetAllSourceMapFilesReactNative(t *testing.T) {
tempDir, err := os.MkdirTemp("", "sourcemap-rn-test")
assert.NoError(t, err)
defer os.RemoveAll(tempDir)

// The names React Native's `react-native bundle` produces.
rnFiles := []string{
"main.jsbundle",
"main.jsbundle.map",
"index.android.bundle",
"index.android.bundle.map",
}
for _, name := range rnFiles {
err = os.WriteFile(filepath.Join(tempDir, name), []byte("{}"), 0644)
assert.NoError(t, err)
}
// A non-sourcemap file that must be skipped.
err = os.WriteFile(filepath.Join(tempDir, "assets.png"), []byte("x"), 0644)
assert.NoError(t, err)

files, err := getAllSourceMapFiles(tempDir)
assert.NoError(t, err)

found := make(map[string]bool)
for _, f := range files {
found[f.Name] = true
}
for _, name := range rnFiles {
assert.True(t, found[name], "expected %s to be discovered for upload", name)
}
assert.False(t, found["assets.png"], "non-sourcemap files must be skipped")
}

func TestGetSourceMapUploadUrlsErrors(t *testing.T) {
Expand Down
Loading