This repository has been archived by the owner on Nov 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpolymer-imports.js
423 lines (372 loc) · 15 KB
/
polymer-imports.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#!/usr/bin/env node
const fs = require('fs'),
resolve = require('path').resolve
const filePath = resolve(process.argv[2]);
const outputPath = filePath;
if (!filePath) {
const msg = "polymer-imports require first argument to be a file path to the file for inspection, got: " + filePath;
throw new Error(msg)
}
//stop here if not .html
if (!hasExtension(filePath, "html")) {
console.log("Not a html file, exiting")
return;
}
const parse5 = require("parse5");
const treeAdapter = parse5.treeAdapters.default;
const separator = '/';
var isDemo = false;
const config = {
rootDir: '',
bowerFolderName: "bower_components",
ignoredFolders: ["test", "demo"],
ignoredComponents: [],
attributes: [
"effects",
"entry-animation",
"exit-animation"
],
resolve: {
"iron-flex": "iron-flex-layout-classes",
"iron-flex-reverse": "iron-flex-layout-classes",
"iron-flex-alignment": "iron-flex-layout-classes",
"iron-flex-factors": "iron-flex-layout-classes",
"iron-positioning": "iron-flex-layout-classes"
}
}
const rootDir = findRootDir(filePath, parseOptionalArguments(process.argv[3], process.argv[4]));
//calculate prefix for relative path
var relativePrefix = '';
for (var i = 0; i < filePath.substr(rootDir.length, 999).split(separator).length - 2; i++) {
relativePrefix += ".." + separator;
}
const file = fs.readFileSync(filePath, 'utf8');
const isFragment = file.indexOf("<html") === -1;
const d = isFragment ? parse5.parseFragment(file) : parse5.parse(file);
//aggregator object
const allNodeNames = isFragment ? {'polymer': ''} : {};
collectAllWebComponentsTagNames(d, allNodeNames);
resolveResources(rootDir, allNodeNames, true);
var sortedImports = sortResources(allNodeNames);
//aggregator object is full with all information we need
const tagsToRemove = [];
const oldImportsMap = findOldImportsAndCreateImportsNode(d);
var nodeToInsertBefore;
sortedImports.forEach(function (importSection) {
if (importSection.name) {
addComment(importSection.name + ' elements', nodeToInsertBefore);
}
importSection.imports.forEach(function (importUrl, i) {
var attrs = oldImportsMap[importUrl] ? oldImportsMap[importUrl] :
[{
name: 'rel',
value: 'import'
}, {
name: 'href',
value: importUrl
}];
addLink(attrs, importSection.imports.length !== i + 1, nodeToInsertBefore)
delete oldImportsMap[importUrl];
});
});
//Add unused imports
var keys = Object.keys(oldImportsMap);
if (keys.length) {
addComment('UNUSED DIRECTLY', nodeToInsertBefore);
keys.forEach(function (key, i) {
var attrs = oldImportsMap[key];
addLink(attrs, keys.length !== i + 1, nodeToInsertBefore)
})
}
//Remove old imports
tagsToRemove.forEach(function (el) {
treeAdapter.detachNode(el);
})
fs.writeFileSync(outputPath, parse5.serialize(d, {booleanAttributes: true}));
//Methods
function addLink(attrs, addLineBreak, nodeToInsertBefore) {
var importNode = treeAdapter.createElement('link', 'http://www.w3.org/1999/xhtml', attrs);
treeAdapter.insertBefore(nodeToInsertBefore.parentNode, importNode, nodeToInsertBefore);
treeAdapter.insertTextBefore(nodeToInsertBefore.parentNode, '\n', nodeToInsertBefore);
}
function addComment(text, nodeToInsertBefore) {
var comment = treeAdapter.createCommentNode(text);
treeAdapter.insertTextBefore(nodeToInsertBefore.parentNode, '\n', nodeToInsertBefore)
treeAdapter.insertBefore(nodeToInsertBefore.parentNode, comment, nodeToInsertBefore);
treeAdapter.insertTextBefore(nodeToInsertBefore.parentNode, '\n', nodeToInsertBefore);
}
function parseOptionalArguments(arg1, arg2) {
var rootDir;
[arg1, arg2].forEach(function (arg) {
if (arg && hasExtension(arg, "json") && fs.lstatSync(arg).isFile()) {
try {
const customConfig = JSON.parse(fs.readFileSync(arg, 'utf8'));
for (var prop in customConfig) {
config[prop] = customConfig[prop] || [];
}
} catch (e) {
console.error("Tried to parse config file but got a error. Ignoring. Error: " + e)
}
} else if (arg && fs.lstatSync(arg).isDirectory()) {
rootDir = arg
}
})
return rootDir || config.rootDir;
}
function getElementNameFromImportPath(path) {
return path.replace(".html", "").replace(/.*\//g, "");
}
function findOldImportsAndCreateImportsNode(d) {
var importsMap = {};
var headTag;
function findAndRemoveImports(doc) {
var arr = doc.childNodes || [];
const likeBreaksToRemove = [];
if (doc.tagName === "head") {
headTag = doc;
}
arr.forEach(function (el, i) {
if (el.tagName === "link") {
var isImport;
var attrMap = {};
el.attrs.forEach(function (attr) {
if (attr.name === "rel") {
isImport = attr.value.toLowerCase() === "import";
}
attrMap[attr.name] = attr.value;
});
} else if (treeAdapter.isCommentNode(el) && (el.data.match(' elements') || el.data.match('UNUSED DIRECTLY'))) {
tagsToRemove.push(el);
if (arr[i - 1] && treeAdapter.isTextNode(arr[i - 1]) && arr[i - 1].value.match(/\s/g)) {
likeBreaksToRemove.push(arr[i - 1]);
}
if (arr[i + 1] && treeAdapter.isTextNode(arr[i + 1]) && arr[i + 1].value.match(/\s/g)) {
likeBreaksToRemove.push(arr[i + 1]);
}
}
if (isImport && attrMap["href"]) {
importsMap[attrMap["href"]] = el.attrs;
tagsToRemove.push(el);
if (arr[i + 1] && treeAdapter.isTextNode(arr[i + 1]) && arr[i + 1].value.match(/\s/g)) {
likeBreaksToRemove.push(arr[i + 1]);
}
}
if (el.childNodes) {
findAndRemoveImports(el);
}
});
likeBreaksToRemove.forEach(function (el) {
treeAdapter.detachNode(el);
});
}
findAndRemoveImports(d);
if (!isFragment) {
if (tagsToRemove.length) {
nodeToInsertBefore = tagsToRemove[0];
} else if (headTag && headTag.childNodes && headTag.childNodes.length) {
nodeToInsertBefore = headTag.childNodes[0];
} else if (headTag) {
nodeToInsertBefore = treeAdapter.createCommentNode('End Imports');
treeAdapter.appendChild(headTag, nodeToInsertBefore);
}
} else {
nodeToInsertBefore = d.childNodes[0];
}
return importsMap;
}
function collectAllWebComponentsTagNames(documentFragment, aggregatorObject) {
var nodes = documentFragment.childNodes || [];
var contentNodes = documentFragment.content ? documentFragment.content.childNodes || [] : [];
nodes.concat(contentNodes).forEach(function (node) {
var name = node.nodeName;
if (name.indexOf("dom-") === -1 &&
name.indexOf("-") !== -1 &&
config.ignoredComponents.indexOf(name) === -1 && !node.attrs.find(function (attr) {
return attr.name === "noimport";
})) {
checkAttributes(node.attrs, aggregatorObject);
aggregatorObject[config.resolve[name] || name] = '';
} else if (name === "style") {
node.attrs.forEach(function (attr) {
if ((attr.name === "effects" || attr.name === "include") && attr.value) {
var includesArray = attr.value.split(" ");
includesArray.forEach(function (includeVal) {
aggregatorObject[config.resolve[includeVal] || includeVal] = '';
})
}
})
}
collectAllWebComponentsTagNames(node, aggregatorObject);
});
//if script tag, check if it has Polymer({ inside
if (documentFragment.nodeName === "#text" && documentFragment.parentNode.nodeName === "script") {
checkPolymerObject(documentFragment.value, aggregatorObject);
}
}
function getParentDir(path) {
return path.substr(0, path.lastIndexOf(separator));
}
function findRootDir(filePath, fromConfig) {
//search for demo folder
var possibleDemoPath = filePath.replace(new RegExp(separator + "demo" + separator + ".*"), "");
if (possibleDemoPath !== filePath && fs.readdirSync(possibleDemoPath).indexOf(config.bowerFolderName) !== -1) {
isDemo = true;
return fromConfig || possibleDemoPath;
}
if (fromConfig) {
return fromConfig;
}
var parentDir = getParentDir(filePath);
while (parentDir.indexOf(separator) !== -1) {
var files = fs.readdirSync(parentDir);
if (files.indexOf('polymer.json') !== -1) {
return parentDir;
}
parentDir = getParentDir(parentDir);
}
var err = "No polymer.json file found. Can't find root directory for polymer project. File path: ".concat(filePath)
throw new Error(err)
}
function resolveResources(dir, nodesObject, checkBowerComponentsFolder) {
fs.readdirSync(dir).forEach(function (content) {
var path = dir + separator + content;
if (content.indexOf(".") !== 0 && fs.lstatSync(path).isDirectory() &&
config.ignoredFolders.indexOf(content) === -1 &&
(checkBowerComponentsFolder || content !== config.bowerFolderName)) {
resolveResources(path, nodesObject)
} else if (hasExtension(content, "html") && nodesObject.hasOwnProperty(getElementNameFromImportPath(content))) {
var relativePathToRoot = relativePrefix + path.substr(rootDir.length + 1, 999);
if (isDemo) {
relativePathToRoot = relativePathToRoot.replace(config.bowerFolderName, "..")
}
nodesObject[content.substr(0, content.length - 5)] = relativePathToRoot;
}
});
}
function checkAttributes(attrs, aggregatorObject) {
attrs.forEach(function (attr) {
//check icons
if (attr.name == "icon" && attr.value && !isImageUrl(attr.value)) {
var split = attr.value.split(":")
if (split.length == 1 || split[0] === "icons") {
aggregatorObject["iron-icons"] = '';
} else {
var name = split[0];
if (name.indexOf('-') === -1) {
name += "-icons"
}
aggregatorObject[config.resolve[name] || name] = '';
}
} //check other attributes
else if (attr.value && config.attributes.indexOf(attr.name) !== -1) {
var valueArray = attr.value.split(" ");
valueArray.forEach(function (name) {
aggregatorObject[config.resolve[name] || name] = '';
});
}
})
}
function checkPolymerObject(string, aggregatorObject) {
var trim = string.trim();
if (trim.match(/Polymer\(/gm)) {
//check behaviors
var behaviorsDraftArr = trim.match(/behaviors\s*:\s*\[[^\]]*/g);
if (behaviorsDraftArr && behaviorsDraftArr.length) {
var behaviorsDraft = behaviorsDraftArr[0];
behaviorsDraft = behaviorsDraft + '"]';
behaviorsDraft = behaviorsDraft.replace(",", '","').replace(/behaviors\s*:\s*\[/, '["').replace(/\s*/g, "");
var behaviors = JSON.parse(behaviorsDraft);
behaviors.forEach(function (behavior) {
behavior = behavior.replace(',', "").trim();
if (behavior.length) {
var name = behavior.split(".");
name = name[name.length - 1].replace(/,/g, "");
aggregatorObject[decamelcase(name)] = '';
}
})
}
//check animations
try {
var polymerObj
function Polymer(obj) {
polymerObj = obj;
}
//hacky part. Before eval we need to replace all 'deep' calls to avoid ReferenceError
//get all problem calls
const problemCalls = trim.match(/(\s|,|:)\w*\.(\$|\w|\.)*/g);
var FAKE_SCOPE = {};
problemCalls.forEach(function (pc) {
const fakeCall = pc.replace(/\w*\.(\$|\w|\.)*/, "FAKE_SCOPE");
trim = trim.replace(pc, fakeCall);
});
eval(trim);
if (polymerObj.properties && polymerObj.properties.animationConfig && typeof polymerObj.properties.animationConfig.value === "function") {
var ac = polymerObj.properties.animationConfig.value();
Object.keys(ac).forEach(function (animationKey) {
var animation = ac[animationKey];
if (animation.length) {
animation.forEach(function (a) {
if (a.name) {
aggregatorObject[a.name] = '';
}
})
} else if (animation.name) {
aggregatorObject[animation.name] = '';
}
})
}
} catch (e) {
console.error("Can not parse js part of polymer element. Most probably because there is something else in 'script' tag, " +
"try to put third party code into different script tags.", e)
}
}
}
function sortResources(importsObject) {
const keys = Object.keys(importsObject);
const arr = [];
const classificationMap = {};
keys.forEach(function (key) {
if (key === "polymer") {
arr.unshift({
name: null,
imports: [
importsObject[key]
]
})
} else if (importsObject[key]) {
var firstName = extractElementsName(importsObject[key]);
firstName = firstName.charAt(0).toUpperCase() + firstName.slice(1);
if (!classificationMap[firstName]) {
classificationMap[firstName] = []
}
classificationMap[firstName].push(importsObject[key])
}
});
Object.keys(classificationMap).forEach(function (key) {
arr.push({
name: key,
imports: classificationMap[key]
})
})
return arr;
}
function extractElementsName(path) {
var arr = path.split(separator);
var lastIndexOfWebComponentName = arr.length - 1;
for (var i = arr.length - 1; i !== -1; i--) {
if (arr[i].indexOf("-") !== -1) {
lastIndexOfWebComponentName = i;
}
}
return arr[lastIndexOfWebComponentName].replace(/-.*/, '');
}
function isImageUrl(value) {
return !!value.match(/(\/|\.)/)
}
function decamelcase(string) {
return string.match(/([A-Z]?[^A-Z]*)/g).slice(0, -1).join('-').toLowerCase();
}
function hasExtension(filePath, extension) {
var extensionWithDot = '.' + extension;
return filePath.lastIndexOf(extensionWithDot) === filePath.length - extensionWithDot.length
}