_injectHtmlImportsForBundle(document, ast, bundle, bundleManifest) {
        // Gather all the document's direct html imports.  We want the direct (not
        // transitive) imports only here, because we'll be using their AST nodes as
        // targets to prepended injected imports to.
        const existingImports = [
            ...document.getFeatures({ kind: 'html-import', noLazyImports: true, imported: false })
        ].filter((i) => !i.lazy);
        const existingImportDependencies = new Map(existingImports.map((existingImport) => [existingImport.document.url, [
                ...existingImport.document.getFeatures({ kind: 'html-import', imported: true, noLazyImports: true })
            ].filter((i) => !i.lazy).map((feature) => feature.document.url)]));
        // Every file in the bundle is a candidate for injection into the document.
        for (const importUrl of bundle.bundle.files) {
            const relativeImportUrl = urlUtils.relativeUrl(bundle.url, importUrl);
            // If relative URL is empty, it is because the import URL and the document
            // URL are the same, so there is no need to inject.
            if (!relativeImportUrl) {
                continue;
            }
            // If there is an existing import in the document that matches the import
            // URL already, we don't need to inject one.
            if (existingImports.find((e) => e.document.url === importUrl)) {
                continue;
            }
            // We are looking for the earliest eager import of an html document which
            // has a dependency on the html import we want to inject.
            let prependTarget = undefined;
            // We are only concerned with imports that are not of files in this
            // bundle.
            for (const existingImport of existingImports.filter((e) => !bundle.bundle.files.has(e.document.url))) {
                // If the existing import has a dependency on the import we are about
                // to inject, it may be our new target.
                if (existingImportDependencies.get(existingImport.document.url)
                    .indexOf(importUrl) !== -1) {
                    const newPrependTarget = dom5.query(ast, (node) => astUtils.sameNode(node, existingImport.astNode));
                    // IF we don't have a target already or if the old target comes after
                    // the new one in the source code, the new one will replace the old
                    // one.
                    if (newPrependTarget &&
                        (!prependTarget ||
                            astUtils.inSourceOrder(newPrependTarget, prependTarget))) {
                        prependTarget = newPrependTarget;
                    }
                }
            }
            // Inject the new html import into the document.
            const newHtmlImport = this._createHtmlImport(relativeImportUrl);
            if (prependTarget) {
                dom5.insertBefore(prependTarget.parentNode, prependTarget, newHtmlImport);
            }
            else {
                const hiddenDiv = this._findOrCreateHiddenDiv(ast);
                dom5.append(hiddenDiv.parentNode, newHtmlImport);
            }
        }
    }
/**
     * Add HTML Import elements for each file in the bundle.  Efforts are made to
     * ensure that imports are injected prior to any eager imports of other
     * bundles which are known to depend on them, to preserve expectations of
     * evaluation order.
     */
Add HTML Import elements for each file in the bundle.  Efforts are made to
ensure that imports are injected prior to any eager imports of other
bundles which are known to depend on them, to preserve expectations of
evaluation order.
Add HTML Import elements for each file in the bundle . Efforts are made to ensure that imports are injected prior to any eager imports of other bundles which are known to depend on them to preserve expectations of evaluation order .
_injectHtmlImportsForBundle(document, ast, bundle, bundleManifest) {
        const existingImports = [
            ...document.getFeatures({ kind: 'html-import', noLazyImports: true, imported: false })
        ].filter((i) => !i.lazy);
        const existingImportDependencies = new Map(existingImports.map((existingImport) => [existingImport.document.url, [
                ...existingImport.document.getFeatures({ kind: 'html-import', imported: true, noLazyImports: true })
            ].filter((i) => !i.lazy).map((feature) => feature.document.url)]));
        for (const importUrl of bundle.bundle.files) {
            const relativeImportUrl = urlUtils.relativeUrl(bundle.url, importUrl);
            if (!relativeImportUrl) {
                continue;
            }
            if (existingImports.find((e) => e.document.url === importUrl)) {
                continue;
            }
            let prependTarget = undefined;
            for (const existingImport of existingImports.filter((e) => !bundle.bundle.files.has(e.document.url))) {
                if (existingImportDependencies.get(existingImport.document.url)
                    .indexOf(importUrl) !== -1) {
                    const newPrependTarget = dom5.query(ast, (node) => astUtils.sameNode(node, existingImport.astNode));
                    if (newPrependTarget &&
                        (!prependTarget ||
                            astUtils.inSourceOrder(newPrependTarget, prependTarget))) {
                        prependTarget = newPrependTarget;
                    }
                }
            }
            const newHtmlImport = this._createHtmlImport(relativeImportUrl);
            if (prependTarget) {
                dom5.insertBefore(prependTarget.parentNode, prependTarget, newHtmlImport);
            }
            else {
                const hiddenDiv = this._findOrCreateHiddenDiv(ast);
                dom5.append(hiddenDiv.parentNode, newHtmlImport);
            }
        }
    }
_injectHtmlImportsForBundle ( document , ast , bundle , bundleManifest ) { const existingImports = [ ... document . getFeatures ( { kind : 'html-import' , noLazyImports : true , imported : false } ) ] . filter ( ( i ) => ! i . lazy ) ; const existingImportDependencies = new Map ( existingImports . map ( ( existingImport ) => [ existingImport . document . url , [ ... existingImport . document . getFeatures ( { kind : 'html-import' , imported : true , noLazyImports : true } ) ] . filter ( ( i ) => ! i . lazy ) . map ( ( feature ) => feature . document . url ) ] ) ) ; for ( const importUrl of bundle . bundle . files ) { const relativeImportUrl = urlUtils . relativeUrl ( bundle . url , importUrl ) ; if ( ! relativeImportUrl ) { continue ; } if ( existingImports . find ( ( e ) => e . document . url === importUrl ) ) { continue ; } let prependTarget = undefined ; for ( const existingImport of existingImports . filter ( ( e ) => ! bundle . bundle . files . has ( e . document . url ) ) ) { if ( existingImportDependencies . get ( existingImport . document . url ) . indexOf ( importUrl ) !== - 1 ) { const newPrependTarget = dom5 . query ( ast , ( node ) => astUtils . sameNode ( node , existingImport . astNode ) ) ; if ( newPrependTarget && ( ! prependTarget || astUtils . inSourceOrder ( newPrependTarget , prependTarget ) ) ) { prependTarget = newPrependTarget ; } } } const newHtmlImport = this . _createHtmlImport ( relativeImportUrl ) ; if ( prependTarget ) { dom5 . insertBefore ( prependTarget . parentNode , prependTarget , newHtmlImport ) ; } else { const hiddenDiv = this . _findOrCreateHiddenDiv ( ast ) ; dom5 . append ( hiddenDiv . parentNode , newHtmlImport ) ; } } }
Add HTML Import elements for each file in the bundle.
Add HTML Import elements for each file in the bundle .
// Gather all the document's direct html imports.  We want the direct (not // transitive) imports only here, because we'll be using their AST nodes as // targets to prepended injected imports to. // Every file in the bundle is a candidate for injection into the document. // If relative URL is empty, it is because the import URL and the document // URL are the same, so there is no need to inject. // If there is an existing import in the document that matches the import // URL already, we don't need to inject one. // We are looking for the earliest eager import of an html document which // has a dependency on the html import we want to inject. // We are only concerned with imports that are not of files in this // bundle. // If the existing import has a dependency on the import we are about // to inject, it may be our new target. // IF we don't have a target already or if the old target comes after // the new one in the source code, the new one will replace the old // one. // Inject the new html import into the document.