@@ -30,10 +30,14 @@ const writeFile = promiseify(fs.writeFile);
30
30
31
31
32
32
function inlineResources ( globs ) {
33
+ if ( typeof globs == 'string' ) {
34
+ globs = [ globs ] ;
35
+ }
36
+
33
37
/**
34
38
* For every argument, inline the templates and styles under it and write the new file.
35
39
*/
36
- for ( let pattern of globs ) {
40
+ return Promise . all ( globs . map ( pattern => {
37
41
if ( pattern . indexOf ( '*' ) < 0 ) {
38
42
// Argument is a directory target, add glob patterns to include every files.
39
43
pattern = path . join ( pattern , '**' , '*' ) ;
@@ -43,17 +47,32 @@ function inlineResources(globs) {
43
47
. filter ( name => / \. j s $ / . test ( name ) ) ; // Matches only JavaScript files.
44
48
45
49
// 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
+ } ) )
51
55
. then ( content => writeFile ( filePath , content ) )
52
56
. catch ( err => {
53
57
console . error ( 'An error occured: ' , err ) ;
54
58
} ) ;
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 ) ;
57
76
}
58
77
59
78
if ( require . main === module ) {
@@ -64,13 +83,13 @@ if (require.main === module) {
64
83
/**
65
84
* Inline the templates for a source file. Simply search for instances of `templateUrl: ...` and
66
85
* replace with `template: ...` (with the content of the file included).
67
- * @param filePath {string} The path of the source file.
68
86
* @param content {string} The source file's content.
87
+ * @param urlResolver {Function} A resolver that takes a URL and return a path.
69
88
* @return {string } The content with all templates inlined.
70
89
*/
71
- function inlineTemplate ( filePath , content ) {
90
+ function inlineTemplate ( content , urlResolver ) {
72
91
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 ) ;
74
93
const templateContent = fs . readFileSync ( templateFile , 'utf-8' ) ;
75
94
const shortenedTemplate = templateContent
76
95
. replace ( / ( [ \n \r ] \s * ) + / gm, ' ' )
@@ -83,16 +102,16 @@ function inlineTemplate(filePath, content) {
83
102
/**
84
103
* Inline the styles for a source file. Simply search for instances of `styleUrls: [...]` and
85
104
* 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 .
87
106
* @param content {string} The source file's content.
88
107
* @return {string } The content with all styles inlined.
89
108
*/
90
- function inlineStyle ( filePath , content ) {
109
+ function inlineStyle ( content , urlResolver ) {
91
110
return content . replace ( / s t y l e U r l s : \s * ( \[ [ \s \S ] * ?\] ) / gm, function ( m , styleUrls ) {
92
111
const urls = eval ( styleUrls ) ;
93
112
return 'styles: ['
94
113
+ urls . map ( styleUrl => {
95
- const styleFile = path . join ( path . dirname ( filePath ) , styleUrl ) ;
114
+ const styleFile = urlResolver ( styleUrl ) ;
96
115
const styleContent = fs . readFileSync ( styleFile , 'utf-8' ) ;
97
116
const shortenedStyle = styleContent
98
117
. replace ( / ( [ \n \r ] \s * ) + / gm, ' ' )
@@ -107,13 +126,13 @@ function inlineStyle(filePath, content) {
107
126
108
127
/**
109
128
* Remove every mention of `moduleId: module.id`.
110
- * @param _ {string} The file path of the source file, currently ignored.
111
129
* @param content {string} The source file's content.
112
130
* @returns {string } The content with all moduleId: mentions removed.
113
131
*/
114
- function removeModuleId ( _ , content ) {
132
+ function removeModuleId ( content ) {
115
133
return content . replace ( / \s * m o d u l e I d : \s * m o d u l e \. i d \s * , ? \s * / gm, '' ) ;
116
134
}
117
135
118
136
119
137
module . exports = inlineResources ;
138
+ module . exports . inlineResourcesFromString = inlineResourcesFromString ;
0 commit comments