Skip to content

Commit 52b6692

Browse files
devversionmmalerba
authored andcommitted
build: move inline resources script to tools (#4894)
* build: move inline resources script to tools * Moves the inline resources script to the `tools/gulp/packaging` folder. This is necessary when exposing the build tools (e.g for flex-layout) * This also makes the packaging more clean because no external script is referenced (also switched to TypeScript and improved code) * Address comments * Update comment
1 parent 965bf18 commit 52b6692

File tree

3 files changed

+59
-140
lines changed

3 files changed

+59
-140
lines changed

scripts/release/inline-resources.js

Lines changed: 0 additions & 138 deletions
This file was deleted.

tools/gulp/packaging/build-tasks-gulp.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import {SOURCE_ROOT, DIST_ROOT, HTML_MINIFIER_OPTIONS} from '../build-config';
55
import {sequenceTask, sassBuildTask, copyTask, triggerLivereload} from '../util/task_helpers';
66
import {composeRelease} from './build-release';
77
import {buildPackageBundles} from './build-bundles';
8+
import {inlineResourcesForDirectory} from './inline-resources';
89

910
// There are no type definitions available for these imports.
10-
const inlineResources = require('../../../scripts/release/inline-resources');
1111
const htmlmin = require('gulp-htmlmin');
1212

1313
/**
@@ -84,7 +84,7 @@ export function createPackageBuildTasks(packageName: string, requiredPackages: s
8484
return src(htmlGlob).pipe(htmlmin(HTML_MINIFIER_OPTIONS)).pipe(dest(packageOut));
8585
});
8686

87-
task(`${packageName}:assets:inline`, () => inlineResources(packageOut));
87+
task(`${packageName}:assets:inline`, () => inlineResourcesForDirectory(packageOut));
8888

8989
/**
9090
* Watch tasks, that will rebuild the package whenever TS, SCSS, or HTML files change.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/* tslint:disable:no-eval */
2+
3+
import {dirname, join} from 'path';
4+
import {readFileSync, writeFileSync} from 'fs';
5+
import {sync as glob} from 'glob';
6+
7+
/** Finds all JavaScript files in a directory and inlines all resources of Angular components. */
8+
export function inlineResourcesForDirectory(folderPath: string) {
9+
glob(join(folderPath, '**/*.js')).forEach(filePath => inlineResources(filePath));
10+
}
11+
12+
/** Inlines the external resources of Angular components of a file. */
13+
export function inlineResources(filePath: string) {
14+
let fileContent = readFileSync(filePath, 'utf-8');
15+
16+
fileContent = inlineTemplate(fileContent, filePath);
17+
fileContent = inlineStyles(fileContent, filePath);
18+
fileContent = removeModuleId(fileContent);
19+
20+
writeFileSync(filePath, fileContent, 'utf-8');
21+
}
22+
23+
/** Inlines the templates of Angular components for a specified source file. */
24+
function inlineTemplate(fileContent: string, filePath: string) {
25+
return fileContent.replace(/templateUrl:\s*'([^']+?\.html)'/g, (match, templateUrl) => {
26+
const templatePath = join(dirname(filePath), templateUrl);
27+
const templateContent = loadResourceFile(templatePath);
28+
return `template: "${templateContent}"`;
29+
});
30+
}
31+
32+
/** Inlines the external styles of Angular components for a specified source file. */
33+
function inlineStyles(fileContent: string, filePath: string) {
34+
return fileContent.replace(/styleUrls:\s*(\[[\s\S]*?])/gm, (match, styleUrlsValue) => {
35+
// The RegExp matches the array of external style files. This is a string right now and
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(',')}"]`;
44+
});
45+
}
46+
47+
/** Remove every mention of `moduleId: module.id` */
48+
function removeModuleId(fileContent: string) {
49+
return fileContent.replace(/\s*moduleId:\s*module\.id\s*,?\s*/gm, '');
50+
}
51+
52+
/** Loads the specified resource file and drops line-breaks of the content. */
53+
function loadResourceFile(filePath: string): string {
54+
return readFileSync(filePath, 'utf-8')
55+
.replace(/([\n\r]\s*)+/gm, ' ')
56+
.replace(/"/g, '\\"');
57+
}

0 commit comments

Comments
 (0)