Skip to content

Commit

Permalink
in /src/loader.ts, modify the GenericLoader baseclass to make it …
Browse files Browse the repository at this point in the history
…optional to store and validate metadata of the content's dependencies, through the use of the `meta: boolean` config option in the constructor.
  • Loading branch information
omar-azmi committed Nov 30, 2024
1 parent 07aec00 commit eb3e607
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ import type { ContentDependencies, GenericLoaderConfig, ImportMetadata, ImportMe


const
defaultGenericLoaderConfig: GenericLoaderConfig = { meta: true },
escape_regex_for_string_raw = /[\$\`]/g,
imports_beginning_marker = "globalThis.start_of_imports()",
imports_ending_marker = "globalThis.end_of_imports()",
Expand All @@ -102,11 +103,12 @@ await import(${json_stringify(import_path)})`
* - each loader _instance_ handles **one file**, and can be used only **once**, so that it does not hog onto resources.
*/
export abstract class GenericLoader<K = string> {
public config: GenericLoaderConfig
public meta: { imports: ImportMetadata<K> } = { imports: [] }

constructor(
public config: GenericLoaderConfig = {},
) { }
constructor(config?: Partial<GenericLoaderConfig>) {
this.config = { ...defaultGenericLoaderConfig, ...config }
}

/** this abstract method is supposed to consume the provided raw {@link content}
* and return back the object {@link ContentDependencies} that describes the list of dependencies,
Expand Down Expand Up @@ -221,17 +223,17 @@ ${content_export_js}`
// now we dynamically load our bundled js script that contains the raw contents (`content`),
// and the ordered list of uniqie keys associated with each import path (`importKeys`)
{ content, importKeys } = await import(js_blob_url) as ScriptWrappedContent<K>,
metaImports = this.meta.imports,
{ meta: { imports: metaImports }, config: { meta: metaEnabled } } = this,
number_of_imports = importKeys.length

if (DEBUG.ASSERT && (
number_of_imports !== importPaths.length
|| (DEBUG.META && number_of_imports !== metaImports.length)
|| (DEBUG.META && metaEnabled && number_of_imports !== metaImports.length)
)) {
throw new Error("encountered a mismatch between number of imported dependencies, and number of keys assigned to dependencies")
}

if (DEBUG.META) {
if (DEBUG.META && metaEnabled) {
for (const [key, path, import_entry] of zipArrays<[K, string, ImportMetadataEntry<K>]>(importKeys, importPaths, metaImports)) {
if (DEBUG.ASSERT && (json_stringify(key) !== json_stringify(import_entry.key))) {
throw new Error("encountered a mismatch between the original key and the key obtained from evaluating javascript module")
Expand Down
11 changes: 11 additions & 0 deletions src/typedefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ export interface GenericLoaderConfig {
* - `"inject-link"`: similar to `"bundle"`, but will also inject a `<link>` element into your `document`'s head that link's to the output bundled file.
*/
mode?: "bundle" | "inject" | "inject-link"

/** enable or disable meta data generation and verification in {@link GenericLoader.meta}.
*
* this is useful when developing plugins, because that way the {@link GenericLoader} baseclass can ensure that you are accounting for all dependencies when unparsing, based on the parsing step.
* however, this also means that you would be forced to use the same loader object for parsing and unparsing, even if the loader parsing and unparsing steps are functionally pure (the baseclass is also pure, unless `meta` is enabled).
* so, in order to use the {@link GenericLoader.parseToJs} and {@link GenericLoader.unparseFromJs} methods arbitrarily anywhere (i.e. stateless, loader-object wise) without having errors throw to you,
* you will have to explicitly disable this config option (i.e. set it to `false`).
*
* @defaultValue `true`
*/
meta: boolean
}

/** an interface that describes the dependencies and contents of a single file that is parsed by your loader's {@link GenericLoader.extractDeps} method. */
Expand Down

0 comments on commit eb3e607

Please sign in to comment.