text: (function (global, factory) {
      typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('fs'), require('tslib'), require('@angular/compiler'), require('@angular/core'), require('@angular/platform-server'), require('@nguniversal/express-engine/tokens')) :
      typeof define === 'function' && define.amd ? define('@nguniversal/express-engine', ['exports', 'fs', 'tslib', '@angular/compiler', '@angular/core', '@angular/platform-server', '@nguniversal/express-engine/tokens'], factory) :
      (global = global || self, factory((global.nguniversal = global.nguniversal || {}, global.nguniversal.expressEngine = {}), global.fs, global.tslib, global.ng.compiler, global.ng.core, global.ng.platformServer, global.nguniversal.expressEngine.tokens));
  }(this, (function (exports, fs, tslib, compiler, core, platformServer, tokens) { 'use strict';
  
      /** ResourceLoader implementation for loading files */
      var FileLoader = /** @class */ (function () {
          function FileLoader() {
          }
          FileLoader.prototype.get = function (url) {
              return new Promise(function (resolve, reject) {
                  fs.readFile(url, function (err, data) {
                      if (err) {
                          return reject(err);
                      }
                      resolve(data.toString());
                  });
              });
          };
          return FileLoader;
      }());
  
      /**
       * A common rendering engine utility. This abstracts the logic
       * for handling the platformServer compiler, the module cache, and
       * the document loader
       */
      var CommonEngine = /** @class */ (function () {
          function CommonEngine(moduleOrFactory, providers) {
              if (providers === void 0) { providers = []; }
              this.moduleOrFactory = moduleOrFactory;
              this.providers = providers;
              this.factoryCacheMap = new Map();
              this.templateCache = {};
          }
          /** Return an instance of the platformServer compiler */
          CommonEngine.prototype.getCompiler = function () {
              var compilerFactory = platformServer.platformDynamicServer().injector.get(core.CompilerFactory);
              return compilerFactory.createCompiler([
                  { providers: [{ provide: compiler.ResourceLoader, useClass: FileLoader, deps: [] }] }
              ]);
          };
          /**
           * Render an HTML document for a specific URL with specified
           * render options
           */
          CommonEngine.prototype.render = function (opts) {
              return tslib.__awaiter(this, void 0, void 0, function () {
                  var doc, _a, extraProviders, moduleOrFactory, factory;
                  return tslib.__generator(this, function (_b) {
                      switch (_b.label) {
                          case 0:
                              _a = opts.document;
                              if (_a) return [3 /*break*/, 2];
                              return [4 /*yield*/, this.getDocument(opts.documentFilePath)];
                          case 1:
                              _a = (_b.sent());
                              _b.label = 2;
                          case 2:
                              doc = _a;
                              extraProviders = tslib.__spread((opts.providers || []), (this.providers || []), [
                                  {
                                      provide: platformServer.INITIAL_CONFIG,
                                      useValue: {
                                          document: doc,
                                          url: opts.url
                                      }
                                  }
                              ]);
                              moduleOrFactory = this.moduleOrFactory || opts.bootstrap;
                              return [4 /*yield*/, this.getFactory(moduleOrFactory)];
                          case 3:
                              factory = _b.sent();
                              return [2 /*return*/, platformServer.renderModuleFactory(factory, { extraProviders: extraProviders })];
                      }
                  });
              });
          };
          /** Return the factory for a given engine instance */
          CommonEngine.prototype.getFactory = function (moduleOrFactory) {
              return tslib.__awaiter(this, void 0, void 0, function () {
                  var moduleFactory, factory;
                  return tslib.__generator(this, function (_a) {
                      switch (_a.label) {
                          case 0:
                              if (!(moduleOrFactory instanceof core.NgModuleFactory)) return [3 /*break*/, 1];
                              return [2 /*return*/, moduleOrFactory];
                          case 1:
                              moduleFactory = this.factoryCacheMap.get(moduleOrFactory);
                              // If module factory is cached
                              if (moduleFactory) {
                                  return [2 /*return*/, moduleFactory];
                              }
                              return [4 /*yield*/, this.getCompiler().compileModuleAsync(moduleOrFactory)];
                          case 2:
                              factory = _a.sent();
                              this.factoryCacheMap.set(moduleOrFactory, factory);
                              return [2 /*return*/, factory];
                      }
                  });
              });
          };
          /** Retrieve the document from the cache or the filesystem */
          CommonEngine.prototype.getDocument = function (filePath) {
              var doc = this.templateCache[filePath] = this.templateCache[filePath] ||
                  fs.readFileSync(filePath).toString();
              // As  promise so we can change the API later without breaking
              return Promise.resolve(doc);
          };
          return CommonEngine;
      }());
  
      /**
       * @license
       * Copyright Google LLC All Rights Reserved.
       *
       * Use of this source code is governed by an MIT-style license that can be
       * found in the LICENSE file at https://angular.io/license
       */
  
      /**
       * @license
       * Copyright Google LLC All Rights Reserved.
       *
       * Use of this source code is governed by an MIT-style license that can be
       * found in the LICENSE file at https://angular.io/license
       */
  
      /**
       * @license
       * Copyright Google LLC All Rights Reserved.
       *
       * Use of this source code is governed by an MIT-style license that can be
       * found in the LICENSE file at https://angular.io/license
       */
  
      /**
       * This holds a cached version of each index used.
       */
      var templateCache = {};
      /**
       * This is an express engine for handling Angular Applications
       */
      function ngExpressEngine(setupOptions) {
          var engine = new CommonEngine(setupOptions.bootstrap, setupOptions.providers);
          return function (filePath, options, callback) {
              try {
                  if (!setupOptions.bootstrap && !options.bootstrap) {
                      throw new Error('You must pass in a NgModule or NgModuleFactory to be bootstrapped');
                  }
                  var req = options.req;
                  var renderOptions = Object.assign({}, options);
                  renderOptions.url =
                      options.url || req.protocol + "://" + (req.get('host') || '') + req.originalUrl;
                  renderOptions.document = options.document || getDocument(filePath);
                  renderOptions.providers = options.providers || [];
                  renderOptions.providers =
                      renderOptions.providers.concat(getReqResProviders(options.req, options.res));
                  engine.render(renderOptions)
                      .then(function (html) { return callback(null, html); })
                      .catch(callback);
              }
              catch (err) {
                  callback(err);
              }
          };
      }
      /**
       * Get providers of the request and response
       */
      function getReqResProviders(req, res) {
          var providers = [
              {
                  provide: tokens.REQUEST,
                  useValue: req
              }
          ];
          if (res) {
              providers.push({
                  provide: tokens.RESPONSE,
                  useValue: res
              });
          }
          return providers;
      }
      /**
       * Get the document at the file path
       */
      function getDocument(filePath) {
          return templateCache[filePath] = templateCache[filePath] || fs.readFileSync(filePath).toString();
      }
  
      /**
       * @license
       * Copyright Google LLC All Rights Reserved.
       *
       * Use of this source code is governed by an MIT-style license that can be
       * found in the LICENSE file at https://angular.io/license
       */
  
      /**
       * @license
       * Copyright Google LLC All Rights Reserved.
       *
       * Use of this source code is governed by an MIT-style license that can be
       * found in the LICENSE file at https://angular.io/license
       */
  
      /**
       * Generated bundle index. Do not edit.
       */
  
      exports.ngExpressEngine = ngExpressEngine;
  
      Object.defineProperty(exports, '__esModule', { value: true });
  
  })));
  //# sourceMappingURL=express-engine.umd.js.map
  