Skip to content

Commit 8b0da1d

Browse files
committed
fix #2337: add support for font file MIME types
1 parent 20f4b45 commit 8b0da1d

File tree

4 files changed

+201
-6
lines changed

4 files changed

+201
-6
lines changed

CHANGELOG.md

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

3+
## Unreleased
4+
5+
* Add support for font file MIME types ([#2337](https://github.com/evanw/esbuild/issues/2337))
6+
7+
This release adds support for font file MIME types to esbuild, which means they are now recognized by the built-in local web server and they are now used when a font file is loaded using the `dataurl` loader. The full set of newly-added file extension MIME type mappings is as follows:
8+
9+
* `.eot` => `application/vnd.ms-fontobject`
10+
* `.otf` => `font/otf`
11+
* `.sfnt` => `font/sfnt`
12+
* `.ttf` => `font/ttf`
13+
* `.woff` => `font/woff`
14+
* `.woff2` => `font/woff2`
15+
316
## 0.14.47
417

518
* Make global names more compact when `||=` is available ([#2331](https://github.com/evanw/esbuild/issues/2331))

internal/bundler/bundler_loader_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,86 @@ func TestLoaderDataURLUnknownMIME(t *testing.T) {
960960
})
961961
}
962962

963+
func TestLoaderDataURLExtensionBasedMIME(t *testing.T) {
964+
default_suite.expectBundled(t, bundled{
965+
files: map[string]string{
966+
"/entry.foo": `
967+
export { default as css } from "./example.css"
968+
export { default as eot } from "./example.eot"
969+
export { default as gif } from "./example.gif"
970+
export { default as htm } from "./example.htm"
971+
export { default as html } from "./example.html"
972+
export { default as jpeg } from "./example.jpeg"
973+
export { default as jpg } from "./example.jpg"
974+
export { default as js } from "./example.js"
975+
export { default as json } from "./example.json"
976+
export { default as mjs } from "./example.mjs"
977+
export { default as otf } from "./example.otf"
978+
export { default as pdf } from "./example.pdf"
979+
export { default as png } from "./example.png"
980+
export { default as sfnt } from "./example.sfnt"
981+
export { default as svg } from "./example.svg"
982+
export { default as ttf } from "./example.ttf"
983+
export { default as wasm } from "./example.wasm"
984+
export { default as webp } from "./example.webp"
985+
export { default as woff } from "./example.woff"
986+
export { default as woff2 } from "./example.woff2"
987+
export { default as xml } from "./example.xml"
988+
`,
989+
"/example.css": `css`,
990+
"/example.eot": `eot`,
991+
"/example.gif": `gif`,
992+
"/example.htm": `htm`,
993+
"/example.html": `html`,
994+
"/example.jpeg": `jpeg`,
995+
"/example.jpg": `jpg`,
996+
"/example.js": `js`,
997+
"/example.json": `json`,
998+
"/example.mjs": `mjs`,
999+
"/example.otf": `otf`,
1000+
"/example.pdf": `pdf`,
1001+
"/example.png": `png`,
1002+
"/example.sfnt": `sfnt`,
1003+
"/example.svg": `svg`,
1004+
"/example.ttf": `ttf`,
1005+
"/example.wasm": `wasm`,
1006+
"/example.webp": `webp`,
1007+
"/example.woff": `woff`,
1008+
"/example.woff2": `woff2`,
1009+
"/example.xml": `xml`,
1010+
},
1011+
entryPaths: []string{"/entry.foo"},
1012+
options: config.Options{
1013+
Mode: config.ModeBundle,
1014+
AbsOutputDir: "/out",
1015+
ExtensionToLoader: map[string]config.Loader{
1016+
".foo": config.LoaderJS,
1017+
".css": config.LoaderDataURL,
1018+
".eot": config.LoaderDataURL,
1019+
".gif": config.LoaderDataURL,
1020+
".htm": config.LoaderDataURL,
1021+
".html": config.LoaderDataURL,
1022+
".jpeg": config.LoaderDataURL,
1023+
".jpg": config.LoaderDataURL,
1024+
".js": config.LoaderDataURL,
1025+
".json": config.LoaderDataURL,
1026+
".mjs": config.LoaderDataURL,
1027+
".otf": config.LoaderDataURL,
1028+
".pdf": config.LoaderDataURL,
1029+
".png": config.LoaderDataURL,
1030+
".sfnt": config.LoaderDataURL,
1031+
".svg": config.LoaderDataURL,
1032+
".ttf": config.LoaderDataURL,
1033+
".wasm": config.LoaderDataURL,
1034+
".webp": config.LoaderDataURL,
1035+
".woff": config.LoaderDataURL,
1036+
".woff2": config.LoaderDataURL,
1037+
".xml": config.LoaderDataURL,
1038+
},
1039+
},
1040+
})
1041+
}
1042+
9631043
func TestLoaderCopyWithBundleFromJS(t *testing.T) {
9641044
default_suite.expectBundled(t, bundled{
9651045
files: map[string]string{

internal/bundler/snapshots/snapshots_default.txt

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1967,6 +1967,95 @@ console.log([
19671967
json_charset_UTF_8_base64_eyJ3b3JrcyI6dHJ1ZX0_default
19681968
]);
19691969

1970+
================================================================================
1971+
TestLoaderDataURLExtensionBasedMIME
1972+
---------- /out/entry.js ----------
1973+
// example.css
1974+
var example_default = "data:text/css;charset=utf-8;base64,Y3Nz";
1975+
1976+
// example.eot
1977+
var example_default2 = "data:application/vnd.ms-fontobject;base64,ZW90";
1978+
1979+
// example.gif
1980+
var example_default3 = "data:image/gif;base64,Z2lm";
1981+
1982+
// example.htm
1983+
var example_default4 = "data:text/html;charset=utf-8;base64,aHRt";
1984+
1985+
// example.html
1986+
var example_default5 = "data:text/html;charset=utf-8;base64,aHRtbA==";
1987+
1988+
// example.jpeg
1989+
var example_default6 = "data:image/jpeg;base64,anBlZw==";
1990+
1991+
// example.jpg
1992+
var example_default7 = "data:image/jpeg;base64,anBn";
1993+
1994+
// example.js
1995+
var example_default8 = "data:text/javascript;charset=utf-8;base64,anM=";
1996+
1997+
// example.json
1998+
var example_default9 = "data:application/json;base64,anNvbg==";
1999+
2000+
// example.mjs
2001+
var example_default10 = "data:text/javascript;charset=utf-8;base64,bWpz";
2002+
2003+
// example.otf
2004+
var example_default11 = "data:font/otf;base64,b3Rm";
2005+
2006+
// example.pdf
2007+
var example_default12 = "data:application/pdf;base64,cGRm";
2008+
2009+
// example.png
2010+
var example_default13 = "data:image/png;base64,cG5n";
2011+
2012+
// example.sfnt
2013+
var example_default14 = "data:font/sfnt;base64,c2ZudA==";
2014+
2015+
// example.svg
2016+
var example_default15 = "data:image/svg+xml;base64,c3Zn";
2017+
2018+
// example.ttf
2019+
var example_default16 = "data:font/ttf;base64,dHRm";
2020+
2021+
// example.wasm
2022+
var example_default17 = "data:application/wasm;base64,d2FzbQ==";
2023+
2024+
// example.webp
2025+
var example_default18 = "data:image/webp;base64,d2VicA==";
2026+
2027+
// example.woff
2028+
var example_default19 = "data:font/woff;base64,d29mZg==";
2029+
2030+
// example.woff2
2031+
var example_default20 = "data:font/woff2;base64,d29mZjI=";
2032+
2033+
// example.xml
2034+
var example_default21 = "data:text/xml;charset=utf-8;base64,eG1s";
2035+
export {
2036+
example_default as css,
2037+
example_default2 as eot,
2038+
example_default3 as gif,
2039+
example_default4 as htm,
2040+
example_default5 as html,
2041+
example_default6 as jpeg,
2042+
example_default7 as jpg,
2043+
example_default8 as js,
2044+
example_default9 as json,
2045+
example_default10 as mjs,
2046+
example_default11 as otf,
2047+
example_default12 as pdf,
2048+
example_default13 as png,
2049+
example_default14 as sfnt,
2050+
example_default15 as svg,
2051+
example_default16 as ttf,
2052+
example_default17 as wasm,
2053+
example_default18 as webp,
2054+
example_default19 as woff,
2055+
example_default20 as woff2,
2056+
example_default21 as xml
2057+
};
2058+
19702059
================================================================================
19712060
TestLoaderDataURLTextCSS
19722061
---------- /out/entry.css ----------

internal/helpers/mime.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,34 @@ package helpers
33
import "strings"
44

55
var builtinTypesLower = map[string]string{
6+
// Text
67
".css": "text/css; charset=utf-8",
7-
".gif": "image/gif",
88
".htm": "text/html; charset=utf-8",
99
".html": "text/html; charset=utf-8",
10-
".jpeg": "image/jpeg",
11-
".jpg": "image/jpeg",
1210
".js": "text/javascript; charset=utf-8",
1311
".json": "application/json",
1412
".mjs": "text/javascript; charset=utf-8",
15-
".pdf": "application/pdf",
13+
".xml": "text/xml; charset=utf-8",
14+
15+
// Images
16+
".gif": "image/gif",
17+
".jpeg": "image/jpeg",
18+
".jpg": "image/jpeg",
1619
".png": "image/png",
1720
".svg": "image/svg+xml",
18-
".wasm": "application/wasm",
1921
".webp": "image/webp",
20-
".xml": "text/xml; charset=utf-8",
22+
23+
// Fonts
24+
".eot": "application/vnd.ms-fontobject",
25+
".otf": "font/otf",
26+
".sfnt": "font/sfnt",
27+
".ttf": "font/ttf",
28+
".woff": "font/woff",
29+
".woff2": "font/woff2",
30+
31+
// Other
32+
".pdf": "application/pdf",
33+
".wasm": "application/wasm",
2134
}
2235

2336
// This is used instead of Go's built-in "mime.TypeByExtension" function because

0 commit comments

Comments
 (0)