index.js
function optimizer(Pagelet, next) {
    var prototype = Pagelet.prototype
      , method = prototype.method
      , status = prototype.status
      , router = prototype.path
      , name = prototype.name
      , view = prototype.view
      , log = debug('pagelet:'+ name);

    //
    // Generate a unique ID used for real time connection lookups.
    //
    prototype.id = options.id || [0, 1, 1, 1].map(generator).join('-');

    //
    // Parse the methods to an array of accepted HTTP methods. We'll only accept
    // these requests and should deny every other possible method.
    //
    log('Optimizing pagelet');
    if (!Array.isArray(method)) method = method.split(/[\s\,]+?/);
    Pagelet.method = method.filter(Boolean).map(function transformation(method) {
      return method.toUpperCase();
    });

    //
    // Add the actual HTTP route and available HTTP methods.
    //
    if (router) {
      log('Instantiating router for path %s', router);
      Pagelet.router = new Route(router);
    }

    //
    // Prefetch the template if a view is available. The view property is
    // mandatory for all pagelets except the bootstrap Pagelet or if the
    // Pagelet is just doing a redirect. We can resolve this edge case by
    // checking if statusCode is in the 300~ range.
    //
    if (!view && name !== 'bootstrap' && !(status >= 300 && status < 400)) return next(
      new Error('The '+ name +' pagelet should have a .view property.')
    );

    //
    // Resolve the view to ensure the path is correct and prefetch
    // the template through Temper.
    //
    if (view) {
      prototype.view = view = path.resolve(prototype.directory, view);
      temper.prefetch(view, prototype.engine);
    }

    //
    // Ensure we have a custom error pagelet when we fail to render this fragment.
    //
    if (prototype.error) {
      temper.prefetch(prototype.error, path.extname(prototype.error).slice(1));
    }

    //
    // Map all dependencies to an absolute path or URL.
    //
    helpers.resolve(Pagelet, ['css', 'js', 'dependencies']);

    //
    // Find all child pagelets and optimize the found children.
    //
    async.map(Pagelet.children(name), function map(Child, step) {
      if (Array.isArray(Child)) return async.map(Child, map, step);

      Child.optimize({
        temper: temper,
        bigpipe: bigpipe,
        transform: {
          before: bigpipe.emits && bigpipe.emits('transform:pagelet:before'),
          after: bigpipe.emits && bigpipe.emits('transform:pagelet:after')
        }
      }, step);
    }, function optimized(error, children) {
      log('optimized all %d child pagelets', children.length);

      if (error) return next(error);

      //
      // Store the optimized children on the prototype, wrapping the Pagelet
      // in an array makes it a lot easier to work with conditional Pagelets.
      //
      prototype._children = children.map(function map(Pagelet) {
        return Array.isArray(Pagelet) ? Pagelet : [Pagelet];
      });

      //
      // Always return a reference to the parent Pagelet.
      // Otherwise the stack of parents would be infested
      // with children returned by this async.map.
      //
      next(null, Pagelet);
    });
  }
function optimizer(Pagelet, next) {
    var prototype = Pagelet.prototype
      , method = prototype.method
      , status = prototype.status
      , router = prototype.path
      , name = prototype.name
      , view = prototype.view
      , log = debug('pagelet:'+ name);

    //
    // Generate a unique ID used for real time connection lookups.
    //
    prototype.id = options.id || [0, 1, 1, 1].map(generator).join('-');

    //
    // Parse the methods to an array of accepted HTTP methods. We'll only accept
    // these requests and should deny every other possible method.
    //
    log('Optimizing pagelet');
    if (!Array.isArray(method)) method = method.split(/[\s\,]+?/);
    Pagelet.method = method.filter(Boolean).map(function transformation(method) {
      return method.toUpperCase();
    });

    //
    // Add the actual HTTP route and available HTTP methods.
    //
    if (router) {
      log('Instantiating router for path %s', router);
      Pagelet.router = new Route(router);
    }

    //
    // Prefetch the template if a view is available. The view property is
    // mandatory for all pagelets except the bootstrap Pagelet or if the
    // Pagelet is just doing a redirect. We can resolve this edge case by
    // checking if statusCode is in the 300~ range.
    //
    if (!view && name !== 'bootstrap' && !(status >= 300 && status < 400)) return next(
      new Error('The '+ name +' pagelet should have a .view property.')
    );

    //
    // Resolve the view to ensure the path is correct and prefetch
    // the template through Temper.
    //
    if (view) {
      prototype.view = view = path.resolve(prototype.directory, view);
      temper.prefetch(view, prototype.engine);
    }

    //
    // Ensure we have a custom error pagelet when we fail to render this fragment.
    //
    if (prototype.error) {
      temper.prefetch(prototype.error, path.extname(prototype.error).slice(1));
    }

    //
    // Map all dependencies to an absolute path or URL.
    //
    helpers.resolve(Pagelet, ['css', 'js', 'dependencies']);

    //
    // Find all child pagelets and optimize the found children.
    //
    async.map(Pagelet.children(name), function map(Child, step) {
      if (Array.isArray(Child)) return async.map(Child, map, step);

      Child.optimize({
        temper: temper,
        bigpipe: bigpipe,
        transform: {
          before: bigpipe.emits && bigpipe.emits('transform:pagelet:before'),
          after: bigpipe.emits && bigpipe.emits('transform:pagelet:after')
        }
      }, step);
    }, function optimized(error, children) {
      log('optimized all %d child pagelets', children.length);

      if (error) return next(error);

      //
      // Store the optimized children on the prototype, wrapping the Pagelet
      // in an array makes it a lot easier to work with conditional Pagelets.
      //
      prototype._children = children.map(function map(Pagelet) {
        return Array.isArray(Pagelet) ? Pagelet : [Pagelet];
      });

      //
      // Always return a reference to the parent Pagelet.
      // Otherwise the stack of parents would be infested
      // with children returned by this async.map.
      //
      next(null, Pagelet);
    });
  }
