-
Notifications
You must be signed in to change notification settings - Fork 0
/
inline-resources.js
68 lines (55 loc) · 2.33 KB
/
inline-resources.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// tslint:disable:no-eval
const { dirname, join } = require('path')
const { readFileSync, writeFileSync } = require('fs')
const glob = require('glob').sync
/** Finds all JavaScript files in a directory and inlines all resources of Angular components. */
function inlineResourcesForDirectory(folderPath) {
glob(join(folderPath, '**/*.js')).forEach(filePath => inlineResources(filePath));
}
/** Inlines the external resources of Angular components of a file. */
function inlineResources(filePath) {
let fileContent = readFileSync(filePath, 'utf-8');
fileContent = inlineTemplate(fileContent, filePath);
fileContent = inlineStyles(fileContent, filePath);
fileContent = removeModuleId(fileContent);
writeFileSync(filePath, fileContent, 'utf-8');
}
/** Inlines the templates of Angular components for a specified source file. */
function inlineTemplate(fileContent, filePath) {
return fileContent.replace(/templateUrl:\s*'([^']+?\.html)'/g, (_match, templateUrl) => {
const templatePath = join(dirname(filePath), templateUrl);
const templateContent = loadResourceFile(templatePath);
return `template: "${templateContent}"`;
});
}
/** Inlines the external styles of Angular components for a specified source file. */
function inlineStyles(fileContent, filePath) {
return fileContent.replace(/styleUrls:\s*(\[[\s\S]*?])/gm, (_match, styleUrlsValue) => {
// The RegExp matches the array of external style files. This is a string right now and
// can to be parsed using the `eval` method. The value looks like "['AAA.css', 'BBB.css']"
const styleUrls = eval(styleUrlsValue)
const styleContents = styleUrls
.map(url => {
const arr = url.split('.')
arr.pop()
arr.push('css')
return arr.join('.')
})
.map(url => join(dirname(filePath), url))
.map(path => loadResourceFile(path));
return `styles: ["${styleContents.join(' ')}"]`;
});
}
/** Remove every mention of `moduleId: module.id` */
function removeModuleId(fileContent) {
return fileContent.replace(/\s*moduleId:\s*module\.id\s*,?\s*/gm, '');
}
/** Loads the specified resource file and drops line-breaks of the content. */
function loadResourceFile(filePath) {
return readFileSync(filePath, 'utf-8')
.replace(/([\n\r]\s*)+/gm, ' ')
.replace(/"/g, '\\"');
}
module.exports = {
inlineResourcesForDirectory
}