Skip to content

Commit 5f13d97

Browse files
authored
Fix browser exclude (#1182)
1 parent 11d7231 commit 5f13d97

File tree

6 files changed

+40
-30
lines changed

6 files changed

+40
-30
lines changed

server/build.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,11 @@ func (ctx *BuildContext) buildModule(analyzeMode bool) (meta *BuildMeta, include
510510

511511
// resolve specifier using the `browser` field of package.json
512512
if !isRelPathSpecifier(specifier) && len(pkgJson.Browser) > 0 && ctx.isBrowserTarget() {
513-
if name, ok := pkgJson.Browser[specifier]; ok {
513+
name, ok := pkgJson.Browser[specifier]
514+
if !ok && strings.HasPrefix(specifier, "node:") {
515+
name, ok = pkgJson.Browser[specifier[5:]]
516+
}
517+
if ok {
514518
if name == "" {
515519
return esbuild.OnResolveResult{
516520
Path: args.Path,
@@ -1009,12 +1013,8 @@ func (ctx *BuildContext) buildModule(analyzeMode bool) (meta *BuildMeta, include
10091013
"global.process.env.NODE_ENV": fmt.Sprintf(`"%s"`, nodeEnv),
10101014
}
10111015
} else {
1012-
if ctx.isBrowserTarget() {
1013-
switch ctx.esmPath.PkgName {
1014-
case "react", "react-dom", "typescript":
1015-
// safe to reserve `process` for these packages
1016-
delete(define, "process")
1017-
}
1016+
if ctx.isBrowserTarget() && safeReserveProcessPackages[ctx.esmPath.PkgName] {
1017+
delete(define, "process")
10181018
}
10191019
if ctx.isDenoTarget() {
10201020
// deno 2 has removed the `window` global object, let's replace it with `globalThis`

server/build_resolver.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,11 +1281,12 @@ func normalizeSavePath(zoneId string, pathname string) string {
12811281

12821282
// normalizeImportSpecifier normalizes the given specifier.
12831283
func normalizeImportSpecifier(specifier string) string {
1284-
if specifier == "." {
1284+
switch specifier {
1285+
case ".":
12851286
specifier = "./index"
1286-
} else if specifier == ".." {
1287+
case "..":
12871288
specifier = "../index"
1288-
} else {
1289+
default:
12891290
specifier = strings.TrimPrefix(specifier, "npm:")
12901291
}
12911292
if nodeBuiltinModules[specifier] {

server/consts.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,10 @@ var forceNpmSpecifiers = map[string]bool{
137137
"zlib-sync": true,
138138
"css-tree": true,
139139
}
140+
141+
// packages that are safe to reserve global `process` variable for browser target
142+
var safeReserveProcessPackages = map[string]bool{
143+
"react": true,
144+
"react-dom": true,
145+
"typescript": true,
146+
}

test/common/misc.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { assertEquals } from "jsr:@std/assert";
33
import { BigInteger } from "http://localhost:8080/[email protected]";
44
import { Netmask } from "http://localhost:8080/[email protected]";
55
import { parseStringPromise } from "http://localhost:8080/[email protected]";
6-
import compareVersions from "http://localhost:8080/[email protected]";
76
import { createTheme } from "http://localhost:8080/[email protected]";
7+
import compareVersions from "http://localhost:8080/[email protected]";
88

99
Deno.test("fix some invalid exports", () => {
1010
assertEquals(typeof BigInteger, "function");

test/common/typescript.test.ts

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

test/typescript/test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { assertEquals } from "jsr:@std/assert";
2+
3+
Deno.test("typescript", async () => {
4+
const ts = await import("http://localhost:8080/[email protected]");
5+
const result = ts.transpileModule(`let x: string = "string"`, {
6+
compilerOptions: { module: ts.ModuleKind.CommonJS },
7+
});
8+
assertEquals(ts.version, "5.5.4");
9+
assertEquals(result.outputText, `var x = "string";\n`);
10+
});
11+
12+
Deno.test("typescript (target=browser)", async () => {
13+
const js = await fetch("http://localhost:8080/[email protected]", {
14+
headers: {
15+
"user-agent": "i'm a browser",
16+
},
17+
}).then(res => res.text());
18+
if (/\/node\/\w+\.mjs/.test(js)) {
19+
throw new Error("node builtin modules should not be included in browser target");
20+
}
21+
});

0 commit comments

Comments
 (0)