-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
406 lines (319 loc) · 10.8 KB
/
index.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
/** @type {Repl.IComponentConfig[]} */
const componentsConfig = [
{
name: 'MyComponent',
type: 'svelte'
},
{
name: 'YourComponent',
type: 'svelte'
}
];
init();
// .catch(e => console.log(e));
let components;
async function init () {
const editor = setupEditor();
// Grab the component contents... Keep them available to save another load.
// @todo: Will need to keep a cache.
components = await loadComponentSourceFiles(componentsConfig);
const bundle = await build(editor.getValue());
// initial render
render(bundle);
}
// /////////////////////////////////////////////////////////////////////////////////////////////////
// /////////////////////////////////////////////////////////////////////////////////////////////////
// /////////////////////////////////////////////////////////////////////////////////////////////////
/**
*
* @param {string} markup The html string from the editor
* @returns {Promise<Repl.IBundle>} The bundle of view code for the preview.
*/
async function build(markup = '') {
const componentsAndApp = components.concat({
name: 'App',
type: 'svelte',
source: createAppTemplate(markup, components)
});
// ...compile the contents to js using the svelte compiler.
const compiled = compileComponents(componentsAndApp);
const bundle = await createBundle(compiled);
return bundle;
}
/**
* Prints out svelte compiler warnings.
*
* @param {Repl.ICompiledComponent} component Svelte compiler output for a component.
* @returns {string}
*/
function printCompilerWarnings(component) {
const w = component && component.warnings && component.warnings.length;
if (!w) {
return;
}
console.group(`${component.name}.${component.type}: ${w} ${w === 1 ? 'warning' : 'warnings'}`);
component.warnings.forEach(warning => {
console.warn(warning.message);
console.log(warning.frame);
});
console.groupEnd();
}
function createAppTemplate(markup, components) {
// NOTE: we replace //imports//, **markup** and [components]
const template = `**markup**
<script>
//imports//
export default {
components: {
[components]
}
};
</script>`;
// If the component name cannot be found, we probably don't need it.
const componentsFoundInMarkup = components
.filter(component => markup.includes(component.name));
// Create es import formatters
const imports = componentsFoundInMarkup.map(component => {
return `import ${component.name} from './${component.name}';\n`;
}).join('');
// Format the list of components for the svelte `components` property
const componentNames = componentsFoundInMarkup
.map(component => `${component.name}`)
.join(',\n');
return template
.replace('**markup**', markup)
.replace('//imports//', imports)
.replace('[components]', componentNames);
}
/**
* Fetches the raw file contents and attaches it to the component.source property.
* For now we store them in the components folder. If you supply a `path` it should
* contain the filename in the path.
*
* @param {Repl.IComponentConfig[]} components
* @returns {Promise<Repl.IComponentConfig[]>}
*/
async function loadComponentSourceFiles(components) {
const requests = await components.map(loadComponentSource);
return Promise.all(requests);
}
/**
* Fetches the raw file contents and attaches it to the component.source property.
*
* @param {Repl.IComponentConfig} component
* @returns {Promise<Repl.IComponentConfig>}
*/
async function loadComponentSource(component) {
const path = component.path || `./components/${component.name}.${component.type}`;
const source = await fetch(path).then(res => res.text());
return {...component, source};
}
/**
* Compile an array of components.
*
* @param {Repl.IComponentConfig[]} components
* @returns {Repl.ICompiledComponent[]}
*/
function compileComponents(components) {
console.info(`Svelte compiler version %c${svelte.VERSION}`, 'font-weight: bold');
const complilations = components
.filter(component => component.type === 'svelte')
.map((component, index) => {
const compiled = compileComponent(component);
if (compiled) {
printCompilerWarnings(compiled);
}
return {...compiled, ...components[index]};
});
return complilations;
}
/**
* Compile a component with the Svelte compiler.
*
* @param {Repl.IComponentConfig} component
* @returns {Repl.ICompiledComponent}
*/
function compileComponent(component) {
const warnings = [];
const compileOptions = {
cascade: false,
name: component.name,
format: 'es',
filename: component.name + '.svelte',
onwarn: warning => {
warnings.push(warning);
}
};
try {
const {js} = svelte.compile(component.source || '', compileOptions);
return {...js, warnings};
} catch (e) {}
}
function render(bundle) {
/** @type {HTMLIFrameElement} */
const preview = document.querySelector('#preview');
preview.onload = () => {
let markup = `
<App></App>
<script>
${bundle.code}
const target = document.querySelector('App');
new App({target});
console.log('App code running');
</script>`;
preview.contentDocument.body.innerHTML = markup;
// We need to eval the js since it's been injected.
const scriptTags = preview.contentDocument.body.getElementsByTagName('script');
[].forEach.call(scriptTags, scriptTag => {
try {
preview.contentWindow.eval(scriptTag.text);
} catch(err) {
console.warn('eval of scripts failed');
}
});
};
// @TODO: See how to garbage collect eval() scripts.
preview.src = 'about:blank';
}
/**
* Removes original mount element from DOM.
*
* @param {HTMLTextAreaElement} target
* @returns {(HTMLElement) => void}
*/
function mountReplace(target) {
/**
* @param {HTMLElement} elt
* @returns {void}
*/
return elt => {
target.parentNode.replaceChild(elt, target);
};
}
/**
* @param {Repl.ICompiledComponent[]} components
* @returns {Promise<Repl.IBundle>}
*/
async function createBundle(components) {
if (!components || !components.length) {
console.info('no components to bundle');
return;
}
if (components.some(c => !c.code)) {
console.warn('uncompiled components found');
return;
}
// https://rollupjs.org/guide/en#rollup-rollup
const bundler = await rollup.rollup({
input: './App', // relative syntax, no file extension
// Comma-separate list of module IDs to exclude
external: id => id[0] !== '.',
plugins: [{
resolveId(importee, importer) {
if (importee[0] === '.') return importee;
},
load(id) {
const appCode = components.find(component => './' + component.name === id);
return appCode;
}
}],
onwarn(warning) {
if (warning.code === 'MISSING_GLOBAL_NAME') return;
console.warn(warning.message);
}
});
let uid = 1;
const {code, map} = await bundler.generate({
format: 'iife',
name: 'App',
globals: id => `import_${uid++}`,
sourcemap: true
});
// Why is this is a store?
const bundle = {
code,
map,
imports: bundler.imports
};
return bundle;
}
// /////////////////////////////////////////////////////////////////////////////////////////////////
// /////////////////////////////////////////////////////////////////////////////////////////////////
// /////////////////////////////////////////////////////////////////////////////////////////////////
function setupEditor() {
/** @type {HTMLTextAreaElement} */
const textarea = document.querySelector('#editor');
/** @type {string} */
// Default content to render.
textarea.value = `
<h1 class="foobar">HelloWorld</h1>
<p>Here is a component to play with. Goody.</p>
<MyComponent></MyComponent>
<YourComponent></YourComponent>`;
/** @type {CodeMirror.EditorConfiguration} */
const codeMirrorConfig = {
extraKeys: {
'Ctrl-Space': 'autocomplete',
"'<'": completeAfter,
"'/'": completeIfAfterLt,
"' '": completeIfInTag,
"'='": completeIfInTag,
},
lineNumbers: true,
mode: 'text/html',
value: textarea.value,
autoCloseTags: true,
showTrailingSpace: true,
theme: 'ambiance'
};
// ///////////////////////////////////////// HINTING ///////////////////////////////////////////
// We need to provide a list of tags in the autocomplete.
const originalHTMLHint = CodeMirror.hint.html;
// All we really do in this override, is to add our custom components to the
// HTML autocomplete list.
CodeMirror.hint.html = function(cm) {
const componentNames = componentsConfig.map(component => '<' + component.name);
const inner = originalHTMLHint(cm) || {from: cm.getCursor(), to: cm.getCursor(), list: []};
inner.list.unshift.apply(inner.list, componentNames);
return inner;
};
function completeAfter(cm, pred) {
var cur = cm.getCursor();
if (!pred || pred()) {
setTimeout(function() {
if (!cm.state.completionActive) {
cm.showHint({completeSingle: false});
}
}, 100);
}
return CodeMirror.Pass;
}
function completeIfInTag(cm) {
return completeAfter(cm, function() {
const tok = cm.getTokenAt(cm.getCursor());
if (tok.type === 'string' &&
!/['"]/.test(tok.string.charAt(tok.string.length - 1)) || tok.string.length == 1
) {
return false;
}
const inner = CodeMirror.innerMode(cm.getMode(), tok.state).state;
return inner.tagName;
});
}
function completeIfAfterLt(cm) {
return completeAfter(cm, function () {
var cur = cm.getCursor();
return cm.getRange(CodeMirror.Pos(cur.line, cur.ch - 1), cur) === '<';
});
}
/** @type {CodeMirror.Editor} */
const codeMirror = CodeMirror(mountReplace(textarea), codeMirrorConfig);
codeMirror.on('change', _.throttle(async editor => {
const bundle = await build(editor.getValue());
if (bundle) {
return render(bundle);
}
console.warn('Hey, there was no bundle')
}, 500));
return codeMirror;
}