Skip to content

Commit dc418cc

Browse files
Just Use Prettier™
1 parent ac4e5d7 commit dc418cc

File tree

2 files changed

+123
-90
lines changed

2 files changed

+123
-90
lines changed

rollup.config.js

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import commonjs from 'rollup-plugin-commonjs';
66
let MINIFY = process.env.MINIFY;
77

88
let pkg = require('./package.json');
9-
let banner =
10-
`/**
9+
let banner = `/**
1110
* ${pkg.description}
1211
* @version v${pkg.version}
1312
* @link ${pkg.homepage}
@@ -16,14 +15,9 @@ let banner =
1615

1716
let uglifyOpts = { output: {} };
1817
// retain multiline comment with @license
19-
uglifyOpts.output.comments = (node, comment) =>
20-
comment.type === 'comment2' && /@license/i.test(comment.value);
18+
uglifyOpts.output.comments = (node, comment) => comment.type === 'comment2' && /@license/i.test(comment.value);
2119

22-
let plugins = [
23-
nodeResolve({ jsnext: true }),
24-
sourcemaps(),
25-
commonjs(),
26-
];
20+
let plugins = [nodeResolve({ jsnext: true }), sourcemaps(), commonjs()];
2721

2822
if (MINIFY) plugins.push(uglify(uglifyOpts));
2923

@@ -38,11 +32,7 @@ function onwarn(warning) {
3832

3933
function isExternal(id) {
4034
// @uirouter/* , @angular/* , and angular (angularjs) should be external
41-
let externals = [
42-
/^@uirouter\/.*/,
43-
/^@angular\/.*/,
44-
/^angular$/,
45-
];
35+
let externals = [/^@uirouter\/.*/, /^@angular\/.*/, /^angular$/];
4636
return externals.map(regex => regex.exec(id)).reduce((acc, val) => acc || !!val, false);
4737
}
4838

@@ -63,14 +53,13 @@ const CONFIG = {
6353
'@angular/core': 'ng.core',
6454
'@angular/common': 'ng.common',
6555
'@angular/upgrade/static': 'ng.upgrade.static',
66-
'angular': 'angular',
56+
angular: 'angular',
6757
},
6858
},
6959

7060
plugins: plugins,
7161
onwarn: onwarn,
7262
external: isExternal,
73-
7463
};
7564

7665
export default CONFIG;

src/angular-hybrid.ts

