refactor: replace local namespaced imports with named (#28642)

At the moment, the API extractor doesn't support local namespaced imports, this will break the generation of flat dts files. When we turn on dts bundling for this package it will break. Hence this is the ground work needed for making this package compatable with the API extractor.

See: https://github.com/Microsoft/web-build-tools/issues/1029

Relates to #28588

PR Close #28642
This commit is contained in:
Alan
2019-02-11 11:12:57 +01:00
committed by Igor Minar
parent 599e2e22bc
commit 7c1b9ff5ec
11 changed files with 114 additions and 115 deletions

View File

@ -9,7 +9,7 @@
import {Compiler, CompilerOptions, Injector, NgModule, NgModuleRef, NgZone, StaticProvider, Testability, Type, resolveForwardRef} from '@angular/core';
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import * as angular from '../common/angular1';
import {IAngularBootstrapConfig, IAugmentedJQuery, IInjectorService, IModule, IProvideService, IRootScopeService, ITestabilityService, bootstrap, element as angularElement, module as angularModule} from '../common/angular1';
import {$$TESTABILITY, $COMPILE, $INJECTOR, $ROOT_SCOPE, COMPILER_KEY, INJECTOR_KEY, LAZY_MODULE_REF, NG_ZONE_KEY, UPGRADE_APP_TYPE_KEY} from '../common/constants';
import {downgradeComponent} from '../common/downgrade_component';
import {downgradeInjectable} from '../common/downgrade_injectable';
@ -117,10 +117,10 @@ export class UpgradeAdapter {
// TODO(issue/24571): remove '!'.
private ngZone !: NgZone;
// TODO(issue/24571): remove '!'.
private ng1Module !: angular.IModule;
private ng1Module !: IModule;
private moduleRef: NgModuleRef<any>|null = null;
// TODO(issue/24571): remove '!'.
private ng2BootstrapDeferred !: Deferred<angular.IInjectorService>;
private ng2BootstrapDeferred !: Deferred<IInjectorService>;
constructor(private ng2AppModule: Type<any>, private compilerOptions?: CompilerOptions) {
if (!ng2AppModule) {
@ -382,7 +382,7 @@ export class UpgradeAdapter {
* });
* ```
*/
bootstrap(element: Element, modules?: any[], config?: angular.IAngularBootstrapConfig):
bootstrap(element: Element, modules?: any[], config?: IAngularBootstrapConfig):
UpgradeAdapterRef {
this.declareNg1Module(modules);
@ -392,7 +392,7 @@ export class UpgradeAdapter {
const windowAngular = (window as any /** TODO #???? */)['angular'];
windowAngular.resumeBootstrap = undefined;
this.ngZone.run(() => { angular.bootstrap(element, [this.ng1Module.name], config !); });
this.ngZone.run(() => { bootstrap(element, [this.ng1Module.name], config !); });
const ng1BootstrapPromise = new Promise((resolve) => {
if (windowAngular.resumeBootstrap) {
const originalResumeBootstrap: () => void = windowAngular.resumeBootstrap;
@ -408,7 +408,7 @@ export class UpgradeAdapter {
});
Promise.all([this.ng2BootstrapDeferred.promise, ng1BootstrapPromise]).then(([ng1Injector]) => {
angular.element(element).data !(controllerKey(INJECTOR_KEY), this.moduleRef !.injector);
angularElement(element).data !(controllerKey(INJECTOR_KEY), this.moduleRef !.injector);
this.moduleRef !.injector.get<NgZone>(NgZone).run(
() => { (<any>upgrade)._bootstrapDone(this.moduleRef, ng1Injector); });
}, onError);
@ -450,7 +450,7 @@ export class UpgradeAdapter {
const token = options && options.asToken || name;
this.upgradedProviders.push({
provide: token,
useFactory: ($injector: angular.IInjectorService) => $injector.get(name),
useFactory: ($injector: IInjectorService) => $injector.get(name),
deps: [$INJECTOR]
});
}
@ -495,13 +495,13 @@ export class UpgradeAdapter {
* upgradeAdapter.declareNg1Module(['heroApp']);
* ```
*/
private declareNg1Module(modules: string[] = []): angular.IModule {
private declareNg1Module(modules: string[] = []): IModule {
const delayApplyExps: Function[] = [];
let original$applyFn: Function;
let rootScopePrototype: any;
let rootScope: angular.IRootScopeService;
let rootScope: IRootScopeService;
const upgradeAdapter = this;
const ng1Module = this.ng1Module = angular.module(this.idPrefix, modules);
const ng1Module = this.ng1Module = angularModule(this.idPrefix, modules);
const platformRef = platformBrowserDynamic();
this.ngZone = new NgZone({enableLongStackTrace: Zone.hasOwnProperty('longStackTraceZoneSpec')});
@ -515,10 +515,10 @@ export class UpgradeAdapter {
.factory(COMPILER_KEY, () => this.moduleRef !.injector.get(Compiler))
.config([
'$provide', '$injector',
(provide: angular.IProvideService, ng1Injector: angular.IInjectorService) => {
(provide: IProvideService, ng1Injector: IInjectorService) => {
provide.decorator($ROOT_SCOPE, [
'$delegate',
function(rootScopeDelegate: angular.IRootScopeService) {
function(rootScopeDelegate: IRootScopeService) {
// Capture the root apply so that we can delay first call to $apply until we
// bootstrap Angular and then we replay and restore the $apply.
rootScopePrototype = rootScopeDelegate.constructor.prototype;
@ -534,7 +534,7 @@ export class UpgradeAdapter {
if (ng1Injector.has($$TESTABILITY)) {
provide.decorator($$TESTABILITY, [
'$delegate',
function(testabilityDelegate: angular.ITestabilityService) {
function(testabilityDelegate: ITestabilityService) {
const originalWhenStable: Function = testabilityDelegate.whenStable;
// Cannot use arrow function below because we need the context
const newWhenStable = function(callback: Function) {
@ -559,7 +559,7 @@ export class UpgradeAdapter {
ng1Module.run([
'$injector', '$rootScope',
(ng1Injector: angular.IInjectorService, rootScope: angular.IRootScopeService) => {
(ng1Injector: IInjectorService, rootScope: IRootScopeService) => {
UpgradeNg1ComponentAdapterBuilder.resolve(this.ng1ComponentsToBeUpgraded, ng1Injector)
.then(() => {
// Note: There is a bug in TS 2.4 that prevents us from
@ -620,7 +620,7 @@ class ParentInjectorPromise {
private injector !: Injector;
private callbacks: ((injector: Injector) => any)[] = [];
constructor(private element: angular.IAugmentedJQuery) {
constructor(private element: IAugmentedJQuery) {
// store the promise on the element
element.data !(controllerKey(INJECTOR_KEY), this);
}
@ -660,13 +660,13 @@ export class UpgradeAdapterRef {
/* @internal */
private _readyFn: ((upgradeAdapterRef?: UpgradeAdapterRef) => void)|null = null;
public ng1RootScope: angular.IRootScopeService = null !;
public ng1Injector: angular.IInjectorService = null !;
public ng1RootScope: IRootScopeService = null !;
public ng1Injector: IInjectorService = null !;
public ng2ModuleRef: NgModuleRef<any> = null !;
public ng2Injector: Injector = null !;
/* @internal */
private _bootstrapDone(ngModuleRef: NgModuleRef<any>, ng1Injector: angular.IInjectorService) {
private _bootstrapDone(ngModuleRef: NgModuleRef<any>, ng1Injector: IInjectorService) {
this.ng2ModuleRef = ngModuleRef;
this.ng2Injector = ngModuleRef.injector;
this.ng1Injector = ng1Injector;

View File

@ -8,7 +8,7 @@
import {Directive, DoCheck, ElementRef, EventEmitter, Inject, Injector, OnChanges, OnDestroy, OnInit, SimpleChange, SimpleChanges, Type} from '@angular/core';
import * as angular from '../common/angular1';
import {IAttributes, IDirective, IDirectivePrePost, IInjectorService, ILinkFn, IScope, ITranscludeFunction} from '../common/angular1';
import {$SCOPE} from '../common/constants';
import {IBindingDestination, IControllerInstance, UpgradeHelper} from '../common/upgrade_helper';
import {isFunction, strictEquals} from '../common/util';
@ -31,7 +31,7 @@ export class UpgradeNg1ComponentAdapterBuilder {
propertyOutputs: string[] = [];
checkProperties: string[] = [];
propertyMap: {[name: string]: string} = {};
directive: angular.IDirective|null = null;
directive: IDirective|null = null;
// TODO(issue/24571): remove '!'.
template !: string;
@ -48,8 +48,7 @@ export class UpgradeNg1ComponentAdapterBuilder {
@Directive({jit: true, ...directive})
class MyClass extends UpgradeNg1ComponentAdapter implements OnInit, OnChanges, DoCheck,
OnDestroy {
constructor(
@Inject($SCOPE) scope: angular.IScope, injector: Injector, elementRef: ElementRef) {
constructor(@Inject($SCOPE) scope: IScope, injector: Injector, elementRef: ElementRef) {
super(
new UpgradeHelper(injector, name, elementRef, self.directive || undefined), scope,
self.template, self.inputs, self.outputs, self.propertyOutputs, self.checkProperties,
@ -121,7 +120,7 @@ export class UpgradeNg1ComponentAdapterBuilder {
*/
static resolve(
exportedComponents: {[name: string]: UpgradeNg1ComponentAdapterBuilder},
$injector: angular.IInjectorService): Promise<string[]> {
$injector: IInjectorService): Promise<string[]> {
const promises = Object.keys(exportedComponents).map(name => {
const exportedComponent = exportedComponents[name];
exportedComponent.directive = UpgradeHelper.getDirective($injector, name);
@ -140,13 +139,13 @@ class UpgradeNg1ComponentAdapter implements OnInit, OnChanges, DoCheck {
private controllerInstance: IControllerInstance|null = null;
destinationObj: IBindingDestination|null = null;
checkLastValues: any[] = [];
directive: angular.IDirective;
directive: IDirective;
element: Element;
$element: any = null;
componentScope: angular.IScope;
componentScope: IScope;
constructor(
private helper: UpgradeHelper, scope: angular.IScope, private template: string,
private helper: UpgradeHelper, scope: IScope, private template: string,
private inputs: string[], private outputs: string[], private propOuts: string[],
private checkProperties: string[], private propertyMap: {[key: string]: string}) {
this.directive = helper.directive;
@ -180,7 +179,7 @@ class UpgradeNg1ComponentAdapter implements OnInit, OnChanges, DoCheck {
ngOnInit() {
// Collect contents, insert and compile template
const attachChildNodes: angular.ILinkFn|undefined = this.helper.prepareTransclusion();
const attachChildNodes: ILinkFn|undefined = this.helper.prepareTransclusion();
const linkFn = this.helper.compileTemplate(this.template);
// Instantiate controller (if not already done so)
@ -201,10 +200,10 @@ class UpgradeNg1ComponentAdapter implements OnInit, OnChanges, DoCheck {
// Linking
const link = this.directive.link;
const preLink = (typeof link == 'object') && (link as angular.IDirectivePrePost).pre;
const postLink = (typeof link == 'object') ? (link as angular.IDirectivePrePost).post : link;
const attrs: angular.IAttributes = NOT_SUPPORTED;
const transcludeFn: angular.ITranscludeFunction = NOT_SUPPORTED;
const preLink = (typeof link == 'object') && (link as IDirectivePrePost).pre;
const postLink = (typeof link == 'object') ? (link as IDirectivePrePost).post : link;
const attrs: IAttributes = NOT_SUPPORTED;
const transcludeFn: ITranscludeFunction = NOT_SUPPORTED;
if (preLink) {
preLink(this.componentScope, this.$element, attrs, requiredControllers, transcludeFn);
}