Skip to content

Commit ec58e05

Browse files
committed
feat(packaging): add aot-friendly packaging
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.
1 parent 90ccc2d commit ec58e05

16 files changed

+94
-199
lines changed

karma-test-shim.js

+4-82
Original file line numberDiff line numberDiff line change
@@ -2,85 +2,7 @@
22
Error.stackTraceLimit = Infinity;
33
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
44

5-
__karma__.loaded = function () {
6-
};
7-
8-
9-
function isJsFile(path) {
10-
return path.slice(-3) == '.js';
11-
}
12-
13-
function isSpecFile(path) {
14-
return path.slice(-7) == 'spec.js';
15-
}
16-
17-
function isBuiltFile(path) {
18-
var builtPath = '/base/dist/';
19-
return isJsFile(path) && (path.substr(0, builtPath.length) == builtPath);
20-
}
21-
22-
var allSpecFiles = Object.keys(window.__karma__.files)
23-
.filter(isSpecFile)
24-
.filter(isBuiltFile);
25-
26-
// Load our SystemJS configuration.
27-
System.config({
28-
baseURL: '/base'
29-
});
30-
31-
System.config(
32-
{
33-
paths: {
34-
// paths serve as alias
35-
'npm:': 'node_modules/'
36-
},
37-
map: {
38-
'app': 'dist',
39-
'@angular/core': 'npm:@angular/core/bundles/core.umd.js',
40-
'@angular/common': 'npm:@angular/common/bundles/common.umd.js',
41-
'@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
42-
'@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
43-
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
44-
45-
// angular testing umd bundles
46-
'@angular/core/testing': 'npm:@angular/core/bundles/core-testing.umd.js',
47-
'@angular/common/testing': 'npm:@angular/common/bundles/common-testing.umd.js',
48-
'@angular/compiler/testing': 'npm:@angular/compiler/bundles/compiler-testing.umd.js',
49-
'@angular/platform-browser/testing': 'npm:@angular/platform-browser/bundles/platform-browser-testing.umd.js',
50-
'@angular/platform-browser-dynamic/testing': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic-testing.umd.js',
51-
52-
// other libraries
53-
'rxjs': 'npm:rxjs',
54-
'firebase': 'npm:firebase/firebase.js'
55-
},
56-
packages: {
57-
'app': {
58-
defaultExtension: 'js'
59-
},
60-
'rxjs': {
61-
main: 'Rx.js',
62-
defaultExtension: 'js'
63-
}
64-
}
65-
});
66-
67-
Promise.all([
68-
System.import('@angular/core/testing'),
69-
System.import('@angular/platform-browser-dynamic/testing')
70-
]).then(function (providers) {
71-
var testing = providers[0];
72-
var testingBrowser = providers[1];
73-
74-
testing.TestBed.initTestEnvironment(
75-
testingBrowser.BrowserDynamicTestingModule,
76-
testingBrowser.platformBrowserDynamicTesting()
77-
);
78-
79-
}).then(function() {
80-
// Finally, load all spec files.
81-
// This will run the tests directly.
82-
return Promise.all(
83-
allSpecFiles.map(function (moduleName) {
84-
return System.import(moduleName);
85-
}));
86-
}).then(__karma__.start, __karma__.error);
5+
ng.core.testing.TestBed.initTestEnvironment(
6+
ng.platformBrowserDynamic.testing.BrowserDynamicTestingModule,
7+
ng.platformBrowserDynamic.testing.platformBrowserDynamicTesting()
8+
);

karma.conf.js

+14-18
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ module.exports = function(config) {
1212

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

15-
// System.js for module loading
16-
'node_modules/systemjs/dist/system-polyfills.js',
17-
'node_modules/systemjs/dist/system.src.js',
18-
1915
// Zone.js dependencies
2016
'node_modules/zone.js/dist/zone.js',
2117
'node_modules/zone.js/dist/proxy.js',
@@ -24,21 +20,13 @@ module.exports = function(config) {
2420
'node_modules/zone.js/dist/async-test.js',
2521
'node_modules/zone.js/dist/fake-async-test.js',
2622

27-
// RxJs.
28-
{ pattern: 'node_modules/rxjs/**/*.js', included: false, watched: false },
29-
{ pattern: 'node_modules/rxjs/**/*.js.map', included: false, watched: false },
30-
31-
32-
{ pattern: 'karma-test-shim.js', included: true, watched: true },
23+
'node_modules/rxjs/bundles/Rx.{js,map}',
3324

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

39-
{ pattern: 'node_modules/firebase/firebase.js', included: false, watched: false },
40-
{ pattern: 'dist/**/*.js', included: false, watched: true },
41-
{ pattern: 'dist/**/*.js.map', included: false, watched: false }
27+
'karma-test-shim.js',
28+
'node_modules/firebase/firebase.js',
29+
'dist/bundles/test-root.umd.{js,map}',
4230
],
4331

4432
port: 9876,
@@ -48,4 +36,12 @@ module.exports = function(config) {
4836
browsers: ['Chrome'],
4937
singleRun: false
5038
})
51-
};
39+
};
40+
41+
function getAngularFiles(packages) {
42+
return packages.reduce((files, pkg) => {
43+
files.push(`node_modules/@angular/${pkg}/bundles/${pkg}.umd.js`);
44+
files.push(`node_modules/@angular/${pkg}/bundles/${pkg}-testing.umd.js`);
45+
return files;
46+
}, []);
47+
}

