-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreatePage.js
More file actions
138 lines (98 loc) · 3.11 KB
/
createPage.js
File metadata and controls
138 lines (98 loc) · 3.11 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
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
import decache from 'decache';
import path from 'path';
import { JSDOM } from 'jsdom';
// This shouldn't be necessary...
//import reefUtils from './reef/utilities';
import importScript from './importScript.js';
import pluginList from './edge-plugins.js';
import cache from './cache.js';
const createDOMFilters = [];
const createPageFilters = [];
if( !! pluginList?.length ) {
for( let pluginConfig of pluginList ) {
const hasOptions = typeof pluginConfig === 'object';
const name = hasOptions ? pluginConfig.name : pluginConfig;
const isLocal = pluginConfig?.local;
const pluginPath = !! isLocal ? `./plugins/${name}/edge-node.js` : `${name}/edge-node.js`;
const plugin = require( pluginPath );
if( !! plugin.onCreateDOM ) {
createDOMFilters.push( plugin.onCreateDOM );
}
if( !! plugin.onCreatePage ) {
createPageFilters.push( plugin.onCreatePage );
}
}
}
const onCreateDOM = async( { html, templateFile } ) => {
if( ! createDOMFilters?.length ) {
console.log( 'No DOM filters' );
return html;
}
let parsedHTML = html;
console.log( 'Ready to start onCreateDOM' );
if( !! createDOMFilters.length ) {
for( let createDOMFilter of createDOMFilters ) {
parsedHTML = await createDOMFilter( { html: parsedHTML, templateFile } );
}
}
return parsedHTML;
}
const onCreatePage = async( { dom, html, templateFile } ) => {
if( process.env.NODE_ENV == 'development' ) {
const diffScripts = await importScript( './reef/dom.js', [ 'diff' ] );
const head = dom.window.document.querySelector( 'head' );
//const watcher = Watcher();
head.insertAdjacentHTML( 'beforeend',`
<script>
${diffScripts}
const templateFile = '${templateFile}';
const events = new EventSource( '//localhost:3000/__watch?template=${templateFile}' );
events.onmessage = function( e ) {
if( ! e.data ) {
return;
}
const data = JSON.parse( e.data );
const { newHTML } = data;
const parser = new DOMParser();
const newDoc = parser.parseFromString( newHTML, 'text/html' );
diff( newDoc, document );
}
</script>
` );
}
console.log( { createPageFilters } );
if( !! createPageFilters.length ) {
for( let createPageFilter of createPageFilters ) {
console.log( { createPageFilter } );
await createPageFilter( { dom, templateFile } );
}
}
return { dom };
}
// const Watcher = () => {
//
// return(
// <script>
// const events = new EventSource( '//localhost:3000/__watch' );
// events.onmessage = function( e ) {
// console.log( e );
// }
// </script>
// )
// }
export default async function createPage( {
templateFile,
component,
}) {
let page = component();
page = `${page}`;
page = await onCreateDOM( { html: page, templateFile } );
const dom = new JSDOM( page );
await onCreatePage( { dom, templateFile } );
const html = dom.serialize();
if( process.env.NODE_ENV == 'development' ) {
cache.set( 'html', html );
decache( templateFile );
}
return html;
}