Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Linting/formatting
  • Loading branch information
camdecoster committed Oct 15, 2025
commit fd910a19cd04b469a4b1ea8b2eddb6c0eee96ed7
81 changes: 34 additions & 47 deletions devtools/regl_codegen/server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,7 @@ var args = minimist(process.argv.slice(2), {});
var PORT = args.port || 3000;
var strict = args.strict;

var reglTraceList = [
'parcoords',
'scattergl',
'scatterpolargl',
'splom'
];


var reglTraceList = ['parcoords', 'scattergl', 'scatterpolargl', 'splom'];

// Create server
var _static = ecstatic({
Expand All @@ -32,21 +25,21 @@ var _static = ecstatic({

var tracesReceived = [];

var server = http.createServer(function(req, res) {
if(req.method === 'POST' && req.url === '/api/submit-code') {
var server = http.createServer(function (req, res) {
if (req.method === 'POST' && req.url === '/api/submit-code') {
var body = '';
req.on('data', function(data) {
req.on('data', function (data) {
body += data;
});
req.on('end', function() {
req.on('end', function () {
var data = JSON.parse(body);

tracesReceived.push(data.trace);
handleCodegen(data);
res.statusCode = 200;
res.end();
});
} else if(req.method === 'GET' && req.url === '/api/codegen-done') {
} else if (req.method === 'GET' && req.url === '/api/codegen-done') {
console.log('Codegen complete');
console.log('Traces received:', tracesReceived);

Expand Down Expand Up @@ -79,9 +72,9 @@ config.minify = false;
await build(config);

function getMockFiles() {
return new Promise(function(resolve, reject) {
fs.readdir(constants.pathToTestImageMocks, function(err, files) {
if(err) {
return new Promise(function (resolve, reject) {
fs.readdir(constants.pathToTestImageMocks, function (err, files) {
if (err) {
reject(err);
} else {
resolve(files);
Expand All @@ -91,7 +84,7 @@ function getMockFiles() {
}

function readFiles(files) {
var promises = files.map(function(file) {
var promises = files.map(function (file) {
var filePath = path.join(constants.pathToTestImageMocks, file);
return readFilePromise(filePath);
});
Expand All @@ -101,22 +94,24 @@ function readFiles(files) {

function createMocksList(files) {
// eliminate pollutants (e.g .DS_Store) that can accumulate in the mock directory
var jsonFiles = files.filter(function(file) {
var jsonFiles = files.filter(function (file) {
return file.name.substr(-5) === '.json';
});

var mocksList = jsonFiles.map(function(file) {
var mocksList = jsonFiles.map(function (file) {
var contents = JSON.parse(file.contents);

// get plot type keywords from mocks
var types = contents.data.map(function(trace) {
return trace.type || 'scatter';
}).reduce(function(acc, type, i, arr) {
if(arr.lastIndexOf(type) === i) {
acc.push(type);
}
return acc;
}, []);
var types = contents.data
.map(function (trace) {
return trace.type || 'scatter';
})
.reduce(function (acc, type, i, arr) {
if (arr.lastIndexOf(type) === i) {
acc.push(type);
}
return acc;
}, []);

var filename = file.name.split(path.sep).pop();

Expand Down Expand Up @@ -145,9 +140,9 @@ function saveReglTracesToFile(traces) {
}

function readFilePromise(file) {
return new Promise(function(resolve, reject) {
fs.readFile(file, { encoding: 'utf-8' }, function(err, contents) {
if(err) {
return new Promise(function (resolve, reject) {
fs.readFile(file, { encoding: 'utf-8' }, function (err, contents) {
if (err) {
reject(err);
} else {
resolve({
Expand All @@ -160,9 +155,9 @@ function readFilePromise(file) {
}

function writeFilePromise(path, contents) {
return new Promise(function(resolve, reject) {
fs.writeFile(path, contents, function(err) {
if(err) {
return new Promise(function (resolve, reject) {
fs.writeFile(path, contents, function (err) {
if (err) {
reject(err);
} else {
resolve(path);
Expand All @@ -178,31 +173,23 @@ function handleCodegen(data) {
var pathToReglCodegenSrc = constants.pathToReglCodegenSrc;
var pathToReglPrecompiledSrc = path.join(constants.pathToSrc, 'traces', trace, 'regl_precompiled.js');

var header = [
'\'use strict\';',
'',
].join('\n');
var header = ["'use strict';", ''].join('\n');
var imports = '';
var exports = [
'',
'/* eslint-disable quote-props */',
'module.exports = {',
'',
].join('\n');
var exports = ['', '/* eslint-disable quote-props */', 'module.exports = {', ''].join('\n');
var varId = 0;

Object.entries(generated).forEach(function(kv) {
Object.entries(generated).forEach(function (kv) {
var key = kv[0];
var value = kv[1];
var filePath = path.join(pathToReglCodegenSrc, key);
fs.writeFileSync(filePath, 'module.exports = ' + value);

imports += 'var v' + varId + ' = require(\'../../' + path.join(constants.reglCodegenSubdir, key) + '\');\n';
exports += ' \'' + key + '\': v' + varId + ',\n';
imports += 'var v' + varId + " = require('../../" + path.join(constants.reglCodegenSubdir, key) + "');\n";
exports += " '" + key + "': v" + varId + ',\n';
varId++;
});

if(varId > 0) {
if (varId > 0) {
exports = exports.slice(0, -2) + '\n};\n';
} else {
exports = 'module.exports = {};\n';
Expand Down
81 changes: 39 additions & 42 deletions devtools/test_dashboard/server.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs';
import path from 'path';
import path from 'path';
import http from 'http';
import ecstatic from 'ecstatic';
import open from 'open';
Expand All @@ -17,7 +17,7 @@ var strict = args.strict;
var mathjax3 = args.mathjax3;
var mathjax3chtml = args.mathjax3chtml;

if(strict) {
if (strict) {
config.entryPoints = ['./lib/index-strict.js'];
}

Expand All @@ -26,16 +26,11 @@ config.outfile = './build/plotly.js';
var mockFolder = constants.pathToTestImageMocks;

// mock list
await getMockFiles()
.then(readFiles)
.then(createMocksList)
.then(saveMockListToFile);
await getMockFiles().then(readFiles).then(createMocksList).then(saveMockListToFile);

// Devtools config
var devtoolsConfig = {
entryPoints: [
path.join(constants.pathToRoot, 'devtools', 'test_dashboard', 'devtools.js')
],
entryPoints: [path.join(constants.pathToRoot, 'devtools', 'test_dashboard', 'devtools.js')],
outfile: path.join(constants.pathToRoot, 'build', 'test_dashboard-bundle.js'),
format: 'cjs',
globalName: 'Tabs',
Expand All @@ -44,14 +39,14 @@ var devtoolsConfig = {
sourcemap: false,
plugins: [
glsl({
minify: true,
}),
minify: true
})
],
define: {
global: 'window',
global: 'window'
},
target: 'es2016',
logLevel: 'info',
logLevel: 'info'
};

build(devtoolsConfig);
Expand All @@ -70,7 +65,7 @@ function devServer() {
});

const server = http.createServer((req, res) => {
if(strict) {
if (strict) {
res.setHeader(
'Content-Security-Policy',
// Comment/uncomment for testing CSP. Changes require a server restart.
Expand All @@ -83,30 +78,30 @@ function devServer() {
// "connect-src 'self'",
// "object-src 'none'",
// "base-uri 'self';",
"worker-src blob:",
].join("; ")
)
'worker-src blob:'
].join('; ')
);
}

staticFilesHandler(req, res)
})
staticFilesHandler(req, res);
});

// Start the server up!
server.listen(PORT);

let indexName = 'index';
if(mathjax3) indexName += '-mathjax3'
else if(mathjax3chtml) indexName += '-mathjax3chtml'
indexName += '.html'
if (mathjax3) indexName += '-mathjax3';
else if (mathjax3chtml) indexName += '-mathjax3chtml';
indexName += '.html';

// open up browser window
open(`http://localhost:${PORT}/devtools/test_dashboard/${indexName}${strict ? '?strict=true' : ''}`);
}

function getMockFiles() {
return new Promise(function(resolve, reject) {
fs.readdir(mockFolder, function(err, files) {
if(err) {
return new Promise(function (resolve, reject) {
fs.readdir(mockFolder, function (err, files) {
if (err) {
reject(err);
} else {
resolve(files);
Expand All @@ -116,7 +111,7 @@ function getMockFiles() {
}

function readFiles(files) {
var promises = files.map(function(file) {
var promises = files.map(function (file) {
var filePath = path.join(mockFolder, file);
return readFilePromise(filePath);
});
Expand All @@ -126,22 +121,24 @@ function readFiles(files) {

function createMocksList(files) {
// eliminate pollutants (e.g .DS_Store) that can accumulate in the mock directory
var jsonFiles = files.filter(function(file) {
var jsonFiles = files.filter(function (file) {
return file.name.substr(-5) === '.json';
});

var mocksList = jsonFiles.map(function(file) {
var mocksList = jsonFiles.map(function (file) {
var contents = JSON.parse(file.contents);

// get plot type keywords from mocks
var types = contents.data.map(function(trace) {
return trace.type || 'scatter';
}).reduce(function(acc, type, i, arr) {
if(arr.lastIndexOf(type) === i) {
acc.push(type);
}
return acc;
}, []);
var types = contents.data
.map(function (trace) {
return trace.type || 'scatter';
})
.reduce(function (acc, type, i, arr) {
if (arr.lastIndexOf(type) === i) {
acc.push(type);
}
return acc;
}, []);

var filename = file.name.split(path.sep).pop();

Expand All @@ -163,9 +160,9 @@ function saveMockListToFile(mocksList) {
}

function readFilePromise(file) {
return new Promise(function(resolve, reject) {
fs.readFile(file, { encoding: 'utf-8' }, function(err, contents) {
if(err) {
return new Promise(function (resolve, reject) {
fs.readFile(file, { encoding: 'utf-8' }, function (err, contents) {
if (err) {
reject(err);
} else {
resolve({
Expand All @@ -178,9 +175,9 @@ function readFilePromise(file) {
}

function writeFilePromise(path, contents) {
return new Promise(function(resolve, reject) {
fs.writeFile(path, contents, function(err) {
if(err) {
return new Promise(function (resolve, reject) {
fs.writeFile(path, contents, function (err) {
if (err) {
reject(err);
} else {
resolve(path);
Expand Down
17 changes: 4 additions & 13 deletions tasks/util/bundle_wrapper.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ export default async function _bundle(pathToIndex, pathToBundle, opts, cb) {
config.outfile = pathToBundle;
config.minify = !!opts.minify;

if(!opts.noCompressAttributes) {
if (!opts.noCompressAttributes) {
config.plugins = basePlugins.concat([esbuildPluginStripMeta]);
}

if(opts.noPlugins) config.plugins = [];
if (opts.noPlugins) config.plugins = [];

await build(config);

addWrapper(pathToBundle);

if(cb) cb();
if (cb) cb();
}

// Until https://github.com/evanw/esbuild/pull/513 is merged
Expand All @@ -67,14 +67,5 @@ function addWrapper(path) {
common.throwOnError
);

fsExtra.appendFile(
path,
[
'',
'window.Plotly = Plotly;',
'return Plotly;',
'}));',
].join('\n'),
common.throwOnError
);
fsExtra.appendFile(path, ['', 'window.Plotly = Plotly;', 'return Plotly;', '}));'].join('\n'), common.throwOnError);
}
Loading