Skip to content

Commit

Permalink
feat(packaging): add aot-friendly packaging
Browse files Browse the repository at this point in the history
This change introduces a UMD bundle to the published package, and
adds ES2015 modules to the root of the project. Some changes
were necessary to fix imports and make rollup work correctly.

This change should make AngularFire2 ahead-of-time-compilation
-friendly.
  • Loading branch information
jeffbcross committed Sep 16, 2016
1 parent 90ccc2d commit ec58e05
Show file tree
Hide file tree
Showing 16 changed files with 94 additions and 199 deletions.
86 changes: 4 additions & 82 deletions karma-test-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,85 +2,7 @@
Error.stackTraceLimit = Infinity;
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;

__karma__.loaded = function () {
};


function isJsFile(path) {
return path.slice(-3) == '.js';
}

function isSpecFile(path) {
return path.slice(-7) == 'spec.js';
}

function isBuiltFile(path) {
var builtPath = '/base/dist/';
return isJsFile(path) && (path.substr(0, builtPath.length) == builtPath);
}

var allSpecFiles = Object.keys(window.__karma__.files)
.filter(isSpecFile)
.filter(isBuiltFile);

// Load our SystemJS configuration.
System.config({
baseURL: '/base'
});

System.config(
{
paths: {
// paths serve as alias
'npm:': 'node_modules/'
},
map: {
'app': 'dist',
'@angular/core': 'npm:@angular/core/bundles/core.umd.js',
'@angular/common': 'npm:@angular/common/bundles/common.umd.js',
'@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
'@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',

// angular testing umd bundles
'@angular/core/testing': 'npm:@angular/core/bundles/core-testing.umd.js',
'@angular/common/testing': 'npm:@angular/common/bundles/common-testing.umd.js',
'@angular/compiler/testing': 'npm:@angular/compiler/bundles/compiler-testing.umd.js',
'@angular/platform-browser/testing': 'npm:@angular/platform-browser/bundles/platform-browser-testing.umd.js',
'@angular/platform-browser-dynamic/testing': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic-testing.umd.js',

// other libraries
'rxjs': 'npm:rxjs',
'firebase': 'npm:firebase/firebase.js'
},
packages: {
'app': {
defaultExtension: 'js'
},
'rxjs': {
main: 'Rx.js',
defaultExtension: 'js'
}
}
});

Promise.all([
System.import('@angular/core/testing'),
System.import('@angular/platform-browser-dynamic/testing')
]).then(function (providers) {
var testing = providers[0];
var testingBrowser = providers[1];

testing.TestBed.initTestEnvironment(
testingBrowser.BrowserDynamicTestingModule,
testingBrowser.platformBrowserDynamicTesting()
);

}).then(function() {
// Finally, load all spec files.
// This will run the tests directly.
return Promise.all(
allSpecFiles.map(function (moduleName) {
return System.import(moduleName);
}));
}).then(__karma__.start, __karma__.error);
ng.core.testing.TestBed.initTestEnvironment(
ng.platformBrowserDynamic.testing.BrowserDynamicTestingModule,
ng.platformBrowserDynamic.testing.platformBrowserDynamicTesting()
);
32 changes: 14 additions & 18 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ module.exports = function(config) {

'node_modules/reflect-metadata/Reflect.js',

// System.js for module loading
'node_modules/systemjs/dist/system-polyfills.js',
'node_modules/systemjs/dist/system.src.js',

// Zone.js dependencies
'node_modules/zone.js/dist/zone.js',
'node_modules/zone.js/dist/proxy.js',
Expand All @@ -24,21 +20,13 @@ module.exports = function(config) {
'node_modules/zone.js/dist/async-test.js',
'node_modules/zone.js/dist/fake-async-test.js',

// RxJs.
{ pattern: 'node_modules/rxjs/**/*.js', included: false, watched: false },
{ pattern: 'node_modules/rxjs/**/*.js.map', included: false, watched: false },


{ pattern: 'karma-test-shim.js', included: true, watched: true },
'node_modules/rxjs/bundles/Rx.{js,map}',

// paths loaded via module imports
// Angular itself
{ pattern: 'node_modules/@angular/**/*.js', included: false, watched: true },
{ pattern: 'node_modules/@angular/**/*.js.map', included: false, watched: true },
...getAngularFiles(['core','common','compiler','platform-browser','platform-browser-dynamic']),

{ pattern: 'node_modules/firebase/firebase.js', included: false, watched: false },
{ pattern: 'dist/**/*.js', included: false, watched: true },
{ pattern: 'dist/**/*.js.map', included: false, watched: false }
'karma-test-shim.js',
'node_modules/firebase/firebase.js',
'dist/bundles/test-root.umd.{js,map}',
],

port: 9876,
Expand All @@ -48,4 +36,12 @@ module.exports = function(config) {
browsers: ['Chrome'],
singleRun: false
})
};
};

