Commit 57853cb
authored
feat(bundle): support
Closes #30024
## Summary
Implements the object form of the npm `browser` field for `deno bundle
--platform browser`. The simple string form was already supported; the
mapped form was silently dropped.
```json
"browser": {
"./bar.js": "./bar.browser.js",
"crypto": false,
"foo": "./shims/foo.js"
}
```
- relative-path keys remap the resolved file (`./bar.js` →
`./bar.browser.js`)
- bare-name keys remap an import specifier (`foo` → `./shims/foo.js`),
with `node:` prefix stripped before lookup so `import "node:crypto"`
matches a `"crypto"` key
- a value of `false` disables the module — the bundler substitutes an
empty stub
Esbuild's built-in browser-map handling is bypassed because Deno's
`on_resolve` plugin claims every resolve with filter `.*`, so we have to
apply the mapping ourselves.
## Where the logic lives
- **`libs/package_json`** — parses the object form into `browser_map:
Option<IndexMap<String, BrowserMapEntry>>` (`Replace(String) |
Disabled`). The string form keeps populating the existing `browser`
field.
- **`libs/node_resolver`** — substitution happens here, gated on the
existing `prefer_browser_field` flag (only set when bundling with
`--platform browser`). Hooks in `resolve`, `resolve_package`, and
`resolve_package_subpath_from_deno_module`. A new
`BrowserMapDisabledError` propagates `false` entries up to the caller.
- **`cli/tools/bundle/mod.rs`** — only catches `BrowserMapDisabledError`
from the resolver, returns a `\0deno-browser-disabled:<orig>` sentinel
path, and the `on_load` hook turns it into `module.exports = {}`.
## Test plan
- [x] New spec `tests/specs/bundle/browser_platform_map` covers all four
cases (relative remap, bare remap, bare disabled, transitive
within-package import) using new npm fixture
`@denotest/browser-field-map`
- [x] All 49 existing `bundle::*` spec tests still pass
- [x] `node_resolver` unit tests still pass
- [x] `--platform deno` bundles are unchanged (gate is
`prefer_browser_field`)browser field map in package.json (#34407)1 parent 3cdfe7e commit 57853cb
15 files changed
Lines changed: 437 additions & 8 deletions
File tree
- cli/tools/bundle
- libs
- node_resolver
- package_json
- resolver/npm
- tests
- registry/npm/@denotest/browser-field-map/1.0.0
- dir-extensionless
- shims
- specs/bundle/browser_platform_map
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
851 | 851 | | |
852 | 852 | | |
853 | 853 | | |
| 854 | + | |
| 855 | + | |
| 856 | + | |
| 857 | + | |
854 | 858 | | |
855 | 859 | | |
856 | 860 | | |
| |||
1013 | 1017 | | |
1014 | 1018 | | |
1015 | 1019 | | |
| 1020 | + | |
| 1021 | + | |
| 1022 | + | |
| 1023 | + | |
| 1024 | + | |
| 1025 | + | |
| 1026 | + | |
| 1027 | + | |
| 1028 | + | |
| 1029 | + | |
| 1030 | + | |
1016 | 1031 | | |
1017 | 1032 | | |
1018 | 1033 | | |
| |||
1068 | 1083 | | |
1069 | 1084 | | |
1070 | 1085 | | |
| 1086 | + | |
| 1087 | + | |
| 1088 | + | |
| 1089 | + | |
| 1090 | + | |
| 1091 | + | |
| 1092 | + | |
1071 | 1093 | | |
1072 | 1094 | | |
1073 | 1095 | | |
| |||
1215 | 1237 | | |
1216 | 1238 | | |
1217 | 1239 | | |
| 1240 | + | |
| 1241 | + | |
| 1242 | + | |
| 1243 | + | |
| 1244 | + | |
| 1245 | + | |
| 1246 | + | |
| 1247 | + | |
| 1248 | + | |
| 1249 | + | |
| 1250 | + | |
| 1251 | + | |
| 1252 | + | |
| 1253 | + | |
| 1254 | + | |
| 1255 | + | |
| 1256 | + | |
| 1257 | + | |
| 1258 | + | |
| 1259 | + | |
| 1260 | + | |
| 1261 | + | |
| 1262 | + | |
1218 | 1263 | | |
1219 | 1264 | | |
1220 | 1265 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
227 | 227 | | |
228 | 228 | | |
229 | 229 | | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
230 | 233 | | |
231 | 234 | | |
232 | 235 | | |
| |||
240 | 243 | | |
241 | 244 | | |
242 | 245 | | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
243 | 249 | | |
244 | 250 | | |
245 | 251 | | |
| |||
251 | 257 | | |
252 | 258 | | |
253 | 259 | | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
254 | 263 | | |
255 | 264 | | |
256 | 265 | | |
| |||
264 | 273 | | |
265 | 274 | | |
266 | 275 | | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
267 | 279 | | |
268 | 280 | | |
269 | 281 | | |
| |||
277 | 289 | | |
278 | 290 | | |
279 | 291 | | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
280 | 295 | | |
281 | 296 | | |
282 | 297 | | |
| |||
691 | 706 | | |
692 | 707 | | |
693 | 708 | | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
694 | 720 | | |
695 | 721 | | |
696 | 722 | | |
| |||
714 | 740 | | |
715 | 741 | | |
716 | 742 | | |
717 | | - | |
| 743 | + | |
| 744 | + | |
718 | 745 | | |
719 | 746 | | |
720 | 747 | | |
| |||
751 | 778 | | |
752 | 779 | | |
753 | 780 | | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
754 | 784 | | |
755 | 785 | | |
756 | 786 | | |
| |||
769 | 799 | | |
770 | 800 | | |
771 | 801 | | |
| 802 | + | |
772 | 803 | | |
773 | 804 | | |
774 | 805 | | |
| |||
785 | 816 | | |
786 | 817 | | |
787 | 818 | | |
| 819 | + | |
788 | 820 | | |
789 | 821 | | |
790 | 822 | | |
| |||
0 commit comments