Lines changed: 118 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,36 @@ import { Component, ElementRef, Inject, Injector, Input, ModuleWithProviders, Ng
44
import { downgradeComponent, UpgradeModule } from '@angular/upgrade/static';
55

66
import {
7-
StateObject, forEach, PathNode, Resolvable, StateRegistry, UIRouter, ViewConfig, ViewService
7+
StateObject,
8+
forEach,
9+
PathNode,
10+
Resolvable,
11+
StateRegistry,
12+
UIRouter,
13+
ViewConfig,
14+
ViewService,
815
} from '@uirouter/core';
916

1017
import {
11-
applyModuleConfig, NATIVE_INJECTOR_TOKEN, ng2LazyLoadBuilder, Ng2ViewConfig, UIView, _UIROUTER_SERVICE_PROVIDERS,
12-
Ng2ViewDeclaration, ParentUIViewInject, StatesModule, UIROUTER_MODULE_TOKEN, UIROUTER_ROOT_MODULE, UIRouterModule,
18+
applyModuleConfig,
19+
NATIVE_INJECTOR_TOKEN,
20+
ng2LazyLoadBuilder,
21+
Ng2ViewConfig,
22+
UIView,
23+
_UIROUTER_SERVICE_PROVIDERS,
24+
Ng2ViewDeclaration,
25+
ParentUIViewInject,
26+
StatesModule,
27+
UIROUTER_MODULE_TOKEN,
28+
UIROUTER_ROOT_MODULE,
29+
UIRouterModule,
1330
makeChildProviders,
1431
} from '@uirouter/angular';
1532

1633
import { $InjectorLike, Ng1ViewConfig } from '@uirouter/angularjs';
1734

1835
import { UIRouterRx } from '@uirouter/rx';
19-
import { NgHybridStatesModule } from './interfaces';
36+
import { NgHybridStatesModule } from './interfaces';
2037

2138
/**
2239
* Create a ng1 module for the ng1 half of the hybrid application to depend on.
@@ -26,7 +43,9 @@ import { NgHybridStatesModule } from './interfaces';
2643
*/
2744
export const upgradeModule = angular.module('ui.router.upgrade', ['ui.router']);
2845

29-
export function objectFactory() { return {}; }
46+
export function objectFactory() {
47+
return {};
48+
}
3049

3150
/**
3251
* UIViewNgUpgrade is a component bridge from ng1 ui-view to ng2 ui-view
@@ -99,41 +118,44 @@ export function objectFactory() { return {}; }
99118
template: `<ui-view [name]="name"></ui-view>`,
100119
// provide a blank object as PARENT_INJECT.
101120
// The component will add property getters when it is constructed.
102-
viewProviders: [ { provide: UIView.PARENT_INJECT, useFactory: objectFactory } ],
121+
viewProviders: [{ provide: UIView.PARENT_INJECT, useFactory: objectFactory }],
103122
})
104123
export class UIViewNgUpgrade {
105124
// The ui-view's name (or '$default')
106125
@Input() name: string;
107126

108127
constructor(
109-
ref: ElementRef,
110-
@Inject(UIView.PARENT_INJECT) parent: ParentUIViewInject,
111-
registry: StateRegistry // access the root state
128+
ref: ElementRef,
129+
@Inject(UIView.PARENT_INJECT) parent: ParentUIViewInject,
130+
registry: StateRegistry, // access the root state
112131
) {
113132
// From the ui-view-ng-upgrade component's element ref, walk up the DOM two elements...
114133
// There will first be an ng1 ui-view which hosts this element, and then that ui-view's parent element.
115134
// That (parent) element has access to the proper "parent viewcontext"
116135

117136
// The ng2 ui-view component is inside this ui-view-ng-upgrade directive, which is inside the ng1 "host" ui-view.
118137
// Both ui-views share the same "view context" information (the view's fqn and created-by-state context information)
119-
const ng1elem = angular.element(ref.nativeElement).parent().parent();
138+
const ng1elem = angular
139+
.element(ref.nativeElement)
140+
.parent()
141+
.parent();
120142

121143
// Expose getters on PARENT_INJECT for context (creation state) and fqn (view address)
122144
// These will be used by further nested UIView
123-
Object.defineProperty(parent, "context", {
145+
Object.defineProperty(parent, 'context', {
124146
get: function() {
125147
const data = ng1elem['inheritedData']('$uiView');
126-
return (data && data.$cfg) ? data.$cfg.viewDecl.$context : registry.root();
148+
return data && data.$cfg ? data.$cfg.viewDecl.$context : registry.root();
127149
},
128-
enumerable: true
150+
enumerable: true,
129151
});
130152

131-
Object.defineProperty(parent, "fqn", {
153+
Object.defineProperty(parent, 'fqn', {
132154
get: function() {
133155
const data = ng1elem['inheritedData']('$uiView');
134-
return (data && data.$uiView) ? data.$uiView.fqn : null;
156+
return data && data.$uiView ? data.$uiView.fqn : null;
135157
},
136-
enumerable: true
158+
enumerable: true,
137159
});
138160
}
139161
}
@@ -165,7 +187,7 @@ export function getParentUIViewInject(r: StateRegistry): ParentUIViewInject {
165187
declarations: [UIViewNgUpgrade],
166188
providers: [
167189
// @uirouter/angular code will use the ng1 $uiRouter instance instead of creating its own.
168-
{ provide: '$uiRouter', useFactory: getUIRouter, deps: ['$injector']},
190+
{ provide: '$uiRouter', useFactory: getUIRouter, deps: ['$injector'] },
169191

170192
{ provide: UIRouter, useFactory: uiRouterUpgradeFactory, deps: ['$uiRouter', Injector] },
171193

@@ -175,11 +197,10 @@ export function getParentUIViewInject(r: StateRegistry): ParentUIViewInject {
175197

176198
..._UIROUTER_SERVICE_PROVIDERS,
177199
],
178-
entryComponents: [
179-
UIViewNgUpgrade
180-
],
181-
exports: [UIViewNgUpgrade, UIRouterModule]
182-
}) export class UIRouterUpgradeModule {
200+
entryComponents: [UIViewNgUpgrade],
201+
exports: [UIViewNgUpgrade, UIRouterModule],
202+
})
203+
export class UIRouterUpgradeModule {
183204
static forChild(module: NgHybridStatesModule = {}): ModuleWithProviders {
184205
return {
185206
ngModule: UIRouterUpgradeModule,
@@ -191,35 +212,41 @@ export function getParentUIViewInject(r: StateRegistry): ParentUIViewInject {
191212
// Downgrade the UIViewNgUpgrade ng2 Component to an ng1 directive.
192213
// The directive is used in a (generated) view template by the (host) ng1 ui-router,
193214
// whenever it finds a view configured with a `component: <Ng2ComponentClass>`
194-
upgradeModule.directive("uiViewNgUpgrade", <any> downgradeComponent({
215+
upgradeModule.directive('uiViewNgUpgrade', <any>downgradeComponent({
195216
component: UIViewNgUpgrade,
196-
inputs: ['name']
217+
inputs: ['name'],
197218
}));
198219

199-
upgradeModule.run(['$injector', (ng1Injector: $InjectorLike) => {
200-
const $uiRouter: UIRouter = ng1Injector.get('$uiRouter');
201-
202-
/** Add support for observable state and param changes */
203-
$uiRouter.plugin(UIRouterRx);
204-
205-
// Expose a merged ng1/ng2 injector as a Resolvable (on the root state).
206-
// This mimics how ui-router-ng2 exposes the root ng2 Injector, but
207-
// it retrieves from ng1 injector first, then ng2 injector if the token isn't found.
208-
const mergedInjector = {
209-
get: function(token: any, ng2NotFoundValue?: any) {
210-
const ng2Injector = ng1Injector.get('$$angularInjector');
211-
return (ng1Injector.has(token) && ng1Injector.get(token)) || ng2Injector.get(token, ng2NotFoundValue)
212-
}
213-
};
220+
upgradeModule.run([
221+
'$injector',
222+
(ng1Injector: $InjectorLike) => {
223+
const $uiRouter: UIRouter = ng1Injector.get('$uiRouter');
224+
225+
/** Add support for observable state and param changes */
226+
$uiRouter.plugin(UIRouterRx);
227+
228+
// Expose a merged ng1/ng2 injector as a Resolvable (on the root state).
229+
// This mimics how ui-router-ng2 exposes the root ng2 Injector, but
230+
// it retrieves from ng1 injector first, then ng2 injector if the token isn't found.
231+
const mergedInjector = {
232+
get: function(token: any, ng2NotFoundValue?: any) {
233+
const ng2Injector = ng1Injector.get('$$angularInjector');
234+
return (ng1Injector.has(token) && ng1Injector.get(token)) || ng2Injector.get(token, ng2NotFoundValue);
235+
},
236+
};
214237

215-
const ng2InjectorResolvable = Resolvable.fromData(NATIVE_INJECTOR_TOKEN, mergedInjector);
216-
$uiRouter.stateRegistry.root().resolvables.push(ng2InjectorResolvable);
217-
}]);
238+
const ng2InjectorResolvable = Resolvable.fromData(NATIVE_INJECTOR_TOKEN, mergedInjector);
239+
$uiRouter.stateRegistry.root().resolvables.push(ng2InjectorResolvable);
240+
},
241+
]);
218242