package.json

+13-8
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@
22
"name": "angularfire2",
33
"version": "2.0.0-beta.5",
44
"description": "",
5-
"main": "./dist/angularfire2.js",
6-
"jsnext:main": "./dist/esm/angularfire2.js",
5+
"main": "bundles/angularfire2.umd.js",
6+
"module": "index.js",
77
"scripts": {
88
"test": "npm run build; karma start --single-run",
9-
"test:watch": "karma start",
10-
"build": "rm -rf dist; tsc",
11-
"build:watch": "rm -rf dist && tsc -w",
12-
"build_npm": "rm -rf dist && ngc -p tsconfig.publish.es5.json && ngc -p tsconfig.publish.es6.json && npm run postbuild_npm",
9+
"test:watch": "concurrently 'npm run build:watch' 'npm run delayed_karma'",
10+
"delayed_karma": "sleep 10 && karma start",
11+
"delayed_rollup": "sleep 5 && rollup --watch -c rollup.test.config.js",
12+
"build:watch": "rm -rf dist && concurrently 'tsc -w' 'npm run delayed_rollup'",
13+
"build": "rm -rf dist && tsc && rollup -c rollup.test.config.js",
14+
"build_npm": "rm -rf dist && ngc -p tsconfig.publish.es6.json && rollup -c rollup.publish.config.js && npm run postbuild_npm",
15+
"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/",
1316
"postbuild_npm": "cp package.json README.md .npmignore dist/ && npm run rewrite_npm_package",
1417
"rewrite_npm_package": "node --harmony_destructuring tools/rewrite-published-package.js",
1518
"e2e_test": "webdriver-manager update && npm run build_e2e && protractor",
16-
"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/",
1719
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 1"
1820
},
1921
"keywords": [
@@ -44,6 +46,7 @@
4446
"devDependencies": {
4547
"@angular/compiler-cli": "^0.5.0",
4648
"@angular/platform-server": "^2.0.0-rc.5",
49+
"concurrently": "^2.2.0",
4750
"conventional-changelog-cli": "^1.2.0",
4851
"es6-module-loader": "^0.17.10",
4952
"es6-shim": "^0.35.0",
@@ -64,6 +67,8 @@
6467
"parse5": "^1.3.2",
6568
"protractor": "3.0.0",
6669
"reflect-metadata": "0.1.2",
70+
"rollup": "^0.35.11",
71+
"rollup-watch": "^2.5.0",
6772
"systemjs": "^0.19.16",
6873
"systemjs-builder": "^0.15.7",
6974
"traceur": "0.0.96",
@@ -73,5 +78,5 @@
7378
"typings": "^1.3.2",
7479
"zone.js": "^0.6.21"
7580
},
76-
"typings": "dist/angularfire2.d.ts"
81+
"typings": "index.d.ts"
7782
}

rollup-globals.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export default (mod) => {
2+
if (mod === 'rxjs') return 'Rx';
3+
if (mod.indexOf('rxjs/operator') === 0) return `Rx.Observable.prototype`;
4+
if (mod === 'rxjs/scheduler/queue') return 'Rx.Scheduler';
5+
if (mod.indexOf('rxjs/') === 0) return 'Rx';
6+
7+
if (mod === 'firebase') return 'firebase';
8+
if (mod === '@angular/core') return 'ng.core';
9+
if (mod === '@angular/core/testing') return 'ng.core.testing';
10+
}

rollup.publish.config.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import globals from './rollup-globals';
2+
3+
export default {
4+
entry: 'dist/index.js',
5+
dest: 'dist/bundles/angularFire2.umd.js',
6+
format: 'umd',
7+
moduleName: 'angularFire2',
8+
globals
9+
}

rollup.test.config.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import globals from './rollup-globals';
2+
3+
export default {
4+
entry: 'dist/test-root.js',
5+
dest: 'dist/bundles/test-root.umd.js',
6+
format: 'umd',
7+
moduleName: 'angularFire2.test',
8+
globals
9+
}

src/auth/firebase_sdk_auth_backend.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,4 @@ export class FirebaseSdkAuthBackend extends AuthBackend {
123123
// Cast Firebase promises as Zone-patched Promises
124124
function castPromise<T>(promiseLike: PromiseLike<T>): Promise<T> {
125125
return Promise.resolve(promiseLike) as Promise<T>;
126-
}
126+
}

