All files / app loader.ts

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 991x     1x 1x 1x   1x                                           1x 9x     9x   9x 9x   9x 36x   9x 18x     9x 18x       9x 18x       9x 18x       9x       9x         9x 9x       9x   9x               27x   9x         9x   1x 1x        
import { getScriptLoader, getStylesheetLoader } from '@bigcommerce/script-loader';
 
import { RenderCheckoutOptions } from './checkout';
import { configurePublicPath } from './common/bundler';
import { isRecordContainingKey, joinPaths } from './common/utility';
import { getDefaultTranslations, isLanguageWindow } from './locale';
import { RenderOrderConfirmationOptions } from './order';
import { isAppExport } from './AppExport';
 
declare const LIBRARY_NAME: string;
declare const MANIFEST_JSON: AssetManifest;
 
export interface AssetManifest {
    appVersion: string;
    css: string[];
    dynamicChunks: { [key: string]: string[] };
    js: string[];
}
 
export interface LoadFilesOptions {
    publicPath?: string;
}
 
export interface LoadFilesResult {
    appVersion: string;
    renderCheckout(options: RenderCheckoutOptions): void;
    renderOrderConfirmation(options: RenderOrderConfirmationOptions): void;
}
 
export function loadFiles(options?: LoadFilesOptions): Promise<LoadFilesResult> {
    const publicPath = configurePublicPath(options && options.publicPath);
    const {
        appVersion,
        css = [],
        dynamicChunks: {
            css: cssDynamicChunks = [],
            js: jsDynamicChunks = [],
        },
        js = [],
    } = MANIFEST_JSON;
 
    const scripts = getScriptLoader().loadScripts(
        js.map(path => joinPaths(publicPath, path))
    );
 
    const stylesheets = getStylesheetLoader().loadStylesheets(
        css.map(path => joinPaths(publicPath, path)),
        { prepend: true }
    );
 
    getScriptLoader().preloadScripts(
        jsDynamicChunks.map(path => joinPaths(publicPath, path)),
        { prefetch: true }
    );
 
    getStylesheetLoader().preloadStylesheets(
        cssDynamicChunks.map(path => joinPaths(publicPath, path)),
        { prefetch: true }
    );
 
    const languageConfig = isLanguageWindow(window) ?
        window.language :
        { locale: 'en', locales: {}, translations: {} };
 
    return Promise.all([
        getDefaultTranslations(languageConfig.locale),
        scripts,
        stylesheets,
    ])
        .then(([defaultTranslations]) => {
            Iif (!isRecordContainingKey(window, LIBRARY_NAME)) {
                throw new Error(`'${LIBRARY_NAME}' property is not available in window.`);
            }
 
            const appExport = window[LIBRARY_NAME];
 
            Iif (!isAppExport(appExport)) {
                throw new Error('The functions required to bootstrap the application are not available.');
            }
 
            const {
                renderCheckout,
                renderOrderConfirmation,
                initializeLanguageService,
            } = appExport;
 
            initializeLanguageService({
                ...languageConfig,
                defaultTranslations,
            });
 
            return {
                appVersion,
                renderCheckout: renderOptions => renderCheckout({ publicPath, ...renderOptions }),
                renderOrderConfirmation: renderOptions => renderOrderConfirmation({ publicPath, ...renderOptions }),
            };
        });
}