Skip to content

Commit 280f2af

Browse files
committed
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)
1 parent 0c03946 commit 280f2af

File tree

3 files changed

+60
-140
lines changed

3 files changed

+60
-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 {inlineResourcesFolder} 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`, () => inlineResourcesFolder(packageOut));
8888

8989
/**
9090
* Watch tasks, that will rebuild the package whenever TS, SCSS, or HTML files change.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 and inlines all external resources of Angular components. */
8+
export function inlineResourcesFolder(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 templateFile = join(dirname(filePath), templateUrl);
27+
const templateContent = loadResourceFile(templateFile);
28+
return `template: "${templateContent}"`;
29+
});
30+
}
31+
32+
33+
/** Inlines the external styles of Angular components for a specified source file. */
34+
function inlineStyles(fileContent: string, filePath: string) {
35+
return fileContent.replace(/styleUrls:\s*(\[[\s\S]*?])/gm, (match, styleUrls) => {
36+
// 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') + ']';
45+
});
46+
}
47+
48+
/** Remove every mention of `moduleId: module.id` */
49+
function removeModuleId(fileContent: string) {
50+
return fileContent.replace(/\s*moduleId:\s*module\.id\s*,?\s*/gm, '');
51+
}
52+
53+
/** Loads the specified resource file and drops line-breaks of the content. */
54+
function loadResourceFile(filePath: string): string {
55+
return readFileSync(filePath, 'utf-8')
56+
.replace(/([\n\r]\s*)+/gm, ' ')
57+
.replace(/"/g, '\\"');
58+
}

0 commit comments

Comments
 (0)