6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- import type { CompilerHost , NgtscProgram } from '@angular/compiler-cli' ;
9
+ import type { NgtscProgram } from '@angular/compiler-cli' ;
10
10
import { transformAsync } from '@babel/core' ;
11
11
import type {
12
12
OnStartResult ,
@@ -25,6 +25,7 @@ import ts from 'typescript';
25
25
import angularApplicationPreset from '../../babel/presets/application' ;
26
26
import { requiresLinking } from '../../babel/webpack-loader' ;
27
27
import { loadEsmModule } from '../../utils/load-esm' ;
28
+ import { createAngularCompilerHost , ensureSourceFileVersions } from './angular-host' ;
28
29
import {
29
30
logCumulativeDurations ,
30
31
profileAsync ,
@@ -268,77 +269,39 @@ export function createCompilerPlugin(
268
269
// Reset stylesheet resource output files
269
270
stylesheetResourceFiles = [ ] ;
270
271
271
- // Create TypeScript compiler host
272
- const host = ts . createIncrementalCompilerHost ( compilerOptions ) ;
273
-
274
- // Temporarily process external resources via readResource.
275
- // The AOT compiler currently requires this hook to allow for a transformResource hook.
276
- // Once the AOT compiler allows only a transformResource hook, this can be reevaluated.
277
- ( host as CompilerHost ) . readResource = async function ( fileName ) {
278
- // Template resources (.html/.svg) files are not bundled or transformed
279
- if ( fileName . endsWith ( '.html' ) || fileName . endsWith ( '.svg' ) ) {
280
- return this . readFile ( fileName ) ?? '' ;
281
- }
282
-
283
- const { contents, resourceFiles, errors, warnings } = await bundleStylesheetFile (
284
- fileName ,
285
- styleOptions ,
286
- ) ;
287
-
288
- ( result . errors ??= [ ] ) . push ( ...errors ) ;
289
- ( result . warnings ??= [ ] ) . push ( ...warnings ) ;
290
- stylesheetResourceFiles . push ( ...resourceFiles ) ;
291
-
292
- return contents ;
293
- } ;
294
-
295
- // Add an AOT compiler resource transform hook
296
- ( host as CompilerHost ) . transformResource = async function ( data , context ) {
297
- // Only inline style resources are transformed separately currently
298
- if ( context . resourceFile || context . type !== 'style' ) {
299
- return null ;
300
- }
301
-
302
- // The file with the resource content will either be an actual file (resourceFile)
303
- // or the file containing the inline component style text (containingFile).
304
- const file = context . resourceFile ?? context . containingFile ;
305
-
306
- const { contents, resourceFiles, errors, warnings } = await bundleStylesheetText (
307
- data ,
308
- {
309
- resolvePath : path . dirname ( file ) ,
310
- virtualName : file ,
311
- } ,
312
- styleOptions ,
313
- ) ;
314
-
315
- ( result . errors ??= [ ] ) . push ( ...errors ) ;
316
- ( result . warnings ??= [ ] ) . push ( ...warnings ) ;
317
- stylesheetResourceFiles . push ( ...resourceFiles ) ;
318
-
319
- return { content : contents } ;
320
- } ;
321
-
322
- // Temporary deep import for host augmentation support
323
- const {
324
- augmentHostWithCaching,
325
- augmentHostWithReplacements,
326
- augmentProgramWithVersioning,
327
- } = require ( '@ngtools/webpack/src/ivy/host' ) ;
272
+ // Create Angular compiler host
273
+ const host = createAngularCompilerHost ( compilerOptions , {
274
+ fileReplacements : pluginOptions . fileReplacements ,
275
+ modifiedFiles : pluginOptions . sourceFileCache ?. modifiedFiles ,
276
+ sourceFileCache : pluginOptions . sourceFileCache ,
277
+ async transformStylesheet ( data , containingFile , stylesheetFile ) {
278
+ // Stylesheet file only exists for external stylesheets
279
+ const filename = stylesheetFile ?? containingFile ;
280
+
281
+ // Temporary workaround for lack of virtual file support in the Sass plugin.
282
+ // External Sass stylesheets are transformed using the file instead of the already read content.
283
+ let stylesheetResult ;
284
+ if ( filename . endsWith ( '.scss' ) || filename . endsWith ( '.sass' ) ) {
285
+ stylesheetResult = await bundleStylesheetFile ( filename , styleOptions ) ;
286
+ } else {
287
+ stylesheetResult = await bundleStylesheetText (
288
+ data ,
289
+ {
290
+ resolvePath : path . dirname ( filename ) ,
291
+ virtualName : filename ,
292
+ } ,
293
+ styleOptions ,
294
+ ) ;
295
+ }
328
296
329
- // Augment TypeScript Host for file replacements option
330
- if ( pluginOptions . fileReplacements ) {
331
- augmentHostWithReplacements ( host , pluginOptions . fileReplacements ) ;
332
- }
297
+ const { contents , resourceFiles , errors , warnings } = stylesheetResult ;
298
+ ( result . errors ??= [ ] ) . push ( ... errors ) ;
299
+ ( result . warnings ??= [ ] ) . push ( ... warnings ) ;
300
+ stylesheetResourceFiles . push ( ... resourceFiles ) ;
333
301
334
- // Augment TypeScript Host with source file caching if provided
335
- if ( pluginOptions . sourceFileCache ) {
336
- augmentHostWithCaching ( host , pluginOptions . sourceFileCache ) ;
337
- // Allow the AOT compiler to request the set of changed templates and styles
338
- ( host as CompilerHost ) . getModifiedResourceFiles = function ( ) {
339
- return pluginOptions . sourceFileCache ?. modifiedFiles ;
340
- } ;
341
- }
302
+ return contents ;
303
+ } ,
304
+ } ) ;
342
305
343
306
// Create the Angular specific program that contains the Angular compiler
344
307
const angularProgram = profileSync (
@@ -348,7 +311,7 @@ export function createCompilerPlugin(
348
311
previousAngularProgram = angularProgram ;
349
312
const angularCompiler = angularProgram . compiler ;
350
313
const typeScriptProgram = angularProgram . getTsProgram ( ) ;
351
- augmentProgramWithVersioning ( typeScriptProgram ) ;
314
+ ensureSourceFileVersions ( typeScriptProgram ) ;
352
315
353
316
const builder = ts . createEmitAndSemanticDiagnosticsBuilderProgram (
354
317
typeScriptProgram ,
0 commit comments