Skip to content

Commit 8f506d5

Browse files
committed
fix #4292: support with { type: bytes }
1 parent d6b668f commit 8f506d5

4 files changed

Lines changed: 78 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
* Add support for `with { type: 'bytes' }` imports ([#4292](https://github.com/evanw/esbuild/issues/4292))
6+
7+
The [import bytes](https://github.com/tc39/proposal-import-bytes) proposal has reached stage 2.7 in the TC39 process, which means that although it isn't quite recommended for implementation, it's generally approved and ready for validation. Furthermore it has already been implemented by [Deno](https://docs.deno.com/examples/importing_bytes/) and [Webpack](https://github.com/webpack/webpack/pull/19928). So with this release, esbuild will also add support for this. It behaves exactly the same as esbuild's existing [`binary` loader](https://esbuild.github.io/content-types/#binary). Here's an example:
8+
9+
```js
10+
import data from './image.png' with { type: 'bytes' }
11+
const view = new DataView(data.buffer, 0, 24)
12+
const width = view.getInt32(16)
13+
const height = view.getInt32(20)
14+
console.log('size:', width + '\xD7' + height)
15+
```
16+
317
## 0.25.10
418

519
* Fix a panic in a minification edge case ([#4287](https://github.com/evanw/esbuild/issues/4287))

internal/bundler/bundler.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,16 @@ func parseFile(args parseArgs) {
203203
var errorText string
204204
var errorRange js_lexer.KeyOrValue
205205

206-
// We only currently handle "type: json"
206+
// We only currently handle "type: json" and "type: bytes"
207207
if attr.Key != "type" {
208208
errorText = fmt.Sprintf("Importing with the %q attribute is not supported", attr.Key)
209209
errorRange = js_lexer.KeyRange
210210
} else if attr.Value == "json" {
211211
loader = config.LoaderWithTypeJSON
212212
continue
213+
} else if attr.Value == "bytes" {
214+
loader = config.LoaderBinary
215+
continue
213216
} else {
214217
errorText = fmt.Sprintf("Importing with a type attribute of %q is not supported", attr.Value)
215218
errorRange = js_lexer.ValueRange

internal/bundler_tests/bundler_loader_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,39 @@ func TestWithTypeJSONOverrideLoaderGlob(t *testing.T) {
13211321
})
13221322
}
13231323

1324+
func TestWithTypeBytesOverrideLoader(t *testing.T) {
1325+
loader_suite.expectBundled(t, bundled{
1326+
files: map[string]string{
1327+
"/entry.js": `
1328+
import foo from './foo.js' with { type: 'bytes' }
1329+
console.log(foo)
1330+
`,
1331+
"/foo.js": `export default 'js'`,
1332+
},
1333+
entryPaths: []string{"/entry.js"},
1334+
options: config.Options{
1335+
Mode: config.ModeBundle,
1336+
AbsOutputFile: "/out.js",
1337+
},
1338+
})
1339+
}
1340+
1341+
func TestWithTypeBytesOverrideLoaderGlob(t *testing.T) {
1342+
loader_suite.expectBundled(t, bundled{
1343+
files: map[string]string{
1344+
"/entry.js": `
1345+
import("./foo" + bar, { with: { type: 'bytes' } }).then(console.log)
1346+
`,
1347+
"/foo.js": `export default 'js'`,
1348+
},
1349+
entryPaths: []string{"/entry.js"},
1350+
options: config.Options{
1351+
Mode: config.ModeBundle,
1352+
AbsOutputFile: "/out.js",
1353+
},
1354+
})
1355+
}
1356+
13241357
func TestWithBadType(t *testing.T) {
13251358
loader_suite.expectBundled(t, bundled{
13261359
files: map[string]string{

internal/bundler_tests/snapshots/snapshots_loader.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,6 +1163,33 @@ var require_test = __commonJS({
11631163
// entry.js
11641164
console.log(require_test());
11651165

1166+
================================================================================
1167+
TestWithTypeBytesOverrideLoader
1168+
---------- /out.js ----------
1169+
// foo.js
1170+
var foo_default = __toBinary("ZXhwb3J0IGRlZmF1bHQgJ2pzJw==");
1171+
1172+
// entry.js
1173+
console.log(foo_default);
1174+
1175+
================================================================================
1176+
TestWithTypeBytesOverrideLoaderGlob
1177+
---------- /out.js ----------
1178+
// foo.js
1179+
var require_foo = __commonJS({
1180+
"foo.js"(exports, module) {
1181+
module.exports = __toBinary("ZXhwb3J0IGRlZmF1bHQgJ2pzJw==");
1182+
}
1183+
});
1184+
1185+
// import("./foo*") in entry.js
1186+
var globImport_foo = __glob({
1187+
"./foo.js": () => Promise.resolve().then(() => __toESM(require_foo()))
1188+
});
1189+
1190+
// entry.js
1191+
globImport_foo("./foo" + bar).then(console.log);
1192+
11661193
================================================================================
11671194
TestWithTypeJSONOverrideLoader
11681195
---------- /out.js ----------

0 commit comments

Comments
 (0)