@@ -30,10 +30,14 @@ const writeFile = promiseify(fs.writeFile);
3030
3131
3232function inlineResources ( globs ) {
33+ if ( typeof globs == 'string' ) {
34+ globs = [ globs ] ;
35+ }
36+
3337 /**
3438 * For every argument, inline the templates and styles under it and write the new file.
3539 */
36- for ( let pattern of globs ) {
40+ return Promise . all ( globs . map ( pattern => {
3741 if ( pattern . indexOf ( '*' ) < 0 ) {
3842 // Argument is a directory target, add glob patterns to include every files.
3943 pattern = path . join ( pattern , '**' , '*' ) ;
@@ -43,17 +47,32 @@ function inlineResources(globs) {
4347 . filter ( name => / \. j s $ / . test ( name ) ) ; // Matches only JavaScript files.
4448
4549 // Generate all files content with inlined templates.
46- files . forEach ( filePath => {
47- readFile ( filePath , 'utf-8' )
48- . then ( content => inlineTemplate ( filePath , content ) )
49- . then ( content => inlineStyle ( filePath , content ) )
50- . then ( content => removeModuleId ( filePath , content ) )
50+ return Promise . all ( files . map ( filePath => {
51+ return readFile ( filePath , 'utf-8' )
52+ . then ( content => inlineResourcesFromString ( content , url => {
53+ return path . join ( path . dirname ( filePath ) , url ) ;
54+ } ) )
5155 . then ( content => writeFile ( filePath , content ) )
5256 . catch ( err => {
5357 console . error ( 'An error occured: ' , err ) ;
5458 } ) ;
55- } ) ;
56- }
59+ } ) ) ;
60+ } ) ) ;
61+ }
62+
63+ /**
64+ * Inline resources from a string content.
65+ * @param content {string} The source file's content.
66+ * @param urlResolver {Function} A resolver that takes a URL and return a path.
67+ * @returns {string } The content with resources inlined.
68+ */
69+ function inlineResourcesFromString ( content , urlResolver ) {
70+ // Curry through the inlining functions.
71+ return [
72+ inlineTemplate ,
73+ inlineStyle ,
74+ removeModuleId
75+ ] . reduce ( ( content , fn ) => fn ( content , urlResolver ) , content ) ;
5776}
5877
5978if ( require . main === module ) {
@@ -64,13 +83,13 @@ if (require.main === module) {
6483/**
6584 * Inline the templates for a source file. Simply search for instances of `templateUrl: ...` and
6685 * replace with `template: ...` (with the content of the file included).
67- * @param filePath {string} The path of the source file.
6886 * @param content {string} The source file's content.
87+ * @param urlResolver {Function} A resolver that takes a URL and return a path.
6988 * @return {string } The content with all templates inlined.
7089 */
71- function inlineTemplate ( filePath , content ) {
90+ function inlineTemplate ( content , urlResolver ) {
7291 return content . replace ( / t e m p l a t e U r l : \s * ' ( [ ^ ' ] + ?\. h t m l ) ' / g, function ( m , templateUrl ) {
73- const templateFile = path . join ( path . dirname ( filePath ) , templateUrl ) ;
92+ const templateFile = urlResolver ( templateUrl ) ;
7493 const templateContent = fs . readFileSync ( templateFile , 'utf-8' ) ;
7594 const shortenedTemplate = templateContent
7695 . replace ( / ( [ \n \r ] \s * ) + / gm, ' ' )
@@ -83,16 +102,16 @@ function inlineTemplate(filePath, content) {
83102/**
84103 * Inline the styles for a source file. Simply search for instances of `styleUrls: [...]` and
85104 * replace with `styles: [...]` (with the content of the file included).
86- * @param filePath {string} The path of the source file .
105+ * @param urlResolver {Function} A resolver that takes a URL and return a path .
87106 * @param content {string} The source file's content.
88107 * @return {string } The content with all styles inlined.
89108 */
90- function inlineStyle ( filePath , content ) {
109+ function inlineStyle ( content , urlResolver ) {
91110 return content . replace ( / s t y l e U r l s : \s * ( \[ [ \s \S ] * ?\] ) / gm, function ( m , styleUrls ) {
92111 const urls = eval ( styleUrls ) ;
93112 return 'styles: ['
94113 + urls . map ( styleUrl => {
95- const styleFile = path . join ( path . dirname ( filePath ) , styleUrl ) ;
114+ const styleFile = urlResolver ( styleUrl ) ;
96115 const styleContent = fs . readFileSync ( styleFile , 'utf-8' ) ;
97116 const shortenedStyle = styleContent
98117 . replace ( / ( [ \n \r ] \s * ) + / gm, ' ' )
@@ -107,13 +126,13 @@ function inlineStyle(filePath, content) {
107126
108127/**
109128 * Remove every mention of `moduleId: module.id`.
110- * @param _ {string} The file path of the source file, currently ignored.
111129 * @param content {string} The source file's content.
112130 * @returns {string } The content with all moduleId: mentions removed.
113131 */
114- function removeModuleId ( _ , content ) {
132+ function removeModuleId ( content ) {
115133 return content . replace ( / \s * m o d u l e I d : \s * m o d u l e \. i d \s * , ? \s * / gm, '' ) ;
116134}
117135
118136
119137module . exports = inlineResources ;
138+ module . exports . inlineResourcesFromString = inlineResourcesFromString ;
0 commit comments