-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
70 lines (59 loc) · 2.07 KB
/
index.js
File metadata and controls
70 lines (59 loc) · 2.07 KB
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
/**
* Build HTML custom elements from html files
* @author Dan McCarthy
*/
const fs = require("fs").promises;
const path = require("path");
/**
* Creates a class definition for a given HTML component
*
* @param {string} name Name used when
* @param {string} component
* @param {Boolean} keepElements
* @returns {string} The class definition for a given HTML component
*/
function buildClass(name, component, keepElements) {
// keepElements determines if the custom element remains in the HTML
return `\n\nclass ${name.replace("-", "")} extends HTMLElement {
constructor() {
super();
}
connectedCallback() {
this.innerHTML = \`${component}\`
${keepElements ? "" : "this.replaceWith(this.children[0]);"}
}
}
customElements.define("${name}", ${name.replace("-", "")});`;
}
/**
* This is the main loader used to add custom elements into the JS, contents
* of the target file are provided by webpack prior to the compilation
*
* @param {string|Buffer} content Content of the resource file (Webpack Loader)
* @returns {string|Buffer} Returns updated content to webpack compiler
*/
async function elementLoader(content) {
// Get options from webpack
const options = this.getOptions();
const source = options.source;
const keepElements = options.keepElements || false;
const callback = this.async();
// Catch any errors and return to callback
try {
// Get array of files in given source dir
let files = await fs.readdir(path.resolve(source));
// Append each files contents as a class in the target js file
for (let i = 0; i < files.length; i++) {
let component = await fs.readFile(
path.resolve(`${source}/${files[i]}`)
);
let name = files[i].split(".")[0];
content += buildClass(name, component, keepElements);
}
// Return altered content through callback
callback(null, content);
} catch (err) {
callback(err);
}
}
module.exports = elementLoader;