├── ng-conf-library.ts ├── src └── service.ts ├── typings.json ├── tsconfig.json ├── test └── service.spec.ts ├── .npmignore ├── .gitignore ├── LICENSE ├── package.json ├── karma-test-shim.js ├── karma.conf.js └── README.md /ng-conf-library.ts: -------------------------------------------------------------------------------- 1 | export * from "./src/service"; -------------------------------------------------------------------------------- /src/service.ts: -------------------------------------------------------------------------------- 1 | import {Injectable} from "@angular/core"; 2 | 3 | @Injectable() 4 | export class SampleService { 5 | public title: string = "Sample service"; 6 | 7 | constructor() { 8 | console.log('hello from sample service'); 9 | } 10 | } -------------------------------------------------------------------------------- /typings.json: -------------------------------------------------------------------------------- 1 | { 2 | "ambientDevDependencies": { 3 | "jasmine": "github:DefinitelyTyped/DefinitelyTyped/jasmine/jasmine.d.ts#dd638012d63e069f2c99d06ef4dcc9616a943ee4" 4 | }, 5 | "ambientDependencies": { 6 | "es6-shim": "github:DefinitelyTyped/DefinitelyTyped/es6-shim/es6-shim.d.ts#6697d6f7dadbf5773cb40ecda35a76027e0783b2", 7 | "node": "github:DefinitelyTyped/DefinitelyTyped/node/node.d.ts#aee0039a2d6686ec78352125010ebb38a7a7d743" 8 | } 9 | } -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "noImplicitAny": true, 4 | "module": "commonjs", 5 | "target": "es5", 6 | "emitDecoratorMetadata": true, 7 | "experimentalDecorators": true, 8 | "inlineSourceMap": true, 9 | "inlineSources": true, 10 | "declaration": true, 11 | "moduleResolution": "node" 12 | }, 13 | "files": [ 14 | "./typings/main.d.ts", 15 | "./ng-conf-library.ts", 16 | "./src/service.ts", 17 | "./test/service.spec.ts" 18 | ] 19 | } -------------------------------------------------------------------------------- /test/service.spec.ts: -------------------------------------------------------------------------------- 1 | import {SampleService} from "../src/service"; 2 | import {beforeEachProviders, inject, expect, it, describe} from "@angular/core/testing"; 3 | 4 | export function main() { 5 | describe("Sample service", () => { 6 | beforeEachProviders(() => [SampleService]); 7 | 8 | it('should work', inject([SampleService], (service: SampleService) => { 9 | expect(SampleService).toBeDefined(); 10 | expect(service).toBeDefined(); 11 | expect(service).toBeAnInstanceOf(SampleService); 12 | expect(service.title).toEqual("Sample service"); 13 | })); 14 | }); 15 | } -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 18 | .grunt 19 | 20 | # Compiled binary addons (http://nodejs.org/api/addons.html) 21 | build/Release 22 | 23 | # Users Environment Variables 24 | .lock-wscript 25 | 26 | # OS generated files # 27 | .DS_Store 28 | ehthumbs.db 29 | Icon? 30 | Thumbs.db 31 | 32 | # Node Files # 33 | /node_modules 34 | /bower_components 35 | 36 | # Coverage # 37 | /coverage/ 38 | 39 | # Typing # 40 | /src/typings/tsd/ 41 | /typings/ 42 | /tsd_typings/ 43 | 44 | # Dist # 45 | /dist 46 | /public/__build__/ 47 | /src/*/__build__/ 48 | __build__/** 49 | .webpack.json 50 | 51 | # Doc # 52 | /doc/ 53 | 54 | # IDE # 55 | .idea/ 56 | *.swp 57 | 58 | # Dev # 59 | *.ts 60 | !*.d.ts 61 | tests 62 | karma.conf.js 63 | karma-test-shim.js -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 18 | .grunt 19 | 20 | # Compiled binary addons (http://nodejs.org/api/addons.html) 21 | build/Release 22 | 23 | # Users Environment Variables 24 | .lock-wscript 25 | 26 | # OS generated files # 27 | .DS_Store 28 | ehthumbs.db 29 | Icon? 30 | Thumbs.db 31 | 32 | # Node Files # 33 | /node_modules 34 | /bower_components 35 | 36 | # Coverage # 37 | /coverage/ 38 | 39 | # Typing # 40 | /src/typings/tsd/ 41 | /typings/ 42 | /tsd_typings/ 43 | 44 | # Dist # 45 | /dist 46 | /public/__build__/ 47 | /src/*/__build__/ 48 | __build__/** 49 | .webpack.json 50 | 51 | # Doc # 52 | /doc/ 53 | 54 | # IDE # 55 | .idea/ 56 | *.swp 57 | 58 | # Generated # 59 | *.js 60 | !karma.conf.js 61 | !karma-test-shim.js 62 | *.map 63 | *.d.ts 64 | typings -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Olivier Combe 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ng-conf-library", 3 | "version": "0.0.1", 4 | "description": "An awesome library", 5 | "main": "ng-conf-library.js", 6 | "scripts": { 7 | "test": "tsc && karma start", 8 | "prepublish": "typings install && npm test" 9 | }, 10 | "typings": "./ng-conf-library.d.ts", 11 | "repository": { 12 | "type": "git", 13 | "url": "git+https://github.com/ocombe/ng-conf-library.git" 14 | }, 15 | "keywords": [ 16 | "ng2", 17 | "angular2" 18 | ], 19 | "author": "Olivier Combe", 20 | "license": "MIT", 21 | "bugs": { 22 | "url": "https://github.com/ocombe/ng-conf-library/issues" 23 | }, 24 | "homepage": "https://github.com/ocombe/ng-conf-library#readme", 25 | "peerDependencies": { 26 | "@angular/common": "^2.0.0-rc.0", 27 | "@angular/compiler": "^2.0.0-rc.0", 28 | "@angular/core": "^2.0.0-rc.0", 29 | "@angular/platform-browser": "^2.0.0-rc.0", 30 | "@angular/platform-browser-dynamic": "^2.0.0-rc.0" 31 | }, 32 | "devDependencies": { 33 | "@angular/common": "^2.0.0-rc.0", 34 | "@angular/compiler": "^2.0.0-rc.0", 35 | "@angular/core": "^2.0.0-rc.0", 36 | "@angular/platform-browser": "^2.0.0-rc.0", 37 | "@angular/platform-browser-dynamic": "^2.0.0-rc.0", 38 | "es6-promise": "^3.0.2", 39 | "es6-shim": "^0.35.0", 40 | "jasmine-core": "^2.4.1", 41 | "karma": "^0.13.22", 42 | "karma-jasmine": "^0.3.8", 43 | "karma-phantomjs-launcher": "^1.0.0", 44 | "karma-typescript-preprocessor": "0.0.21", 45 | "phantomjs-prebuilt": "^2.1.7", 46 | "reflect-metadata": "0.1.2", 47 | "rxjs": "5.0.0-beta.6", 48 | "systemjs": "^0.19.26", 49 | "typescript": "^1.8.10", 50 | "typings": "^0.8.1", 51 | "zone.js": "^0.6.12" 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /karma-test-shim.js: -------------------------------------------------------------------------------- 1 | // Turn on full stack traces in errors to help debugging 2 | Error.stackTraceLimit=Infinity; 3 | 4 | jasmine.DEFAULT_TIMEOUT_INTERVAL = 100; 5 | 6 | // Cancel Karma's synchronous start, 7 | // we will call `__karma__.start()` later, once all the specs are loaded. 8 | __karma__.loaded = function() {}; 9 | 10 | System.config({ 11 | baseURL: '/base/', 12 | defaultJSExtensions: true, 13 | map: { 14 | 'rxjs': 'node_modules/rxjs', 15 | '@angular': 'node_modules/@angular' 16 | }, 17 | packages: { 18 | '@angular/core': { 19 | main: 'index.js' 20 | }, 21 | '@angular/compiler': { 22 | main: 'index.js' 23 | }, 24 | '@angular/common': { 25 | main: 'index.js' 26 | }, 27 | '@angular/platform-browser': { 28 | main: 'index.js' 29 | }, 30 | '@angular/platform-browser-dynamic': { 31 | main: 'index.js' 32 | } 33 | } 34 | }); 35 | 36 | Promise.all([ 37 | System.import('@angular/core/testing'), 38 | System.import('@angular/platform-browser-dynamic/testing') 39 | ]).then(function (providers) { 40 | var testing = providers[0]; 41 | var testingBrowser = providers[1]; 42 | 43 | testing.setBaseTestProviders(testingBrowser.TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS, 44 | testingBrowser.TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS); 45 | 46 | }).then(function() { 47 | // Finally, load all spec files. 48 | // This will run the tests directly. 49 | return Promise.all( 50 | Object.keys(window.__karma__.files) // All files served by Karma. 51 | .filter(onlySpecFiles) 52 | .map(file2moduleName) 53 | .map(function(path) { 54 | return System.import(path).then(function(module) { 55 | if (module.hasOwnProperty('main')) { 56 | module.main(); 57 | } else { 58 | throw new Error('Module ' + path + ' does not implement main() method.'); 59 | } 60 | }); 61 | })); 62 | }).then(__karma__.start, __karma__.error); 63 | 64 | function onlySpecFiles(path) { 65 | return /[\.|_]spec\.js$/.test(path); 66 | } 67 | 68 | // Normalize paths to module names. 69 | function file2moduleName(filePath) { 70 | return filePath.replace(/\\/g, '/') 71 | .replace(/^\/base\//, '') 72 | .replace(/\.js/, ''); 73 | } -------------------------------------------------------------------------------- /karma.conf.js: -------------------------------------------------------------------------------- 1 | // Karma configuration 2 | module.exports = function(config) { 3 | config.set({ 4 | 5 | // base path that will be used to resolve all patterns (eg. files, exclude) 6 | basePath: './', 7 | 8 | // frameworks to use 9 | // available frameworks: https://npmjs.org/browse/keyword/karma-adapter 10 | frameworks: ['jasmine'], 11 | 12 | // list of files / patterns to load in the browser 13 | files: [ 14 | // for Travis 15 | 'node_modules/es6-shim/es6-shim.js', 16 | 'node_modules/zone.js/dist/zone.js', 17 | 'node_modules/zone.js/dist/long-stack-trace-zone.js', 18 | 'node_modules/zone.js/dist/jasmine-patch.js', 19 | 'node_modules/systemjs/dist/system.src.js', 20 | 'node_modules/reflect-metadata/Reflect.js', 21 | 22 | { pattern: 'node_modules/@angular/**/*.js', included: false, watched: false, served: true }, 23 | { pattern: 'node_modules/rxjs/**/*.js', included: false, watched: false, served: true }, 24 | { pattern: 'node_modules/systemjs/dist/system-polyfills.js', included: false, watched: false, served: true }, // PhantomJS2 (and possibly others) might require it 25 | 26 | { pattern: 'src/**/*.ts', included: false, watched: true }, // source files 27 | { pattern: 'test/**/*.ts', included: false, watched: true }, // test files 28 | 'karma-test-shim.js' 29 | ], 30 | 31 | // list of files to exclude 32 | exclude: [ 33 | 'node_modules/angular2/**/*_spec.js' 34 | ], 35 | 36 | preprocessors: { 37 | '**/*.ts': ['typescript'] 38 | }, 39 | 40 | typescriptPreprocessor: { 41 | options: require('./tsconfig.json').compilerOptions, 42 | typings: [ 43 | "typings/main.d.ts" 44 | ] 45 | }, 46 | 47 | // test results reporter to use 48 | // possible values: 'dots', 'progress' 49 | // available reporters: https://npmjs.org/browse/keyword/karma-reporter 50 | reporters: ['progress'], 51 | 52 | 53 | // web server port 54 | port: 9876, 55 | 56 | // enable / disable colors in the output (reporters and logs) 57 | colors: true, 58 | 59 | // level of logging 60 | // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG 61 | logLevel: config.LOG_INFO, 62 | 63 | // enable / disable watching file and executing tests whenever any file changes 64 | autoWatch: false, 65 | 66 | // start these browsers 67 | // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher 68 | browsers: ['PhantomJS'], 69 | 70 | // Continuous Integration mode 71 | // if true, Karma captures browsers, runs the tests and exits 72 | singleRun: true, 73 | 74 | // Concurrency level 75 | // how many browser should be started simultanous 76 | concurrency: Infinity 77 | }) 78 | }; -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # How to create a library for Angular 2 - ng-conf 2016 2 | This is a basic setup for Angular 2 (or TypeScript in general) libraries, if you want something more advanced check out [Angular 2 Library Seed](https://github.com/preboot/angular2-library-seed) 3 | 4 | ### 1. Init the project 5 | - Create a repository on Github: 6 | - Use the name of your library 7 | - Init with a readme, a license (MIT), and a .gitignore file (type node) 8 | 9 | - Git clone that repository locally 10 | - Go to that repository and type `npm init` // [Video](https://youtu.be/LHKrJGW_QX0) 11 | - For the name of your library: no spaces, no special characters (except dashes & underscore) and all in lowercase 12 | - Give the name of your library to the main file. Example: my-lib.js 13 | - Don't bother with the test command for now 14 | 15 | - Install the dev dependencies for Angular 2: `npm install -D @angular/core @angular/compiler @angular/common @angular/platform-browser @angular/platform-browser-dynamic es6-shim@^0.35.0 es6-promise@^3.0.2 reflect-metadata@0.1.2 rxjs@5.0.0-beta.6 zone.js@^0.6.12` (-D means save in package.json as a dev dependency) 16 | - Add Angular 2 as a peer dependency in your package.json file 17 | ```js 18 | "peerDependencies": { 19 | "@angular/common": "^2.0.0-rc.0", 20 | "@angular/compiler": "^2.0.0-rc.0", 21 | "@angular/core": "^2.0.0-rc.0", 22 | "@angular/platform-browser": "^2.0.0-rc.0", 23 | "@angular/platform-browser-dynamic": "^2.0.0-rc.0" 24 | } 25 | ``` 26 | 27 | ### 2. Write a service 28 | - Create a folder named `src` 29 | - In this folder add a Typescript file for your service, the name of the file doesn't matter 30 | - Write a simple service (don't forget to export it) // [Video](https://youtu.be/3miw5X6pUDA) 31 | - Create a main Typescript file at the root named after your library and export * from your library // [Video](https://youtu.be/H_kZ7vLowBw) 32 | 33 | ### 3. Typescript & typings 34 | - Install Typescript & typings: `npm install -D typescript typings` 35 | - Create 2 file at the root named `tsconfig.json` & `typings.json` 36 | - In `tsconfig.json` write: 37 | ```js 38 | { 39 | "compilerOptions": { 40 | "noImplicitAny": true, 41 | "module": "commonjs", 42 | "target": "es5", 43 | "emitDecoratorMetadata": true, 44 | "experimentalDecorators": true, 45 | "inlineSourceMap": true, 46 | "inlineSources": true, 47 | "declaration": true, 48 | "moduleResolution": "node" 49 | }, 50 | "files": [ 51 | 52 | ] 53 | } 54 | ``` 55 | - In `typings.json` write: 56 | ```js 57 | { 58 | "ambientDevDependencies": { 59 | "jasmine": "github:DefinitelyTyped/DefinitelyTyped/jasmine/jasmine.d.ts#dd638012d63e069f2c99d06ef4dcc9616a943ee4" 60 | }, 61 | "ambientDependencies": { 62 | "es6-shim": "github:DefinitelyTyped/DefinitelyTyped/es6-shim/es6-shim.d.ts#6697d6f7dadbf5773cb40ecda35a76027e0783b2", 63 | "node": "github:DefinitelyTyped/DefinitelyTyped/node/node.d.ts#aee0039a2d6686ec78352125010ebb38a7a7d743" 64 | } 65 | } 66 | ``` 67 | - In the file `tsconfig.json` edit the `files` property and add the following (rename `ng-conf-library` with the name of your lib): // [Video](https://youtu.be/dv7ml7LXuqM) 68 | ``` 69 | "./typings/main.d.ts", 70 | "./ng-conf-library.ts", 71 | "./src/service.ts" 72 | ``` 73 | - In `package.json` add a prepublish script with `typings install && tsc` // [Video](https://youtu.be/dv7ml7LXuqM?t=11s) 74 | - Test that script with the command `npm run prepublish` // [Video](https://youtu.be/dv7ml7LXuqM?t=20s) 75 | - Add a `typings` property in your `package.json` and add it the name of your main typings file (extension .d.ts) that was just created by your prepublish script // [Video](https://youtu.be/dv7ml7LXuqM?t=36s) 76 | 77 | ### 4. Tests 78 | - Install karma & its dependencies: `npm install -D karma karma-phantomjs-launcher phantomjs-prebuilt karma-jasmine karma-typescript-preprocessor@0.0.21 jasmine-core systemjs` 79 | - Create a file named `karma.conf.js` and copy the content of [this file](https://github.com/ocombe/ng-conf-library/blob/master/karma.conf.js) in it 80 | - Create a file named `karma-test-shim.js` and copy the content of [this file](https://github.com/ocombe/ng-conf-library/blob/master/karma-test-shim.js) in it 81 | - Create a `test` folder and create a file named `service.spec.ts` 82 | - Add this new file to the files property of `tsconfig.json` // [Video](https://youtu.be/fsEZiCkos-Q) 83 | - Edit the test script of `package.json` and type `tsc && karma start` // [Video](https://youtu.be/fsEZiCkos-Q?t=6s) 84 | - Write some tests // [Video](https://youtu.be/fsEZiCkos-Q?t=13s) 85 | - Run the tests with `npm test` // [Video](https://youtu.be/fsEZiCkos-Q?t=2m24s) 86 | 87 | ### 5. Publish 88 | - Create a file `.npmignore` and copy the content of `.gitignore` in it 89 | - Fix `.gitignore` to avoid the commit of generated files. Add the following at the end: 90 | ``` 91 | # Generated # 92 | *.js 93 | !karma.conf.js 94 | !karma-test-shim.js 95 | *.map 96 | *.d.ts 97 | typings 98 | ``` 99 | - Fix `.npmignore` to avoid publishing the Typescript files & the test files. Add the following at the end: 100 | ``` 101 | # Dev # 102 | *.ts 103 | !*.d.ts 104 | tests 105 | karma.conf.js 106 | karma-test-shim.js 107 | ``` 108 | - Update the `prepublish` script in `package.json` to run your tests with `typings install && npm test` 109 | - Check the version and publish your library with `npm publish` ! 110 | --------------------------------------------------------------------------------