@oazmi/esbuild-generic-loader - v0.1.3
    Preparing search index...

    Class GenericLoader<K>Abstract

    the base class for creating custom loaders for any file type that is natively unsupported by esbuild.

    • each loader class handles one type of new file type.
    • each loader instance handles one file, and can be used only once, so that it does not hog onto resources.

    Type Parameters

    • K = string
    Index

    Constructors

    Properties

    meta: { imports: ImportMetadata<K> } = ...

    Methods

    • this abstract method is supposed to consume the provided raw content and return back the object ContentDependencies that describes the list of dependencies, in addition to providing a unique immutable key for each dependency path (so that it can be recognized and re-injected after being transformed by esbuild).

      the actions of this function should be invertible by the insertDeps method.

      Parameters

      • content: string

      Returns Promise<ContentDependencies<K>>

    • an overloadable method that should return a javascript-code string that exports the provided content parameter in the form of export const content = ....

      by default, the baseclass GenericLoader escapes all characters of the content parameter, so that the string is perfectly preserved after the virtual module's evaluation.
      this is achieved by using String.raw and escaping all dollarsigns ("$") and backticks ("`") with template expressions. however, such a thing may not be desirable, and you may want the evaluation of the template expressions within your content, rather than suppressing it. or you may wish to introduce additional functions to the script so that it evaluates the output content through a series of transformations.
      in such cases, you would want to overload this method to suit your transformations needs. but make sure to always export variable named content.

      another very important escaping transformation that must take place is for the "</script>" closing tag. this is because esbuild explicitly transforms all strings containing this literal into "<\\/script>", which is equivalent to the original string and a non-issue, unless String.raw is used, where the underlying string becomes deformed. the reason why esbuild does this explicitly for the "</script>" tag is because if someone were to copy and paste their bundled js code into an html's script block, then the original form of the `"" string would close the script block pre-maturely, leaking the contents ahead of it and turning the html code illegible.
      check out the following github issue comment for more info about this transformation: github.com/evanw/esbuild/issues/2267#issuecomment-1149396846

      Parameters

      • content: string

      Returns Promise<string>

    • this method parses the provided raw_content parameter, extracts its dependencies by calling the extractDeps method, and then converts it to an equivalent javascript code that can be consumed and analyzed by esbuild.

      the generated javascript code looks like the following:

      export const importKeys = []
      globalThis.start_of_imports()
      importKeys.push("key_1")
      await import("path_1")
      // ...
      importKeys.push("key_N")
      await import("path_N")
      globalThis.end_of_imports()
      export const content = `${ORIGINAL_RAW_CONTENT}`

      Parameters

      • raw_content: string

      Returns Promise<string>

    • this method unparses the esbuild-bundled javascript code generated by parseToJs, and analyzes the transformed paths of the imported dependencies, and then injects back the transformed paths back to the original raw contents through the insertDeps method.

      Parameters

      • js_content: string

      Returns Promise<string>