function getAngularFiles(packages) {
return packages.reduce((files, pkg) => {
files.push(`node_modules/@angular/${pkg}/bundles/${pkg}.umd.js`);
files.push(`node_modules/@angular/${pkg}/bundles/${pkg}-testing.umd.js`);
return files;
}, []);
}
21 changes: 13 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@
"name": "angularfire2",
"version": "2.0.0-beta.5",
"description": "",
"main": "./dist/angularfire2.js",
"jsnext:main": "./dist/esm/angularfire2.js",
"main": "bundles/angularfire2.umd.js",
"module": "index.js",
"scripts": {
"test": "npm run build; karma start --single-run",
"test:watch": "karma start",
"build": "rm -rf dist; tsc",
"build:watch": "rm -rf dist && tsc -w",
"build_npm": "rm -rf dist && ngc -p tsconfig.publish.es5.json && ngc -p tsconfig.publish.es6.json && npm run postbuild_npm",
"test:watch": "concurrently 'npm run build:watch' 'npm run delayed_karma'",
"delayed_karma": "sleep 10 && karma start",
"delayed_rollup": "sleep 5 && rollup --watch -c rollup.test.config.js",
"build:watch": "rm -rf dist && concurrently 'tsc -w' 'npm run delayed_rollup'",
"build": "rm -rf dist && tsc && rollup -c rollup.test.config.js",
"build_npm": "rm -rf dist && ngc -p tsconfig.publish.es6.json && rollup -c rollup.publish.config.js && npm run postbuild_npm",
"build_e2e": "rm -rf dist-test && npm run build && tsc -p test/ && cp test/e2e/firebase_object/index.html dist-test/e2e/firebase_object/ && cp test/e2e/firebase_list/index.html dist-test/e2e/firebase_list/ && cp test/e2e/auth/index.html dist-test/e2e/auth/",
"postbuild_npm": "cp package.json README.md .npmignore dist/ && npm run rewrite_npm_package",
"rewrite_npm_package": "node --harmony_destructuring tools/rewrite-published-package.js",
"e2e_test": "webdriver-manager update && npm run build_e2e && protractor",
"build_e2e": "rm -rf dist-test && npm run build && tsc -p test/ && cp test/e2e/firebase_object/index.html dist-test/e2e/firebase_object/ && cp test/e2e/firebase_list/index.html dist-test/e2e/firebase_list/ && cp test/e2e/auth/index.html dist-test/e2e/auth/",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 1"
},
"keywords": [
Expand Down Expand Up @@ -44,6 +46,7 @@
"devDependencies": {
"@angular/compiler-cli": "^0.5.0",
"@angular/platform-server": "^2.0.0-rc.5",
"concurrently": "^2.2.0",
"conventional-changelog-cli": "^1.2.0",
"es6-module-loader": "^0.17.10",
"es6-shim": "^0.35.0",
Expand All @@ -64,6 +67,8 @@
"parse5": "^1.3.2",
"protractor": "3.0.0",
"reflect-metadata": "0.1.2",
"rollup": "^0.35.11",
"rollup-watch": "^2.5.0",
"systemjs": "^0.19.16",
"systemjs-builder": "^0.15.7",
"traceur": "0.0.96",
Expand All @@ -73,5 +78,5 @@
"typings": "^1.3.2",
"zone.js": "^0.6.21"
},
"typings": "dist/angularfire2.d.ts"
"typings": "index.d.ts"
}
10 changes: 10 additions & 0 deletions rollup-globals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default (mod) => {
if (mod === 'rxjs') return 'Rx';
if (mod.indexOf('rxjs/operator') === 0) return `Rx.Observable.prototype`;
if (mod === 'rxjs/scheduler/queue') return 'Rx.Scheduler';
if (mod.indexOf('rxjs/') === 0) return 'Rx';

if (mod === 'firebase') return 'firebase';
if (mod === '@angular/core') return 'ng.core';
if (mod === '@angular/core/testing') return 'ng.core.testing';
}
9 changes: 9 additions & 0 deletions rollup.publish.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import globals from './rollup-globals';

export default {
entry: 'dist/index.js',
dest: 'dist/bundles/angularFire2.umd.js',
format: 'umd',
moduleName: 'angularFire2',
globals
}
9 changes: 9 additions & 0 deletions rollup.test.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import globals from './rollup-globals';

