Skip to content

Commit

Permalink
refactor(Root): Clean ssr bin and Add increment timeout of test 'shou…
Browse files Browse the repository at this point in the history
…ld create a page component'
  • Loading branch information
eanorambuena committed Jul 30, 2024
1 parent 6e191a1 commit c78f2af
Show file tree
Hide file tree
Showing 7 changed files with 333 additions and 333 deletions.
78 changes: 39 additions & 39 deletions dist/ssr/bin/index
Original file line number Diff line number Diff line change
@@ -1,61 +1,61 @@
#!/usr/bin/env node

const path = require("path");
const args = require("yargs").argv;
const path = require('path')
const args = require('yargs').argv
const babelConfig = (() => {
try {
return require(path.join(process.cwd(), args.babel));
} catch (e) {}
})();

require("babel-register")(babelConfig);
require("../register");

const render = require("..");
const glob = require("glob");
const fs = require("fs");
const mkdirp = require("mkdirp");

const debug = args.debug;
const full = args.hasOwnProperty("full");
const out = args.out || "static";
const props = args.props;
const rehydrate = args.rehydrate !== false;
const src = args.src || "src/**/*.js";
const suffix = args.suffix || "html";
return require(path.join(process.cwd(), args.babel))
} catch (e) { /* empty */ }
})()

require('babel-register')(babelConfig)
require('../register')

const render = require('..')
const glob = require('glob')
const fs = require('fs')
const mkdirp = require('mkdirp')

const debug = args.debug
const full = args.hasOwnProperty('full')
const out = args.out || 'static'
const props = args.props
const rehydrate = args.rehydrate !== false
const src = args.src || 'src/**/*.js'
const suffix = args.suffix || 'html'

glob(src, (err, files) => {
files.forEach(file => {
const reqd = require(path.resolve(file));
const Page = reqd.default || reqd;
const reqd = require(path.resolve(file))
const Page = reqd.default || reqd

if (typeof Page !== "function") {
throw new Error(`${file} must export a constructor.`);
if (typeof Page !== 'function') {
throw new Error(`${file} must export a constructor.`)
}

const page = new Page();
const page = new Page()

if (full) {
document.body.appendChild(page);
document.body.appendChild(page)
}

if (props) {
Object.assign(page, JSON.parse(props));
Object.assign(page, JSON.parse(props))
}

render(full ? document : page, { debug, rehydrate }).then(rendered => {
const basename = path.basename(file);
const namePart = basename.split(".");
const basename = path.basename(file)
const namePart = basename.split('.')

namePart.pop();
namePart.pop()

const destName = `${namePart.join(".") || "index"}.${suffix}`;
const destPath = path.join(process.cwd(), out);
const destName = `${namePart.join('.') || 'index'}.${suffix}`
const destPath = path.join(process.cwd(), out)
mkdirp(destPath, () => {
fs.writeFile(path.join(destPath, destName), rendered, () => {
console.log(`${file} -> ${path.join(out, destName)}`);
});
});
});
});
});
console.log(`${file} -> ${path.join(out, destName)}`)
})
})
})
})
})
104 changes: 52 additions & 52 deletions dist/ssr/index.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@
const { walk } = require('./register/util')

const shadowRootScript = '<script>function __ssr() {var r,s=document.currentScript,f=s.parentNode;h=f.parentNode;f.removeChild(s);h.removeChild(f);r=h.attachShadow({mode:h.getAttribute(\'mode\')||\'open\'});while(f&&f.firstChild)r.appendChild(f.firstChild);}</script>'
const shadowRootScriptCall = '<script>__ssr()</script>'

const defaultResolver = node => yup => setTimeout(() => yup(node))

function stringify(node) {
let str = ''

if (node.nodeName === '#document') {
node = node.documentElement
str += '<!doctype html>'
}

if (node.nodeName === '#text') {
return node.textContent
}

str += `<${node.localName}${(node.attributes || [])
.map(a => ` ${a.name}="${a.value}"`)
.join('')}>`

if (node.nodeName === 'BODY') {
str += shadowRootScript
}

if (node.shadowRoot) {
str += `<shadowroot>${node.shadowRoot.childNodes
.map(stringify)
.join('')}${shadowRootScriptCall}</shadowroot>`
}

if (node.childNodes) {
str += node.childNodes.map(stringify).join('')
}

str += `</${node.localName}>`

return str
}

const render = (node = document, resolver = defaultResolver) => {
node.connectedCallback && node.connectedCallback()
return new Promise(resolver(node)).then(root => {
const str = stringify(root)
root.disconnectedCallback && root.disconnectedCallback()
return str
})
}

module.exports = render
const { walk } = require('./register/util')

const shadowRootScript = '<script>function __ssr() {var r,s=document.currentScript,f=s.parentNode;h=f.parentNode;f.removeChild(s);h.removeChild(f);r=h.attachShadow({mode:h.getAttribute(\'mode\')||\'open\'});while(f&&f.firstChild)r.appendChild(f.firstChild);}</script>'
const shadowRootScriptCall = '<script>__ssr()</script>'

const defaultResolver = node => yup => setTimeout(() => yup(node))

function stringify(node) {
let str = ''

if (node.nodeName === '#document') {
node = node.documentElement
str += '<!doctype html>'
}

if (node.nodeName === '#text') {
return node.textContent
}

str += `<${node.localName}${(node.attributes || [])
.map(a => ` ${a.name}="${a.value}"`)
.join('')}>`

if (node.nodeName === 'BODY') {
str += shadowRootScript
}

if (node.shadowRoot) {
str += `<shadowroot>${node.shadowRoot.childNodes
.map(stringify)
.join('')}${shadowRootScriptCall}</shadowroot>`
}

if (node.childNodes) {
str += node.childNodes.map(stringify).join('')
}

str += `</${node.localName}>`

return str
}

const render = (node = document, resolver = defaultResolver) => {
node.connectedCallback && node.connectedCallback()
return new Promise(resolver(node)).then(root => {
const str = stringify(root)
root.disconnectedCallback && root.disconnectedCallback()
return str
})
}

module.exports = render
150 changes: 75 additions & 75 deletions dist/ssr/register/util/index.js
Original file line number Diff line number Diff line change
@@ -1,75 +1,75 @@
const fs = require('fs')
const path = require('path')
const vm = require('vm')

function each(node, call) {
if (!node) {
return node
}
if (node.nodeName === '#document-fragment') {
Array.from(node.childNodes).forEach(call)
} else {
call(node)
}
return node
}

function execCode(data, opts = {}) {
const { context, ...args } = opts
return vm.runInNewContext(data, context || window, opts)
}

function execFile(file, opts = {}) {
const filename = path.resolve(path.join(document.ssr.scriptBase, file))
const filedata = fs.readFileSync(filename).toString('utf-8')
return execCode(filedata, { ...opts, ...{ filename } })
}

function expose(name, value) {
return (global[name] = window[name] = value || require(`./${name}`)[name])
}

function find(root, call, opts = {}) {
const tree = document.createTreeWalker(root)

// Since we short-circuit in the loop, we can initialise this to the default
// return value we'd expect if nothing is found.
const list = opts.one ? null : []

while (tree.nextNode()) {
if (call(tree.currentNode)) {
// Short-circuit if only returning one.
if (opts.one) {
return tree.currentNode
}
list.push(tree.currentNode)
}
}

return list
}

function prop(obj, name, opts) {
Object.defineProperty(obj, name, {
...{ configurable: true, enumerable: true },
...opts
})
}

function walk(root, call) {
if (!root) return
const tree = document.createTreeWalker(root)
while (tree.nextNode()) {
call(tree.currentNode, tree)
}
}

module.exports = {
each,
execCode,
execFile,
expose,
find,
prop,
walk
}
const fs = require('fs')
const path = require('path')
const vm = require('vm')

function each(node, call) {
if (!node) {
return node
}
if (node.nodeName === '#document-fragment') {
Array.from(node.childNodes).forEach(call)
} else {
call(node)
}
return node
}

function execCode(data, opts = {}) {
const { context, ...args } = opts
return vm.runInNewContext(data, context || window, opts)
}

function execFile(file, opts = {}) {
const filename = path.resolve(path.join(document.ssr.scriptBase, file))
const filedata = fs.readFileSync(filename).toString('utf-8')
return execCode(filedata, { ...opts, ...{ filename } })
}

function expose(name, value) {
return (global[name] = window[name] = value || require(`./${name}`)[name])
}

function find(root, call, opts = {}) {
const tree = document.createTreeWalker(root)

// Since we short-circuit in the loop, we can initialise this to the default
// return value we'd expect if nothing is found.
const list = opts.one ? null : []

while (tree.nextNode()) {
if (call(tree.currentNode)) {
// Short-circuit if only returning one.
if (opts.one) {
return tree.currentNode
}
list.push(tree.currentNode)
}
}

return list
}

function prop(obj, name, opts) {
Object.defineProperty(obj, name, {
...{ configurable: true, enumerable: true },
...opts
})
}

function walk(root, call) {
if (!root) return
const tree = document.createTreeWalker(root)
while (tree.nextNode()) {
call(tree.currentNode, tree)
}
}

module.exports = {
each,
execCode,
execFile,
expose,
find,
prop,
walk
}
Loading

0 comments on commit c78f2af

Please sign in to comment.