diff --git a/src/loader.ts b/src/loader.ts index b3723ec..1f12881 100644 --- a/src/loader.ts +++ b/src/loader.ts @@ -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()", @@ -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 { + public config: GenericLoaderConfig public meta: { imports: ImportMetadata } = { imports: [] } - constructor( - public config: GenericLoaderConfig = {}, - ) { } + constructor(config?: Partial) { + 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, @@ -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, - 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]>(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") diff --git a/src/typedefs.ts b/src/typedefs.ts index 1ce2867..9a0d4b9 100644 --- a/src/typedefs.ts +++ b/src/typedefs.ts @@ -33,6 +33,17 @@ export interface GenericLoaderConfig { * - `"inject-link"`: similar to `"bundle"`, but will also inject a `` 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. */