Skip to content

Commit

Permalink
allow loaders for extensionless files
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Dec 28, 2022
1 parent 1755592 commit f42ca4a
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 7 deletions.
19 changes: 13 additions & 6 deletions internal/bundler/bundler.go
Expand Up @@ -1012,15 +1012,22 @@ func loaderFromFileExtension(extensionToLoader map[string]config.Loader, base st
// Pick the loader with the longest matching extension. So if there's an
// extension for ".css" and for ".module.css", we want to match the one for
// ".module.css" before the one for ".css".
for {
i := strings.IndexByte(base, '.')
if i == -1 {
break
if i := strings.IndexByte(base, '.'); i != -1 {
for {
if loader, ok := extensionToLoader[base[i:]]; ok {
return loader
}
base = base[i+1:]
i = strings.IndexByte(base, '.')
if i == -1 {
break
}
}
if loader, ok := extensionToLoader[base[i:]]; ok {
} else {
// If there's no extension, explicitly check for an extensionless loader
if loader, ok := extensionToLoader[""]; ok {
return loader
}
base = base[i+1:]
}
return config.LoaderNone
}
Expand Down
38 changes: 38 additions & 0 deletions internal/bundler_tests/bundler_loader_test.go
Expand Up @@ -1337,3 +1337,41 @@ func TestEmptyLoaderCSS(t *testing.T) {
},
})
}

func TestExtensionlessLoaderJS(t *testing.T) {
loader_suite.expectBundled(t, bundled{
files: map[string]string{
"/entry.js": `
import './what'
`,
"/what": `foo()`,
},
entryPaths: []string{"/entry.js"},
options: config.Options{
Mode: config.ModeBundle,
ExtensionToLoader: map[string]config.Loader{
".js": config.LoaderJS,
"": config.LoaderJS,
},
},
})
}

func TestExtensionlessLoaderCSS(t *testing.T) {
loader_suite.expectBundled(t, bundled{
files: map[string]string{
"/entry.css": `
@import './what';
`,
"/what": `.foo { color: red }`,
},
entryPaths: []string{"/entry.css"},
options: config.Options{
Mode: config.ModeBundle,
ExtensionToLoader: map[string]config.Loader{
".css": config.LoaderCSS,
"": config.LoaderCSS,
},
},
})
}
16 changes: 16 additions & 0 deletions internal/bundler_tests/snapshots/snapshots_loader.txt
Expand Up @@ -173,6 +173,22 @@ console.log(ns, import_c.default, void 0);
}
}

================================================================================
TestExtensionlessLoaderCSS
---------- entry.css ----------
/* what */
.foo {
color: red;
}

/* entry.css */

================================================================================
TestExtensionlessLoaderJS
---------- entry.js ----------
// what
foo();

================================================================================
TestJSXAutomaticNoNameCollision
---------- /out.js ----------
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/api_impl.go
Expand Up @@ -545,7 +545,7 @@ func validateLoaders(log logger.Log, loaders map[string]Loader) map[string]confi
result := bundler.DefaultExtensionToLoaderMap()
if loaders != nil {
for ext, loader := range loaders {
if !isValidExtension(ext) {
if ext != "" && !isValidExtension(ext) {
log.AddError(nil, logger.Range{}, fmt.Sprintf("Invalid file extension: %q", ext))
}
result[ext] = validateLoader(loader)
Expand Down

0 comments on commit f42ca4a

Please sign in to comment.