src/database/query_observable.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
import { Observable } from 'rxjs/Observable';
2-
import { ScalarObservable } from 'rxjs/observable/ScalarObservable';
2+
import 'rxjs/add/observable/of';
33
import { Operator } from 'rxjs/Operator';
44
import { Observer } from 'rxjs/Observer';
55
import { merge } from 'rxjs/operator/merge';
66
import { map } from 'rxjs/operator/map';
7-
import {
8-
Query,
9-
ScalarQuery,
10-
OrderByOptions,
11-
OrderBySelection,
12-
LimitToOptions,
13-
LimitToSelection,
14-
Primitive
7+
import {
8+
Query,
9+
ScalarQuery,
10+
OrderByOptions,
11+
OrderBySelection,
12+
LimitToOptions,
13+
LimitToSelection,
14+
Primitive
1515
} from '../interfaces';
1616
import 'rxjs/add/operator/merge';
1717
import 'rxjs/add/operator/combineLatest';
1818

1919
export function observeQuery(query: Query): Observable<ScalarQuery> {
2020
if (!isPresent(query)) {
21-
return new ScalarObservable(null);
21+
return Observable.of(null);
2222
}
2323

2424
return Observable.create((observer: Observer<ScalarQuery>) => {
@@ -97,7 +97,7 @@ export function getOrderObservables(query: Query): Observable<OrderBySelection>
9797
} else {
9898
return new Observable<OrderBySelection>(subscriber => {
9999
subscriber.next(null);
100-
});
100+
});
101101
}
102102
}
103103

src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './angularfire2';

src/test-root.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export * from './angularfire2.spec';
2+
export * from './database/firebase_list_factory.spec';
3+
export * from './database/firebase_object_factory.spec';
4+
export * from './database/firebase_list_observable.spec';
5+
export * from './database/firebase_object_observable.spec';
6+
export * from './database/query_observable.spec';
7+
export * from './auth/auth.spec';
8+
export * from './auth/auth_backend.spec';

src/utils.ts

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Subscription } from 'rxjs/Subscription';
2-
import { QueueScheduler } from 'rxjs/scheduler/QueueScheduler';
3-
import { QueueAction } from 'rxjs/scheduler/QueueAction';
42
import { Scheduler } from 'rxjs/Scheduler';
3+
import { queue } from 'rxjs/scheduler/queue';
54
import { AFUnwrappedDataSnapshot} from './interfaces';
65

76
export function isPresent(obj: any): boolean {
@@ -40,7 +39,7 @@ export interface CheckUrlRef {
4039
}
4140

4241
/**
43-
* 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.
42+
* 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.
4443
* @param {DataSnapshot} snapshot - The snapshot to unwrap
4544
* @return AFUnwrappedDataSnapshot
4645
* @example
@@ -95,12 +94,10 @@ export function stripLeadingSlash(value: string): string {
9594
* TODO: remove this scheduler once Rx has a more robust story for working
9695
* with zones.
9796
*/
98-
export class ZoneScheduler extends QueueScheduler {
99-
constructor(public zone: Zone) {
100-
super(QueueAction);
101-
}
97+
export class ZoneScheduler {
98+
constructor(public zone: Zone) {}
10299

103100
schedule(...args): Subscription {
104-
return <Subscription>this.zone.run(() => super.schedule.apply(this, args));
101+
return <Subscription>this.zone.run(() => queue.schedule.apply(queue, args));
105102
}
106103
}

tools/rewrite-published-package.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@
55
*/
66
var fs = require('fs');
77
var srcPackage = require('../package.json');
8-
var [MAIN, JSNEXT_MAIN] = ['main', 'jsnext:main'].map(k => srcPackage[k].replace('/dist/', '/'));
8+
9+
delete srcPackage.scripts;
910

1011
var peerDependencies = Object.assign({}, srcPackage.dependencies);
1112
// See note about including firebase as dependency
1213
delete peerDependencies.firebase;
14+
1315
var outPackage = Object.assign({}, srcPackage, {
1416
peerDependencies,
15-
main: MAIN,
16-
typings: "angularfire2.d.ts",
17-
"jsnext:main": JSNEXT_MAIN,
1817
dependencies: {
1918
/**
2019
* Firebase SDK should be a dependency since it's not required that

tsconfig.json

+4-13
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"compilerOptions": {
33
"experimentalDecorators": true,
44
"emitDecoratorMetadata": true,
5-
"module": "commonjs",
5+
"module": "es2015",
66
"target": "es5",
77
"noImplicitAny": false,
88
"outDir": "dist",
@@ -15,20 +15,11 @@
1515
"es2015",
1616
"dom"
1717
],
18-
"skipLibCheck": true
18+
"skipLibCheck": true,
19+
"moduleResolution": "node"
1920
},
2021
"files": [
21-
"src/angularfire2.ts",
22-
"src/angularfire2.spec.ts",
23-
"src/database/index.ts",
24-
"src/database/firebase_list_factory.spec.ts",
25-
"src/database/firebase_object_factory.spec.ts",
26-
"src/database/firebase_list_observable.spec.ts",
27-
"src/database/firebase_object_observable.spec.ts",
28-
"src/database/query_observable.spec.ts",
29-
"src/auth/auth.ts",
30-
"src/auth/auth.spec.ts",
31-
"src/auth/auth_backend.spec.ts",
22+
"src/test-root.ts",
3223
"typings/index.d.ts",
3324
"node_modules/zone.js/dist/zone.js.d.ts"
3425
],

0 commit comments

Comments
 (0)