Skip to content

Commit f29067f

Browse files
authored
Support import with {type: json} (#1202)
1 parent bcbcd20 commit f29067f

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

server/build.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,14 @@ func (ctx *BuildContext) buildModule(analyzeMode bool) (meta *BuildMeta, include
685685
}, nil
686686
}
687687

688+
if len(args.With) > 0 && args.With["type"] == "json" {
689+
return esbuild.OnResolveResult{
690+
Path: "/" + ctx.esmPath.Name() + utils.NormalizePathname(modulePath) + "?module",
691+
External: true,
692+
SideEffects: esbuild.SideEffectsFalse,
693+
}, nil
694+
}
695+
688696
filename = path.Join(ctx.wd, "node_modules", ctx.esmPath.PkgName, modulePath)
689697

690698
// split the module that includes `export * from "external"` statement

test/issue-1197/test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { assert, assertEquals } from "jsr:@std/assert";
2+
3+
// related issue: https://github.com/esm-dev/esm.sh/issues/1197
4+
Deno.test(
5+
"import with { type: 'json' }",
6+
async () => {
7+
const res = await fetch("http://localhost:8080/aleman@1.1.0/es2022/menu/menu.mjs");
8+
const text = await res.text();
9+
assert(res.ok);
10+
assertEquals(res.headers.get("content-type"), "application/javascript; charset=utf-8");
11+
assertEquals(res.headers.get("cache-control"), "public, max-age=31536000, immutable");
12+
assert(text.includes(`import("/aleman@1.1.0/menu/importmap.json?module")`));
13+
},
14+
);
15+
16+
Deno.test(
17+
"json?module",
18+
async () => {
19+
const res = await fetch("http://localhost:8080/aleman@1.1.0/menu/importmap.json?module");
20+
const text = await res.text();
21+
assert(res.ok);
22+
assertEquals(res.headers.get("content-type"), "application/javascript; charset=utf-8");
23+
assertEquals(res.headers.get("cache-control"), "public, max-age=31536000, immutable");
24+
},
25+
);

0 commit comments

Comments
 (0)