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

Add empty loader #180

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 5 additions & 0 deletions internal/bundler/bundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,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 @@ -424,6 +428,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/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/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