Skip to content

Commit

Permalink
feat(config): load local deno.json file as import map (#2824)
Browse files Browse the repository at this point in the history
* feat: load local deno.json file

* move logic to config

* use pattern logic and funcSlugPattern

* fix new tests

* chore: undo functions glob

---------

Co-authored-by: Qiao Han <[email protected]>
  • Loading branch information
jgoux and sweatybridge authored Nov 3, 2024
1 parent fefeebd commit 4cf189a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 8 deletions.
8 changes: 6 additions & 2 deletions internal/functions/deploy/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,12 +322,16 @@ func TestImportMapPath(t *testing.T) {
}
// Setup in-memory fs
fsys := afero.NewMemMapFs()
// Custom global import map loaded via cli flag
customImportMapPath := filepath.Join(utils.FunctionsDir, "custom_import_map.json")
require.NoError(t, afero.WriteFile(fsys, customImportMapPath, []byte("{}"), 0644))
// Create fallback import map to test precedence order
require.NoError(t, afero.WriteFile(fsys, utils.FallbackImportMapPath, []byte("{}"), 0644))
// Run test
fc, err := GetFunctionConfig([]string{slug}, utils.FallbackImportMapPath, cast.Ptr(false), fsys)
fc, err := GetFunctionConfig([]string{slug}, customImportMapPath, cast.Ptr(false), fsys)
// Check error
assert.NoError(t, err)
assert.Equal(t, utils.FallbackImportMapPath, fc[slug].ImportMap)
assert.Equal(t, customImportMapPath, fc[slug].ImportMap)
})

t.Run("returns empty string if no fallback", func(t *testing.T) {
Expand Down
19 changes: 13 additions & 6 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ import (
"github.com/joho/godotenv"
"github.com/mitchellh/mapstructure"
"github.com/spf13/viper"
"golang.org/x/mod/semver"

"github.com/supabase/cli/pkg/fetcher"
"golang.org/x/mod/semver"
)

// Type for turning human-friendly bytes string ("5MB", "32kB") into an int64 during toml decoding.
Expand Down Expand Up @@ -654,21 +653,29 @@ func (c *config) Load(path string, fsys fs.FS) error {
}
c.Storage.Buckets[name] = bucket
}
// Resolve functions config
for slug, function := range c.Functions {
// TODO: support configuring alternative entrypoint path, such as index.js
if len(function.Entrypoint) == 0 {
function.Entrypoint = filepath.Join(builder.FunctionsDir, slug, "index.ts")
} else if !filepath.IsAbs(function.Entrypoint) {
// Append supabase/ because paths in configs are specified relative to config.toml
function.Entrypoint = filepath.Join(builder.SupabaseDirPath, function.Entrypoint)
}
// Functions may not use import map so we don't set a default value
if len(function.ImportMap) > 0 && !filepath.IsAbs(function.ImportMap) {
if len(function.ImportMap) == 0 {
functionDir := filepath.Dir(function.Entrypoint)
denoJsonPath := filepath.Join(functionDir, "deno.json")
denoJsoncPath := filepath.Join(functionDir, "deno.jsonc")
if _, err := fs.Stat(fsys, denoJsonPath); err == nil {
function.ImportMap = denoJsonPath
} else if _, err := fs.Stat(fsys, denoJsoncPath); err == nil {
function.ImportMap = denoJsoncPath
}
// Functions may not use import map so we don't set a default value
} else if !filepath.IsAbs(function.ImportMap) {
function.ImportMap = filepath.Join(builder.SupabaseDirPath, function.ImportMap)
}
c.Functions[slug] = function
}

if err := c.Db.Seed.loadSeedPaths(builder.SupabaseDirPath, fsys); err != nil {
return err
}
Expand Down
51 changes: 51 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,3 +356,54 @@ func TestLoadEnv(t *testing.T) {
assert.Equal(t, "test-secret", config.Auth.JwtSecret)
assert.Equal(t, "test-root-key", config.Db.RootKey)
}

func TestLoadFunctionImportMap(t *testing.T) {
t.Run("uses deno.json as import map when present", func(t *testing.T) {
config := NewConfig()
fsys := fs.MapFS{
"supabase/config.toml": &fs.MapFile{Data: []byte(`
project_id = "test"
[functions.hello]
`)},
"supabase/functions/hello/deno.json": &fs.MapFile{},
"supabase/functions/hello/index.ts": &fs.MapFile{},
}
// Run test
assert.NoError(t, config.Load("", fsys))
// Check that deno.json was set as import map
assert.Equal(t, "supabase/functions/hello/deno.json", config.Functions["hello"].ImportMap)
})

t.Run("uses deno.jsonc as import map when present", func(t *testing.T) {
config := NewConfig()
fsys := fs.MapFS{
"supabase/config.toml": &fs.MapFile{Data: []byte(`
project_id = "test"
[functions.hello]
`)},
"supabase/functions/hello/deno.jsonc": &fs.MapFile{},
"supabase/functions/hello/index.ts": &fs.MapFile{},
}
// Run test
assert.NoError(t, config.Load("", fsys))
// Check that deno.json was set as import map
assert.Equal(t, "supabase/functions/hello/deno.jsonc", config.Functions["hello"].ImportMap)
})

t.Run("config.toml takes precedence over deno.json", func(t *testing.T) {
config := NewConfig()
fsys := fs.MapFS{
"supabase/config.toml": &fs.MapFile{Data: []byte(`
project_id = "test"
[functions]
hello.import_map = "custom_import_map.json"
`)},
"supabase/functions/hello/deno.json": &fs.MapFile{},
"supabase/functions/hello/index.ts": &fs.MapFile{},
}
// Run test
assert.NoError(t, config.Load("", fsys))
// Check that config.toml takes precedence over deno.json
assert.Equal(t, "supabase/custom_import_map.json", config.Functions["hello"].ImportMap)
})
}

0 comments on commit 4cf189a

Please sign in to comment.