Skip to content

Commit 4f2e466

Browse files
committed
build: inline metadata.json resources
1 parent 4f9eecd commit 4f2e466

File tree

1 file changed

+62
-10
lines changed

1 file changed

+62
-10
lines changed

tools/gulp/tasks/release.ts

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import {spawn} from 'child_process';
2-
import {existsSync, statSync, copySync, writeFileSync} from 'fs-extra';
3-
import {join} from 'path';
2+
import {existsSync, statSync, copySync, writeFileSync, readFileSync} from 'fs-extra';
3+
import {join, basename} from 'path';
44
import {task, src, dest} from 'gulp';
55
import {execTask, sequenceTask} from '../util/task_helpers';
66
import {
7-
DIST_RELEASE, DIST_BUNDLES, DIST_MATERIAL, COMPONENTS_DIR, LICENSE_BANNER
7+
DIST_RELEASE, DIST_BUNDLES, DIST_MATERIAL, COMPONENTS_DIR, LICENSE_BANNER, DIST_ROOT
88
} from '../constants';
99
import * as minimist from 'minimist';
1010

1111
// There are no type definitions available for these imports.
1212
const gulpRename = require('gulp-rename');
13-
13+
const glob = require('glob');
1414
/** Parse command-line arguments for release task. */
1515
const argv = minimist(process.argv.slice(3));
1616

@@ -38,22 +38,74 @@ task(':package:release', [
3838
]);
3939

4040
/** Copy metatadata.json and associated d.ts files to the root of the package structure. */
41-
task(':package:metadata', [':package:fix-metadata'], () => {
41+
task(':package:metadata', [':inline-metadata-resources'], () => {
4242
// See: https://github.com/angular/angular/blob/master/build.sh#L293-L294
4343
copySync(join(DIST_MATERIAL, 'index.metadata.json'),
4444
join(DIST_RELEASE, 'material.metadata.json'));
4545
});
4646

47+
48+
49+
task(':inline-metadata-resources', () => {
50+
const componentResources = new Map<string, string>();
51+
52+
glob(join(DIST_MATERIAL, '**/*.+(html|css)'), (err: any, resourceFilePaths: any) => {
53+
for (const path of resourceFilePaths) {
54+
componentResources.set(basename(path), path);
55+
}
56+
});
57+
58+
glob(join(DIST_ROOT, '**/*.metadata.json'), (err: any, metadataFilePaths: any) => {
59+
for (const path of metadataFilePaths) {
60+
let metadata = JSON.parse(readFileSync(path, 'utf-8'));
61+
inlineMetadataResources(metadata, componentResources);
62+
writeFileSync(path , JSON.stringify(metadata), 'utf-8');
63+
}
64+
})
65+
});
66+
67+
/**
68+
* Recurse through a parsed metadata.json file and inline all html and css.
69+
* Note: this assumes that all html and css files have a unique name.
70+
*/
71+
function inlineMetadataResources(metadata: any, componentResources: Map<string, string>) {
72+
// Convert `templateUrl` to `template`
73+
if (metadata.templateUrl) {
74+
const fullResourcePath = componentResources.get(metadata.templateUrl);
75+
metadata.template = readFileSync(fullResourcePath, 'utf-8');
76+
delete metadata.templateUrl;
77+
}
78+
79+
// Convert `styleUrls` to `styles`
80+
if (metadata.styleUrls && metadata.styleUrls.length) {
81+
metadata.styles = [];
82+
for (const styleUrl of metadata.styleUrls) {
83+
const fullResourcePath = componentResources.get(styleUrl);
84+
metadata.styles.push(readFileSync(fullResourcePath, 'utf-8'));
85+
}
86+
delete metadata.styleUrls;
87+
}
88+
89+
// We we did nothing at this node, go deeper.
90+
if (!metadata.template && !metadata.styles) {
91+
for (const property in metadata) {
92+
if (typeof metadata[property] == 'object' && metadata[property]) {
93+
inlineMetadataResources(metadata[property], componentResources);
94+
}
95+
}
96+
}
97+
}
98+
4799
/**
48100
* Workaround for a @angular/tsc-wrapped issue, where the compiler looks for component assets
49101
* in the wrong folder. This issue only appears for bundled metadata files.
50102
* As a workaround, we just copy all assets next to the metadata bundle.
51103
**/
52-
task(':package:fix-metadata', () => {
53-
return src('**/*.+(html|css)', { cwd: DIST_MATERIAL })
54-
.pipe(gulpRename({dirname: ''}))
55-
.pipe(dest(DIST_RELEASE));
56-
});
104+
// task(':package:fix-metadata', () => {
105+
// return src('**/*.+(html|css)', { cwd: DIST_MATERIAL })
106+
// .pipe(gulpRename({dirname: ''}))
107+
// .pipe(dest(DIST_RELEASE));
108+
// });
57109

58110
task(':package:assets', () => src(assetsGlob).pipe(dest(DIST_RELEASE)));
59111

0 commit comments

Comments
 (0)