|
| 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 | +}; |
0 commit comments