-
Notifications
You must be signed in to change notification settings - Fork 50
/
gulpfile.cjs
155 lines (147 loc) · 4.8 KB
/
gulpfile.cjs
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
const gulp = require("gulp");
const terser = require("gulp-terser");
const { glob } = require("glob");
const fs = require("fs");
const path = require("path");
// mirror version numbers
gulp.task(
"version-match", async () => {
const packageVm = require("./node_modules/@haxtheweb/haxcms-elements/package.json");
fs.writeFileSync('./VERSION.txt', packageVm.version , {encoding:'utf8',flag:'w'});
console.log(`${packageVm.version} written to VERSION.txt`);
}
);
gulp.task(
"terser", () => {
// now work on all the other files
return gulp.src([
'./build/es6/**/*.js'
]).pipe(terser({
ecma: 2018,
keep_fnames: true,
mangle: true,
module: true,
}))
.pipe(gulp.dest('./build/es6/'));
}
);
// https://html.spec.whatwg.org/multipage/scripting.html#valid-custom-element-name
const reservedNames = new Set([
'annotation-xml',
'color-profile',
'font-face',
'font-face-src',
'font-face-uri',
'font-face-format',
'font-face-name',
'missing-glyph'
]);
function hasError(name) {
if (!name) {
return 'Missing element name.';
}
if (/[A-Z]/.test(name)) {
return 'Custom element names must not contain uppercase ASCII characters.';
}
if (!name.includes('-')) {
return 'Custom element names must contain a hyphen. Example: unicorn-cake';
}
if (/^\d/i.test(name)) {
return 'Custom element names must not start with a digit.';
}
if (/^-/i.test(name)) {
return 'Custom element names must not start with a hyphen.';
}
if (reservedNames.has(name)) {
return 'The supplied element name is reserved and can\'t be used.\nSee: https://html.spec.whatwg.org/multipage/scripting.html#valid-custom-element-name';
}
}
function validateElementName(name) {
const errorMessage = hasError(name);
return !errorMessage;
}
gulp.task("wc-autoloader", async () => {
glob(path.join("./build/es6/node_modules/**/*.js"), (er, files) => {
let elements = {};
// async loop over files
files.forEach((file) => {
// grab the name of the file
if (fs.existsSync(file)) {
let fLocation = file.replace("build/es6/node_modules/", "");
const contents = fs.readFileSync(file, "utf8");
// This Regex is looking for tags that are defined by string values
// this will work for customElements.define("local-time",s))
// This will NOT work for customElements.define(LocalTime.tagName,s))
const defineStatements = /customElements\.define\(["|'|`](.*?)["|'|`]/gm.exec(
contents
);
// basic
if (defineStatements && validateElementName(defineStatements[1])) {
elements[defineStatements[1]] = fLocation;
}
// .tag calls
else {
const hasDefine = /customElements\.define\((.*?),(.*?)\)/gm.exec(
contents
);
// check for a define still
if (hasDefine && hasDefine[1] && hasDefine[1].includes('.tag')) {
const tagStatements = /static get tag\(\){return"(.*?)"}/gm.exec(
contents
);
if (tagStatements && validateElementName(tagStatements[1])) {
elements[tagStatements[1]] = fLocation;
}
}
else if (hasDefine && hasDefine[1] && hasDefine[1].includes('.is')) {
const tagStatements = /static get is\(\){return"(.*?)"}/gm.exec(
contents
);
if (tagStatements && validateElementName(tagStatements[1])) {
elements[tagStatements[1]] = fLocation;
}
}
else {
if (!hasDefine) {
// support for polymer legacy class housing
const PolymerLegacy = /is\:\"(.*?)\"/gm.exec(
contents
);
if (PolymerLegacy && PolymerLegacy[1] && validateElementName(PolymerLegacy[1])) {
elements[PolymerLegacy[1]] = fLocation;
}
else {
// if we got here, it wasn't a file w/ a custom element definition
// so it's not an entry point
}
}
}
}
}
});
// write entries to file
fs.writeFileSync(
path.join(__dirname, "wc-registry.json"),
JSON.stringify(elements),
{encoding:'utf8',flag:'w'}
);
// write entries to demo for local work
fs.writeFileSync(
"./dist/wc-registry.json",
JSON.stringify(elements),
{encoding:'utf8',flag:'w'}
);
// write entries to demo for local work
fs.writeFileSync(
"../webcomponents/elements/haxcms-elements/demo/wc-registry.json",
JSON.stringify(elements),
{encoding:'utf8',flag:'w'}
);
// write entries for other things to leverage
fs.writeFileSync(
"../webcomponents/wc-registry.json",
JSON.stringify(elements),
{encoding:'utf8',flag:'w'}
);
});
});