-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathvulcanize.js
148 lines (126 loc) · 3.19 KB
/
vulcanize.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
var vulcan = Npm.require('vulcanize');
var crypto = Npm.require('crypto');
var Future = Npm.require('fibers/future');
var url = Npm.require('url');
var fs = Npm.require('fs');
/**
* Vulcanize now requires a target input file.
* We need to create a temp file located within the same
* 'abspath' as the actual imports. Otherwise, vulcanize
* gets confused during the process.
*/
var tmpFile = '_imports.html';
var tmpDir = 'public';
var tmpPath = tmpDir + '/' + tmpFile;
/**
* Log
*/
function log() {
args = _.values(arguments);
args.unshift("=> Vulcanize:");
console.log.apply(this, args);
}
/**
* Get script tag with specified path.
*/
function scriptTag(path) {
return '<script src="' + path + '"></script>';
}
/**
* Get link tag with specified path.
*/
function linkTag(path) {
return '<link rel="import" href="' + path + '">';
}
/**
* Add config for dom mode to head.
*/
function addShadowDomConfig(file) {
file.addHtml({
section: 'head',
data: '<script> window.Polymer = {dom: "shadow"}; </script>'
});
}
/**
* Add webcomponentsjs script to head.
* @todo read first line and check for script tag
*/
function addPolyfillTag(file, path) {
file.addHtml({
section: 'head',
data: scriptTag(path)
});
}
/**
* Add imports to head.
*/
function addImportTag(file, path) {
file.addHtml({
section: 'head',
data: linkTag(path)
});
}
/**
* Vulcanize all files and add output file to head.
*/
function vulcanizeImports(file, imports) {
var future = new Future();
var tags = _.map(imports, function(path) {
return linkTag(path);
});
fs.writeFileSync(tmpPath, tags.join("\n"));
vulcan.setOptions({ abspath: tmpDir });
vulcan.process(tmpFile, function(err, html) {
fs.unlinkSync(tmpPath);
var filenameHash = crypto.createHash('md5').update(html).digest('hex');
var filePath = '/vulcanized-' + filenameHash + '.html';
file.addAsset({
path: filePath,
data: html
});
if (_.isString(process.env.CDN_PREFIX)) {
filePath = url.resolve(process.env.CDN_PREFIX, filePath);
}
addImportTag(file, filePath);
future.return(true);
});
return future.wait();
}
/**
* Add individual import tags
*/
function individualImports(file, imports) {
_.each(imports, function(path) {
addImportTag(file, path);
});
}
function VulcanizeCompiler() {}
VulcanizeCompiler.prototype.processFilesForTarget = function (files) {
files.forEach(function (file) {
// Get JSON file.
var json = JSON.parse(file.getContentsAsString());
// Add polyfill to html if defined.
if (_.isString(json.polyfill)) {
addPolyfillTag(file, json.polyfill);
}
// Optionally opt into shadow dom, rather than shady dom.
if (json.useShadowDom) {
addShadowDomConfig(file);
}
// Add imports if defined.
if (process.env.VULCANIZE && _.isArray(json.imports)) {
log("Importing vulcanized file...");
vulcanizeImports(file, json.imports);
} else {
log("Importing individual files...");
individualImports(file, json.imports);
}
});
};
Plugin.registerCompiler({
extensions: ["vulcanize"],
filenames: ["config"],
archMatching: 'web',
}, function(){
return new VulcanizeCompiler();
});