Skip to content

Commit 8a368af

Browse files
authored
Fix json import (#1213)
1 parent fa7cfa2 commit 8a368af

4 files changed

Lines changed: 71 additions & 35 deletions

File tree

server/build.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ func (ctx *BuildContext) buildModule(analyzeMode bool) (meta *BuildMeta, include
489489
if isRelPathSpecifier(specifier) {
490490
specifier = ctx.esmPath.PkgName + "/" + strings.TrimPrefix(specifier, "./")
491491
}
492-
} else if m, ok := v.(map[string]interface{}); ok {
492+
} else if m, ok := v.(map[string]any); ok {
493493
targets := []string{"browser", "module", "import", "default"}
494494
if ctx.isDenoTarget() {
495495
targets = []string{"deno", "module", "import", "default"}
@@ -690,13 +690,18 @@ func (ctx *BuildContext) buildModule(analyzeMode bool) (meta *BuildMeta, include
690690
}, nil
691691
}
692692

693-
if len(args.With) > 0 && args.With["type"] == "json" {
694-
return esbuild.OnResolveResult{
695-
Path: "/" + ctx.esmPath.Name() + utils.NormalizePathname(modulePath) + "?module",
696-
External: true,
697-
SideEffects: esbuild.SideEffectsFalse,
698-
}, nil
699-
}
693+
// if len(args.With) > 0 && args.With["type"] == "json" {
694+
// path := "/" + ctx.esmPath.Name() + utils.NormalizePathname(modulePath)
695+
// if args.Kind == esbuild.ResolveJSDynamicImport {
696+
// // esbuild removes the `{ type: "json" }` when it's a dynamic import
697+
// path += "?module"
698+
// }
699+
// return esbuild.OnResolveResult{
700+
// Path: path,
701+
// External: true,
702+
// SideEffects: esbuild.SideEffectsFalse,
703+
// }, nil
704+
// }
700705

701706
filename = path.Join(ctx.wd, "node_modules", ctx.esmPath.PkgName, modulePath)
702707

server/build_resolver.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -741,8 +741,8 @@ func (ctx *BuildContext) resolveExternalModule(specifier string, kind esbuild.Re
741741
}
742742

743743
// if it's a sub-module of current package
744-
if strings.HasPrefix(specifier, ctx.pkgJson.Name+"/") {
745-
subPath := strings.TrimPrefix(specifier, ctx.pkgJson.Name+"/")
744+
if after, ok := strings.CutPrefix(specifier, ctx.pkgJson.Name+"/"); ok {
745+
subPath := after
746746
subModule := EsmPath{
747747
GhPrefix: ctx.esmPath.GhPrefix,
748748
PrPrefix: ctx.esmPath.PrPrefix,
@@ -759,6 +759,10 @@ func (ctx *BuildContext) resolveExternalModule(specifier string, kind esbuild.Re
759759
resolvedPath = "/" + subModule.Name() + entry.main[1:]
760760
}
761761
}
762+
if kind == esbuild.ResolveJSDynamicImport {
763+
// esbuild removes the `{ type: "json" }` when it's a dynamic import
764+
resolvedPath += "?module"
765+
}
762766
} else {
763767
resolvedPath = ctx.getImportPath(subModule, ctx.getBuildArgsPrefix(false), ctx.externalAll)
764768
if ctx.bundleMode == BundleFalse {
@@ -861,6 +865,10 @@ func (ctx *BuildContext) resolveExternalModule(specifier string, kind esbuild.Re
861865
resolvedPath = "/" + dep.Name() + entry.main[1:]
862866
}
863867
}
868+
if kind == esbuild.ResolveJSDynamicImport {
869+
// esbuild removes the `{ type: "json" }` when it's a dynamic import
870+
resolvedPath += "?module"
871+
}
864872
return
865873
}
866874

test/import-json/test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { assert, assertEquals, assertStringIncludes } from "jsr:@std/assert";
2+
3+
Deno.test(
4+
"bundle json",
5+
async () => {
6+
const res = await fetch("http://localhost:8080/cli-spinners@3.2.1/denonext/cli-spinners.mjs");
7+
const text = await res.text();
8+
assert(res.ok);
9+
assertEquals(res.headers.get("content-type"), "application/javascript; charset=utf-8");
10+
assertEquals(res.headers.get("cache-control"), "public, max-age=31536000, immutable");
11+
assertStringIncludes(text, `dots:{interval:80,frames:[`);
12+
},
13+
);
14+
15+
Deno.test(
16+
"import mod from 'url' with { type: 'json' }",
17+
async () => {
18+
const res = await fetch("http://localhost:8080/cli-spinners@3.2.1/denonext/cli-spinners.nobundle.mjs");
19+
const text = await res.text();
20+
assert(res.ok);
21+
assertEquals(res.headers.get("content-type"), "application/javascript; charset=utf-8");
22+
assertEquals(res.headers.get("cache-control"), "public, max-age=31536000, immutable");
23+
assertStringIncludes(text, `from"/cli-spinners@3.2.1/spinners.json"with{type:"json"}`);
24+
},
25+
);
26+
27+
Deno.test(
28+
"import(url, { type: 'json' })",
29+
async () => {
30+
const res = await fetch("http://localhost:8080/aleman@1.1.0/es2022/menu/menu.mjs");
31+
const text = await res.text();
32+
assert(res.ok);
33+
assertEquals(res.headers.get("content-type"), "application/javascript; charset=utf-8");
34+
assertEquals(res.headers.get("cache-control"), "public, max-age=31536000, immutable");
35+
assertStringIncludes(text, `import("/aleman@1.1.0/menu/importmap.json?module")`);
36+
},
37+
);
38+
39+
Deno.test(
40+
"json?module",
41+
async () => {
42+
const res = await fetch("http://localhost:8080/aleman@1.1.0/menu/importmap.json?module");
43+
const text = await res.text();
44+
assert(res.ok);
45+
assertEquals(res.headers.get("content-type"), "application/javascript; charset=utf-8");
46+
assertEquals(res.headers.get("cache-control"), "public, max-age=31536000, immutable");
47+
},
48+
);

test/issue-1197/test.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)