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',
|
refactor(router): improve recognition and generation pipeline
This is a big change. @matsko also deserves much of the credit for the implementation.
Previously, `ComponentInstruction`s held all the state for async components.
Now, we introduce several subclasses for `Instruction` to describe each type of navigation.
BREAKING CHANGE:
Redirects now use the Link DSL syntax. Before:
```
@RouteConfig([
{ path: '/foo', redirectTo: '/bar' },
{ path: '/bar', component: BarCmp }
])
```
After:
```
@RouteConfig([
{ path: '/foo', redirectTo: ['Bar'] },
{ path: '/bar', component: BarCmp, name: 'Bar' }
])
```
BREAKING CHANGE:
This also introduces `useAsDefault` in the RouteConfig, which makes cases like lazy-loading
and encapsulating large routes with sub-routes easier.
Previously, you could use `redirectTo` like this to expand a URL like `/tab` to `/tab/posts`:
@RouteConfig([
{ path: '/tab', redirectTo: '/tab/users' }
{ path: '/tab', component: TabsCmp, name: 'Tab' }
])
AppCmp { ... }
Now the recommended way to handle this is case is to use `useAsDefault` like so:
```
@RouteConfig([
{ path: '/tab', component: TabsCmp, name: 'Tab' }
])
AppCmp { ... }
@RouteConfig([
{ path: '/posts', component: PostsCmp, useAsDefault: true, name: 'Posts' },
{ path: '/users', component: UsersCmp, name: 'Users' }
])
TabsCmp { ... }
```
In the above example, you can write just `['/Tab']` and the route `Users` is automatically selected as a child route.
Closes #4728
Closes #4228
Closes #4170
Closes #4490
Closes #4694
Closes #5200
Closes #5475
2015-11-23 21:07:37 -05:00
|
|
|
'route_recognizer.ts',
|
2015-08-20 16:19:34 -04:00
|
|
|
'route_config_impl.ts',
|
|
|
|
'async_route_handler.ts',
|
|
|
|
'sync_route_handler.ts',
|
refactor(router): improve recognition and generation pipeline
This is a big change. @matsko also deserves much of the credit for the implementation.
Previously, `ComponentInstruction`s held all the state for async components.
Now, we introduce several subclasses for `Instruction` to describe each type of navigation.
BREAKING CHANGE:
Redirects now use the Link DSL syntax. Before:
```
@RouteConfig([
{ path: '/foo', redirectTo: '/bar' },
{ path: '/bar', component: BarCmp }
])
```
After:
```
@RouteConfig([
{ path: '/foo', redirectTo: ['Bar'] },
{ path: '/bar', component: BarCmp, name: 'Bar' }
])
```
BREAKING CHANGE:
This also introduces `useAsDefault` in the RouteConfig, which makes cases like lazy-loading
and encapsulating large routes with sub-routes easier.
Previously, you could use `redirectTo` like this to expand a URL like `/tab` to `/tab/posts`:
@RouteConfig([
{ path: '/tab', redirectTo: '/tab/users' }
{ path: '/tab', component: TabsCmp, name: 'Tab' }
])
AppCmp { ... }
Now the recommended way to handle this is case is to use `useAsDefault` like so:
```
@RouteConfig([
{ path: '/tab', component: TabsCmp, name: 'Tab' }
])
AppCmp { ... }
@RouteConfig([
{ path: '/posts', component: PostsCmp, useAsDefault: true, name: 'Posts' },
{ path: '/users', component: UsersCmp, name: 'Users' }
])
TabsCmp { ... }
```
In the above example, you can write just `['/Tab']` and the route `Users` is automatically selected as a child route.
Closes #4728
Closes #4228
Closes #4170
Closes #4490
Closes #4694
Closes #5200
Closes #5475
2015-11-23 21:07:37 -05:00
|
|
|
'component_recognizer.ts',
|
2015-08-20 16:19:34 -04:00
|
|
|
'instruction.ts',
|
refactor(router): improve recognition and generation pipeline
This is a big change. @matsko also deserves much of the credit for the implementation.
Previously, `ComponentInstruction`s held all the state for async components.
Now, we introduce several subclasses for `Instruction` to describe each type of navigation.
BREAKING CHANGE:
Redirects now use the Link DSL syntax. Before:
```
@RouteConfig([
{ path: '/foo', redirectTo: '/bar' },
{ path: '/bar', component: BarCmp }
])
```
After:
```
@RouteConfig([
{ path: '/foo', redirectTo: ['Bar'] },
{ path: '/bar', component: BarCmp, name: 'Bar' }
])
```
BREAKING CHANGE:
This also introduces `useAsDefault` in the RouteConfig, which makes cases like lazy-loading
and encapsulating large routes with sub-routes easier.
Previously, you could use `redirectTo` like this to expand a URL like `/tab` to `/tab/posts`:
@RouteConfig([
{ path: '/tab', redirectTo: '/tab/users' }
{ path: '/tab', component: TabsCmp, name: 'Tab' }
])
AppCmp { ... }
Now the recommended way to handle this is case is to use `useAsDefault` like so:
```
@RouteConfig([
{ path: '/tab', component: TabsCmp, name: 'Tab' }
])
AppCmp { ... }
@RouteConfig([
{ path: '/posts', component: PostsCmp, useAsDefault: true, name: 'Posts' },
{ path: '/users', component: UsersCmp, name: 'Users' }
])
TabsCmp { ... }
```
In the above example, you can write just `['/Tab']` and the route `Users` is automatically selected as a child route.
Closes #4728
Closes #4228
Closes #4170
Closes #4490
Closes #4694
Closes #5200
Closes #5475
2015-11-23 21:07:37 -05:00
|
|
|
'path_recognizer.ts',
|
2015-08-20 16:19:34 -04:00
|
|
|
'route_config_nomalizer.ts',
|
|
|
|
'route_lifecycle_reflector.ts',
|
|
|
|
'route_registry.ts',
|
|
|
|
'router.ts'
|
|
|
|
];
|
|
|
|
|
|
|
|
var PRELUDE = '(function(){\n';
|
|
|
|
var POSTLUDE = '\n}());\n';
|
refactor(angular_1_router): use directives for route targets
BREAKING CHANGE:
Previously, route configuration took a controller constructor function as the value of
`component` in a route definition:
```
$route.config([
{ route: '/', component: MyController }
])
```
Based on the name of the controller, we used to use a componentMapper service to
determine what template to pair with each controller, how to bind the instance to
the $scope.
To make the 1.x router more semantically alligned with Angular 2, we now route to a directive.
Thus a route configuration takes a normalized directive name:
```
$route.config([
{ route: '/', component: 'myDirective' }
])
```
BREAKING CHANGE:
In order to avoid name collisions, lifecycle hooks are now prefixed with `$`. Before:
```
MyController.prototype.onActivate = ...
```
After:
```
MyController.prototype.$onActivate = ...
```
Same for `$canActivate` (which now lives on the directive factory function),
`$canDeactivate`, `$canReuse`, and `$onDeactivate` hooks.
2015-09-18 18:53:50 -04:00
|
|
|
|
2016-01-29 16:28:59 -05:00
|
|
|
function main(modulesDirectory) {
|
|
|
|
|
|
|
|
var angular1RouterModuleDirectory = modulesDirectory + '/angular1_router';
|
|
|
|
|
|
|
|
var facades = fs.readFileSync(angular1RouterModuleDirectory + '/lib/facades.es5', 'utf8');
|
|
|
|
var directives = fs.readFileSync(angular1RouterModuleDirectory + '/src/ng_outlet.ts', 'utf8');
|
|
|
|
var moduleTemplate = fs.readFileSync(angular1RouterModuleDirectory + '/src/module_template.js', 'utf8');
|
|
|
|
|
|
|
|
var dir = modulesDirectory + '/angular2/src/router/';
|
2015-09-27 05:46:09 -04:00
|
|
|
var sharedCode = files.reduce(function (prev, file) {
|
|
|
|
return prev + transform(fs.readFileSync(dir + file, 'utf8'));
|
|
|
|
}, '');
|
2015-08-20 16:19:34 -04:00
|
|
|
|
2016-01-29 16:28:59 -05:00
|
|
|
var out = moduleTemplate.replace('//{{FACADES}}', facades).replace('//{{SHARED_CODE}}', sharedCode);
|
|
|
|
return PRELUDE + transform(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');
|
refactor(router): improve recognition and generation pipeline
This is a big change. @matsko also deserves much of the credit for the implementation.
Previously, `ComponentInstruction`s held all the state for async components.
Now, we introduce several subclasses for `Instruction` to describe each type of navigation.
BREAKING CHANGE:
Redirects now use the Link DSL syntax. Before:
```
@RouteConfig([
{ path: '/foo', redirectTo: '/bar' },
{ path: '/bar', component: BarCmp }
])
```
After:
```
@RouteConfig([
{ path: '/foo', redirectTo: ['Bar'] },
{ path: '/bar', component: BarCmp, name: 'Bar' }
])
```
BREAKING CHANGE:
This also introduces `useAsDefault` in the RouteConfig, which makes cases like lazy-loading
and encapsulating large routes with sub-routes easier.
Previously, you could use `redirectTo` like this to expand a URL like `/tab` to `/tab/posts`:
@RouteConfig([
{ path: '/tab', redirectTo: '/tab/users' }
{ path: '/tab', component: TabsCmp, name: 'Tab' }
])
AppCmp { ... }
Now the recommended way to handle this is case is to use `useAsDefault` like so:
```
@RouteConfig([
{ path: '/tab', component: TabsCmp, name: 'Tab' }
])
AppCmp { ... }
@RouteConfig([
{ path: '/posts', component: PostsCmp, useAsDefault: true, name: 'Posts' },
{ path: '/users', component: UsersCmp, name: 'Users' }
])
TabsCmp { ... }
```
In the above example, you can write just `['/Tab']` and the route `Users` is automatically selected as a child route.
Closes #4728
Closes #4228
Closes #4170
Closes #4490
Closes #4694
Closes #5200
Closes #5475
2015-11-23 21:07:37 -05:00
|
|
|
var INJECT_RE = new RegExp("@Inject\\(ROUTER_PRIMARY_COMPONENT\\)", 'g');
|
|
|
|
var IMJECTABLE_RE = new RegExp("@Injectable\\(\\)", 'g');
|
2015-09-27 05:46:09 -04:00
|
|
|
function transform(contents) {
|
refactor(router): improve recognition and generation pipeline
This is a big change. @matsko also deserves much of the credit for the implementation.
Previously, `ComponentInstruction`s held all the state for async components.
Now, we introduce several subclasses for `Instruction` to describe each type of navigation.
BREAKING CHANGE:
Redirects now use the Link DSL syntax. Before:
```
@RouteConfig([
{ path: '/foo', redirectTo: '/bar' },
{ path: '/bar', component: BarCmp }
])
```
After:
```
@RouteConfig([
{ path: '/foo', redirectTo: ['Bar'] },
{ path: '/bar', component: BarCmp, name: 'Bar' }
])
```
BREAKING CHANGE:
This also introduces `useAsDefault` in the RouteConfig, which makes cases like lazy-loading
and encapsulating large routes with sub-routes easier.
Previously, you could use `redirectTo` like this to expand a URL like `/tab` to `/tab/posts`:
@RouteConfig([
{ path: '/tab', redirectTo: '/tab/users' }
{ path: '/tab', component: TabsCmp, name: 'Tab' }
])
AppCmp { ... }
Now the recommended way to handle this is case is to use `useAsDefault` like so:
```
@RouteConfig([
{ path: '/tab', component: TabsCmp, name: 'Tab' }
])
AppCmp { ... }
@RouteConfig([
{ path: '/posts', component: PostsCmp, useAsDefault: true, name: 'Posts' },
{ path: '/users', component: UsersCmp, name: 'Users' }
])
TabsCmp { ... }
```
In the above example, you can write just `['/Tab']` and the route `Users` is automatically selected as a child route.
Closes #4728
Closes #4228
Closes #4170
Closes #4490
Closes #4694
Closes #5200
Closes #5475
2015-11-23 21:07:37 -05:00
|
|
|
contents = contents.replace(INJECT_RE, '').replace(IMJECTABLE_RE, '');
|
2015-08-20 16:19:34 -04:00
|
|
|
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,
|
2015-09-27 05:46:09 -04:00
|
|
|
module: ts.ModuleKind.CommonJS
|
2015-08-20 16:19:34 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-01-29 16:28:59 -05:00
|
|
|
module.exports = function (modulesDirectory, outputDirectory) {
|
|
|
|
if (!fs.existsSync(outputDirectory)) {
|
|
|
|
fs.mkdirSync(outputDirectory);
|
2015-08-20 16:19:34 -04:00
|
|
|
}
|
2016-01-29 16:28:59 -05:00
|
|
|
fs.writeFileSync(outputDirectory + '/angular_1_router.js', main(modulesDirectory));
|
2015-08-20 16:19:34 -04:00
|
|
|
};
|