fix(core): update
This commit is contained in:
parent
b0e17be31e
commit
2158b366cb
10195
package-lock.json
generated
10195
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
17
package.json
17
package.json
@ -13,19 +13,20 @@
|
|||||||
"format": "(gitzone format)"
|
"format": "(gitzone format)"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.0.22",
|
"@gitzone/tsbuild": "^2.1.24",
|
||||||
"@gitzone/tstest": "^1.0.15",
|
"@gitzone/tstest": "^1.0.43",
|
||||||
"@pushrocks/tapbundle": "^3.0.7",
|
"@pushrocks/smartserve": "^1.1.41",
|
||||||
"@types/node": "^13.7.4",
|
"@pushrocks/tapbundle": "^3.2.9",
|
||||||
"tslint": "^6.0.0",
|
"@types/node": "^14.0.27",
|
||||||
|
"tslint": "^6.1.3",
|
||||||
"tslint-config-prettier": "^1.15.0"
|
"tslint-config-prettier": "^1.15.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/smartdelay": "^2.0.6",
|
"@pushrocks/smartdelay": "^2.0.10",
|
||||||
"@pushrocks/smartfile": "^7.0.8",
|
"@pushrocks/smartfile": "^7.0.12",
|
||||||
"@pushrocks/smartpromise": "^3.0.6",
|
"@pushrocks/smartpromise": "^3.0.6",
|
||||||
"@pushrocks/smartpuppeteer": "^1.0.15",
|
"@pushrocks/smartpuppeteer": "^1.0.15",
|
||||||
"@pushrocks/smarttime": "^3.0.12"
|
"@pushrocks/smarttime": "^3.0.24"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"ts/**/*",
|
"ts/**/*",
|
||||||
|
9
scripts/serve.ts
Normal file
9
scripts/serve.ts
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
import * as smartserve from '@pushrocks/smartserve';
|
||||||
|
import * as path from 'path';
|
||||||
|
|
||||||
|
const smartserveInstance = new smartserve.SmartServe({
|
||||||
|
injectReload: true,
|
||||||
|
serveDir: path.join(__dirname, '../.nogit/')
|
||||||
|
});
|
||||||
|
|
||||||
|
smartserveInstance.start();
|
@ -4,7 +4,9 @@ import * as smartssr from '../ts/index';
|
|||||||
let testSSRInstance: smartssr.SmartSSR;
|
let testSSRInstance: smartssr.SmartSSR;
|
||||||
|
|
||||||
tap.test('should create a valid smartssr instance', async () => {
|
tap.test('should create a valid smartssr instance', async () => {
|
||||||
testSSRInstance = new smartssr.SmartSSR();
|
testSSRInstance = new smartssr.SmartSSR({
|
||||||
|
debug: true
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should start the smartssr instance', async () => {
|
tap.test('should start the smartssr instance', async () => {
|
||||||
@ -12,10 +14,10 @@ tap.test('should start the smartssr instance', async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should render central.eu', async tools => {
|
tap.test('should render central.eu', async tools => {
|
||||||
await testSSRInstance.renderPage('https://central.eu');
|
await testSSRInstance.renderPage('https://central.eu/article/5e76873b9cf69b7bf6bc78bc/Introducing%3A%20central.eu');
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should render lossless.com', async () => {
|
tap.skip.test('should render lossless.com', async () => {
|
||||||
await testSSRInstance.renderPage('https://lossless.com');
|
await testSSRInstance.renderPage('https://lossless.com');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -3,11 +3,26 @@ import * as paths from './smartssr.paths';
|
|||||||
|
|
||||||
import { serializeFunction } from './smartssr.function.serialize';
|
import { serializeFunction } from './smartssr.function.serialize';
|
||||||
|
|
||||||
|
export interface ISmartSSROptions {
|
||||||
|
debug: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
export class SmartSSR {
|
export class SmartSSR {
|
||||||
public browser: plugins.smartpuppeteer.puppeteer.Browser;
|
public browser: plugins.smartpuppeteer.puppeteer.Browser;
|
||||||
|
public options: ISmartSSROptions;
|
||||||
|
|
||||||
|
constructor(optionsArg?: ISmartSSROptions) {
|
||||||
|
this.options = {
|
||||||
|
... {
|
||||||
|
debug: false
|
||||||
|
},
|
||||||
|
...optionsArg
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
public async start() {
|
public async start() {
|
||||||
this.browser = await plugins.smartpuppeteer.getEnvAwareBrowserInstance();
|
this.browser = await plugins.smartpuppeteer.getEnvAwareBrowserInstance();
|
||||||
}
|
}
|
||||||
@ -32,15 +47,29 @@ export class SmartSSR {
|
|||||||
});
|
});
|
||||||
|
|
||||||
page.on('load', async (...args) => {
|
page.on('load', async (...args) => {
|
||||||
// await plugins.smartdelay.delayFor(2000);
|
await plugins.smartdelay.delayFor(5000);
|
||||||
|
let screenshotBuffer: Buffer;
|
||||||
|
|
||||||
|
if (this.options.debug) {
|
||||||
|
screenshotBuffer = await page.screenshot({
|
||||||
|
encoding: 'binary'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
await page.$eval('body', serializeFunction);
|
await page.$eval('body', serializeFunction);
|
||||||
const pageContent = await page.content();
|
const pageContent = await page.content();
|
||||||
const renderedPageString = pageContent;
|
const renderedPageString = pageContent;
|
||||||
resultDeferred.resolve(renderedPageString);
|
resultDeferred.resolve(renderedPageString);
|
||||||
plugins.smartfile.memory.toFsSync(
|
|
||||||
renderedPageString,
|
if (this.options.debug) {
|
||||||
plugins.path.join(paths.noGitDir, 'test.html')
|
plugins.smartfile.memory.toFsSync(
|
||||||
);
|
renderedPageString,
|
||||||
|
plugins.path.join(paths.noGitDir, 'test.html')
|
||||||
|
);
|
||||||
|
const fs = await import('fs');
|
||||||
|
fs.writeFileSync(plugins.path.join(paths.noGitDir, 'test.png'), screenshotBuffer);
|
||||||
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const renderTimeMeasurement = new plugins.smarttime.HrtMeasurement();
|
const renderTimeMeasurement = new plugins.smarttime.HrtMeasurement();
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
declare var document: Document;
|
declare var document: Document;
|
||||||
export function serializeFunction(rootNode) {
|
export function serializeFunction(rootNode) {
|
||||||
const uuidv4 = () => {
|
const uuidv4 = () => {
|
||||||
return 'unixxxxxxxxxxx'.replace(/[xy]/g, c => {
|
return 'unixxxxxxxxxxx'.replace(/[xy]/g, (c) => {
|
||||||
const r = (Math.random() * 16) | 0;
|
const r = (Math.random() * 16) | 0;
|
||||||
const v = c === 'x' ? r : (r & 0x3) | 0x8;
|
const v = c === 'x' ? r : (r & 0x3) | 0x8;
|
||||||
return v.toString(16);
|
return v.toString(16);
|
||||||
@ -24,55 +24,57 @@ export function serializeFunction(rootNode) {
|
|||||||
styleTemplate = styleTemplate.replace(/}[ \t\n]+div/g, `}\n\n.${uuidID} div`);
|
styleTemplate = styleTemplate.replace(/}[ \t\n]+div/g, `}\n\n.${uuidID} div`);
|
||||||
return styleTemplate;
|
return styleTemplate;
|
||||||
};
|
};
|
||||||
|
const loopProtection: any[] = [];
|
||||||
|
|
||||||
function serializeNode(node: HTMLElement) {
|
function serializeNode(nodeArg: HTMLElement, logThis = false) {
|
||||||
if (!node.tagName.includes('-')) {
|
if (loopProtection.includes(nodeArg)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (node.hasAttribute('smartssr')) {
|
loopProtection.push(nodeArg);
|
||||||
console.log(`${node.tagName} is already serialized`);
|
// console.log(nodeArg.nodeName);
|
||||||
return;
|
if (nodeArg.shadowRoot) {
|
||||||
}
|
nodeArg.setAttribute('smartssr', 'yes');
|
||||||
node.setAttribute('smartssr', 'yes');
|
|
||||||
// lets handle the current node
|
|
||||||
const nodeUUID = uuidv4();
|
|
||||||
|
|
||||||
try {
|
// lets handle the current node
|
||||||
node.classList.add(nodeUUID);
|
const nodeUUID = uuidv4();
|
||||||
|
|
||||||
|
nodeArg.classList.add(nodeUUID);
|
||||||
|
const slots = nodeArg.shadowRoot.querySelectorAll('slot');
|
||||||
|
|
||||||
|
// handle slot element
|
||||||
|
slots.forEach((slot) => {
|
||||||
|
nodeArg.childNodes.forEach((lightNode) => slot.parentNode.insertBefore(lightNode, slot));
|
||||||
|
slot.parentNode.removeChild(slot);
|
||||||
|
});
|
||||||
|
|
||||||
// lets modify the css
|
// lets modify the css
|
||||||
const children: HTMLElement[] = node.shadowRoot.children as any;
|
const childNodes = nodeArg.shadowRoot.childNodes;
|
||||||
for (const childElement of children) {
|
// tslint:disable-next-line: prefer-for-of
|
||||||
if (childElement.tagName === 'STYLE') {
|
const noteForAppending: HTMLElement[] = [];
|
||||||
childElement.textContent = prependCss(nodeUUID, childElement.textContent);
|
childNodes.forEach((childNode) => {
|
||||||
|
if (childNode instanceof HTMLElement) {
|
||||||
|
if (childNode.tagName === 'STYLE') {
|
||||||
|
childNode.textContent = prependCss(nodeUUID, childNode.textContent);
|
||||||
|
} else {
|
||||||
|
if (nodeArg.tagName?.includes('ARTICLEGRID')) {
|
||||||
|
console.log('hello ' + childNode.id);
|
||||||
|
}
|
||||||
|
serializeNode(childNode, logThis);
|
||||||
|
}
|
||||||
|
noteForAppending.push(childNode);
|
||||||
}
|
}
|
||||||
serializeFunction(childElement);
|
});
|
||||||
}
|
noteForAppending.forEach(childNode => {
|
||||||
|
nodeArg.append(childNode);
|
||||||
const templateDom = document.createElement('template');
|
});
|
||||||
templateDom.innerHTML = node.innerHTML;
|
} else {
|
||||||
|
nodeArg.childNodes.forEach((nodeArg2: any) => {
|
||||||
const slot = node.shadowRoot.querySelector('slot');
|
serializeNode(nodeArg2, logThis);
|
||||||
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.message);
|
|
||||||
console.log(node.tagName);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[...rootNode.querySelectorAll('*')]
|
rootNode.childNodes.forEach((nodeArg) => {
|
||||||
.filter(element => /-/.test(element.nodeName))
|
serializeNode(nodeArg);
|
||||||
.forEach(serializeNode);
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user