219243
/** Adds support for `loadChildren`: Angular NgModule lazy loading via @gntools/webpack */
220-
upgradeModule.config(['$stateRegistryProvider', ($stateRegistry: StateRegistry) => {
221-
$stateRegistry.decorator('lazyLoad', ng2LazyLoadBuilder);
222-
}]);
244+
upgradeModule.config([
245+
'$stateRegistryProvider',
246+
($stateRegistry: StateRegistry) => {
247+
$stateRegistry.decorator('lazyLoad', ng2LazyLoadBuilder);
248+
},
249+
]);
223250

224251
/**
225252
* Define a stateProvider `views` builder decorator.
@@ -230,36 +257,53 @@ upgradeModule.config(['$stateRegistryProvider', ($stateRegistry: StateRegistry)
230257
* In place of the template provider, it simply puts a <ui-view-ng-upgrade/> component
231258
* which that provides a ng1 -> ng2 boundary in the component tree.
232259
*/
233-
upgradeModule.config(['$stateRegistryProvider', ($stateRegistry: StateRegistry) => {
234-
$stateRegistry.decorator('views', function(state: StateObject, parentFn: Function) {
235-
const views = parentFn(state);
236-
237-
forEach(views, (viewDecl: any, viewName: string) => {
238-
if (viewDecl.$type === 'ng1-to-ng2' || typeof viewDecl.component === 'function') {
239-
// Update the view config.
240-
// Override default ng1 `component:` behavior (of defining a templateProvider)
241-
// with a <ui-view-ng-upgrade> adapter directive template
242-
viewDecl.$type = "ng1-to-ng2";
243-
viewDecl.templateProvider = null;
244-
viewDecl.template = `<ui-view-ng-upgrade name='${viewDecl.$uiViewName}'></ui-view-ng-upgrade>`;
245-
}
260+
upgradeModule.config([
261+
'$stateRegistryProvider',
262+
($stateRegistry: StateRegistry) => {
263+
$stateRegistry.decorator('views', function(state: StateObject, parentFn: Function) {
264+
const views = parentFn(state);
265+
266+
forEach(views, (viewDecl: any, viewName: string) => {
267+
if (viewDecl.$type === 'ng1-to-ng2' || typeof viewDecl.component === 'function') {
268+
// Update the view config.
269+
// Override default ng1 `component:` behavior (of defining a templateProvider)
270+
// with a <ui-view-ng-upgrade> adapter directive template
271+
viewDecl.$type = 'ng1-to-ng2';
272+
viewDecl.templateProvider = null;
273+
viewDecl.template = `<ui-view-ng-upgrade name='${viewDecl.$uiViewName}'></ui-view-ng-upgrade>`;
274+
}
275+
});
276+
return views;
246277
});
247-
return views;
248-
});
249-
}]);
278+
},
279+
]);
250280

251281
// UI-Router ViewConfig factories take a view declaration object from a state.views: { foo: <ViewDeclaration> }
252282
// and return a runtime config object (a ViewConfig)
253-
upgradeModule.run(['$view', '$templateFactory', ($view: ViewService, $templateFactory: any) => {
254-
// Register a ViewConfig factory for views of type `ng2`
255-
$view._pluginapi._viewConfigFactory('ng2', (path: PathNode[], config: Ng2ViewDeclaration) => new Ng2ViewConfig(path, config));
256-
257-
// Register a ViewConfig factory for views of type `ng1-to-ng2`.
258-
// Returns both an ng1 config and an ng2 config allowing either ng1 or ng2 ui-view components to be targeted.
259-
$view._pluginapi._viewConfigFactory('ng1-to-ng2', (path: PathNode[], config: Ng2ViewDeclaration) => {
260-
const ng1ViewConfig: ViewConfig = <any> new Ng1ViewConfig(<any> path, <any> Object.assign({}, config, { $type: 'ng1'}), $templateFactory);
261-
const ng2ViewConfig: ViewConfig = <any> new Ng2ViewConfig(<any> path, <any> Object.assign({}, config, { $type: 'ng2'}));
262-
263-
return [ ng2ViewConfig, ng1ViewConfig ];
264-
});
265-
}]);
283+
upgradeModule.run([
284+
'$view',
285+
'$templateFactory',
286+
($view: ViewService, $templateFactory: any) => {
287+
// Register a ViewConfig factory for views of type `ng2`
288+
$view._pluginapi._viewConfigFactory(
289+
'ng2',
290+
(path: PathNode[], config: Ng2ViewDeclaration) => new Ng2ViewConfig(path, config),
291+
);
292+
293+
// Register a ViewConfig factory for views of type `ng1-to-ng2`.
294+
// Returns both an ng1 config and an ng2 config allowing either ng1 or ng2 ui-view components to be targeted.
295+
$view._pluginapi._viewConfigFactory('ng1-to-ng2', (path: PathNode[], config: Ng2ViewDeclaration) => {
296+
const ng1ViewConfig: ViewConfig = <any>new Ng1ViewConfig(
297+
<any>path,
298+
<any>Object.assign({}, config, { $type: 'ng1' }),
299+
$templateFactory,
300+
);
301+
const ng2ViewConfig: ViewConfig = <any>new Ng2ViewConfig(
302+
<any>path,
303+
<any>Object.assign({}, config, { $type: 'ng2' }),
304+
);
305+
306+
return [ng2ViewConfig, ng1ViewConfig];
307+
});
308+
},
309+
]);

0 commit comments

Comments
 (0)