|
1 | 1 | 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'; |
4 | 4 | import {task, src, dest} from 'gulp';
|
5 | 5 | import {execTask, sequenceTask} from '../util/task_helpers';
|
6 | 6 | 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 |
8 | 8 | } from '../constants';
|
9 | 9 | import * as minimist from 'minimist';
|
10 | 10 |
|
11 | 11 | // There are no type definitions available for these imports.
|
12 | 12 | const gulpRename = require('gulp-rename');
|
13 |
| - |
| 13 | +const glob = require('glob'); |
14 | 14 | /** Parse command-line arguments for release task. */
|
15 | 15 | const argv = minimist(process.argv.slice(3));
|
16 | 16 |
|
@@ -38,22 +38,74 @@ task(':package:release', [
|
38 | 38 | ]);
|
39 | 39 |
|
40 | 40 | /** 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'], () => { |
42 | 42 | // See: https://github.com/angular/angular/blob/master/build.sh#L293-L294
|
43 | 43 | copySync(join(DIST_MATERIAL, 'index.metadata.json'),
|
44 | 44 | join(DIST_RELEASE, 'material.metadata.json'));
|
45 | 45 | });
|
46 | 46 |
|
| 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 | + |
47 | 99 | /**
|
48 | 100 | * Workaround for a @angular/tsc-wrapped issue, where the compiler looks for component assets
|
49 | 101 | * in the wrong folder. This issue only appears for bundled metadata files.
|
50 | 102 | * As a workaround, we just copy all assets next to the metadata bundle.
|
51 | 103 | **/
|
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 | +// }); |
57 | 109 |
|
58 | 110 | task(':package:assets', () => src(assetsGlob).pipe(dest(DIST_RELEASE)));
|
59 | 111 |
|
|
0 commit comments