Skip to content

Commit 70547f4

Browse files
committed
build: inline metadata.json resources
1 parent 53f9925 commit 70547f4

File tree

5 files changed

+70
-24
lines changed

5 files changed

+70
-24
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108
"ts-node": "^3.0.0",
109109
"tslint": "^4.4.2",
110110
"tslint-no-unused-var": "0.0.6",
111-
"typescript": "~2.2.1",
111+
"typescript": "~2.1.1",
112112
"uglify-js": "^2.8.14",
113113
"web-animations-js": "^2.2.2"
114114
}

src/lib/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"compilerOptions": {
33
"baseUrl": ".",
44
"declaration": true,
5-
"stripInternal": true,
5+
"stripInternal": false,
66
"experimentalDecorators": true,
77
"module": "es2015",
88
"moduleResolution": "node",

src/lib/typedoc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@
1717
"suppressExcessPropertyErrors": "true",
1818
"suppressImplicitAnyIndexErrors": "true",
1919
"module": "commonjs"
20-
}
20+
}

tools/gulp/tasks/aot.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ task('aot:deps', sequenceTask('build:devapp', ':package:release', 'aot:copy-rele
1414
task('aot:copy-release', () => copySync(DIST_RELEASE, join(DIST_DEMOAPP, 'material')));
1515

1616
/** Build the demo-app and a release to confirm that the library is AOT-compatible. */
17-
task('aot:build', ['aot:deps'], execNodeTask(
17+
task('aot:build', sequenceTask('aot:deps', 'aot:compiler-cli'));
18+
19+
/** Build the demo-app and a release to confirm that the library is AOT-compatible. */
20+
task('aot:compiler-cli', execNodeTask(
1821
'@angular/compiler-cli', 'ngc', ['-p', tsconfigFile]
1922
));

tools/gulp/tasks/release.ts

Lines changed: 63 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
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.
12-
const gulpRename = require('gulp-rename');
12+
const glob = require('glob');
1313

1414
/** Parse command-line arguments for release task. */
1515
const argv = minimist(process.argv.slice(3));
@@ -29,30 +29,40 @@ task('build:release', sequenceTask(
2929
));
3030

3131
/** Task that combines intermediate build artifacts into the release package structure. */
32-
task(':package:release', [
32+
task(':package:release', sequenceTask(
33+
[':package:typings', ':package:umd', ':package:fesm', ':package:assets'],
34+
':inline-metadata-resources',
3335
':package:metadata',
34-
':package:typings',
35-
':package:umd',
36-
':package:fesm',
37-
':package:assets'
38-
]);
36+
));
3937

4038
/** Copy metatadata.json and associated d.ts files to the root of the package structure. */
41-
task(':package:metadata', [':package:fix-metadata'], () => {
39+
task(':package:metadata', () => {
4240
// See: https://github.com/angular/angular/blob/master/build.sh#L293-L294
4341
copySync(join(DIST_MATERIAL, 'index.metadata.json'),
4442
join(DIST_RELEASE, 'material.metadata.json'));
4543
});
4644

47-
/**
48-
* Workaround for a @angular/tsc-wrapped issue, where the compiler looks for component assets
49-
* in the wrong folder. This issue only appears for bundled metadata files.
50-
* As a workaround, we just copy all assets next to the metadata bundle.
51-
**/
52-
task(':package:fix-metadata', () => {
53-
return src('**/*.+(html|css)', { cwd: DIST_MATERIAL })
54-
.pipe(gulpRename({dirname: ''}))
55-
.pipe(dest(DIST_RELEASE));
45+
/** Inlines the html and css resources into all metadata.json files in dist/ */
46+
task(':inline-metadata-resources', () => {
47+
// Create a map of fileName -> fullFilePath. This is needed because the templateUrl and
48+
// styleUrls for each component use just the filename because, in the source, the component
49+
// and the resources live in the same directory.
50+
const componentResources = new Map<string, string>();
51+
glob(join(DIST_ROOT, '**/*.+(html|css)'), (err: any, resourceFilePaths: any) => {
52+
for (const path of resourceFilePaths) {
53+
componentResources.set(basename(path), path);
54+
}
55+
});
56+
57+
// Find all metadata files. For each one, parse the JSON content, inline the resources, and
58+
// reserialize and rewrite back to the original location.
59+
glob(join(DIST_ROOT, '**/*.metadata.json'), (err: any, metadataFilePaths: any) => {
60+
for (const path of metadataFilePaths) {
61+
let metadata = JSON.parse(readFileSync(path, 'utf-8'));
62+
inlineMetadataResources(metadata, componentResources);
63+
writeFileSync(path , JSON.stringify(metadata), 'utf-8');
64+
}
65+
});
5666
});
5767

5868
task(':package:assets', () => src(assetsGlob).pipe(dest(DIST_RELEASE)));
@@ -155,3 +165,36 @@ task('publish', sequenceTask(
155165
':publish',
156166
':publish:logout',
157167
));
168+
169+
170+
/**
171+
* Recurse through a parsed metadata.json file and inline all html and css.
172+
* Note: this assumes that all html and css files have a unique name.
173+
*/
174+
function inlineMetadataResources(metadata: any, componentResources: Map<string, string>) {
175+
// Convert `templateUrl` to `template`
176+
if (metadata.templateUrl) {
177+
const fullResourcePath = componentResources.get(metadata.templateUrl);
178+
metadata.template = readFileSync(fullResourcePath, 'utf-8');
179+
delete metadata.templateUrl;
180+
}
181+
182+
// Convert `styleUrls` to `styles`
183+
if (metadata.styleUrls && metadata.styleUrls.length) {
184+
metadata.styles = [];
185+
for (const styleUrl of metadata.styleUrls) {
186+
const fullResourcePath = componentResources.get(styleUrl);
187+
metadata.styles.push(readFileSync(fullResourcePath, 'utf-8'));
188+
}
189+
delete metadata.styleUrls;
190+
}
191+
192+
// We we did nothing at this node, go deeper.
193+
if (!metadata.template && !metadata.styles) {
194+
for (const property in metadata) {
195+
if (typeof metadata[property] == 'object' && metadata[property]) {
196+
inlineMetadataResources(metadata[property], componentResources);
197+
}
198+
}
199+
}
200+
}

0 commit comments

Comments
 (0)