Пример #1
0
 ComponentDecoratorHandler.prototype.analyze = function (node, decorator) {
     var meta = this._resolveLiteral(decorator);
     this.literalCache.delete(decorator);
     // @Component inherits @Directive, so begin by extracting the @Directive metadata and building
     // on it.
     var directiveResult = directive_1.extractDirectiveMetadata(node, decorator, this.checker, this.reflector, this.isCore);
     if (directiveResult === undefined) {
         // `extractDirectiveMetadata` returns undefined when the @Directive has `jit: true`. In this
         // case, compilation of the decorator is skipped. Returning an empty object signifies
         // that no analysis was produced.
         return {};
     }
     // Next, read the `@Component`-specific fields.
     var decoratedElements = directiveResult.decoratedElements, component = directiveResult.decorator, metadata = directiveResult.metadata;
     var templateStr = null;
     if (component.has('templateUrl')) {
         var templateUrlExpr = component.get('templateUrl');
         var templateUrl = metadata_1.staticallyResolve(templateUrlExpr, this.reflector, this.checker);
         if (typeof templateUrl !== 'string') {
             throw new diagnostics_1.FatalDiagnosticError(diagnostics_1.ErrorCode.VALUE_HAS_WRONG_TYPE, templateUrlExpr, 'templateUrl must be a string');
         }
         var url = path.posix.resolve(path.dirname(node.getSourceFile().fileName), templateUrl);
         templateStr = this.resourceLoader.load(url);
     }
     else if (component.has('template')) {
         var templateExpr = component.get('template');
         var resolvedTemplate = metadata_1.staticallyResolve(templateExpr, this.reflector, this.checker);
         if (typeof resolvedTemplate !== 'string') {
             throw new diagnostics_1.FatalDiagnosticError(diagnostics_1.ErrorCode.VALUE_HAS_WRONG_TYPE, templateExpr, 'template must be a string');
         }
         templateStr = resolvedTemplate;
     }
     else {
         throw new diagnostics_1.FatalDiagnosticError(diagnostics_1.ErrorCode.COMPONENT_MISSING_TEMPLATE, decorator.node, 'component is missing a template');
     }
     var preserveWhitespaces = false;
     if (component.has('preserveWhitespaces')) {
         var expr = component.get('preserveWhitespaces');
         var value = metadata_1.staticallyResolve(expr, this.reflector, this.checker);
         if (typeof value !== 'boolean') {
             throw new diagnostics_1.FatalDiagnosticError(diagnostics_1.ErrorCode.VALUE_HAS_WRONG_TYPE, expr, 'preserveWhitespaces must be a boolean');
         }
         preserveWhitespaces = value;
     }
     // Go through the root directories for this project, and select the one with the smallest
     // relative path representation.
     var filePath = node.getSourceFile().fileName;
     var relativeFilePath = this.rootDirs.reduce(function (previous, rootDir) {
         var candidate = path.posix.relative(rootDir, filePath);
         if (previous === undefined || candidate.length < previous.length) {
             return candidate;
         }
         else {
             return previous;
         }
     }, undefined);
     var template = compiler_1.parseTemplate(templateStr, node.getSourceFile().fileName + "#" + node.name.text + "/template.html", { preserveWhitespaces: preserveWhitespaces }, relativeFilePath);
     if (template.errors !== undefined) {
         throw new Error("Errors parsing template: " + template.errors.map(function (e) { return e.toString(); }).join(', '));
     }
     // If the component has a selector, it should be registered with the `SelectorScopeRegistry` so
     // when this component appears in an `@NgModule` scope, its selector can be determined.
     if (metadata.selector !== null) {
         var ref = new metadata_1.ResolvedReference(node, node.name);
         this.scopeRegistry.registerDirective(node, tslib_1.__assign({ ref: ref, name: node.name.text, directive: ref, selector: metadata.selector, exportAs: metadata.exportAs, inputs: metadata.inputs, outputs: metadata.outputs, queries: metadata.queries.map(function (query) { return query.propertyName; }), isComponent: true }, util_1.extractDirectiveGuards(node, this.reflector)));
     }
     // Construct the list of view queries.
     var coreModule = this.isCore ? undefined : '@angular/core';
     var viewChildFromFields = directive_1.queriesFromFields(metadata_1.filterToMembersWithDecorator(decoratedElements, 'ViewChild', coreModule), this.reflector, this.checker);
     var viewChildrenFromFields = directive_1.queriesFromFields(metadata_1.filterToMembersWithDecorator(decoratedElements, 'ViewChildren', coreModule), this.reflector, this.checker);
     var viewQueries = tslib_1.__spread(viewChildFromFields, viewChildrenFromFields);
     if (component.has('queries')) {
         var queriesFromDecorator = directive_1.extractQueriesFromDecorator(component.get('queries'), this.reflector, this.checker, this.isCore);
         viewQueries.push.apply(viewQueries, tslib_1.__spread(queriesFromDecorator.view));
     }
     var styles = null;
     if (component.has('styles')) {
         styles = directive_1.parseFieldArrayValue(component, 'styles', this.reflector, this.checker);
     }
     var encapsulation = 0;
     if (component.has('encapsulation')) {
         encapsulation = parseInt(metadata_1.staticallyResolve(component.get('encapsulation'), this.reflector, this.checker));
     }
     var animations = null;
     if (component.has('animations')) {
         animations = new compiler_1.WrappedNodeExpr(component.get('animations'));
     }
     return {
         analysis: {
             meta: tslib_1.__assign({}, metadata, { template: template,
                 viewQueries: viewQueries,
                 encapsulation: encapsulation, styles: styles || [], 
                 // These will be replaced during the compilation step, after all `NgModule`s have been
                 // analyzed and the full compilation scope for the component can be realized.
                 pipes: EMPTY_MAP, directives: EMPTY_MAP, wrapDirectivesInClosure: false, //
                 animations: animations, viewProviders: null }),
             parsedTemplate: template.nodes,
         },
         typeCheck: true,
     };
 };
