1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | import { CompilerConfig, ResourceLoader } from '@angular/compiler';
|
8 | import * as i0 from '@angular/core';
|
9 | import { InjectionToken, PACKAGE_ROOT_URL, Compiler, ViewEncapsulation, MissingTranslationStrategy, Injector, isDevMode, createPlatformFactory, platformCore, COMPILER_OPTIONS, CompilerFactory, Injectable, PLATFORM_ID, ɵglobal, Version } from '@angular/core';
|
10 | import { ɵPLATFORM_BROWSER_ID } from '@angular/common';
|
11 | import { ɵINTERNAL_BROWSER_PLATFORM_PROVIDERS } from '@angular/platform-browser';
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | const ERROR_COLLECTOR_TOKEN = new InjectionToken('ErrorCollector');
|
21 |
|
22 |
|
23 |
|
24 | const DEFAULT_PACKAGE_URL_PROVIDER = {
|
25 | provide: PACKAGE_ROOT_URL,
|
26 | useValue: '/'
|
27 | };
|
28 | const COMPILER_PROVIDERS = [{ provide: Compiler, useFactory: () => new Compiler() }];
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | class JitCompilerFactory {
|
38 |
|
39 | constructor(defaultOptions) {
|
40 | const compilerOptions = {
|
41 | useJit: true,
|
42 | defaultEncapsulation: ViewEncapsulation.Emulated,
|
43 | missingTranslation: MissingTranslationStrategy.Warning,
|
44 | };
|
45 | this._defaultOptions = [compilerOptions, ...defaultOptions];
|
46 | }
|
47 | createCompiler(options = []) {
|
48 | const opts = _mergeOptions(this._defaultOptions.concat(options));
|
49 | const injector = Injector.create([
|
50 | COMPILER_PROVIDERS, {
|
51 | provide: CompilerConfig,
|
52 | useFactory: () => {
|
53 | return new CompilerConfig({
|
54 |
|
55 |
|
56 | useJit: opts.useJit,
|
57 | jitDevMode: isDevMode(),
|
58 |
|
59 |
|
60 | defaultEncapsulation: opts.defaultEncapsulation,
|
61 | missingTranslation: opts.missingTranslation,
|
62 | preserveWhitespaces: opts.preserveWhitespaces,
|
63 | });
|
64 | },
|
65 | deps: []
|
66 | },
|
67 | opts.providers
|
68 | ]);
|
69 | return injector.get(Compiler);
|
70 | }
|
71 | }
|
72 | function _mergeOptions(optionsArr) {
|
73 | return {
|
74 | useJit: _lastDefined(optionsArr.map(options => options.useJit)),
|
75 | defaultEncapsulation: _lastDefined(optionsArr.map(options => options.defaultEncapsulation)),
|
76 | providers: _mergeArrays(optionsArr.map(options => options.providers)),
|
77 | missingTranslation: _lastDefined(optionsArr.map(options => options.missingTranslation)),
|
78 | preserveWhitespaces: _lastDefined(optionsArr.map(options => options.preserveWhitespaces)),
|
79 | };
|
80 | }
|
81 | function _lastDefined(args) {
|
82 | for (let i = args.length - 1; i >= 0; i--) {
|
83 | if (args[i] !== undefined) {
|
84 | return args[i];
|
85 | }
|
86 | }
|
87 | return undefined;
|
88 | }
|
89 | function _mergeArrays(parts) {
|
90 | const result = [];
|
91 | parts.forEach((part) => part && result.push(...part));
|
92 | return result;
|
93 | }
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 | const platformCoreDynamic = createPlatformFactory(platformCore, 'coreDynamic', [
|
108 | { provide: COMPILER_OPTIONS, useValue: {}, multi: true },
|
109 | { provide: CompilerFactory, useClass: JitCompilerFactory, deps: [COMPILER_OPTIONS] },
|
110 | ]);
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 | class ResourceLoaderImpl extends ResourceLoader {
|
120 | get(url) {
|
121 | let resolve;
|
122 | let reject;
|
123 | const promise = new Promise((res, rej) => {
|
124 | resolve = res;
|
125 | reject = rej;
|
126 | });
|
127 | const xhr = new XMLHttpRequest();
|
128 | xhr.open('GET', url, true);
|
129 | xhr.responseType = 'text';
|
130 | xhr.onload = function () {
|
131 |
|
132 |
|
133 |
|
134 | const response = xhr.response || xhr.responseText;
|
135 |
|
136 | let status = xhr.status === 1223 ? 204 : xhr.status;
|
137 |
|
138 |
|
139 |
|
140 | if (status === 0) {
|
141 | status = response ? 200 : 0;
|
142 | }
|
143 | if (200 <= status && status <= 300) {
|
144 | resolve(response);
|
145 | }
|
146 | else {
|
147 | reject(`Failed to load ${url}`);
|
148 | }
|
149 | };
|
150 | xhr.onerror = function () {
|
151 | reject(`Failed to load ${url}`);
|
152 | };
|
153 | xhr.send();
|
154 | return promise;
|
155 | }
|
156 | }
|
157 | ResourceLoaderImpl.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.2.0", ngImport: i0, type: ResourceLoaderImpl, deps: null, target: i0.ɵɵFactoryTarget.Injectable });
|
158 | ResourceLoaderImpl.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "13.2.0", ngImport: i0, type: ResourceLoaderImpl });
|
159 | i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.2.0", ngImport: i0, type: ResourceLoaderImpl, decorators: [{
|
160 | type: Injectable
|
161 | }] });
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 | const INTERNAL_BROWSER_DYNAMIC_PLATFORM_PROVIDERS = [
|
174 | ɵINTERNAL_BROWSER_PLATFORM_PROVIDERS,
|
175 | {
|
176 | provide: COMPILER_OPTIONS,
|
177 | useValue: { providers: [{ provide: ResourceLoader, useClass: ResourceLoaderImpl, deps: [] }] },
|
178 | multi: true
|
179 | },
|
180 | { provide: PLATFORM_ID, useValue: ɵPLATFORM_BROWSER_ID },
|
181 | ];
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 | class CachedResourceLoader extends ResourceLoader {
|
203 | constructor() {
|
204 | super();
|
205 | this._cache = ɵglobal.$templateCache;
|
206 | if (this._cache == null) {
|
207 | throw new Error('CachedResourceLoader: Template cache was not found in $templateCache.');
|
208 | }
|
209 | }
|
210 | get(url) {
|
211 | if (this._cache.hasOwnProperty(url)) {
|
212 | return Promise.resolve(this._cache[url]);
|
213 | }
|
214 | else {
|
215 | return Promise.reject('CachedResourceLoader: Did not find cached template for ' + url);
|
216 | }
|
217 | }
|
218 | }
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 | const VERSION = new Version('13.2.0');
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 | const RESOURCE_CACHE_PROVIDER = [{ provide: ResourceLoader, useClass: CachedResourceLoader, deps: [] }];
|
255 |
|
256 |
|
257 |
|
258 | const platformBrowserDynamic = createPlatformFactory(platformCoreDynamic, 'browserDynamic', INTERNAL_BROWSER_DYNAMIC_PLATFORM_PROVIDERS);
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 | export { JitCompilerFactory, RESOURCE_CACHE_PROVIDER, VERSION, platformBrowserDynamic, INTERNAL_BROWSER_DYNAMIC_PLATFORM_PROVIDERS as ɵINTERNAL_BROWSER_DYNAMIC_PLATFORM_PROVIDERS, platformCoreDynamic as ɵplatformCoreDynamic };
|
282 |
|