Home

Awesome

AngularJS component library generator

Yeoman generator to create standalone AngularJS component libraries in seconds!

Build Status

generator-angularjs-library

In short

If you want to create a standalone library with filters, directives, services, etc for use in your AngularJS applications, then this generator may just be what you need.

The generator automatically:

This generator is NOT made to generate complete AngularJS applications. If you want to generate a complete AngularJS web application with routes, views, etc then please use generator-angular.

Quick start

Make sure you have yeoman installed:

$ npm install -g yo

Install the generator:

$ npm install -g generator-angularjs-library

Create a new project directory:

$ mkdir sample-project
$ cd sample-project

Run:

$ yo angularjs-library

Answer the questions and the generator will create the boilerplate for your library:

yo-angularjs-library

What the generator does for you

The generator automatically:

Running the generator using library name "Your Library" will result in the following files being generated for you:

.
├── .bowerrc                                  # Configure bower directory for development
├── .editorconfig                             # Editor configuration for code consistency
├── .gitignore                                # Includes files that Git should ignore
├── .jshintrc                                 # JSHint config with angular global support
├── LICENSE                                   # Custom license file with your name in it
├── README.md                                 # Basic README.md with title of your library
├── bower.json                                # Bower configuration with custom devDependencies and ignore files
├── dist                                      # This folder and contents is generated by running gulp
│   ├── sample-library.js                     # Your library, ready to use in your development environment
│   └── sample-library.min.js                 # Your library, ready to use in your production environment
├── gulpfile.js                               # Gulp configuration with definition to build your library
├── karma-dist-concatenated.conf.js           # Karma configuration to run unit tests using sample-library.js
├── karma-dist-minified.conf.js               # Karma configuration to run unit tests using sample-library.min.js
├── karma-src.conf.js                         # Karma configuration to run unit tests using src/**/*.js
├── package.json                              # Npm configuration with necessary dependencies for development
├── src                                       # Source directory
│   └── sample-library
│       ├── directives                        # Directory where you can store directives
│       ├── filters                           # Directory where you can store filters
│       ├── sampleLibrary.module.js           # Main module file
│       └── services                          # Directory where you can store services
└── test
    ├── e2e
    │   └── sample-library                    # Directory where you can store E2E tests
    └── unit
        └── sample-library
            ├── directives                    # Directory where you can store unit tests for directives
            ├── filters                       # Directory where you can store unit tests for filters
            ├── sampleLibrarySpec.js          # Unit tests for main module
            └── services                      # Directory where you can store unit tests for services

How to use the generated boilerplate

The basic library structure is automatically created for you in the src folder.

You can edit the existing files or add additional files in the src folder to add components to your library.

Once you have added files in the src directory, you can update the files in the dist directory using:

$ gulp

First gulp will

to make sure the code is fine.

Then all files in the src directory will be concatenated into 2 files in the dist directory:

gulp

Manually testing your code

The generator creates 3 configurations for unit testing:

By default, gulp will run karma-src.conf.js, but you can use the following preconfigured Gulp tasks to specify the suite you want to run:

# Run unit tests using src/**/*.js
$ gulp test-src

# Run unit tests using dist/<your-library-name>.js
$ gulp test-dist-concatenated

# Run unit tests using dist/<your-library-name>.min.js
$ gulp test-dist-minified

gulp-test-src

This allows you to unit test the different builds of your code to ensure they all work as expected.

How to write a test

Suppose you have a service:

// src/my-lib/services/some-service.js
angular.module('myLib.services')
  .service('someService', function() {
    this.foo = function() {
      return 1234;
    };
  });

Then you write a test like this:


describe('someService', function() {

  var someService;

  // load the module
  beforeEach(module('myLib.services'));

  // inject the service
  beforeEach(inject(function(_someService_) {
    someService = _someService_;
  }));
  
  it('should exist', function() {
    expect(someService).to.be.ok;
  });
  
  it('should have a method called foo', function() {
    expect(someService.foo).to.be.a('function');
  });
  
});

Want to contribute?

Help make this project better - fork and send a PR or create an issue.

Change log

v3.3.1

v3.3.0

v3.2.0

v3.1.0

v3.0.0

v2.0.0

v1.4.0

v1.3.0

v1.2.1

v1.2.0

v1.1.0

v1.0.3

License

MIT