Skip to content

Commit 0648723

Browse files
committed
Revert "Revert "feat(router): add angular 1.x router""
This reverts commit 298f1fb.
1 parent 964884e commit 0648723

19 files changed

+1977
-4
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ env:
3636
- MODE=saucelabs DART_CHANNEL=dev
3737
- MODE=dart_experimental DART_CHANNEL=dev
3838
- MODE=js DART_CHANNEL=dev
39+
- MODE=router DART_CHANNEL=dev
3940
- MODE=lint DART_CHANNEL=dev
4041

4142
matrix:

gulpfile.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ var util = require('./tools/build/util');
3737
var bundler = require('./tools/build/bundle');
3838
var replace = require('gulp-replace');
3939
var insert = require('gulp-insert');
40+
var buildRouter = require('./modules/angular1_router/build');
4041
var uglify = require('gulp-uglify');
4142
var shouldLog = require('./tools/build/logging');
4243
var tslint = require('gulp-tslint');
@@ -604,6 +605,34 @@ gulp.task('!test.unit.js/karma-run', function(done) {
604605
runKarma('karma-js.conf.js', done);
605606
});
606607

608+
gulp.task('test.unit.router', function (done) {
609+
runSequence(
610+
'!test.unit.router/karma-server',
611+
function() {
612+
watch('modules/**', [
613+
'buildRouter.dev',
614+
'!test.unit.router/karma-run'
615+
]);
616+
}
617+
);
618+
});
619+
620+
gulp.task('!test.unit.router/karma-server', function() {
621+
karma.server.start({configFile: __dirname + '/modules/angular1_router/karma-router.conf.js'});
622+
});
623+
624+
625+
gulp.task('!test.unit.router/karma-run', function(done) {
626+
karma.runner.run({configFile: __dirname + '/modules/angular1_router/karma-router.conf.js'}, function(exitCode) {
627+
// ignore exitCode, we don't want to fail the build in the interactive (non-ci) mode
628+
// karma will print all test failures
629+
done();
630+
});
631+
});
632+
633+
gulp.task('buildRouter.dev', function () {
634+
buildRouter();
635+
});
607636

