MCPcopy Create free account
hub / github.com/angular/angular / compileComponent

Function compileComponent

packages/core/src/render3/jit/directive.ts:70–232  ·  view source on GitHub ↗
(type: Type<any>, metadata: Component)

Source from the content-addressed store, hash-verified

68 * until the global queue has been resolved with a call to `resolveComponentResources`.
69 */
70export function compileComponent(type: Type<any>, metadata: Component): void {
71 // Initialize ngDevMode. This must be the first statement in compileComponent.
72 // See the `initNgDevMode` docstring for more information.
73 (typeof ngDevMode === 'undefined' || ngDevMode) && initNgDevMode();
74
75 let ngComponentDef: ComponentDef<unknown> | null = null;
76
77 // Metadata may have resources which need to be resolved.
78 maybeQueueResolutionOfComponentResources(type, metadata);
79
80 // Note that we're using the same function as `Directive`, because that's only subset of metadata
81 // that we need to create the ngFactoryDef. We're avoiding using the component metadata
82 // because we'd have to resolve the asynchronous templates.
83 addDirectiveFactoryDef(type, metadata);
84
85 Object.defineProperty(type, NG_COMP_DEF, {
86 get: () => {
87 if (ngComponentDef === null) {
88 const compiler = getCompilerFacade({
89 usage: JitCompilerUsage.Decorator,
90 kind: 'component',
91 type: type,
92 });
93
94 if (metadata.foreignImports !== undefined) {
95 throw new Error(
96 `Foreign components are not supported in JIT mode. ` +
97 `Component '${type.name}' cannot specify 'foreignImports'.`,
98 );
99 }
100
101 if (componentNeedsResolution(metadata)) {
102 const error = [`Component '${type.name}' is not resolved:`];
103 if (metadata.templateUrl) {
104 error.push(` - templateUrl: ${metadata.templateUrl}`);
105 }
106 if (metadata.styleUrls && metadata.styleUrls.length) {
107 error.push(` - styleUrls: ${JSON.stringify(metadata.styleUrls)}`);
108 }
109 if (metadata.styleUrl) {
110 error.push(` - styleUrl: ${metadata.styleUrl}`);
111 }
112 error.push(`Did you run and wait for 'resolveComponentResources()'?`);
113 throw new Error(error.join('\n'));
114 }
115
116 // This const was called `jitOptions` previously but had to be renamed to `options` because
117 // of a bug with Terser that caused optimized JIT builds to throw a `ReferenceError`.
118 // This bug was investigated in https://github.com/angular/angular-cli/issues/17264.
119 // We should not rename it back until https://github.com/terser/terser/issues/615 is fixed.
120 const options = getJitOptions();
121 let preserveWhitespaces = metadata.preserveWhitespaces;
122 if (preserveWhitespaces === undefined) {
123 if (options !== null && options.preserveWhitespaces !== undefined) {
124 preserveWhitespaces = options.preserveWhitespaces;
125 } else {
126 preserveWhitespaces = false;
127 }

Callers 4

replaceMetadataFunction · 0.90
directives.tsFile · 0.90
compileTypesSyncMethod · 0.85

Calls 15

initNgDevModeFunction · 0.90
getCompilerFacadeFunction · 0.90
componentNeedsResolutionFunction · 0.90
getJitOptionsFunction · 0.90
flattenFunction · 0.90
transitiveScopesForFunction · 0.90
stringifyForErrorFunction · 0.90
addDirectiveFactoryDefFunction · 0.85
directiveMetadataFunction · 0.85

Tested by

no test coverage detected