function _renderUniversal(routes, context, browserResult, serverResult, browserOptions, numProcesses) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            const projectName = context.target && context.target.project;
            if (!projectName) {
                throw new Error('The builder requires a target.');
            }
            const root = core_1.normalize(context.workspaceRoot);
            const projectMetadata = yield context.getProjectMetadata(projectName);
            const projectRoot = core_1.resolve(root, core_1.normalize(projectMetadata.root || ''));
            // Users can specify a different base html file e.g. "src/home.html"
            const indexFile = utils_1.getIndexOutputFile(browserOptions);
            const { styles: normalizedStylesOptimization } = normalize_optimization_1.normalizeOptimization(browserOptions.optimization);
            const { baseOutputPath = '' } = serverResult;
            const workerArgs = {
                indexFile,
                deployUrl: browserOptions.deployUrl || '',
                inlineCriticalCss: !!normalizedStylesOptimization.inlineCritical,
                minifyCss: !!normalizedStylesOptimization.minify,
            };
            const worker = new jest_worker_1.Worker(path.join(__dirname, 'worker.js'), {
                exposedMethods: ['render'],
                enableWorkerThreads: true,
                numWorkers: numProcesses,
                setupArgs: [workerArgs],
            });
            try {
                worker.getStdout().pipe(process.stdout);
                worker.getStderr().pipe(process.stderr);
                // We need to render the routes for each locale from the browser output.
                for (const outputPath of browserResult.outputPaths) {
                    const localeDirectory = path.relative(browserResult.baseOutputPath, outputPath);
                    const serverBundlePath = path.join(baseOutputPath, localeDirectory, 'main.js');
                    if (!fs.existsSync(serverBundlePath)) {
                        throw new Error(`Could not find the main bundle: ${serverBundlePath}`);
                    }
                    const spinner = ora(`Prerendering ${routes.length} route(s) to ${outputPath}...`).start();
                    try {
                        const results = (yield Promise.all(routes.map((route) => worker.render(outputPath, serverBundlePath, route))));
                        let numErrors = 0;
                        for (const { errors, warnings } of results) {
                            spinner.stop();
                            errors === null || errors === void 0 ? void 0 : errors.forEach((e) => context.logger.error(e));
                            warnings === null || warnings === void 0 ? void 0 : warnings.forEach((e) => context.logger.warn(e));
                            spinner.start();
                            numErrors += (_a = errors === null || errors === void 0 ? void 0 : errors.length) !== null && _a !== void 0 ? _a : 0;
                        }
                        if (numErrors > 0) {
                            throw Error(`Rendering failed with ${numErrors} worker errors.`);
                        }
                    }
                    catch (error) {
                        spinner.fail(`Prerendering routes to ${outputPath} failed.`);
                        return { success: false, error: error.message };
                    }
                    spinner.succeed(`Prerendering routes to ${outputPath} complete.`);
                    if (browserOptions.serviceWorker) {
                        spinner.start('Generating service worker...');
                        try {
                            yield service_worker_1.augmentAppWithServiceWorker(root, projectRoot, core_1.normalize(outputPath), browserOptions.baseHref || '/', browserOptions.ngswConfigPath);
                        }
                        catch (error) {
                            spinner.fail('Service worker generation failed.');
                            return { success: false, error: error.message };
                        }
                        spinner.succeed('Service worker generation complete.');
                    }
                }
            }
            finally {
                // const _ = is a workaround to disable tsetse must use promises rule.
                // tslint:disable-next-line: no-floating-promises
                const _ = worker.end();
            }
            return browserResult;
        });
    }
/**
     * Renders each route and writes them to
     * <route>/index.html for each output path in the browser result.
     */
Renders each route and writes them to
/index.html for each output path in the browser result.
Renders each route and writes them to / index . html for each output path in the browser result .
function _renderUniversal(routes, context, browserResult, serverResult, browserOptions, numProcesses) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            const projectName = context.target && context.target.project;
            if (!projectName) {
                throw new Error('The builder requires a target.');
            }
            const root = core_1.normalize(context.workspaceRoot);
            const projectMetadata = yield context.getProjectMetadata(projectName);
            const projectRoot = core_1.resolve(root, core_1.normalize(projectMetadata.root || ''));
            const indexFile = utils_1.getIndexOutputFile(browserOptions);
            const { styles: normalizedStylesOptimization } = normalize_optimization_1.normalizeOptimization(browserOptions.optimization);
            const { baseOutputPath = '' } = serverResult;
            const workerArgs = {
                indexFile,
                deployUrl: browserOptions.deployUrl || '',
                inlineCriticalCss: !!normalizedStylesOptimization.inlineCritical,
                minifyCss: !!normalizedStylesOptimization.minify,
            };
            const worker = new jest_worker_1.Worker(path.join(__dirname, 'worker.js'), {
                exposedMethods: ['render'],
                enableWorkerThreads: true,
                numWorkers: numProcesses,
                setupArgs: [workerArgs],
            });
            try {
                worker.getStdout().pipe(process.stdout);
                worker.getStderr().pipe(process.stderr);
                for (const outputPath of browserResult.outputPaths) {
                    const localeDirectory = path.relative(browserResult.baseOutputPath, outputPath);
                    const serverBundlePath = path.join(baseOutputPath, localeDirectory, 'main.js');
                    if (!fs.existsSync(serverBundlePath)) {
                        throw new Error(`Could not find the main bundle: ${serverBundlePath}`);
                    }
                    const spinner = ora(`Prerendering ${routes.length} route(s) to ${outputPath}...`).start();
                    try {
                        const results = (yield Promise.all(routes.map((route) => worker.render(outputPath, serverBundlePath, route))));
                        let numErrors = 0;
                        for (const { errors, warnings } of results) {
                            spinner.stop();
                            errors === null || errors === void 0 ? void 0 : errors.forEach((e) => context.logger.error(e));
                            warnings === null || warnings === void 0 ? void 0 : warnings.forEach((e) => context.logger.warn(e));
                            spinner.start();
                            numErrors += (_a = errors === null || errors === void 0 ? void 0 : errors.length) !== null && _a !== void 0 ? _a : 0;
                        }
                        if (numErrors > 0) {
                            throw Error(`Rendering failed with ${numErrors} worker errors.`);
                        }
                    }
                    catch (error) {
                        spinner.fail(`Prerendering routes to ${outputPath} failed.`);
                        return { success: false, error: error.message };
                    }
                    spinner.succeed(`Prerendering routes to ${outputPath} complete.`);
                    if (browserOptions.serviceWorker) {
                        spinner.start('Generating service worker...');
                        try {
                            yield service_worker_1.augmentAppWithServiceWorker(root, projectRoot, core_1.normalize(outputPath), browserOptions.baseHref || '/', browserOptions.ngswConfigPath);
                        }
                        catch (error) {
                            spinner.fail('Service worker generation failed.');
                            return { success: false, error: error.message };
                        }
                        spinner.succeed('Service worker generation complete.');
                    }
                }
            }
            finally {
                const _ = worker.end();
            }
            return browserResult;
        });
    }
