Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

angular-lazy-bundler

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

angular-lazy-bundler

## Table of contents - [What does it do?](#what-does-it-do) - [How is it different from tools like Browserify?](#how-is-it-different-from-tools-like-browserify) - [Requirements](#requirements) - [API](#api) - [Usage example](#usage-example)

  • 0.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
decreased by-60%
Maintainers
1
Weekly downloads
 
Created
Source

Angular Lazy Bundler

Table of contents

  • What does it do?
  • How is it different from tools like Browserify?
  • Requirements
  • API
  • Usage example

What does it do?

Lazy loading components at runtime gives us the advantage to ony load code that is going to be used by the client. At the same time it introduces one big disadvantage; it increases the number of network calls we need to make to load a component / library. This leads to slower page load times and affects the user experience negatively. This is what Angular Lazy Bundler tries to solve.

Lets take the following component as an example:

+src
|  +components
|  |  +home-state
|  |  |  home-route.js
|  |  |  home-state.html
|  |  |  home-state-controller.js
|  |  |  index.js

To load the home state component the browser would need to make at least four requests. Angular Lazy Bundler solves this issue by creating a bundle of our source code per component. Given the above example, the bundler would create a combined file containing all four of the home state's files.

+src
|  +build
|  |  +bundles
|  |  |  home-state.js

The same is applied for JSPM packages. As of now, if you load JSPM packages in the browser you at least make two server calls. One for the package file, e.g. lodash@1.x.x.js. The only purpose of that file is to reference the main package file defined in package.json, e.g. index.js for lodash. This is a necessary evil to make NPM packages easy loadable by name in the browser. The Bundler optimizes this load process by combining the package and the main file into one.

After creating the bundles you can tell Angular Lazy Bundler to also update your SystemJS configuration, so that the loader knows about the bundles and loads the combined resources instead of the individual files. See the bundle config API in the SystemJS documentation.

How is it different from tools like Browserify?

Browserify and similar tools, by default, create one big bundle which contains all the application's source code. This works good for smaller applications. But when you have a few megabytes of code this becomes somewhat of a burden. Let's say our application has 20 different states / screens. The only thing the user wants to do is check his new messages which is one state. But even in that scenario the browser would actually download the remaining code of the application before the user can look at his inbox. This unnecessary wait time is what we want to omit by loading only the parts of the application the user is actually going to access.

Requirements

For the bundler to work it's required to have a project structure as generated by Angular Lazy Generator. Each index.js file in the src folder indicates a root of a component. Only resources which are referenced / imported by the corresponding index.js file, and their sub-dependencies, will land in the resulting bundle. Files which are not imported statically from anywhere will not be bundled. But they will still get loaded by SystemJS individually.

API

new Bundler(options)

new Bundler(options)

ParamTypeDefaultDescription
optionsObjectBundler options.
[options.source]StringsrcWhere to search for components / index.js files.
[options.baseUrl]String.Base URL on the file system to use for bundling.
[options.dest]Stringbuild/bundlesDestination folder where the bundled resources will be written to.
[options.bundlesBaseUrl]StringbundlesPath relative to the baseURL of SystemJS in the browser of the destination folder.
[options.systemJsConfig]Stringconfig/system.jsPath to the SystemJS configuration file.
[options.sourceMaps]BooleantrueEnable / disable sourcemap generation.
[options.minify]BooleantrueEnable / disable minification of bundled resources.
[options.tab]String4 spacesWhat to use as tab when formatting the updated SystemJS configuration.

bundler.bundleComponent(index) ⇒ Promise

Bundle a specific application component.

Kind: instance method of Bundler

ParamTypeDescription
indexStringPath to the index.js file of the component.

bundler.bundleComponents() ⇒ Promise

Bundles all application components.

Kind: instance method of Bundler

bundler.bundleDependency(packageName) ⇒ Promise

Bundle a certain vendor package.

Kind: instance method of Bundler

ParamTypeDescription
packageNameStringPackage name, same as in the SystemJS configuration.

bundler.bundleDependencies(packageNames, saveAs) ⇒ Promise

Combine multiple vendor packages into one bundle.

Kind: instance method of Bundler

ParamTypeDescription
packageNamesArrayWhich packages to bundle.
saveAsStringName of the resulting bundle (without .js extension).

bundler.bundlePackageDependencies() ⇒ Promise

Bundles all vendor packages which are not yet part of an existing bundle.

Kind: instance method of Bundler

bundler.saveConfig() ⇒ Promise

Saves bundle information to the SystemJS configuration.

Kind: instance method of Bundler

Usage example

const Bundler = require('angular-lazy-bundler').Bundler;

const bundler = new Bundler({
    systemJsConfig: 'config/system.js'
});

bundler
    //bundles the sources of our application per component
    .bundleComponents()
    //creates a custom bundle with all packages required for boostrapping the application
    .then(() => {
        return bundler.bundleDependencies(
            [
                'angular',
                'angular-resource',
                'angular-sanitize',
                'angular-ui-router',
                'ui-router-extras'
            ],
            'main-vendors'
        );
    })
    //bundles the remaining packages individually
    .then(() => bundler.bundlePackageDependencies())
    //updates our SystemJS configuration
    .then(() => bundler.saveConfig())
    //here we can handle errors
    .catch((err) => { });

FAQs

Package last updated on 23 Nov 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc