@makano/rew 1.2.54 → 1.2.56
Sign up to get free protection for your applications and to get access to all the features.
- package/lib/rew/functions/export.js +1 -1
- package/lib/rew/functions/import.js +3 -1
- package/lib/rew/pkgs/serve.js +33 -10
- package/lib/rew/pkgs/web.js +28 -6
- package/package.json +1 -1
@@ -8,7 +8,9 @@ const jsYaml = require('js-yaml');
|
|
8
8
|
const { execOptions } = require('../const/opt');
|
9
9
|
|
10
10
|
const cachedFiles = [];
|
11
|
-
|
11
|
+
module.exports.cleanCache = () => {
|
12
|
+
while(cachedFiles.length) cachedFiles.pop();
|
13
|
+
};
|
12
14
|
const lookUpInOtherApps = (fullPath) => {
|
13
15
|
const con = conf({});
|
14
16
|
const name = fullPath.indexOf('/') ? fullPath.split('/')[0] : fullPath;
|
package/lib/rew/pkgs/serve.js
CHANGED
@@ -4,6 +4,7 @@ const { IttyRouter, AutoRouter, Router, createResponse, cors, error, StatusError
|
|
4
4
|
const path = require('path');
|
5
5
|
const { run } = require('../main');
|
6
6
|
const { runPath } = require('../modules/runtime');
|
7
|
+
const { cleanCache } = require('../functions/import');
|
7
8
|
module.exports = (context) => {
|
8
9
|
|
9
10
|
// http.createServer((req, res) => {
|
@@ -137,20 +138,24 @@ module.exports = (context) => {
|
|
137
138
|
`;
|
138
139
|
|
139
140
|
const defaultSsrBundlerEntry = (file, layouts, data) => `
|
140
|
-
files = "${layouts.join(',')}
|
141
|
+
files = "${layouts.length ? layouts.join(',')+',' : ''}${file}".split(',')
|
141
142
|
|
142
143
|
renderers = []
|
144
|
+
staticRendering = false
|
143
145
|
|
144
146
|
for key, file of files
|
145
147
|
renderers.push imp file
|
148
|
+
|
149
|
+
staticRendering = true if renderers[renderers.length-1].staticRendering
|
146
150
|
|
147
|
-
render = (data) ->
|
148
|
-
|
151
|
+
render = (req, data) ->
|
152
|
+
target = renderers.pop()
|
153
|
+
page = target.render req, data
|
149
154
|
for renderer in renderers
|
150
|
-
page = renderer.render data, page
|
155
|
+
page = renderer.render req, data, page
|
151
156
|
page
|
152
157
|
|
153
|
-
exports { render }
|
158
|
+
exports { render, staticRendering }
|
154
159
|
`;
|
155
160
|
|
156
161
|
function createFileRouter({
|
@@ -163,7 +168,7 @@ module.exports = (context) => {
|
|
163
168
|
ssrBundlerEntry = defaultSsrBundlerEntry,
|
164
169
|
}) {
|
165
170
|
|
166
|
-
const lookUpFiles = ['route', 'page', 'page.s'
|
171
|
+
const lookUpFiles = ['route', 'page', 'page.s'];
|
167
172
|
|
168
173
|
const params = {};
|
169
174
|
|
@@ -215,14 +220,30 @@ module.exports = (context) => {
|
|
215
220
|
|
216
221
|
async function renderPage(file, basename, req){
|
217
222
|
const page = w.create({ viewportMeta: true });
|
218
|
-
|
223
|
+
let staticRendering = false;
|
224
|
+
if(basename.endsWith('.s')){
|
219
225
|
// SSR is enabled, do only ssr
|
220
226
|
const layouts = findLayoutFiles(file, root, false);
|
221
227
|
const fileContext = runPath(file, { code: ssrBundlerEntry(file, layouts) }).context.module.exports || {};
|
222
228
|
if(typeof fileContext.render !== "function") throw new ReferenceError("Route does not export function render");
|
223
|
-
let pageContent = fileContext.render(req);
|
229
|
+
let pageContent = fileContext.render(req, { page, ...getReqProps(req) });
|
230
|
+
if(fileContext.staticRendering) staticRendering = true;
|
224
231
|
if(!w.isNode(pageContent)) throw new TypeError("Route.render does not return an element");
|
225
|
-
|
232
|
+
if(pageContent?.type?.element == 'head'){
|
233
|
+
page.root.props.children.splice(page.root.props.children.indexOf(page.head), 1);
|
234
|
+
page.head = pageContent;
|
235
|
+
page.root.add(pageContent);
|
236
|
+
} else if(pageContent?.type?.element == 'body'){
|
237
|
+
page.root.props.children.splice(page.root.props.children.indexOf(page.body), 1);
|
238
|
+
page.body = pageContent;
|
239
|
+
page.root.add(pageContent);
|
240
|
+
} else if(pageContent?.type?.element == 'html'){
|
241
|
+
page.root = pageContent;
|
242
|
+
page.body = pageContent.find('body');
|
243
|
+
page.head = pageContent.find('head');
|
244
|
+
} else {
|
245
|
+
page.add(pageContent);
|
246
|
+
}
|
226
247
|
} else {
|
227
248
|
const layouts = findLayoutFiles(file, root, true);
|
228
249
|
const scriptString = await w.bundle(path.join(root, 'bundle.js'), {
|
@@ -230,8 +251,9 @@ module.exports = (context) => {
|
|
230
251
|
code: bundlerEntry(file, layouts, getReqProps(req))
|
231
252
|
});
|
232
253
|
page.script(scriptString);
|
254
|
+
staticRendering = true;
|
233
255
|
}
|
234
|
-
return html(page.render(
|
256
|
+
return html(page.render(staticRendering));
|
235
257
|
}
|
236
258
|
|
237
259
|
async function handleRequest(req, file) {
|
@@ -257,6 +279,7 @@ module.exports = (context) => {
|
|
257
279
|
const url = new URL(req.url);
|
258
280
|
const pathname = basePath ? url.pathname.replace(new RegExp('^'+basePath), '') : url.pathname;
|
259
281
|
const file = lookUp(pathname);
|
282
|
+
cleanCache();
|
260
283
|
|
261
284
|
if (file) {
|
262
285
|
const response = handleRequest(req, file);
|
package/lib/rew/pkgs/web.js
CHANGED
@@ -165,8 +165,8 @@ function addChildren(nest, child) {
|
|
165
165
|
}
|
166
166
|
|
167
167
|
function findChild(nest, prop, value, recurse = false) {
|
168
|
-
let child = nest.props.children.find(element => value ? element.props[prop] == value : element.type.element == prop);
|
169
|
-
if (recurse && !child) return nest.props.children.find(element => findChild(element, prop, value, recurse));
|
168
|
+
let child = nest.props.children.find(element => element instanceof State ? false : value ? element.props[prop] == value : element.type.element == prop);
|
169
|
+
if (recurse && !child) return nest.props.children.find(element => element instanceof State ? false : findChild(element, prop, value, recurse));
|
170
170
|
return child;
|
171
171
|
}
|
172
172
|
|
@@ -210,7 +210,7 @@ function renderToString(element, js = false) {
|
|
210
210
|
return renderToString(type(props))
|
211
211
|
}
|
212
212
|
|
213
|
-
const children = props?.children || []
|
213
|
+
const children = element instanceof TextNode ? [] : props?.children || []
|
214
214
|
const childrenParsed = Array.isArray(children) ? children.map((c) => renderToString(c, js ? 'raw' : false)) : renderToString(children, js ? 'raw' : false)
|
215
215
|
const childrenHTML = Array.isArray(childrenParsed) ? childrenParsed.join('') : childrenParsed;
|
216
216
|
|
@@ -256,11 +256,20 @@ class Page extends Node {
|
|
256
256
|
return this.body.add(...children);
|
257
257
|
}
|
258
258
|
script(scriptString) {
|
259
|
-
if (typeof scriptString == "object" && scriptString.
|
259
|
+
if (typeof scriptString == "object" && scriptString.src)
|
260
260
|
return this.add(createElement('script', { src: scriptString.src }));
|
261
261
|
else
|
262
262
|
return this.add(createElement('script', null, createTextNode(scriptString)));
|
263
263
|
}
|
264
|
+
style(styleString) {
|
265
|
+
if (typeof styleString == "object" && styleString.href)
|
266
|
+
return this.head.add(createElement('link', { href: styleString.href, rel: 'stylesheet' }));
|
267
|
+
else
|
268
|
+
return this.head.add(createElement('style', null, createTextNode(styleString)));
|
269
|
+
}
|
270
|
+
link(rel, href){
|
271
|
+
return this.head.add(createElement('link', { href, rel }));
|
272
|
+
}
|
264
273
|
serializeState() {
|
265
274
|
const states = {};
|
266
275
|
function extractStates(node) {
|
@@ -285,6 +294,19 @@ class Page extends Node {
|
|
285
294
|
const emitter = ${emitter}
|
286
295
|
${State}
|
287
296
|
const states = [];
|
297
|
+
|
298
|
+
function setAttribute(el, key, value){
|
299
|
+
let defVal = value;
|
300
|
+
if(key == 'style'){
|
301
|
+
for(let i in value){
|
302
|
+
const v = value[i];
|
303
|
+
el.style.setProperty(i, value[i]);
|
304
|
+
}
|
305
|
+
return;
|
306
|
+
}
|
307
|
+
el.setAttribute(key, defVal);
|
308
|
+
}
|
309
|
+
|
288
310
|
function rehydrate() {
|
289
311
|
const initialState = __INITIAL_STATE__;
|
290
312
|
|
@@ -306,8 +328,8 @@ class Page extends Node {
|
|
306
328
|
Object.keys(node.props).forEach(key => {
|
307
329
|
if (key !== 'children') {
|
308
330
|
if(key in nodeState){
|
309
|
-
|
310
|
-
} else
|
331
|
+
setAttribute(elt, key, getState(nodeState[key].id)?.value ?? nodeState[key]._value);
|
332
|
+
} else setAttribute(elt, key, node.props[key]);
|
311
333
|
}
|
312
334
|
});
|
313
335
|
if('data-only-if' in node.props){
|