Skip to content

Commit

Permalink
Fix importing json as a module (close #601)
Browse files Browse the repository at this point in the history
  • Loading branch information
ije committed Apr 19, 2023
1 parent 2dc8a05 commit 44c6d74
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
29 changes: 29 additions & 0 deletions server/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"net/url"
"os"
"path"
Expand Down Expand Up @@ -268,6 +269,13 @@ rebuild:
}
}

if strings.HasSuffix(args.Path, ".json") {
jsonFile := filepath.Join(args.ResolveDir, args.Path)
if fileExists(jsonFile) {
return api.OnResolveResult{Path: args.Path, Namespace: "json", PluginData: jsonFile}, nil
}
}

for _, name := range nativeNodePackages {
if args.Path == name || strings.HasPrefix(args.Path, name+"/") {
if task.Target == "deno" || task.Target == "denonext" {
Expand Down Expand Up @@ -484,6 +492,27 @@ rebuild:
},
)

// import info from "./packge.json"
build.OnLoad(
api.OnLoadOptions{Filter: ".*", Namespace: "json"},
func(args api.OnLoadArgs) (ret api.OnLoadResult, err error) {
jsonFile, ok := args.PluginData.(string)
if !ok {
return api.OnLoadResult{}, errors.New("plugin data is not a string")
}
data, err := ioutil.ReadFile(jsonFile)
if err != nil {
return api.OnLoadResult{}, err
}
prefix := []byte("export default ")
code := make([]byte, len(prefix)+len(data))
copy(code, prefix)
copy(code[len(prefix):], data)
contents := string(code)
return api.OnLoadResult{Contents: &contents, Loader: api.LoaderJS}, nil
},
)

// for browser exclude
build.OnLoad(
api.OnLoadOptions{Filter: ".*", Namespace: "browser-exclude"},
Expand Down
7 changes: 7 additions & 0 deletions test/issue-601/issue-601.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";

import {} from "http://localhost:8080/@aws-sdk/[email protected]?dev";

Deno.test("issue #601", async () => {
assertEquals(typeof Location, "function");
});

0 comments on commit 44c6d74

Please sign in to comment.