Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
1 change: 1 addition & 0 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"files": {
"maxSize": 10000000,
"includes": [
"**/esbuild-config.js",
"**/src/**",
"**/lib/**",
"**/test/**",
Expand Down
88 changes: 35 additions & 53 deletions devtools/regl_codegen/server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,13 @@ import minimist from 'minimist';

import constants from '../../tasks/util/constants.js';
import { build } from 'esbuild';
import config from '../../esbuild-config.js';
import { esbuildConfig as config } from '../../esbuild-config.js';

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 All @@ -71,17 +64,12 @@ server.listen(PORT);
// open up browser window
open('http://localhost:' + PORT + '/devtools/regl_codegen/index' + (strict ? '-strict' : '') + '.html');

var devtoolsPath = path.join(constants.pathToRoot, 'devtools/regl_codegen');
config.entryPoints = [path.join(devtoolsPath, 'devtools.js')];
config.outfile = './build/regl_codegen-bundle.js';
config.sourcemap = false;
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 +79,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 +89,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 +135,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 +150,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 +168,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
7 changes: 7 additions & 0 deletions devtools/test_dashboard/build.mjs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@camdecoster After the cleanup you've done, does this step really need a separate file? Couldn't you just now add build(localDevConfig); to the beginning of the makeSchema() function in schema.mjs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a fair question. I envisioned this script as being one that could be called elsewhere, but that seems like it violates the YAGNI principle. I'll do as you suggest. We can always add it back.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { build } from 'esbuild';
import { localDevConfig } from '../../esbuild-config.js';

// Build plotly.js to be used locally, such as when generating the schema.
// This is the same process used in the test dashboard server script, but
// run only once.
build(localDevConfig);
101 changes: 36 additions & 65 deletions devtools/test_dashboard/server.mjs
Original file line number Diff line number Diff line change
@@ -1,62 +1,31 @@
import fs from 'fs';
import path from 'path';
import path from 'path';
import http from 'http';
import ecstatic from 'ecstatic';
import open from 'open';
import minimist from 'minimist';

import constants from '../../tasks/util/constants.js';
import { context, build } from 'esbuild';
import config from '../../esbuild-config.js';

import { glsl } from 'esbuild-plugin-glsl';
import { devtoolsConfig, localDevConfig } from '../../esbuild-config.js';

var args = minimist(process.argv.slice(2), {});
var PORT = args.port || 3000;
var strict = args.strict;
var mathjax3 = args.mathjax3;
var mathjax3chtml = args.mathjax3chtml;

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

config.outfile = './build/plotly.js';
if (strict) localDevConfig.entryPoints = ['./lib/index-strict.js'];

var mockFolder = constants.pathToTestImageMocks;

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

// Devtools config
var devtoolsConfig = {
entryPoints: [
path.join(constants.pathToRoot, 'devtools', 'test_dashboard', 'devtools.js')
],
outfile: path.join(constants.pathToRoot, 'build', 'test_dashboard-bundle.js'),
format: 'cjs',
globalName: 'Tabs',
bundle: true,
minify: false,
sourcemap: false,
plugins: [
glsl({
minify: true,
}),
],
define: {
global: 'window',
},
target: 'es2016',
logLevel: 'info',
};
await getMockFiles().then(readFiles).then(createMocksList).then(saveMockListToFile);

build(devtoolsConfig);

var ctx = await context(config);
var ctx = await context(localDevConfig);
devServer();
console.log('watching esbuild...');
await ctx.watch();
Expand All @@ -70,7 +39,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 +52,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 +85,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 +95,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 +134,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 +149,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
Loading