Skip to content

Commit

Permalink
Add empty loader
Browse files Browse the repository at this point in the history
  • Loading branch information
viankakrisna committed Jun 14, 2020
1 parent c12e0fd commit 29358af
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 2 deletions.
5 changes: 5 additions & 0 deletions internal/bundler/bundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ func parseFile(args parseArgs) {
jsonMetadataChunk: jsonMetadataChunk,
}

case LoaderEmpty:
expr := ast.Expr{ast.Loc{0}, &ast.EString{lexer.StringToUTF16("")}}
result.file.ast = parser.ModuleExportsAST(args.log, source, args.parseOptions, expr)

default:
result.ok = false
args.log.AddRangeError(args.importSource, args.pathRange,
Expand Down Expand Up @@ -401,6 +405,7 @@ const (
LoaderBase64
LoaderDataURL
LoaderFile
LoaderEmpty
)

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

func TestLoaderEmpty(t *testing.T) {
expectBundled(t, bundled{
files: map[string]string{
"/entry.js": `
console.log(require('./test.svg'))
`,

// Use an SVG string that has a base64-encoded SHA1 has with a "/" in it
"/test.svg": "<svg>$</svg>",
},
entryPaths: []string{"/entry.js"},
parseOptions: parser.ParseOptions{
IsBundling: true,
},
bundleOptions: BundleOptions{
IsBundling: true,
AbsOutputDir: "/out/",
ExtensionToLoader: map[string]Loader{
".js": LoaderJS,
".svg": LoaderEmpty,
},
},
expected: map[string]string{
"/out/entry.js": `// /test.svg
var require_test = __commonJS((exports, module) => {
module.exports = "";
});
// /entry.js
console.log(require_test());
`,
},
})
}

func TestLoaderFile(t *testing.T) {
expectBundled(t, bundled{
files: map[string]string{
Expand Down
2 changes: 1 addition & 1 deletion lib/api-types.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' | 'esm';
export declare type Loader = 'js' | 'jsx' | 'ts' | 'tsx' | 'json' | 'text' | 'base64' | 'file' | 'dataurl';
export declare type Loader = 'js' | 'jsx' | 'ts' | 'tsx' | 'json' | 'text' | 'base64' | 'file' | 'dataurl' | 'empty';
export declare type LogLevel = 'info' | 'warning' | 'error' | 'silent';

export interface CommonOptions {
Expand Down
1 change: 1 addition & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ const (
LoaderBase64
LoaderDataURL
LoaderFile
LoaderEmpty
)

type Platform uint8
Expand Down
2 changes: 2 additions & 0 deletions pkg/api/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ func validateLoader(value Loader) bundler.Loader {
return bundler.LoaderDataURL
case LoaderFile:
return bundler.LoaderFile
case LoaderEmpty:
return bundler.LoaderEmpty
default:
panic("Invalid loader")
return ^bundler.Loader(0)
Expand Down
4 changes: 3 additions & 1 deletion pkg/cli/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,11 @@ func parseLoader(text string) (api.Loader, error) {
return api.LoaderDataURL, nil
case "file":
return api.LoaderFile, nil
case "empty":
return api.LoaderEmpty, nil
default:
return ^api.Loader(0), fmt.Errorf("Invalid loader: %q (valid: "+
"js, jsx, ts, tsx, json, text, base64, dataurl, file)", text)
"js, jsx, ts, tsx, json, text, base64, dataurl, file, empty)", text)
}
}

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 @@ -222,6 +222,11 @@ let transformTests = {
assert.strictEqual(js, `module.exports = "data:application/octet-stream;base64,AAEC";\n`)
},

async empty({ service }) {
const { js } = await service.transform(`\x00\x01\x02`, { loader: 'empty' })
assert.strictEqual(js, `module.exports = "";\n`)
},

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

0 comments on commit 29358af

Please sign in to comment.