-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
1,000 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,5 @@ node_modules/ | |
coverage/ | ||
dist/ | ||
temp/ | ||
test/**/__artifacts__/regression | ||
test/**/__artifacts__/diff |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* eslint no-param-reassign: 0 */ | ||
|
||
const puppeteer = require('puppeteer'); | ||
|
||
function snapshooter({ snapshotUrl, chrome = {} } = {}) { | ||
const snapshots = {}; | ||
const images = {}; | ||
let browser; | ||
let cachedPage; | ||
let rendering = false; | ||
|
||
const hasConnectOptions = chrome.browserWSEndpoint || chrome.browserURL; | ||
|
||
const createBrowser = () => (hasConnectOptions ? puppeteer.connect(chrome) : puppeteer.launch(chrome)); | ||
|
||
const doIt = async (snapshot, runCached) => { | ||
browser = browser || (await createBrowser()); | ||
let page; | ||
if (runCached) { | ||
cachedPage = cachedPage || (await browser.newPage()); | ||
page = cachedPage; | ||
} else { | ||
page = await browser.newPage(); | ||
} | ||
await page.setViewport({ | ||
width: snapshot.meta.size.width, | ||
height: snapshot.meta.size.height, | ||
}); | ||
await page.goto(`${snapshotUrl}?snapshot=${snapshot.key}`); | ||
try { | ||
await page.waitFor( | ||
() => | ||
(document.querySelector('.nebulajs-sn') && | ||
+document.querySelector('.nebulajs-sn').getAttribute('data-render-count') > 0) || | ||
document.querySelector('[data-njs-error]') | ||
); | ||
} catch (e) { | ||
// empty | ||
} | ||
const image = await page.screenshot(); | ||
images[snapshot.key] = image; | ||
rendering = false; | ||
return snapshot.key; | ||
}; | ||
|
||
return { | ||
getStoredImage(id) { | ||
return images[id]; | ||
}, | ||
getStoredSnapshot(id) { | ||
return snapshots[id]; | ||
}, | ||
storeSnapshot(snapshot) { | ||
if (!snapshot) { | ||
throw new Error('Empty snapshot'); | ||
} | ||
if (!snapshot.key) { | ||
snapshot.key = String(+Date.now()); | ||
} | ||
snapshots[snapshot.key] = snapshot; | ||
return snapshot.key; | ||
}, | ||
async captureImageOfSnapshot(snapshot) { | ||
this.storeSnapshot(snapshot); | ||
if (rendering) { | ||
return doIt(snapshot); | ||
} | ||
rendering = true; | ||
const key = await doIt(snapshot, true); | ||
rendering = false; | ||
return key; | ||
}, | ||
async close() { | ||
if (browser) { | ||
await browser.close(); | ||
} | ||
}, | ||
}; | ||
} | ||
module.exports = snapshooter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"name": "@nebula.js/snapshooter", | ||
"version": "0.1.0-alpha.25", | ||
"description": "", | ||
"license": "MIT", | ||
"author": "QlikTech International AB", | ||
"keywords": [ | ||
"qlik", | ||
"nebula", | ||
"supernova", | ||
"snapshot" | ||
], | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/qlik-oss/nebula.js.git" | ||
}, | ||
"main": "lib/snapshooter.js", | ||
"files": [ | ||
"lib", | ||
"dist" | ||
], | ||
"scripts": { | ||
"build": "cross-env NODE_ENV=production rollup --config ./rollup.config.js", | ||
"prepublishOnly": "rm -rf dist && yarn run build" | ||
}, | ||
"dependencies": { | ||
"puppeteer": "2.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
const path = require('path'); | ||
const babel = require('rollup-plugin-babel'); // eslint-disable-line | ||
const { terser } = require('rollup-plugin-terser'); // eslint-disable-line | ||
|
||
const cwd = process.cwd(); | ||
const pkg = require(path.join(cwd, 'package.json')); // eslint-disable-line | ||
const { name, version, license } = pkg; | ||
|
||
const banner = `/* | ||
* ${name} v${version} | ||
* Copyright (c) ${new Date().getFullYear()} QlikTech International AB | ||
* Released under the ${license} license. | ||
*/ | ||
`; | ||
|
||
const browserList = [ | ||
'last 2 Chrome versions', | ||
'last 2 Firefox versions', | ||
'last 2 Edge versions', | ||
'Safari >= 10.0', | ||
'iOS >= 11.2', | ||
]; | ||
|
||
const cfg = { | ||
input: path.resolve(cwd, 'src', 'renderer'), | ||
output: { | ||
file: path.resolve(cwd, 'dist/renderer.js'), | ||
format: 'umd', | ||
exports: 'default', | ||
name: 'snapshooter', | ||
banner, | ||
}, | ||
plugins: [ | ||
babel({ | ||
babelrc: false, | ||
presets: [ | ||
[ | ||
'@babel/preset-env', | ||
{ | ||
modules: false, | ||
targets: { | ||
browsers: [...browserList, ...['ie 11', 'chrome 47']], | ||
}, | ||
}, | ||
], | ||
], | ||
}), | ||
], | ||
}; | ||
|
||
if (process.env.NODE_ENV === 'production') { | ||
cfg.plugins.push( | ||
terser({ | ||
output: { | ||
preamble: banner, | ||
}, | ||
}) | ||
); | ||
} | ||
|
||
module.exports = [cfg].filter(Boolean); |
Oops, something went wrong.