Skip to content

Commit 4b5a479

Browse files
author
Alexander Kovelenov
committed
Support bundled builds with embedded benchmarks data
1 parent fb49d7e commit 4b5a479

File tree

6 files changed

+51
-20
lines changed

6 files changed

+51
-20
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"build": "rollup -c rollup/config.lib.js",
4343
"example": "rollup -w -c rollup/config.dev.js",
4444
"parse-analytics": "node ./scripts/analytics_parser.js",
45-
"update-benchmarks": "rimraf benchmarks && mkdir -p benchmarks && mkdir -p benchmarks-min && node ./scripts/update_benchmarks.js && tar -czvf benchmarks.tar.gz benchmarks-min/*.json && rm -rf benchmarks-min"
45+
"update-benchmarks": "rimraf benchmarks && mkdir -p benchmarks && mkdir -p benchmarks-min && node ./scripts/update_benchmarks.js && tar -czvf benchmarks.tar.gz benchmarks-min/*.json && rm -rf benchmarks-min && node ./scripts/combine_benchmarks.js"
4646
},
4747
"dependencies": {
4848
"webgl-constants": "^1.1.1"
@@ -64,6 +64,7 @@
6464
"rollup": "^4.5.0",
6565
"rollup-plugin-copy": "^3.5.0",
6666
"rollup-plugin-filesize": "^10.0.0",
67+
"rollup-plugin-ignore": "^1.0.10",
6768
"rollup-plugin-livereload": "^2.0.5",
6869
"rollup-plugin-serve": "^2.0.2"
6970
},

rollup/config.dev.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// Vendor
21
import json from '@rollup/plugin-json';
32
import resolve from '@rollup/plugin-node-resolve';
43
import copy from 'rollup-plugin-copy';

rollup/config.lib.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,24 @@ import copy from 'rollup-plugin-copy';
55
import filesize from 'rollup-plugin-filesize';
66
import terser from '@rollup/plugin-terser';
77
import { babel } from '@rollup/plugin-babel';
8+
import ignore from 'rollup-plugin-ignore'
89

9-
const formats = ['esm', 'umd'];
10+
const formats = [
11+
['esm', 'esm', './internal/benchmarks.json'],
12+
['umd', 'umd', './internal/benchmarks.json'],
13+
['umd', 'bundle', ''],
14+
];
1015

11-
export default formats.map((format) => ({
16+
export default formats.map(([format, suffix, ignored] = []) => ({
1217
input: './src/index.js',
1318
output: {
14-
file: `./dist/detect-gpu.${format}.js`,
19+
file: `./dist/detect-gpu.${suffix}.js`,
1520
format,
1621
name: 'DetectGPU',
1722
sourcemap: true,
1823
},
1924
plugins: [
25+
ignore(ignored),
2026
babel({
2127
presets: [['@babel/preset-env', { targets: "safari >= 11" }]],
2228
babelHelpers: 'bundled',

scripts/combine_benchmarks.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env node
2+
3+
import fs from 'fs';
4+
import path from 'path';
5+
import url from 'url';
6+
7+
const CURR_DIR = path.dirname(url.fileURLToPath(import.meta.url));
8+
const BENCH_DIR = path.join(CURR_DIR, '..', 'benchmarks');
9+
10+
const pack = {};
11+
12+
fs.readdirSync(BENCH_DIR).forEach(file => {
13+
pack[file] = JSON.parse(fs.readFileSync(path.join(BENCH_DIR, file)));
14+
});
15+
16+
fs.writeFileSync(path.join(CURR_DIR, '..', 'src', 'internal', 'benchmarks.json'), JSON.stringify(pack));

src/index.js

+23-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { version } from '../package.json';
2+
// optional, when ignored in rollup config, becomes {}
3+
import benchmarkFiles from './internal/benchmarks.json';
24

35
import { BLOCKLISTED_GPUS } from './internal/blocklistedGPUS';
46
import { cleanRenderer } from './internal/cleanRenderer';
@@ -23,6 +25,11 @@ export const getGPUTier = async ({
2325
} = {}) => {
2426

2527
const queryCache = {};
28+
29+
// populate cache from bundle if there is somemething in it
30+
for (const benchmarkFile in benchmarkFiles)
31+
queryCache[benchmarkFile] = benchmarkFiles[benchmarkFile].slice(1); // strip version
32+
2633
if (isSSR) {
2734
return {
2835
tier: 0,
@@ -32,22 +39,23 @@ export const getGPUTier = async ({
3239

3340
const {
3441
isIpad = !!deviceInfo?.isIpad,
35-
isMobile = !!deviceInfo?.isMobile,
36-
screenSize = window.screen,
37-
loadBenchmarks = async (file) => {
38-
const data = await fetch(`${benchmarksURL}/${file}`).then((response) =>
39-
response.json()
42+
isMobile = !!deviceInfo?.isMobile,
43+
screenSize = window.screen,
44+
loadBenchmarks = async (file) => {
45+
46+
const data = await fetch(`${benchmarksURL}/${file}`).then((response) =>
47+
response.json()
48+
);
49+
50+
// Remove version tag and check version is supported
51+
const version = parseInt(data.shift().split('.')[0], 10);
52+
if (version < 1) {
53+
throw new OutdatedBenchmarksError(
54+
'Detect GPU benchmark data is out of date. Please update to version 1x'
4055
);
41-
42-
// Remove version tag and check version is supported
43-
const version = parseInt(data.shift().split('.')[0], 10);
44-
if (version < 1) {
45-
throw new OutdatedBenchmarksError(
46-
'Detect GPU benchmark data is out of date. Please update to version 1x'
47-
);
48-
}
49-
return data;
50-
},
56+
}
57+
return data;
58+
},
5159
} = override;
5260

5361
let { renderer } = override;

src/internal/benchmarks.json

+1
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)