Skip to content

Commit 2aad08d

Browse files
refactor(coreservices): Split location services up into location and locationConfig
refactor(angular1): move isolated angular1 code to its own directory refactor(runtime): replaced `runtime` ($q and $injector) with `services` refactor(stateEvents): Do not include the legacy `$stateChange*` in the default build
1 parent 584a255 commit 2aad08d

23 files changed

+373
-347
lines changed

Gruntfile.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@ module.exports = function (grunt) {
3434
},
3535
uglify: {
3636
options: {
37-
banner: '<%= meta.banner %>\n'
37+
banner: '<%= meta.banner %>\n',
38+
mangle: true
3839
},
3940
build: {
4041
files: {
41-
'<%= builddir %>/<%= pkg.name %>.min.js': ['<banner:meta.banner>', '<%= builddir %>/<%= pkg.name %>.js']
42+
'<%= builddir %>/<%= pkg.name %>.min.js': ['<banner:meta.banner>', '<%= builddir %>/<%= pkg.name %>.js'],
43+
'<%= builddir %>/ng1/stateEvents.min.js': ['<banner:meta.banner>', '<%= builddir %>/ng1/stateEvents.js']
4244
}
4345
}
4446
},
@@ -144,18 +146,28 @@ module.exports = function (grunt) {
144146

145147
grunt.registerTask('integrate', ['clean', 'build', 'karma:ng12', 'karma:ng13', 'karma:ng14']);
146148
grunt.registerTask('default', ['build', 'karma:unit', 'docs']);
147-
grunt.registerTask('build', 'Perform a normal build', ['clean', 'ts', 'webpack', 'uglify']);
149+
grunt.registerTask('build', 'Perform a normal build', ['clean', 'ts', 'webpack', 'bundles', 'uglify']);
148150
grunt.registerTask('dist-docs', 'Perform a clean build and generate documentation', ['build', 'ngdocs']);
149151
grunt.registerTask('release', 'Tag and perform a release', ['prepare-release', 'build', 'perform-release']);
150152
grunt.registerTask('dev', 'Run dev server and watch for changes', ['build', 'connect:server', 'karma:background', 'watch']);
151153
grunt.registerTask('sample', 'Run connect server with keepalive:true for sample app development', ['connect:sample']);
152154

153155
grunt.registerTask('docs', 'Generate documentation to _doc', function() {
154156
promising(this,
155-
system('./node_modules/typedoc/bin/typedoc --readme ./README.md --name "UI-Router" --theme default --mode modules --module commonjs --target es5 --out _doc src/params src/path src/resolve src/state src/transition src/url src/view')
157+
system('./node_modules/typedoc/bin/typedoc --readme ./README.md --name "UI-Router" --theme default --mode modules --module commonjs --target es5 --out _doc src/params src/path src/resolve src/state src/transition src/url src/view src/ng1')
156158
);
157159
});
158160

161+
grunt.registerTask('bundles', 'Create the bundles and reorganize any additional dist files (addons, etc)', function() {
162+
var builddir = grunt.config('builddir');
163+
grunt.task.requires([ 'clean', 'ts' ]);
164+
grunt.task.run(['webpack']);
165+
166+
['stateEvents.js', 'stateEvents.js.map'].forEach(function(file) {
167+
grunt.file.copy(builddir + "/es5/ng1/" + file, builddir + "/ng1/" + file);
168+
})
169+
});
170+
159171
grunt.registerTask('publish-pages', 'Publish a clean build, docs, and sample to github.io', function () {
160172
promising(this,
161173
ensureCleanMaster().then(function () {
@@ -207,9 +219,10 @@ module.exports = function (grunt) {
207219
});
208220

209221
grunt.registerTask('perform-release', function () {
210-
grunt.task.requires([ 'prepare-release', 'build' ]);
211-
212222
var version = grunt.config('pkg.version'), releasedir = grunt.config('builddir');
223+
grunt.task.requires([ 'prepare-release', 'build' ]);
224+
grunt.file.delete(releasedir + "/es5");
225+
grunt.file.delete(releasedir + "/es6");
213226
promising(this,
214227
system('git add \'' + releasedir + '\'').then(function () {
215228
return system('git commit -m \'release ' + version + '\'');

files.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ routerFiles = {
33
es6Entrypoint: ['./build/es6/ui-router.js'],
44

55
buildDest: ['build/angular-ui-router.js'], // The distribution file
6-
src: ['src/ui-router.ts'], // Main UI-Router module (imports everything else)
6+
src: [
7+
'src/ui-router.ts', // Main UI-Router module (imports everything else)
8+
'src/ng1/stateEvents.ts' // There might be a better approach to compiling this file
9+
],
710

811
// Test helpers
912
testUtils: [

src/common/coreservices.ts

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,51 @@
1-
import * as foo from "./common";
2-
31
/**
4-
* Services related to the browser location (url)
2+
* This module is a stub for core services such as Dependency Injection or Browser Location.
3+
* Core services may be implemented by a specific framework, such as ng1 or ng2, or be pure javascript.
4+
*
5+
* @module common
56
*/
7+
8+
/** for typedoc */
9+
import * as foo from "./common";
10+
import IQService = angular.IQService;
11+
import IInjectorService = angular.auto.IInjectorService;
12+
13+
let notImplemented = (fnname) => () => {
14+
throw new Error(`${fnname}(): No coreservices implementation for UI-Router is loaded. You should include one of: ['angular1.js']`);
15+
};
16+
17+
let services: CoreServices = {
18+
$q: undefined,
19+
$injector: undefined,
20+
location: <any> {},
21+
locationConfig: <any> {}
22+
};
23+
24+
["replace", "url", "path", "search", "hash"]
25+
.forEach(key => services.location[key] = notImplemented(key));
26+
27+
["port", "protocol", "host", "baseHref", "html5Mode", "hashPrefix" ]
28+
.forEach(key => services.locationConfig[key] = notImplemented(key));
29+
30+
interface CoreServices {
31+
$q: IQService;
32+
$injector: IInjectorService;
33+
/** Services related to getting or setting the browser location (url) */
34+
location: LocationServices;
35+
/** Retrieves configuration for how to construct a URL. */
36+
locationConfig: LocationConfig;
37+
}
38+
639
interface LocationServices {
740
replace(): void;
841
url(newurl: string): string;
942
url(): string;
1043
path(): string;
1144
search(): string;
1245
hash(): string;
46+
}
1347

48+
interface LocationConfig {
1449
port(): number;
1550
protocol(): string;
1651
host(): string;
@@ -21,30 +56,4 @@ interface LocationServices {
2156
hashPrefix(newprefix: string): string;
2257
}
2358

24-
interface Services {
25-
location: LocationServices;
26-
}
27-
28-
let services: Services = {
29-
location: <any> {}
30-
};
31-
32-
let notImplemented = (fnname) => () => {
33-
throw new Error(`${fnname}(): No coreservices implementation for UI-Router is loaded. You should include one of: ['angular1.js']`);
34-
};
35-
36-
[
37-
"replace",
38-
"url",
39-
"path",
40-
"search",
41-
"hash",
42-
"port",
43-
"protocol",
44-
"host",
45-
"baseHref",
46-
"html5Mode",
47-
"hashPrefix"
48-
].forEach(key => services.location[key] = notImplemented(key));
49-
5059
export {services};

src/common/module.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/** @module common */ /** for typedoc */
2-
export * from "./angular1";
32
export * from "./common";
43
export * from "./coreservices";
54
export * from "./queue";
Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,23 @@
1-
/** @module common */ /** for typedoc */
2-
31
/**
4-
* Provides the implementation for services defined in [[coreservices]], and registers some
5-
* with the angular 1 injector.
2+
* Angular 1 plugin:
3+
*
4+
* - Provides an implementation for the [[CoreServices]] API, based on angular 1 services.
5+
* - Also registers some services with the angular 1 injector.
6+
* - Creates and bootstraps a new [[Router]] object, usiong the angular 1 lifecycle
7+
*
8+
* @module ng1
9+
* @preferred
610
*/
711

12+
/** for typedoc */
813
/// <reference path='../../typings/angularjs/angular.d.ts' />
914
import {IQService} from "angular";
1015
import {Router} from "../router";
11-
import {services} from "./coreservices";
12-
import {isObject} from "./common";
16+
import {services} from "../common/coreservices";
17+
import {isObject} from "../common/common";
1318

1419
let app = angular.module("ui.router.angular1", []);
1520

16-
interface IRuntime {
17-
setRuntimeInjector($injector: ng.auto.IInjectorService);
18-
$injector: ng.auto.IInjectorService;
19-
$q: IQService;
20-
}
21-
22-
export let runtime: IRuntime = {
23-
setRuntimeInjector: function($injector: ng.auto.IInjectorService) {
24-
runtime.$injector = $injector;
25-
runtime.$q = $injector.get("$q");
26-
},
27-
$injector: undefined,
28-
$q: undefined
29-
};
30-
3121
/**
3222
* Annotates a controller expression (may be a controller function(), a "controllername",
3323
* or "controllername as name")
@@ -42,7 +32,7 @@ export let runtime: IRuntime = {
4232
*/
4333

4434
export function annotateController(controllerExpression): string[] {
45-
let $injector = runtime.$injector;
35+
let $injector = services.$injector;
4636
let $controller = $injector.get("$controller");
4737
let oldInstantiate = $injector.instantiate;
4838
try {
@@ -61,9 +51,10 @@ export function annotateController(controllerExpression): string[] {
6151
}
6252
}
6353

64-
runBlock.$inject = ["$injector"];
65-
function runBlock($injector) {
66-
runtime.setRuntimeInjector($injector);
54+
runBlock.$inject = ['$injector', '$q'];
55+
function runBlock($injector, $q) {
56+
services.$injector = $injector;
57+
services.$q = $q;
6758
}
6859

6960
app.run(runBlock);
@@ -76,34 +67,34 @@ let router = null;
7667
ng1UIRouter.$inject = ['$locationProvider'];
7768
function ng1UIRouter($locationProvider) {
7869
router = new Router();
79-
bindFunctions(['hashPrefix'], $locationProvider, services.location);
70+
bindFunctions(['hashPrefix'], $locationProvider, services.locationConfig);
8071

8172
this.$get = $get;
8273
$get.$inject = ['$location', '$browser', '$sniffer'];
8374
function $get($location, $browser, $sniffer) {
8475

85-
services.location.html5Mode = function() {
76+
services.locationConfig.html5Mode = function() {
8677
var html5Mode = $locationProvider.html5Mode();
8778
html5Mode = isObject(html5Mode) ? html5Mode.enabled : html5Mode;
8879
return html5Mode && $sniffer.history;
8980
};
9081

91-
var $locationFnNames = ['hash', 'path', 'replace', 'search', 'url', 'port', 'protocol', 'host'];
92-
bindFunctions($locationFnNames, $location, services.location);
93-
bindFunctions(['baseHref'], $browser, services.location);
82+
bindFunctions(["replace", "url", "path", "search", "hash"], $location, services.location);
83+
bindFunctions([ 'port', 'protocol', 'host'], $location, services.locationConfig);
84+
bindFunctions(['baseHref'], $browser, services.locationConfig);
9485

9586
return router;
9687
}
9788
}
9889

99-
angular.module('ui.router.init', ['ng']).provider("ng1UIRouter", <any> ng1UIRouter);
90+
angular.module('ui.router.init', []).provider("ng1UIRouter", <any> ng1UIRouter);
10091
// Register as a provider so it's available to other providers
10192
angular.module('ui.router.util').provider('$urlMatcherFactory', ['ng1UIRouterProvider', () => router.urlMatcherFactory]);
10293
angular.module('ui.router.router').provider('$urlRouter', ['ng1UIRouterProvider', () => router.urlRouterProvider]);
10394
angular.module('ui.router.state').provider('$state', ['ng1UIRouterProvider', () => router.stateProvider]);
10495

10596
/* This effectively calls $get() to init when we enter runtime */
106-
angular.module('ui.router.state').run(['ng1UIRouter', function(ng1UIRouter) { }]);
97+
angular.module('ui.router.init').run(['ng1UIRouter', function(ng1UIRouter) { }]);
10798
angular.module('ui.router.state').run(['$state', function($state) { }]);
10899
angular.module('ui.router.util').run(['$urlMatcherFactory', function($urlMatcherFactory) { }]);
109100

0 commit comments

Comments
 (0)