@oazmi/superbuild - v0.1.0
    Preparing search index...

    Function loaderFromFileExtension

    • this function guesses which esbuild loader to use, based on the files extension of the file.

      the way it works is by finding the longest matching suffix defined in the list of user-defined file-extensions, and the predefined list of known file-extensions (defaultExtensionToLoaderMap), while also taking into account any with attribute arguments that may be present when the import was being performed.

      this function is roughly based on the following esbuild function: "/internal/config/config.go":LoaderFromFileExtension

      Important

      while esbuild generally gives priority to any import-attributes (with_attr) that may be present, over file-extension based loader guessing, it makes an exception with regards to any file-extensions that uses the "copy" loader, giving it the highest priority. you can see this behavior detailed in the following line of esbuild: "/internal/bundler/bundler.go":parseFile

      Note

      the only behavioral difference in this function and esbuild's own function is that the default loader (for unidentified file-extensions, or those with none) resolves to undefined rather than "js".

      the reason for this is because this information can be useful to the person using this function, in addition to not causing any harm if this info is not utilized, as passing an undefined loader to esbuild will cause it to use the "js" loader anyway.

      Parameters

      • ext_to_loader_map: Record<string, EsbuildLoaderTypeOrEmpty>

        a map tying file-extensions with loaders.

      • with_attr_type_map: Record<string, EsbuildLoaderTypeOrEmpty>

        a mapping between with.type attributes and their respective loaders.

      • file_path: string | URL

        the file path or url to guess the loader type from.

      • Optionalwith_attr: any

        specify an optional with attribute dictionary that might be present.

      Returns any

      the appropriate esbuild loader type for the given file path.

      import { assertEquals, assertThrows } from "jsr:@std/assert"
      import { EsbuildLoaderTypeOrEmpty } from "./strongtypes.ts"

      const my_loader_map: Record<string, EsbuildLoaderTypeOrEmpty> = {
      "": undefined,
      ".js": "js",
      ".jsx": "jsx",
      ".ts": "ts",
      ".css": "css",
      ".module.css": "local-css",
      ".json": "json",
      ".txt.json": "text",
      ".txt.json.png": "copy",
      ".png": "file",
      }

      const my_with_type_map: Record<string, EsbuildLoaderTypeOrEmpty> = {
      "json": "json",
      "bytes": "binary",
      "image": "binary",
      "empty": undefined,
      }

      // aliasing our function for brevity
      const fn = (path: string, with_attr?: Record<string, string>) => {
      return loaderFromFileExtension(my_loader_map, my_with_type_map, path, with_attr)
      }

      // non-with import attribute tests:
      assertEquals(fn("./hello/.world/.vscode/settings.json"), "json")
      assertEquals(fn("./settings.json"), "json")
      assertEquals(fn("./.settings.txt.json"), "text")
      assertEquals(fn("~/home/hello/meow.module.css"), "local-css")
      assertEquals(fn("/some/dir/"), undefined)
      assertEquals(fn("/some/dir/file.js"), "js")
      assertEquals(fn("/some/dir/file.js.ts"), "ts")

      // with import attribute tests:
      assertEquals(fn("./settings.txt.json", { type: "empty" }), undefined)
      assertEquals(fn("./settings.txt.json", { type: "image" }), "binary")
      // below, notice that the "copy" loader gets a higher precedence than any import attribute.
      assertEquals(fn("./settings.txt.json.png", { type: "image" }), "copy")