608637
gulp.task('test.unit.dart', function (done) {
609638
runSequence(
@@ -641,6 +670,12 @@ gulp.task('!test.unit.dart/karma-server', function() {
641670
});
642671

643672

673+
gulp.task('test.unit.router/ci', function (done) {
674+
var browserConf = getBrowsersFromCLI();
675+
karma.server.start({configFile: __dirname + '/modules/angular1_router/karma-router.conf.js',
676+
singleRun: true, reporters: ['dots'], browsers: browserConf.browsersToRun}, done);
677+
});
678+
644679
gulp.task('test.unit.js/ci', function (done) {
645680
var browserConf = getBrowsersFromCLI();
646681
karma.server.start({configFile: __dirname + '/karma-js.conf.js',

karma-dart.conf.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ module.exports = function(config) {
2727

2828
exclude: [
2929
'dist/dart/**/packages/**',
30+
'modules/angular1_router/**'
3031
],
3132

3233
karmaDartImports: {

karma-js.conf.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ module.exports = function(config) {
3131

3232
exclude: [
3333
'dist/js/dev/es5/**/e2e_test/**',
34+
'dist/angular1_router.js'
3435
],
3536

3637
customLaunchers: sauceConf.customLaunchers,

modules/angular1_router/build.js

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
var fs = require('fs');
2+
var ts = require('typescript');
3+
4+
var files = [
5+
'lifecycle_annotations_impl.ts',
6+
'url_parser.ts',
7+
'path_recognizer.ts',
8+
'route_config_impl.ts',
9+
'async_route_handler.ts',
10+
'sync_route_handler.ts',
11+
'route_recognizer.ts',
12+
'instruction.ts',
13+
'route_config_nomalizer.ts',
14+
'route_lifecycle_reflector.ts',
15+
'route_registry.ts',
16+
'router.ts'
17+
];
18+
19+
var PRELUDE = '(function(){\n';
20+
var POSTLUDE = '\n}());\n';
21+
var FACADES = fs.readFileSync(__dirname + '/lib/facades.es5', 'utf8');
22+
var TRACEUR_RUNTIME = fs.readFileSync(__dirname + '/../../node_modules/traceur/bin/traceur-runtime.js', 'utf8');
23+
var DIRECTIVES = fs.readFileSync(__dirname + '/src/ng_outlet.js', 'utf8');
24+
function main() {
25+
var dir = __dirname + '/../angular2/src/router/';
26+
27+
var out = '';
28+
29+
var sharedCode = '';
30+
files.forEach(function (file) {
31+
var moduleName = 'router/' + file.replace(/\.ts$/, '');
32+
33+
sharedCode += transform(moduleName, fs.readFileSync(dir + file, 'utf8'));
34+
});
35+
36+
out += "angular.module('ngComponentRouter')";
37+
out += angularFactory('$router', ['$q', '$location', '$$controllerIntrospector',
38+
'$browser', '$rootScope', '$injector'], [
39+
FACADES,
40+
"var exports = {Injectable: function () {}};",
41+
"var require = function () {return exports;};",
42+
sharedCode,
43+
"var RouteConfig = exports.RouteConfig;",
44+
"angular.annotations = {RouteConfig: RouteConfig, CanActivate: exports.CanActivate};",
45+
"angular.stringifyInstruction = exports.stringifyInstruction;",
46+
"var RouteRegistry = exports.RouteRegistry;",
47+
"var RootRouter = exports.RootRouter;",
48+
//TODO: move this code into a templated JS file
49+
"var registry = new RouteRegistry();",
50+
"var location = new Location();",
51+
52+
"$$controllerIntrospector(function (name, constructor) {",
53+
"if (constructor.$canActivate) {",
54+
"constructor.annotations = constructor.annotations || [];",
55+
"constructor.annotations.push(new angular.annotations.CanActivate(function (instruction) {",
56+
"return $injector.invoke(constructor.$canActivate, constructor, {",
57+
"$routeParams: instruction.component ? instruction.component.params : instruction.params",
58+
"});",
59+
"}));",
60+
"}",
61+
"if (constructor.annotations) {",
62+
"constructor.annotations.forEach(function(annotation) {",
63+
"if (annotation instanceof RouteConfig) {",
64+
"annotation.configs.forEach(function (config) {",
65+
"registry.config(constructor, config);",
66+
"});",
67+
"}",
68+
"});",
69+
"}",
70+
"});",
71+
72+
"var router = new RootRouter(registry, undefined, location, new Object());",
73+
"$rootScope.$watch(function () { return $location.path(); }, function (path) { router.navigate(path); });",
74+
75+
"return router;"
76+
].join('\n'));
77+
78+
return PRELUDE + TRACEUR_RUNTIME + DIRECTIVES + out + POSTLUDE;
79+
}
80+
81+
82+
/*
83+
* Given a directory name and a file's TypeScript content, return an object with the ES5 code,
84+
* sourcemap, anf exported variable identifier name for the content.
85+
*/
86+
var IMPORT_RE = new RegExp("import \\{?([\\w\\n_, ]+)\\}? from '(.+)';?", 'g');
87+
function transform(dir, contents) {
88+
contents = contents.replace(IMPORT_RE, function (match, imports, includePath) {
89+
//TODO: remove special-case
90+
if (isFacadeModule(includePath) || includePath === './router_outlet') {
91+
return '';
92+
}
93+
return match;
94+
});
95+
return ts.transpile(contents, {
96+
target: ts.ScriptTarget.ES5,
97+
module: ts.ModuleKind.CommonJS,
98+
sourceRoot: dir
99+
});
100+
}
101+
102+
103+
function angularFactory(name, deps, body) {
104+
return ".factory('" + name + "', [" +
105+
deps.map(function (service) {
106+
return "'" + service + "', ";
107+
}).join('') +
108+
"function (" + deps.join(', ') + ") {\n" + body + "\n}])";
109+
}
110+
111+
112+
function isFacadeModule(modulePath) {
113+
return modulePath.indexOf('facade') > -1 ||
114+
modulePath === 'angular2/src/reflection/reflection';
115+
}
116+
117+
module.exports = function () {
118+
var dist = __dirname + '/../../dist';
119+
if (!fs.existsSync(dist)) {
120+
fs.mkdirSync(dist);
121+
}
122+
fs.writeFileSync(dist + '/angular_1_router.js', main(files));
123+
};

modules/angular1_router/index.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<div ng-app="myApp" ng-controller="MyCtrl">
9+
10+
</div>
11+
<script src="../../node_modules/angular/angular.js"></script>
12+
<script src="../../dist/angular_1_router.js"></script>
13+
<script>
14+
angular.module('myApp', ['ngComponentRouter'])
15+
.controller('MyCtrl', ['$router', function ($router) {
16+
console.log($router);
17+
$router.navigate('/')
18+
.then(console.log.bind(console, 'resolve'), console.log.bind(console, 'reject'));
19+
}]);
20+
</script>
21+
</body>
22+
</html>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
var sauceConf = require('../../sauce.conf');
4+
5+
// This runs the tests for the router in Angular 1.x
6+
7+
module.exports = function (config) {
8+
var options = {
9+
frameworks: ['jasmine'],
10+
11+
files: [
12+
'../../node_modules/angular/angular.js',
13+
'../../node_modules/angular-animate/angular-animate.js',
14+
'../../node_modules/angular-mocks/angular-mocks.js',
15+
16+
'../../dist/angular_1_router.js',
17+
18+
'test/*.es5.js',
19+
'test/*_spec.js'
20+
],
21+
22+
customLaunchers: sauceConf.customLaunchers,
23+
24+
browsers: ['ChromeCanary']
25+
};
26+
27+
config.set(options);
28+
};

0 commit comments

Comments
 (0)