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
17 changes: 9 additions & 8 deletions pkg/function/deno.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"io/fs"
"net/url"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -54,7 +55,7 @@ func (m *ImportMap) Load(imPath string, fsys fs.FS, opts ...func(string, io.Read
if err := m.Parse(data); err != nil {
return err
}
if err := m.Resolve(imPath, fsys); err != nil {
if err := m.Resolve(imPath); err != nil {
return err
}
for _, apply := range opts {
Expand All @@ -74,29 +75,29 @@ func (m *ImportMap) Parse(data []byte) error {
return nil
}

func (m *ImportMap) Resolve(imPath string, fsys fs.FS) error {
func (m *ImportMap) Resolve(imPath string) error {
// Resolve all paths relative to current file
for k, v := range m.Imports {
m.Imports[k] = resolveHostPath(imPath, v, fsys)
m.Imports[k] = resolveHostPath(imPath, v)
}
for module, mapping := range m.Scopes {
for k, v := range mapping {
m.Scopes[module][k] = resolveHostPath(imPath, v, fsys)
m.Scopes[module][k] = resolveHostPath(imPath, v)
}
}
return nil
}

func resolveHostPath(jsonPath, hostPath string, fsys fs.FS) string {
func resolveHostPath(jsonPath, hostPath string) string {
// Leave absolute paths unchanged
if path.IsAbs(hostPath) {
return hostPath
}
resolved := path.Join(path.Dir(jsonPath), hostPath)
if _, err := fs.Stat(fsys, filepath.FromSlash(resolved)); err != nil {
// Leave URLs unchanged
// Leave URLs unchanged
if parsed, err := url.Parse(hostPath); err == nil && len(parsed.Scheme) > 0 {
return hostPath
}
resolved := path.Join(path.Dir(jsonPath), hostPath)
// Directory imports need to be suffixed with /
// Ref: https://deno.com/manual@v1.33.0/basics/import_maps
if strings.HasSuffix(hostPath, "/") {
Expand Down
6 changes: 3 additions & 3 deletions pkg/function/deno_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestImportPaths(t *testing.T) {
im := ImportMap{Imports: map[string]string{
"module-name/": "../shared/",
}}
assert.NoError(t, im.Resolve("testdata/modules/deno.json", testImports))
assert.NoError(t, im.Resolve("testdata/modules/deno.json"))
// Run test
err := im.WalkImportPaths("testdata/modules/imports.ts", fsys.ReadFile)
// Check error
Expand All @@ -79,7 +79,7 @@ func TestImportPaths(t *testing.T) {
im := ImportMap{Imports: map[string]string{
"module-name/": "./shared/",
}}
assert.NoError(t, im.Resolve("testdata/import_map.json", testImports))
assert.NoError(t, im.Resolve("testdata/import_map.json"))
// Run test
err := im.WalkImportPaths("testdata/modules/imports.ts", fsys.ReadFile)
// Check error
Expand Down Expand Up @@ -116,7 +116,7 @@ func TestResolveImports(t *testing.T) {
assert.Equal(t, "./common", resolved.Imports["root"])
assert.Equal(t, "./supabase/tests", resolved.Imports["parent"])
assert.Equal(t, "./supabase/functions/child/", resolved.Imports["child"])
assert.Equal(t, "../missing", resolved.Imports["missing"])
assert.Equal(t, "./supabase/missing", resolved.Imports["missing"])
})

t.Run("resolves parent scopes", func(t *testing.T) {
Expand Down