Skip to content

Commit d9b3838

Browse files
committed
feat: load import map reference in deno config
1 parent fd96e19 commit d9b3838

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

internal/functions/deploy/bundle.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,6 @@ func GetBindMounts(cwd, hostFuncDir, hostOutputDir, hostEntrypointPath, hostImpo
127127
if err != nil {
128128
return nil, err
129129
}
130-
if len(hostImportMapPath) > 0 {
131-
if !filepath.IsAbs(hostImportMapPath) {
132-
hostImportMapPath = filepath.Join(cwd, hostImportMapPath)
133-
}
134-
dockerImportMapPath := utils.ToDockerPath(hostImportMapPath)
135-
modules = append(modules, hostImportMapPath+":"+dockerImportMapPath+":ro")
136-
}
137130
// Remove any duplicate mount points
138131
for _, mod := range modules {
139132
hostPath := strings.Split(mod, ":")[0]

internal/utils/deno.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,19 @@ func CopyDenoScripts(ctx context.Context, fsys afero.Fs) (*DenoScriptDir, error)
210210
}
211211

212212
func BindHostModules(cwd, relEntrypointPath, relImportMapPath string, fsys afero.Fs) ([]string, error) {
213+
var modules []string
214+
bindModule := func(srcPath string, r io.Reader) error {
215+
hostPath := filepath.Join(cwd, filepath.FromSlash(srcPath))
216+
dockerPath := ToDockerPath(hostPath)
217+
modules = append(modules, hostPath+":"+dockerPath+":ro")
218+
return nil
219+
}
213220
importMap := function.ImportMap{}
214221
if imPath := filepath.ToSlash(relImportMapPath); len(imPath) > 0 {
215-
if err := importMap.Load(imPath, afero.NewIOFS(fsys)); err != nil {
222+
if err := importMap.LoadAsDeno(imPath, afero.NewIOFS(fsys), bindModule); err != nil {
216223
return nil, err
217224
}
218225
}
219-
var modules []string
220226
// Resolving all Import Graph
221227
addModule := func(unixPath string, w io.Writer) error {
222228
hostPath := filepath.FromSlash(unixPath)

pkg/function/deploy.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func writeForm(form *multipart.Writer, meta api.FunctionDeployMetadata, fsys fs.
171171
// Add import map
172172
importMap := ImportMap{}
173173
if imPath := cast.Val(meta.ImportMapPath, ""); len(imPath) > 0 {
174-
if err := importMap.Load(imPath, fsys, uploadAsset); err != nil {
174+
if err := importMap.LoadAsDeno(imPath, fsys, uploadAsset); err != nil {
175175
return err
176176
}
177177
}
@@ -192,6 +192,26 @@ func writeForm(form *multipart.Writer, meta api.FunctionDeployMetadata, fsys fs.
192192
type ImportMap struct {
193193
Imports map[string]string `json:"imports"`
194194
Scopes map[string]map[string]string `json:"scopes"`
195+
// Fallback reference for deno.json
196+
ImportMap string `json:"importMap"`
197+
}
198+
199+
func (m *ImportMap) LoadAsDeno(imPath string, fsys fs.FS, opts ...func(string, io.Reader) error) error {
200+
if err := m.Load(imPath, fsys, opts...); err != nil {
201+
return err
202+
}
203+
if strings.EqualFold(path.Base(imPath), "deno.json") && m.IsReference() {
204+
imPath = path.Join(path.Dir(imPath), m.ImportMap)
205+
if err := m.Load(imPath, fsys, opts...); err != nil {
206+
return err
207+
}
208+
}
209+
return nil
210+
}
211+
212+
func (m *ImportMap) IsReference() bool {
213+
// Ref: https://github.com/denoland/deno/blob/main/cli/schemas/config-file.v1.json#L273
214+
return len(m.Imports) == 0 && len(m.Scopes) == 0 && len(m.ImportMap) > 0
195215
}
196216

197217
func (m *ImportMap) Load(imPath string, fsys fs.FS, opts ...func(string, io.Reader) error) error {

0 commit comments

Comments
 (0)