Skip to content

Commit 80c7c7d

Browse files
authored
Merge pull request #7589 from plotly/cam/7538/build-bundle-before-schema
build: Add build step before generating the schema
2 parents 322c251 + 570640c commit 80c7c7d

File tree

9 files changed

+278
-423
lines changed

9 files changed

+278
-423
lines changed

biome.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"files": {
55
"maxSize": 10000000,
66
"includes": [
7+
"**/esbuild-config.js",
78
"**/src/**",
89
"**/lib/**",
910
"**/test/**",

devtools/dashboard_utilities.mjs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
import constants from '../tasks/util/constants.js';
4+
5+
function readFilePromise(file) {
6+
return new Promise((resolve, reject) => {
7+
fs.readFile(file, { encoding: 'utf-8' }, (err, contents) => {
8+
if (err) reject(err);
9+
else resolve({ name: file, contents: contents });
10+
});
11+
});
12+
}
13+
14+
function writeFilePromise(path, contents) {
15+
return new Promise((resolve, reject) => {
16+
fs.writeFile(path, contents, (err) => {
17+
if (err) reject(err);
18+
else resolve(path);
19+
});
20+
});
21+
}
22+
23+
export function getMockFiles() {
24+
return new Promise((resolve, reject) => {
25+
fs.readdir(constants.pathToTestImageMocks, (err, files) => {
26+
if (err) reject(err);
27+
else resolve(files);
28+
});
29+
});
30+
}
31+
32+
export function readFiles(files) {
33+
const promises = files.map((file) => readFilePromise(path.join(constants.pathToTestImageMocks, file)));
34+
35+
return Promise.all(promises);
36+
}
37+
38+
export function createMocksList(files) {
39+
// eliminate pollutants (e.g .DS_Store) that can accumulate in the mock directory
40+
const jsonFiles = files.filter((file) => file.name.substr(-5) === '.json');
41+
42+
const mocksList = jsonFiles.map((file) => {
43+
const contents = JSON.parse(file.contents);
44+
45+
// get plot type keywords from mocks
46+
const types = contents.data
47+
.map((trace) => trace.type || 'scatter')
48+
.reduce((acc, type, i, arr) => (arr.lastIndexOf(type) === i ? [...acc, type] : acc), []);
49+
50+
const filename = file.name.split(path.sep).pop();
51+
52+
return {
53+
name: filename.slice(0, -5),
54+
file: filename,
55+
keywords: types.join(', ')
56+
};
57+
});
58+
59+
return mocksList;
60+
}
61+
62+
function saveListToFile(filePath, fileName) {
63+
return (list) => writeFilePromise(path.join(filePath, fileName), JSON.stringify(list, null, 2));
64+
}
65+
66+
export const saveMockListToFile = saveListToFile(constants.pathToBuild, 'test_dashboard_mocks.json');
67+
export const saveReglTracesToFile = saveListToFile(constants.pathToBuild, 'regl_traces.json');

devtools/regl_codegen/server.mjs

Lines changed: 24 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
1+
import ecstatic from 'ecstatic';
2+
import { build } from 'esbuild';
13
import fs from 'fs';
2-
import path from 'path';
34
import http from 'http';
4-
import ecstatic from 'ecstatic';
5-
import open from 'open';
65
import minimist from 'minimist';
7-
6+
import open from 'open';
7+
import path from 'path';
8+
import { localDevReglCodegenConfig as config } from '../../esbuild-config.js';
89
import constants from '../../tasks/util/constants.js';
9-
import { build } from 'esbuild';
10-
import config from '../../esbuild-config.js';
10+
import {
11+
createMocksList,
12+
getMockFiles,
13+
readFiles,
14+
saveMockListToFile,
15+
saveReglTracesToFile
16+
} from '../dashboard_utilities.mjs';
1117

1218
var args = minimist(process.argv.slice(2), {});
1319
var PORT = args.port || 3000;
1420
var strict = args.strict;
1521

16-
var reglTraceList = [
17-
'parcoords',
18-
'scattergl',
19-
'scatterpolargl',
20-
'splom'
21-
];
22-
23-
22+
var reglTraceList = ['parcoords', 'scattergl', 'scatterpolargl', 'splom'];
2423

2524
// Create server
2625
var _static = ecstatic({
@@ -32,21 +31,21 @@ var _static = ecstatic({
3231

3332
var tracesReceived = [];
3433

35-
var server = http.createServer(function(req, res) {
36-
if(req.method === 'POST' && req.url === '/api/submit-code') {
34+
var server = http.createServer(function (req, res) {
35+
if (req.method === 'POST' && req.url === '/api/submit-code') {
3736
var body = '';
38-
req.on('data', function(data) {
37+
req.on('data', function (data) {
3938
body += data;
4039
});
41-
req.on('end', function() {
40+
req.on('end', function () {
4241
var data = JSON.parse(body);
4342

4443
tracesReceived.push(data.trace);
4544
handleCodegen(data);
4645
res.statusCode = 200;
4746
res.end();
4847
});
49-
} else if(req.method === 'GET' && req.url === '/api/codegen-done') {
48+
} else if (req.method === 'GET' && req.url === '/api/codegen-done') {
5049
console.log('Codegen complete');
5150
console.log('Traces received:', tracesReceived);
5251

@@ -71,138 +70,32 @@ server.listen(PORT);
7170
// open up browser window
7271
open('http://localhost:' + PORT + '/devtools/regl_codegen/index' + (strict ? '-strict' : '') + '.html');
7372

74-
var devtoolsPath = path.join(constants.pathToRoot, 'devtools/regl_codegen');
75-
config.entryPoints = [path.join(devtoolsPath, 'devtools.js')];
76-
config.outfile = './build/regl_codegen-bundle.js';
77-
config.sourcemap = false;
78-
config.minify = false;
7973
await build(config);
8074

81-
function getMockFiles() {
82-
return new Promise(function(resolve, reject) {
83-
fs.readdir(constants.pathToTestImageMocks, function(err, files) {
84-
if(err) {
85-
reject(err);
86-
} else {
87-
resolve(files);
88-
}
89-
});
90-
});
91-
}
92-
93-
function readFiles(files) {
94-
var promises = files.map(function(file) {
95-
var filePath = path.join(constants.pathToTestImageMocks, file);
96-
return readFilePromise(filePath);
97-
});
98-
99-
return Promise.all(promises);
100-
}
101-
102-
function createMocksList(files) {
103-
// eliminate pollutants (e.g .DS_Store) that can accumulate in the mock directory
104-
var jsonFiles = files.filter(function(file) {
105-
return file.name.substr(-5) === '.json';
106-
});
107-
108-
var mocksList = jsonFiles.map(function(file) {
109-
var contents = JSON.parse(file.contents);
110-
111-
// get plot type keywords from mocks
112-
var types = contents.data.map(function(trace) {
113-
return trace.type || 'scatter';
114-
}).reduce(function(acc, type, i, arr) {
115-
if(arr.lastIndexOf(type) === i) {
116-
acc.push(type);
117-
}
118-
return acc;
119-
}, []);
120-
121-
var filename = file.name.split(path.sep).pop();
122-
123-
return {
124-
name: filename.slice(0, -5),
125-
file: filename,
126-
keywords: types.join(', ')
127-
};
128-
});
129-
130-
return mocksList;
131-
}
132-
133-
function saveMockListToFile(mocksList) {
134-
var filePath = path.join(constants.pathToBuild, 'test_dashboard_mocks.json');
135-
var content = JSON.stringify(mocksList, null, 4);
136-
137-
return writeFilePromise(filePath, content);
138-
}
139-
140-
function saveReglTracesToFile(traces) {
141-
var filePath = path.join(constants.pathToBuild, 'regl_traces.json');
142-
var content = JSON.stringify(traces, null, 4);
143-
144-
return writeFilePromise(filePath, content);
145-
}
146-
147-
function readFilePromise(file) {
148-
return new Promise(function(resolve, reject) {
149-
fs.readFile(file, { encoding: 'utf-8' }, function(err, contents) {
150-
if(err) {
151-
reject(err);
152-
} else {
153-
resolve({
154-
name: file,
155-
contents: contents
156-
});
157-
}
158-
});
159-
});
160-
}
161-
162-
function writeFilePromise(path, contents) {
163-
return new Promise(function(resolve, reject) {
164-
fs.writeFile(path, contents, function(err) {
165-
if(err) {
166-
reject(err);
167-
} else {
168-
resolve(path);
169-
}
170-
});
171-
});
172-
}
173-
17475
function handleCodegen(data) {
17576
var trace = data.trace;
17677
var generated = data.generated;
17778

17879
var pathToReglCodegenSrc = constants.pathToReglCodegenSrc;
17980
var pathToReglPrecompiledSrc = path.join(constants.pathToSrc, 'traces', trace, 'regl_precompiled.js');
18081

181-
var header = [
182-
'\'use strict\';',
183-
'',
184-
].join('\n');
82+
var header = ["'use strict';", ''].join('\n');
18583
var imports = '';
186-
var exports = [
187-
'',
188-
'/* eslint-disable quote-props */',
189-
'module.exports = {',
190-
'',
191-
].join('\n');
84+
var exports = ['', '/* eslint-disable quote-props */', 'module.exports = {', ''].join('\n');
19285
var varId = 0;
19386

194-
Object.entries(generated).forEach(function(kv) {
87+
Object.entries(generated).forEach(function (kv) {
19588
var key = kv[0];
19689
var value = kv[1];
19790
var filePath = path.join(pathToReglCodegenSrc, key);
19891
fs.writeFileSync(filePath, 'module.exports = ' + value);
19992

200-
imports += 'var v' + varId + ' = require(\'../../' + path.join(constants.reglCodegenSubdir, key) + '\');\n';
201-
exports += ' \'' + key + '\': v' + varId + ',\n';
93+
imports += 'var v' + varId + " = require('../../" + path.join(constants.reglCodegenSubdir, key) + "');\n";
94+
exports += " '" + key + "': v" + varId + ',\n';
20295
varId++;
20396
});
20497

205-
if(varId > 0) {
98+
if (varId > 0) {
20699
exports = exports.slice(0, -2) + '\n};\n';
207100
} else {
208101
exports = 'module.exports = {};\n';

0 commit comments

Comments
 (0)