Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support importing files as dataurl #107

Merged
merged 19 commits into from
May 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cmd/esbuild/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Options:
--jsx-factory=... What to use instead of React.createElement
--jsx-fragment=... What to use instead of React.Fragment
--loader:X=L Use loader L to load file extension X, where L is
one of: js, jsx, ts, tsx, json, text, base64
one of: js, jsx, ts, tsx, json, text, base64, dataurl

Advanced options:
--version Print the current version and exit (` + esbuildVersion + `)
Expand Down Expand Up @@ -159,6 +159,8 @@ func (args *argsObject) parseLoader(text string) bundler.Loader {
return bundler.LoaderText
case "base64":
return bundler.LoaderBase64
case "dataurl":
return bundler.LoaderDataURL
default:
return bundler.LoaderNone
}
Expand Down
15 changes: 15 additions & 0 deletions internal/bundler/bundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"encoding/base64"
"fmt"
"io/ioutil"
"mime"
"net/http"
"os"
"sort"
"strings"
Expand Down Expand Up @@ -147,6 +149,18 @@ func parseFile(
ast := parser.ModuleExportsAST(log, source, parseOptions, expr)
results <- parseResult{source, ast, true}

case LoaderDataURL:
mimeType := mime.TypeByExtension(extension)

if mimeType == "" {
mimeType = http.DetectContentType([]byte(source.Contents))
}
encoded := base64.StdEncoding.EncodeToString([]byte(source.Contents))
url := "data:" + mimeType + ";base64," + encoded
expr := ast.Expr{ast.Loc{0}, &ast.EString{lexer.StringToUTF16(url)}}
ast := parser.ModuleExportsAST(log, source, parseOptions, expr)
results <- parseResult{source, ast, true}

default:
log.AddRangeError(importSource, pathRange, fmt.Sprintf("File extension not supported: %s", path))
results <- parseResult{}
Expand Down Expand Up @@ -281,6 +295,7 @@ const (
LoaderJSON
LoaderText
LoaderBase64
LoaderDataURL
)

func DefaultExtensionToLoaderMap() map[string]Loader {
Expand Down
74 changes: 74 additions & 0 deletions internal/bundler/bundler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2413,6 +2413,80 @@ func TestRequireCustomExtensionBase64(t *testing.T) {
})
}

func TestRequireCustomExtensionDataURL(t *testing.T) {
expectBundled(t, bundled{
files: map[string]string{
"/entry.js": `
console.log(require('./test.custom'))
`,
"/test.custom": "a\x00b\x80c\xFFd",
},
entryPaths: []string{"/entry.js"},
parseOptions: parser.ParseOptions{
IsBundling: true,
},
bundleOptions: BundleOptions{
IsBundling: true,
AbsOutputFile: "/out.js",
ExtensionToLoader: map[string]Loader{
".js": LoaderJS,
".custom": LoaderDataURL,
},
},
expected: map[string]string{
"/out.js": `bootstrap({
1(exports, module) {
// /test.custom
module.exports = "data:application/octet-stream;base64,YQBigGP/ZA==";
},

0() {
// /entry.js
console.log(__require(1 /* ./test.custom */));
}
}, 0);
`,
},
})
}

func testAutoDetectMimeTypeFromExtension(t *testing.T) {
expectBundled(t, bundled{
files: map[string]string{
"/entry.js": `
console.log(require('./test.svg'))
`,
"/test.svg": "a\x00b\x80c\xFFd",
},
entryPaths: []string{"/entry.js"},
parseOptions: parser.ParseOptions{
IsBundling: true,
},
bundleOptions: BundleOptions{
IsBundling: true,
AbsOutputFile: "/out.js",
ExtensionToLoader: map[string]Loader{
".js": LoaderJS,
".svg": LoaderDataURL,
},
},
expected: map[string]string{
"/out.js": `bootstrap({
1(exports, module) {
// /test.svg
module.exports = "data:image/svg+xml;base64,YQBigGP/ZA==";
},

0() {
// /entry.js
console.log(__require(1 /* ./test.svg */));
}
}, 0);
`,
},
})
}

func TestRequireBadExtension(t *testing.T) {
expectBundled(t, bundled{
files: map[string]string{
Expand Down
2 changes: 1 addition & 1 deletion npm/esbuild/lib/main.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export declare type Target = 'esnext' | 'es6' | 'es2015' | 'es2016' | 'es2017' | 'es2018' | 'es2019' | 'es2020';
export declare type Platform = 'browser' | 'node';
export declare type Format = 'iife' | 'cjs';
export declare type Loader = 'js' | 'jsx' | 'ts' | 'tsx' | 'json' | 'text' | 'base64';
export declare type Loader = 'js' | 'jsx' | 'ts' | 'tsx' | 'json' | 'text' | 'base64' | 'dataurl';

interface CommonOptions {
sourcemap?: boolean | 'inline' | 'external';
Expand Down
5 changes: 5 additions & 0 deletions scripts/js-api-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ let tests = {
assert.strictEqual(js, `module.exports = "AAEC";\n`)
},

async dataurl({ service }) {
const { js } = await service.transform(`\x00\x01\x02`, { loader: 'dataurl' })
assert.strictEqual(js, `module.exports = "data:application/octet-stream;base64,AAEC";\n`)
},

async sourceMap({ service }) {
const { js, jsSourceMap } = await service.transform(`let x`, { sourcemap: true })
assert.strictEqual(js, `let x;\n`)
Expand Down