function _renderUniversal ( routes , context , browserResult , serverResult , browserOptions , numProcesses ) { var _a ; return __awaiter ( this , void 0 , void 0 , function * ( ) { const projectName = context . target && context . target . project ; if ( ! projectName ) { throw new Error ( 'The builder requires a target.' ) ; } const root = core_1 . normalize ( context . workspaceRoot ) ; const projectMetadata = yield context . getProjectMetadata ( projectName ) ; const projectRoot = core_1 . resolve ( root , core_1 . normalize ( projectMetadata . root || '' ) ) ; const indexFile = utils_1 . getIndexOutputFile ( browserOptions ) ; const { styles : normalizedStylesOptimization } = normalize_optimization_1 . normalizeOptimization ( browserOptions . optimization ) ; const { baseOutputPath = '' } = serverResult ; const workerArgs = { indexFile , deployUrl : browserOptions . deployUrl || '' , inlineCriticalCss : ! ! normalizedStylesOptimization . inlineCritical , minifyCss : ! ! normalizedStylesOptimization . minify , } ; const worker = new jest_worker_1 . Worker ( path . join ( __dirname , 'worker.js' ) , { exposedMethods : [ 'render' ] , enableWorkerThreads : true , numWorkers : numProcesses , setupArgs : [ workerArgs ] , } ) ; try { worker . getStdout ( ) . pipe ( process . stdout ) ; worker . getStderr ( ) . pipe ( process . stderr ) ; for ( const outputPath of browserResult . outputPaths ) { const localeDirectory = path . relative ( browserResult . baseOutputPath , outputPath ) ; const serverBundlePath = path . join ( baseOutputPath , localeDirectory , 'main.js' ) ; if ( ! fs . existsSync ( serverBundlePath ) ) { throw new Error ( ` ${ serverBundlePath } ` ) ; } const spinner = ora ( ` ${ routes . length } ${ outputPath } ` ) . start ( ) ; try { const results = ( yield Promise . all ( routes . map ( ( route ) => worker . render ( outputPath , serverBundlePath , route ) ) ) ) ; let numErrors = 0 ; for ( const { errors , warnings } of results ) { spinner . stop ( ) ; errors === null || errors === void 0 ? void 0 : errors . forEach ( ( e ) => context . logger . error ( e ) ) ; warnings === null || warnings === void 0 ? void 0 : warnings . forEach ( ( e ) => context . logger . warn ( e ) ) ; spinner . start ( ) ; numErrors += ( _a = errors === null || errors === void 0 ? void 0 : errors . length ) !== null && _a !== void 0 ? _a : 0 ; } if ( numErrors > 0 ) { throw Error ( ` ${ numErrors } ` ) ; } } catch ( error ) { spinner . fail ( ` ${ outputPath } ` ) ; return { success : false , error : error . message } ; } spinner . succeed ( ` ${ outputPath } ` ) ; if ( browserOptions . serviceWorker ) { spinner . start ( 'Generating service worker...' ) ; try { yield service_worker_1 . augmentAppWithServiceWorker ( root , projectRoot , core_1 . normalize ( outputPath ) , browserOptions . baseHref || '/' , browserOptions . ngswConfigPath ) ; } catch ( error ) { spinner . fail ( 'Service worker generation failed.' ) ; return { success : false , error : error . message } ; } spinner . succeed ( 'Service worker generation complete.' ) ; } } } finally { const _ = worker . end ( ) ; } return browserResult ; } ) ; }
Renders each route and writes them to
 <route>/index.html for each output path in the browser result.
Renders each route and writes them to <route > / index . html for each output path in the browser result .
// Users can specify a different base html file e.g. "src/home.html" // We need to render the routes for each locale from the browser output. // const _ = is a workaround to disable tsetse must use promises rule. // tslint:disable-next-line: no-floating-promises