@@ -4,8 +4,8 @@ import {dirname, join} from 'path';
44import { readFileSync , writeFileSync } from 'fs' ;
55import { sync as glob } from 'glob' ;
66
7- /** Finds all JavaScript files and inlines all external resources of Angular components. */
8- export function inlineResourcesFolder ( folderPath : string ) {
7+ /** Finds all JavaScript files in a directory and inlines all resources of Angular components. */
8+ export function inlineResourcesForDirectory ( folderPath : string ) {
99 glob ( join ( folderPath , '**/*.js' ) ) . forEach ( filePath => inlineResources ( filePath ) ) ;
1010}
1111
@@ -23,25 +23,24 @@ export function inlineResources(filePath: string) {
2323/** Inlines the templates of Angular components for a specified source file. */
2424function inlineTemplate ( fileContent : string , filePath : string ) {
2525 return fileContent . replace ( / t e m p l a t e U r l : \s * ' ( [ ^ ' ] + ?\. h t m l ) ' / g, ( match , templateUrl ) => {
26- const templateFile = join ( dirname ( filePath ) , templateUrl ) ;
27- const templateContent = loadResourceFile ( templateFile ) ;
26+ const templatePath = join ( dirname ( filePath ) , templateUrl ) ;
27+ const templateContent = loadResourceFile ( templatePath ) ;
2828 return `template: "${ templateContent } "` ;
2929 } ) ;
3030}
3131
32-
3332/** Inlines the external styles of Angular components for a specified source file. */
3433function inlineStyles ( fileContent : string , filePath : string ) {
35- return fileContent . replace ( / s t y l e U r l s : \s * ( \[ [ \s \S ] * ?] ) / gm, ( match , styleUrls ) => {
34+ return fileContent . replace ( / s t y l e U r l s : \s * ( \[ [ \s \S ] * ?] ) / gm, ( match , styleUrlsValue ) => {
3635 // The RegExp matches the array of external style files. This is a string right now and
37- // can to be parsed using the `eval` method.
38- const parsedUrls = eval ( styleUrls ) as string [ ] ;
39-
40- return 'styles: [' + parsedUrls . map ( styleUrl => {
41- const stylePath = join ( dirname ( filePath ) , styleUrl ) ;
42- const styleContent = loadResourceFile ( stylePath ) ;
43- return `" ${ styleContent } "` ;
44- } ) . join ( ',\n' ) + ']' ;
36+ // can to be parsed using the `eval` method. The value looks like "['AAA.css', 'BBB.css"]"
37+ const styleUrls = eval ( styleUrlsValue ) as string [ ] ;
38+
39+ const styleContents = styleUrls
40+ . map ( url => join ( dirname ( filePath ) , url ) )
41+ . map ( path => loadResourceFile ( path ) ) ;
42+
43+ return `styles: [" ${ styleContents . join ( ',' ) } "]` ;
4544 } ) ;
4645}
4746
0 commit comments