import { Syntax } from "../BlockParser" ; import { ResolvedConfiguration } from "../configuration" ; import { REGEXP_COMMENT_DEFINITION_REF , REGEXP_COMMENT_FOOTER , REGEXP_COMMENT_HEADER } from "../PrecompiledDefinitions/compiled-comments" ; import { FileIdentifier , ImportedCompiledCssFile , ImportedCompiledCssFileContents , ImportedFile , Importer } from "./Importer" ; /**
 * The BaseImporter is an abstract class that Importer implementations may extend from.
 * This follows the Importer interface that must be used for interacting with the BlockFactory.
 * We also include additional utility methods that are useful for handling CSS Blocks,
 * Compiled CSS, and Definition Files.
 */ export abstract class BaseImporter implements Importer { /**
     * Compute a unique identifier for a given import path. If `fromIdentifier` is provided,
     * the importPath can be relative to the file that is identified by it.
     */ abstract identifier ( fromIdentifier , importPath , config ) ; /**
     * Import the file with the given metadata and return a string and meta data for it.
     */ abstract import ( identifier , config ) ; /**
     * import the file with the given metadata and return a string and meta data for it.
     */ abstract importSync ( identifier , config ) ; /**
     * The default name of the block used unless the block specifies one itself.
     */ abstract defaultName ( identifier , configuration ) ; /**
     * If a file identifier has an on-disk representation, return an absolute path to it.
     */ abstract filesystemPath ( identifier , config ) ; /**
     * Returns a string meant for human consumption that identifies the file.
     * As is used in debug statements and error reporting. Unlike filesystemPath,
     * this needn't resolve to an actual file or be an absolute path.
     */ abstract debugIdentifier ( identifier , config ) ; /**
     * Returns the syntax the contents are written in.
     */ abstract syntax ( identifier , config ) ; param contents returns protected isCompiledBlockCSS ( contents ) { return REGEXP_COMMENT_HEADER . test ( contents ) && REGEXP_COMMENT_DEFINITION_REF . test ( contents ) && REGEXP_COMMENT_FOOTER . test ( contents ) ; } param contents returns protected segmentizeCompiledBlockCSS ( contents ) { const headerRegexpResult = contents . match ( REGEXP_COMMENT_HEADER ) ; const footerRegexpResult = contents . match ( REGEXP_COMMENT_FOOTER ) ; if ( ! headerRegexpResult || ! footerRegexpResult ) { throw new Error ( "Unable to parse compiled CSS file into segments. Expected comments are missing." ) ; } const [ headerFullMatch , blockId ] = headerRegexpResult ; const headerStartIndex = headerRegexpResult . index ; if ( typeof headerStartIndex === "undefined" ) { throw new Error ( "Unable to determine start location of regexp result." ) ; } const headerEndIndex = headerStartIndex + headerFullMatch . length ; const [ footerFullMatch ] = footerRegexpResult ; const footerStartIndex = footerRegexpResult . index ; if ( typeof footerStartIndex === "undefined" ) { throw new Error ( "Unable to determine start location of regexp result." ) ; } const footerEndIndex = footerStartIndex + footerFullMatch . length ; if ( headerStartIndex > footerStartIndex ) { throw new Error ( "Header must exist before footer in imported content." ) ; } const pre = contents . slice ( 0 , headerStartIndex ) ; const post = contents . slice ( footerEndIndex ) ; const fullBlockContents = contents . slice ( headerEndIndex , footerStartIndex ) ; const definitionRegexpResult = fullBlockContents . match ( REGEXP_COMMENT_DEFINITION_REF ) ; if ( definitionRegexpResult === null ) { throw new Error ( "Unable to find definition URL in compiled CSS. This comment must be declared between the header and footer CSS Blocks comments." ) ; } const [ definitionFullMatch , definitionUrl ] = definitionRegexpResult ; const blockCssContents = fullBlockContents . replace ( definitionFullMatch , "" ) ; return { pre , blockId , blockCssContents , definitionUrl , post , raw : contents , } ; } }