fix(core): update

This commit is contained in:
2020-02-12 21:31:22 +00:00
commit d306fd0b4b
16 changed files with 2348 additions and 0 deletions

1
ts/index.ts Normal file
View File

@ -0,0 +1 @@
export * from './smartssr.classes.smartssr';

View File

@ -0,0 +1,43 @@
import * as plugins from './smartssr.plugins';
import * as paths from './smartssr.paths';
import { serializeFunction } from './smartssr.function.serialize';
/**
*
*/
export class SmartSSR {
public browser: plugins.smartpuppeteer.puppeteer.Browser;
public async start() {
this.browser = await plugins.smartpuppeteer.getEnvAwareBrowserInstance();
}
public async stop() {
if (this.browser) {
await plugins.smartdelay.delayFor(3000);
await this.browser.close();
this.browser = null;
} else {
console.log('browser was not in started mode');
}
}
public async renderPage(urlArg: string) {
const resultDeferred = plugins.smartpromise.defer<string>();
const page = await this.browser.newPage();
page.on('console', (event: any) => console.log(event._text));
page.on('load', async (...args) => {
// await plugins.smartdelay.delayFor(2000);
await page.$eval('body', serializeFunction);
const pageContent = await page.content();
const renderedPageString = pageContent;
resultDeferred.resolve(renderedPageString);
plugins.smartfile.memory.toFsSync(renderedPageString, plugins.path.join(paths.noGitDir, 'test.html'));
});
await page.goto(urlArg);
const result = await resultDeferred.promise;
page.close();
return result;
}
}

View File

@ -0,0 +1,78 @@
declare var document: Document;
export function serializeFunction(rootNode) {
const uuidv4 = () => {
return 'unixxxxxxxxxxx'.replace(/[xy]/g, c => {
const r = (Math.random() * 16) | 0;
const v = c === 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
};
const prependCss = (uuidID: string, styleTemplate: string) => {
if (!styleTemplate.includes(':host')) {
styleTemplate = `:host {}\n\n${styleTemplate}`;
}
styleTemplate = styleTemplate.replace(/}[ \t\n]+\./g, `}\n\n.${uuidID} .`);
styleTemplate = styleTemplate.replace(/}[ \t\n]+\*/g, `}\n\n.${uuidID} *`);
styleTemplate = styleTemplate.replace(/\(\[/g, `[`);
styleTemplate = styleTemplate.replace(/\]\)/g, `]`);
styleTemplate = styleTemplate.replace(/:host/g, `.${uuidID}`);
styleTemplate = styleTemplate.replace(/:host/g, `.${uuidID}`);
styleTemplate = styleTemplate.replace(/{[ \t\n]+\./g, `{\n\n.${uuidID} .`);
styleTemplate = styleTemplate.replace(/}[ \t\n]+img/g, `}\n\n.${uuidID} img`);
styleTemplate = styleTemplate.replace(/}[ \t\n]+div/g, `}\n\n.${uuidID} div`);
return styleTemplate;
};
function serializeNode(node: HTMLElement) {
if (!node.tagName.includes('-')) {
return;
}
if (node.hasAttribute('smartssr')) {
console.log(`${node.tagName} is already serialized`);
return;
}
node.setAttribute('smartssr', 'yes');
// lets handle the current node
const nodeUUID = uuidv4();
try {
node.classList.add(nodeUUID);
// lets modify the css
const children: HTMLElement[] = node.shadowRoot.children as any;
for (const childElement of children) {
if (childElement.tagName === 'STYLE') {
childElement.textContent = prependCss(nodeUUID, childElement.textContent);
}
serializeFunction(childElement);
}
const templateDom = document.createElement('template');
templateDom.innerHTML = node.innerHTML;
const slot = node.shadowRoot.querySelector('slot');
node.childNodes.forEach(lightNode => slot.parentNode.insertBefore(lightNode, slot));
// remove slot element
if (slot) {
slot.parentNode.removeChild(slot);
}
// move shadowDom into root node
node.shadowRoot.childNodes.forEach(shadowNode => node.appendChild(shadowNode));
// add original lightDom as template
if (templateDom.innerHTML !== '') {
node.appendChild(templateDom);
}
} catch (err) {
console.log('error:', err);
console.log(node.tagName);
}
}
[...rootNode.querySelectorAll('*')]
.filter(element => /-/.test(element.nodeName))
.forEach(serializeNode);
}

5
ts/smartssr.paths.ts Normal file
View File

@ -0,0 +1,5 @@
import * as plugins from './smartssr.plugins';
export const packageDir = plugins.path.join(__dirname, '../');
export const noGitDir = plugins.path.join(packageDir, './.nogit');

21
ts/smartssr.plugins.ts Normal file
View File

@ -0,0 +1,21 @@
// node native
import * as path from 'path';
import * as fs from 'fs';
export {
path,
fs
};
// @pushrocks scope
import * as smartdelay from '@pushrocks/smartdelay';
import * as smartfile from '@pushrocks/smartfile';
import * as smartpuppeteer from '@pushrocks/smartpuppeteer';
import * as smartpromise from '@pushrocks/smartpromise';
export {
smartdelay,
smartfile,
smartpuppeteer,
smartpromise
};