-
Notifications
You must be signed in to change notification settings - Fork 0
/
webc.js
267 lines (214 loc) · 5.57 KB
/
webc.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
import fs from "fs";
import fastglob from "fast-glob";
import path from "path";
import { parse } from "parse5";
import { Path } from "./src/path.js";
import { AstSerializer } from "./src/ast.js";
import { ModuleScript } from "./src/moduleScript.cjs";
class AstCache {
constructor() {
this.ast = {};
}
get(contents) {
if(!this.ast[contents]) {
this.ast[contents] = parse(contents, {
scriptingEnabled: true,
sourceCodeLocationInfo: true,
});
}
return this.ast[contents];
}
}
const localAstCache = new AstCache();
class WebC {
constructor(options = {}) {
let { file, input } = options;
this.customTransforms = {};
this.customHelpers = {};
this.globalComponents = {};
this.astOptions = {};
this.bundlerMode = false;
this.ignores = options.ignores || [];
if(input || input === "") {
this.rawInput = input;
}
if(file) {
this.setInputPath(file);
}
}
setInputPath(file) {
file = Path.normalizePath(file);
this.filePath = file;
this.astOptions.filePath = file;
}
setContent(input, filePath) {
this.rawInput = input;
if(filePath) {
this.astOptions.filePath = filePath;
}
}
getRenderingMode(content) {
if(!content.startsWith("<!doctype") && !content.startsWith("<!DOCTYPE") && !content.startsWith("<html")) {
return "component";
}
return "page";
}
_getRawContent() {
if(this.rawInput || this.rawInput === "") {
return this.rawInput;
} else if(this.filePath) {
if(!this._cachedContent) {
this._cachedContent = fs.readFileSync(this.filePath, {
encoding: "utf8"
});
}
return this._cachedContent;
} else {
throw new Error("Missing a setInput or setInputPath method call to set the input.");
}
}
getContent() {
let content = this._getRawContent();
let mode = this.getRenderingMode(content);
// prepend for no-quirks mode on components or implicit page rendering modes (starts with <html>)
if(mode === "component" || !content.startsWith("<!doctype ") && !content.startsWith("<!DOCTYPE")) {
content = `<!doctype html>${content}`;
}
return {
content,
mode,
};
}
static async getASTFromString(string) {
let wc = new WebC({
input: string
});
let { content } = wc.getContent();
return wc.getAST(content);
}
// @deprecated for getFromFilePath
static async getASTFromFilePath(filePath) {
let wc = new WebC({
file: filePath
});
let { content } = wc.getContent();
return wc.getAST(content);
}
static async getFromFilePath(filePath) {
let wc = new WebC({
file: filePath
});
let { content, mode } = wc.getContent();
return {
content,
ast: await wc.getAST(content),
mode,
};
}
getAST(content) {
if(!content) {
throw new Error("WebC.getAST() expects a content argument.");
}
return localAstCache.get(content);
}
setTransform(key, callback) {
this.customTransforms[key] = callback;
}
setHelper(key, callback) {
this.customHelpers[key] = callback;
}
setAlias(key, folder) {
if(!this.aliases) {
this.aliases = {};
}
this.aliases[key] = folder;
}
async _defineComponentsObject(obj = {}) {
for(let name in obj) {
let file = obj[name];
if(this.globalComponents[name]) {
throw new Error(`Global component name collision on "${name}" between: ${this.globalComponents[name]} and ${file}`)
}
this.globalComponents[name] = file;
}
}
static getComponentsMap(globOrObject, ignores = []) {
if(typeof globOrObject === "string" || Array.isArray(globOrObject)) {
let files = globOrObject;
if(typeof globOrObject === "string") {
files = fastglob.sync(globOrObject, {
ignore: ignores,
caseSensitiveMatch: false,
dot: false,
});
}
let obj = {};
for(let file of files) {
let {name} = path.parse(file);
if(obj[name]) {
throw new Error(`Global component name collision on "${name}" between: ${obj[name]} and ${file}`)
}
obj[name] = file;
}
return obj;
}
return globOrObject;
}
defineComponents(globOrObject) {
this._defineComponentsObject(WebC.getComponentsMap(globOrObject, this.ignores));
}
setUidFunction(fn) {
this.uidFn = fn;
}
async setup(options = {}) {
let { content, mode } = this.getContent();
let rawAst = this.getAST(content);
let ast = new AstSerializer(this.astOptions);
ast.setBundlerMode(this.bundlerMode);
ast.setMode(mode);
ast.setContent(content);
ast.setData(options.data);
if(this.aliases && Object.keys(this.aliases).length) {
ast.setAliases(this.aliases);
}
if(this.uidFn) {
ast.setUidFunction(this.uidFn);
}
for(let name in this.customTransforms) {
ast.setTransform(name, this.customTransforms[name]);
}
for(let name in this.customHelpers) {
ast.setHelper(name, this.customHelpers[name]);
}
await ast.setComponentsByFilePath(this.globalComponents);
await ast.setComponentsByFilePath(options.components);
return {
ast: rawAst,
serializer: ast,
};
}
getComponents(setup) {
let { ast, serializer } = setup;
let obj = serializer.getComponentList(ast);
return Object.keys(obj);
}
setBundlerMode(mode) {
this.bundlerMode = !!mode;
}
async stream(options = {}) {
let { ast, serializer } = await this.setup(options);
serializer.streams.start();
serializer.compile(ast, options.slots).catch(() => {
// Node requires this to avoid unhandled rejection errors (yes, even with `finally`)
serializer.streams.end();
}).finally(() => {
serializer.streams.end();
});
return serializer.streams.get();
}
async compile(options = {}) {
let { ast, serializer } = await this.setup(options);
return serializer.compile(ast, options.slots);
}
}
export { WebC, ModuleScript };