Skip to content

Commit 8142a83

Browse files
committed
chore: replace gulp-inline-resources with a script
This also improves upon the relative path problem. All template paths now should be relative. Also, we can now include double-quotes in CSS/HTML.
1 parent acce9ba commit 8142a83

File tree

3 files changed

+84
-22
lines changed

3 files changed

+84
-22
lines changed

gulpfile.js

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

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"typings": "typings install --ambient",
1717
"postinstall": "npm run typings",
1818
"e2e": "protractor test/protractor.conf.js",
19-
"inline-resources": "gulp inline-resources",
19+
"inline-resources": "node ./scripts/release/inline-resources.js ./dist/components",
2020
"deploy": "firebase deploy"
2121
},
2222
"version": "2.0.0-alpha.4",
@@ -51,8 +51,6 @@
5151
"firebase-tools": "^2.2.1",
5252
"fs-extra": "^0.26.5",
5353
"glob": "^6.0.4",
54-
"gulp": "^3.9.1",
55-
"gulp-inline-ng2-template": "^1.1.2",
5654
"jasmine-core": "^2.4.1",
5755
"js-yaml": "^3.5.2",
5856
"karma": "^0.13.15",
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/env node
2+
'use strict';
3+
4+
const fs = require('fs');
5+
const path = require('path');
6+
const glob = require('glob');
7+
8+
/**
9+
* Simple Promiseify function that takes a Node API and return a version that supports promises.
10+
*/
11+
function promiseify(fn) {
12+
return function() {
13+
const args = [].slice.call(arguments, 0);
14+
return new Promise((resolve, reject) => {
15+
fn.apply(this, args.concat([function (err, value) {
16+
if (err) {
17+
reject(err);
18+
} else {
19+
resolve(value);
20+
}
21+
}]));
22+
});
23+
};
24+
}
25+
26+
const readFile = promiseify(fs.readFile);
27+
const writeFile = promiseify(fs.writeFile);
28+
29+
30+
/**
31+
* For every argument, inline the templates and styles under it and write the new file.
32+
*/
33+
for (let arg of process.argv.slice(2)) {
34+
if (arg.indexOf('*') < 0) {
35+
// Argument is a directory target, add glob patterns to include every files.
36+
arg = path.join(arg, '**', '*');
37+
}
38+
39+
const files = glob.sync(arg, {})
40+
.filter(name => name.match(/\.js$/)); // Matches only JavaScript files.
41+
42+
// Generate all files content with inlined templates.
43+
files.forEach(filePath => {
44+
readFile(filePath, 'utf-8')
45+
.then(content => inlineTemplate(filePath, content))
46+
.then(content => inlineStyle(filePath, content))
47+
.then(content => writeFile(filePath, content))
48+
// .then(content => console.log(content))
49+
// .then(() => process.exit(0))
50+
.catch(err => {
51+
console.error('An error occured: ', err);
52+
});
53+
});
54+
}
55+
56+
57+
function inlineTemplate(filePath, content) {
58+
return content.replace(/templateUrl:\s*'([^']+\.html)'/g, function(m, templateUrl) {
59+
const templateFile = path.join(path.dirname(filePath), templateUrl);
60+
const templateContent = fs.readFileSync(templateFile, 'utf-8');
61+
const shortenedTemplate = templateContent
62+
.replace(/([\n\r]\s*)+/gm, ' ')
63+
.replace(/"/g, '\\"');
64+
return `template: "${shortenedTemplate}"`;
65+
});
66+
}
67+
68+
function inlineStyle(filePath, content) {
69+
return content.replace(/styleUrls:\s*(\[[\s\S]*?\])/gm, function(m, styleUrls) {
70+
const urls = eval(styleUrls);
71+
return 'styles: ['
72+
+ urls.map(styleUrl => {
73+
const styleFile = path.join(path.dirname(filePath), styleUrl);
74+
const styleContent = fs.readFileSync(styleFile, 'utf-8');
75+
const shortenedStyle = styleContent
76+
.replace(/([\n\r]\s*)+/gm, ' ')
77+
.replace(/"/g, '\\"');
78+
return `"${shortenedStyle}"`;
79+
})
80+
.join(',\n')
81+
+ ']';
82+
});
83+
}

0 commit comments

Comments
 (0)