function optimizer ( Pagelet , next ) { var prototype = Pagelet . prototype , method = prototype . method , status = prototype . status , router = prototype . path , name = prototype . name , view = prototype . view , log = debug ( 'pagelet:' + name ) ; // // Generate a unique ID used for real time connection lookups. // prototype . id = options . id || [ 0 , 1 , 1 , 1 ] . map ( generator ) . join ( '-' ) ; // // Parse the methods to an array of accepted HTTP methods. We'll only accept // these requests and should deny every other possible method. // log ( 'Optimizing pagelet' ) ; if ( ! Array . isArray ( method ) ) method = method . split ( / [\s\,]+? / ) ; Pagelet . method = method . filter ( Boolean ) . map ( function transformation ( method ) { return method . toUpperCase ( ) ; } ) ; // // Add the actual HTTP route and available HTTP methods. // if ( router ) { log ( 'Instantiating router for path %s' , router ) ; Pagelet . router = new Route ( router ) ; } // // Prefetch the template if a view is available. The view property is // mandatory for all pagelets except the bootstrap Pagelet or if the // Pagelet is just doing a redirect. We can resolve this edge case by // checking if statusCode is in the 300~ range. // if ( ! view && name !== 'bootstrap' && ! ( status >= 300 && status < 400 ) ) return next ( new Error ( 'The ' + name + ' pagelet should have a .view property.' ) ) ; // // Resolve the view to ensure the path is correct and prefetch // the template through Temper. // if ( view ) { prototype . view = view = path . resolve ( prototype . directory , view ) ; temper . prefetch ( view , prototype . engine ) ; } // // Ensure we have a custom error pagelet when we fail to render this fragment. // if ( prototype . error ) { temper . prefetch ( prototype . error , path . extname ( prototype . error ) . slice ( 1 ) ) ; } // // Map all dependencies to an absolute path or URL. // helpers . resolve ( Pagelet , [ 'css' , 'js' , 'dependencies' ] ) ; // // Find all child pagelets and optimize the found children. // async . map ( Pagelet . children ( name ) , function map ( Child , step ) { if ( Array . isArray ( Child ) ) return async . map ( Child , map , step ) ; Child . optimize ( { temper : temper , bigpipe : bigpipe , transform : { before : bigpipe . emits && bigpipe . emits ( 'transform:pagelet:before' ) , after : bigpipe . emits && bigpipe . emits ( 'transform:pagelet:after' ) } } , step ) ; } , function optimized ( error , children ) { log ( 'optimized all %d child pagelets' , children . length ) ; if ( error ) return next ( error ) ; // // Store the optimized children on the prototype, wrapping the Pagelet // in an array makes it a lot easier to work with conditional Pagelets. // prototype . _children = children . map ( function map ( Pagelet ) { return Array . isArray ( Pagelet ) ? Pagelet : [ Pagelet ] ; } ) ; // // Always return a reference to the parent Pagelet. // Otherwise the stack of parents would be infested // with children returned by this async.map. // next ( null , Pagelet ) ; } ) ; }
Optimize the pagelet. This function is called by default as part of
the async stack.

@param {Function} next Completion callback
@api private
Optimize the pagelet . This function is called by default as part of the async stack .