export default {
entry: 'dist/test-root.js',
dest: 'dist/bundles/test-root.umd.js',
format: 'umd',
moduleName: 'angularFire2.test',
globals
}
2 changes: 1 addition & 1 deletion src/auth/firebase_sdk_auth_backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,4 @@ export class FirebaseSdkAuthBackend extends AuthBackend {
// Cast Firebase promises as Zone-patched Promises
function castPromise<T>(promiseLike: PromiseLike<T>): Promise<T> {
return Promise.resolve(promiseLike) as Promise<T>;
}
}
22 changes: 11 additions & 11 deletions src/database/query_observable.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { Observable } from 'rxjs/Observable';
import { ScalarObservable } from 'rxjs/observable/ScalarObservable';
import 'rxjs/add/observable/of';
import { Operator } from 'rxjs/Operator';
import { Observer } from 'rxjs/Observer';
import { merge } from 'rxjs/operator/merge';
import { map } from 'rxjs/operator/map';
import {
Query,
ScalarQuery,
OrderByOptions,
OrderBySelection,
LimitToOptions,
LimitToSelection,
Primitive
import {
Query,
ScalarQuery,
OrderByOptions,
OrderBySelection,
LimitToOptions,
LimitToSelection,
Primitive
} from '../interfaces';
import 'rxjs/add/operator/merge';
import 'rxjs/add/operator/combineLatest';

export function observeQuery(query: Query): Observable<ScalarQuery> {
if (!isPresent(query)) {
return new ScalarObservable(null);
return Observable.of(null);
}

return Observable.create((observer: Observer<ScalarQuery>) => {
Expand Down Expand Up @@ -97,7 +97,7 @@ export function getOrderObservables(query: Query): Observable<OrderBySelection>
} else {
return new Observable<OrderBySelection>(subscriber => {
subscriber.next(null);
});
});
}
}

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './angularfire2';
8 changes: 8 additions & 0 deletions src/test-root.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export * from './angularfire2.spec';
export * from './database/firebase_list_factory.spec';
export * from './database/firebase_object_factory.spec';
export * from './database/firebase_list_observable.spec';
export * from './database/firebase_object_observable.spec';
export * from './database/query_observable.spec';
export * from './auth/auth.spec';
export * from './auth/auth_backend.spec';
13 changes: 5 additions & 8 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Subscription } from 'rxjs/Subscription';
import { QueueScheduler } from 'rxjs/scheduler/QueueScheduler';
import { QueueAction } from 'rxjs/scheduler/QueueAction';
import { Scheduler } from 'rxjs/Scheduler';
import { queue } from 'rxjs/scheduler/queue';
import { AFUnwrappedDataSnapshot} from './interfaces';

export function isPresent(obj: any): boolean {
Expand Down Expand Up @@ -40,7 +39,7 @@ export interface CheckUrlRef {
}

/**
* Unwraps the data returned in the DataSnapshot. Exposes the DataSnapshot key and exists methods through the $key and $exists properties respectively. If the value is primitive, it is unwrapped using a $value property. The $ properies mean they cannot be saved in the Database as those characters are invalid.
* Unwraps the data returned in the DataSnapshot. Exposes the DataSnapshot key and exists methods through the $key and $exists properties respectively. If the value is primitive, it is unwrapped using a $value property. The $ properies mean they cannot be saved in the Database as those characters are invalid.
* @param {DataSnapshot} snapshot - The snapshot to unwrap
* @return AFUnwrappedDataSnapshot
* @example
Expand Down Expand Up @@ -95,12 +94,10 @@ export function stripLeadingSlash(value: string): string {
* TODO: remove this scheduler once Rx has a more robust story for working
* with zones.
*/
export class ZoneScheduler extends QueueScheduler {
constructor(public zone: Zone) {
super(QueueAction);
}
export class ZoneScheduler {
constructor(public zone: Zone) {}

schedule(...args): Subscription {
return <Subscription>this.zone.run(() => super.schedule.apply(this, args));
return <Subscription>this.zone.run(() => queue.schedule.apply(queue, args));
}
}
7 changes: 3 additions & 4 deletions tools/rewrite-published-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
*/
var fs = require('fs');
var srcPackage = require('../package.json');
var [MAIN, JSNEXT_MAIN] = ['main', 'jsnext:main'].map(k => srcPackage[k].replace('/dist/', '/'));

delete srcPackage.scripts;

var peerDependencies = Object.assign({}, srcPackage.dependencies);
// See note about including firebase as dependency
delete peerDependencies.firebase;

var outPackage = Object.assign({}, srcPackage, {
peerDependencies,
main: MAIN,
typings: "angularfire2.d.ts",
"jsnext:main": JSNEXT_MAIN,
dependencies: {
/**
* Firebase SDK should be a dependency since it's not required that
Expand Down
17 changes: 4 additions & 13 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"module": "commonjs",
"module": "es2015",
"target": "es5",
"noImplicitAny": false,
"outDir": "dist",
Expand All @@ -15,20 +15,11 @@
"es2015",
"dom"
],
"skipLibCheck": true
"skipLibCheck": true,
"moduleResolution": "node"
},
"files": [
"src/angularfire2.ts",
"src/angularfire2.spec.ts",
"src/database/index.ts",
"src/database/firebase_list_factory.spec.ts",
"src/database/firebase_object_factory.spec.ts",
"src/database/firebase_list_observable.spec.ts",
"src/database/firebase_object_observable.spec.ts",
"src/database/query_observable.spec.ts",
"src/auth/auth.ts",
"src/auth/auth.spec.ts",
"src/auth/auth_backend.spec.ts",
"src/test-root.ts",
"typings/index.d.ts",
"node_modules/zone.js/dist/zone.js.d.ts"
],
Expand Down
Loading

0 comments on commit ec58e05

Please sign in to comment.