This repository has been archived by the owner on Mar 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathwcs.js
179 lines (174 loc) · 5.97 KB
/
wcs.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* @license
* Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
* This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
* The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
* The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
* Code distributed by Google as part of the polymer project is also
* subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
*/
// jshint node:true
'use strict';
var hydrolysis = require('hydrolysis');
var mkdirp = require('mkdirp');
var url = require('url');
var Vulcan = require('vulcanize');
var fs = require('fs');
var Promise = require('es6-promise').Promise;
var path = require('path');
var WebComponentShards = function WebComponentShards(options){
this.root = options.root;
this.endpoints = options.endpoints;
this.bowerdir = options.bowerdir;
this.shared_import = options.shared_import;
this.sharing_threshold = options.sharing_threshold;
this.dest_dir = path.join(path.resolve(options.dest_dir), "/");
this.workdir = options.workdir;
this.built = false;
};
WebComponentShards.prototype = {
_getOptions: function() {
var options = {};
options.attachAST = true;
options.filter = function(){
return false;
};
options.redirect = this.bowerdir;
options.root = this.root;
return options;
},
_getFSResolver: function() {
return new hydrolysis.FSResolver(this._getOptions());
},
_getAnalyzer: function(endpoint) {
return hydrolysis.Analyzer.analyze(endpoint, this._getOptions());
},
_getDeps: function _getDeps(endpoint) {
return this._getAnalyzer(endpoint).then(function(analyzer){
return analyzer._getDependencies(endpoint);
}).catch(function(err){
console.log(err);
console.log("FAILED IN GETDEPS");
});
},
_getCommonDeps: function _getCommonDeps() {
var endpointDeps = [];
for (var i = 0; i < this.endpoints.length; i++) {
endpointDeps.push(this._getDeps(this.endpoints[i]));
}
return Promise.all(endpointDeps).then(function(allEndpointDeps){
var common = {};
allEndpointDeps.forEach(function(endpointDepList){
endpointDepList.forEach(function(dep){
if (!common[dep]) {
common[dep] = 1;
} else {
common[dep] += 1;
}
});
});
var depsOverThreshold = [];
for (var dep in common) {
if (common[dep] >= this.sharing_threshold) {
depsOverThreshold.push(dep);
}
}
return depsOverThreshold;
}.bind(this));
},
_synthesizeImport: function _synthesizeImport() {
return this._getCommonDeps().then(function(commonDeps) {
/** Generate the file of shared imports. */
var output = '';
var workdirPath = url.resolve(this.root, this.workdir);
var outputPath = url.resolve(workdirPath, this.shared_import);
/**
* If the shared import is in a subdirectory, it needs to have a properly adjusted
* base directory.
*/
var baseUrl = path.relative(outputPath, workdirPath);
for (var dep in commonDeps) {
output += '<link rel="import" href="' + baseUrl + '/' + commonDeps[dep] + '">\n';
}
var outDir = path.dirname(outputPath);
mkdirp.sync(outDir);
var fd = fs.openSync(outputPath, 'w');
fs.writeSync(fd, output);
return commonDeps;
}.bind(this));
},
_prepOutput: function _prepOutput() {
mkdirp.sync(this.dest_dir);
},
build: function build() {
if (this.built) {
throw new Error("build may only be called once.");
}
this.built = true;
this._prepOutput();
return this._synthesizeImport().then(function (commonDeps) {
var endpointsVulcanized = [];
// Vulcanize each endpoint
this.endpoints.forEach(function(endpoint){
var outPath = url.resolve(this.dest_dir, endpoint);
var outDir = path.dirname(outPath);
var pathToShared = path.relative(outDir, url.resolve(this.dest_dir, this.shared_import));
var oneEndpointDone = new Promise(function(resolve, reject) {
var vulcan = new Vulcan({
abspath: null,
fsResolver: this._getFSResolver(),
addedImports: [pathToShared],
stripExcludes: commonDeps,
inlineScripts: true,
inlineCss: true,
inputUrl: endpoint
});
try {
vulcan.process(null, function(err, doc) {
if (err) {
reject(err);
} else {
mkdirp.sync(outDir);
var fd = fs.openSync(outPath, 'w');
fs.writeSync(fd, doc);
resolve(outPath);
}
}.bind(this));
} catch (err) {
console.error("Error writing output file!");
reject(err);
}
}.bind(this));
endpointsVulcanized.push(oneEndpointDone);
}.bind(this));
var sharedEndpointDone = new Promise(function(resolve, reject) {
var vulcan = new Vulcan({
fsResolver: this._getFSResolver(),
inlineScripts: true,
inlineCss: true,
inputUrl: url.resolve(this.workdir, this.shared_import)
});
try {
vulcan.process(null, function(err, doc) {
if (err) {
reject(err);
} else {
var outPath = url.resolve(this.dest_dir, this.shared_import);
var outDir = path.dirname(outPath);
mkdirp.sync(outDir);
var fd = fs.openSync(outPath, 'w');
fs.writeSync(fd, doc);
resolve(outPath);
}
}.bind(this));
} catch (err) {
reject(err);
}
}.bind(this));
endpointsVulcanized.push(sharedEndpointDone);
return Promise.all(endpointsVulcanized);
// Vulcanize the shared dep.
}.bind(this));
}
};
module.exports = WebComponentShards;