2015-08-20 16:19:58 -04:00
|
|
|
'use strict';
|
|
|
|
|
2015-08-20 16:19:34 -04:00
|
|
|
var fs = require('fs');
|
|
|
|
var ts = require('typescript');
|
|
|
|
|
|
|
|
var files = [
|
|
|
|
'lifecycle_annotations_impl.ts',
|
|
|
|
'url_parser.ts',
|
|
|
|
'path_recognizer.ts',
|
|
|
|
'route_config_impl.ts',
|
|
|
|
'async_route_handler.ts',
|
|
|
|
'sync_route_handler.ts',
|
|
|
|
'route_recognizer.ts',
|
|
|
|
'instruction.ts',
|
|
|
|
'route_config_nomalizer.ts',
|
|
|
|
'route_lifecycle_reflector.ts',
|
|
|
|
'route_registry.ts',
|
|
|
|
'router.ts'
|
|
|
|
];
|
|
|
|
|
|
|
|
var PRELUDE = '(function(){\n';
|
|
|
|
var POSTLUDE = '\n}());\n';
|
|
|
|
var FACADES = fs.readFileSync(__dirname + '/lib/facades.es5', 'utf8');
|
|
|
|
var DIRECTIVES = fs.readFileSync(__dirname + '/src/ng_outlet.js', 'utf8');
|
|
|
|
function main() {
|
2015-09-14 18:38:10 -04:00
|
|
|
var ES6_SHIM = fs.readFileSync(__dirname + '/../../node_modules/es6-shim/es6-shim.js', 'utf8');
|
2015-08-20 16:19:34 -04:00
|
|
|
var dir = __dirname + '/../angular2/src/router/';
|
|
|
|
|
|
|
|
var out = '';
|
|
|
|
|
|
|
|
var sharedCode = '';
|
|
|
|
files.forEach(function (file) {
|
|
|
|
var moduleName = 'router/' + file.replace(/\.ts$/, '');
|
|
|
|
|
|
|
|
sharedCode += transform(moduleName, fs.readFileSync(dir + file, 'utf8'));
|
|
|
|
});
|
|
|
|
|
|
|
|
out += "angular.module('ngComponentRouter')";
|
|
|
|
out += angularFactory('$router', ['$q', '$location', '$$controllerIntrospector',
|
|
|
|
'$browser', '$rootScope', '$injector'], [
|
|
|
|
FACADES,
|
|
|
|
"var exports = {Injectable: function () {}};",
|
|
|
|
"var require = function () {return exports;};",
|
|
|
|
sharedCode,
|
|
|
|
"var RouteConfig = exports.RouteConfig;",
|
|
|
|
"angular.annotations = {RouteConfig: RouteConfig, CanActivate: exports.CanActivate};",
|
|
|
|
"angular.stringifyInstruction = exports.stringifyInstruction;",
|
|
|
|
"var RouteRegistry = exports.RouteRegistry;",
|
|
|
|
"var RootRouter = exports.RootRouter;",
|
|
|
|
//TODO: move this code into a templated JS file
|
|
|
|
"var registry = new RouteRegistry();",
|
|
|
|
"var location = new Location();",
|
|
|
|
|
|
|
|
"$$controllerIntrospector(function (name, constructor) {",
|
|
|
|
"if (constructor.$canActivate) {",
|
|
|
|
"constructor.annotations = constructor.annotations || [];",
|
|
|
|
"constructor.annotations.push(new angular.annotations.CanActivate(function (instruction) {",
|
|
|
|
"return $injector.invoke(constructor.$canActivate, constructor, {",
|
|
|
|
"$routeParams: instruction.component ? instruction.component.params : instruction.params",
|
|
|
|
"});",
|
|
|
|
"}));",
|
|
|
|
"}",
|
2015-08-20 16:19:58 -04:00
|
|
|
"if (constructor.$routeConfig) {",
|
|
|
|
"constructor.annotations = constructor.annotations || [];",
|
|
|
|
"constructor.annotations.push(new angular.annotations.RouteConfig(constructor.$routeConfig));",
|
|
|
|
"}",
|
2015-08-20 16:19:34 -04:00
|
|
|
"if (constructor.annotations) {",
|
|
|
|
"constructor.annotations.forEach(function(annotation) {",
|
|
|
|
"if (annotation instanceof RouteConfig) {",
|
|
|
|
"annotation.configs.forEach(function (config) {",
|
|
|
|
"registry.config(constructor, config);",
|
|
|
|
"});",
|
|
|
|
"}",
|
|
|
|
"});",
|
|
|
|
"}",
|
|
|
|
"});",
|
|
|
|
|
2015-08-24 17:39:54 -04:00
|
|
|
"var router = new RootRouter(registry, location, new Object());",
|
2015-08-20 16:19:58 -04:00
|
|
|
"$rootScope.$watch(function () { return $location.path(); }, function (path) {",
|
|
|
|
"if (router.lastNavigationAttempt !== path) {",
|
2015-09-09 00:41:56 -04:00
|
|
|
"router.navigateByUrl(path);",
|
2015-08-20 16:19:58 -04:00
|
|
|
"}",
|
|
|
|
"});",
|
2015-08-20 16:19:34 -04:00
|
|
|
|
|
|
|
"return router;"
|
|
|
|
].join('\n'));
|
|
|
|
|
2015-09-11 19:00:26 -04:00
|
|
|
return PRELUDE + ES6_SHIM + DIRECTIVES + out + POSTLUDE;
|
2015-08-20 16:19:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Given a directory name and a file's TypeScript content, return an object with the ES5 code,
|
2015-09-09 00:38:36 -04:00
|
|
|
* sourcemap, and exported variable identifier name for the content.
|
2015-08-20 16:19:34 -04:00
|
|
|
*/
|
|
|
|
var IMPORT_RE = new RegExp("import \\{?([\\w\\n_, ]+)\\}? from '(.+)';?", 'g');
|
|
|
|
function transform(dir, contents) {
|
|
|
|
contents = contents.replace(IMPORT_RE, function (match, imports, includePath) {
|
|
|
|
//TODO: remove special-case
|
|
|
|
if (isFacadeModule(includePath) || includePath === './router_outlet') {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return match;
|
|
|
|
});
|
|
|
|
return ts.transpile(contents, {
|
|
|
|
target: ts.ScriptTarget.ES5,
|
|
|
|
module: ts.ModuleKind.CommonJS,
|
|
|
|
sourceRoot: dir
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function angularFactory(name, deps, body) {
|
|
|
|
return ".factory('" + name + "', [" +
|
|
|
|
deps.map(function (service) {
|
|
|
|
return "'" + service + "', ";
|
|
|
|
}).join('') +
|
|
|
|
"function (" + deps.join(', ') + ") {\n" + body + "\n}])";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function isFacadeModule(modulePath) {
|
|
|
|
return modulePath.indexOf('facade') > -1 ||
|
2015-08-20 17:28:25 -04:00
|
|
|
modulePath === 'angular2/src/core/reflection/reflection';
|
2015-08-20 16:19:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = function () {
|
|
|
|
var dist = __dirname + '/../../dist';
|
|
|
|
if (!fs.existsSync(dist)) {
|
|
|
|
fs.mkdirSync(dist);
|
|
|
|
}
|
|
|
|
fs.writeFileSync(dist + '/angular_1_router.js', main(files));
|
|
|
|
};
|