Пример #2
0
 ComponentDecoratorHandler.prototype.analyze = function (node, decorator) {
     var meta = this._resolveLiteral(decorator);
     this.literalCache.delete(decorator);
     // @Component inherits @Directive, so begin by extracting the @Directive metadata and building
     // on it.
     var directiveResult = directive_1.extractDirectiveMetadata(node, decorator, this.checker, this.reflector, this.isCore);
     if (directiveResult === undefined) {
         // `extractDirectiveMetadata` returns undefined when the @Directive has `jit: true`. In this
         // case, compilation of the decorator is skipped. Returning an empty object signifies
         // that no analysis was produced.
         return {};
     }
     // Next, read the `@Component`-specific fields.
     var decoratedElements = directiveResult.decoratedElements, component = directiveResult.decorator, metadata = directiveResult.metadata;
     var templateStr = null;
     if (component.has('templateUrl')) {
         var templateUrl = metadata_1.staticallyResolve(component.get('templateUrl'), this.reflector, this.checker);
         if (typeof templateUrl !== 'string') {
             throw new Error("templateUrl should be a string");
         }
         var url = path.posix.resolve(path.dirname(node.getSourceFile().fileName), templateUrl);
         templateStr = this.resourceLoader.load(url);
     }
     else if (component.has('template')) {
         var templateExpr = component.get('template');
         var resolvedTemplate = metadata_1.staticallyResolve(templateExpr, this.reflector, this.checker);
         if (typeof resolvedTemplate !== 'string') {
             throw new Error("Template must statically resolve to a string: " + node.name.text);
         }
         templateStr = resolvedTemplate;
     }
     else {
         throw new Error("Component has no template or templateUrl");
     }
     var preserveWhitespaces = false;
     if (component.has('preserveWhitespaces')) {
         var value = metadata_1.staticallyResolve(component.get('preserveWhitespaces'), this.reflector, this.checker);
         if (typeof value !== 'boolean') {
             throw new Error("preserveWhitespaces must resolve to a boolean if present");
         }
         preserveWhitespaces = value;
     }
     var template = compiler_1.parseTemplate(templateStr, node.getSourceFile().fileName + "#" + node.name.text + "/template.html", { preserveWhitespaces: preserveWhitespaces });
     if (template.errors !== undefined) {
         throw new Error("Errors parsing template: " + template.errors.map(function (e) { return e.toString(); }).join(', '));
     }
     // If the component has a selector, it should be registered with the `SelectorScopeRegistry` so
     // when this component appears in an `@NgModule` scope, its selector can be determined.
     if (metadata.selector !== null) {
         this.scopeRegistry.registerSelector(node, metadata.selector);
     }
     // Construct the list of view queries.
     var coreModule = this.isCore ? undefined : '@angular/core';
     var viewChildFromFields = directive_1.queriesFromFields(metadata_1.filterToMembersWithDecorator(decoratedElements, 'ViewChild', coreModule), this.reflector, this.checker);
     var viewChildrenFromFields = directive_1.queriesFromFields(metadata_1.filterToMembersWithDecorator(decoratedElements, 'ViewChildren', coreModule), this.reflector, this.checker);
     var viewQueries = tslib_1.__spread(viewChildFromFields, viewChildrenFromFields);
     if (component.has('queries')) {
         var queriesFromDecorator = directive_1.extractQueriesFromDecorator(component.get('queries'), this.reflector, this.checker, this.isCore);
         viewQueries.push.apply(viewQueries, tslib_1.__spread(queriesFromDecorator.view));
     }
     return {
         analysis: tslib_1.__assign({}, metadata, { template: template,
             viewQueries: viewQueries, 
             // These will be replaced during the compilation step, after all `NgModule`s have been
             // analyzed and the full compilation scope for the component can be realized.
             pipes: EMPTY_MAP, directives: EMPTY_MAP })
     };
 };