Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: esm only package #9527

Draft
wants to merge 18 commits into
base: main
Choose a base branch
from
Draft
  •  
  •  
  •  
2 changes: 0 additions & 2 deletions .eslintignore

This file was deleted.

66 changes: 0 additions & 66 deletions .eslintrc.js

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ site/examples
site/static/bundle.*
test/log
test/log/difflist.json
test-runtime/__screenshots__
yarn-debug.log*
yarn-error.log*
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
build/
coverage/
examples/compiled/*
examples/specs/*.vl.json
examples/specs/normalized/*
site/_includes/*
site/examples/*
site/_site/*
site/_layouts/*
CHANGELOG.md
15 changes: 0 additions & 15 deletions .prettierrc.json

This file was deleted.

4 changes: 1 addition & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
"name": "vscode-jest-tests",
"request": "launch",
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
"args": [
"--runInBand"
],
"args": ["--runInBand"],
"cwd": "${workspaceFolder}",
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
Expand Down
14 changes: 0 additions & 14 deletions babel.config.js

This file was deleted.

14 changes: 8 additions & 6 deletions bin/args.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
module.exports = type => {
import yargs from 'yargs';
import {argv} from 'process';

export default function args(type) {
const helpText = `${type === 'vega' ? 'Compile' : 'Render'} a Vega-Lite specification to ${
type === 'vega' ? 'Vega' : type.toUpperCase()
}.
Expand All @@ -10,7 +13,7 @@ To load data, you may need to set a base directory:
For web retrieval, use '-b http://host/data/'.
For files, use '-b file:///dir/data/' (absolute) or '-b data/' (relative).`;

const args = require('yargs').usage(helpText).demand(0);
const args = yargs(argv.slice(2)).usage(helpText).demand(0);

args
.string('b')
Expand All @@ -37,7 +40,7 @@ To load data, you may need to set a base directory:
.alias('t', 'timeFormat')
.describe(
't',
'Date/time format locale descriptor. Either a JSON file or a .js file that exports the locale object.'
'Date/time format locale descriptor. Either a JSON file or a .js file that exports the locale object.',
);

if (type === 'svg') {
Expand All @@ -50,10 +53,9 @@ To load data, you may need to set a base directory:

if (type === 'vega') {
args.boolean('p').alias('p', 'pretty').describe('p', 'Output human readable/pretty spec.');
}
else if (type === 'png') {
} else if (type === 'png') {
args.number('ppi').describe('ppi', 'Resolution in ppi.');
}

return args.help().version().argv;
};
}
13 changes: 7 additions & 6 deletions bin/read.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
// from vega-cli

const {createReadStream} = require('fs');
import {createReadStream} from 'fs';
import {stdin} from 'process';

module.exports = file => {
export default function read(file) {
return new Promise((resolve, reject) => {
const input = file ? createReadStream(file) : process.stdin;
const input = file ? createReadStream(file) : stdin;
let text = '';

input.setEncoding('utf8');
input.on('error', err => {
input.on('error', (err) => {
reject(err);
});
input.on('data', chunk => {
input.on('data', (chunk) => {
text += chunk;
});
input.on('end', () => {
resolve(text);
});
});
};
}
35 changes: 20 additions & 15 deletions bin/render.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
// modified from vega-cli

const vega = require('vega');
const path = require('path');
const args = require('./args');
const read = require('./read');
const vegaLite = require('..');
import vega from 'vega';
import path from 'path';
import args from './args.js';
import read from './read.js';
import {compile} from '../build/index.js';
import {readFileSync} from 'fs';
import {exit} from 'process';

function load(file) {
return require(path.resolve(file));
return readFileSync(file, 'utf8');
}

const Levels = {
error: vega.Error,
warn: vega.Warn,
info: vega.Info,
debug: vega.Debug
debug: vega.Debug,
};

module.exports = (type, callback, opt) => {
export function render(type, callback, opt) {
// parse command line arguments
const arg = args(type);

Expand Down Expand Up @@ -45,24 +47,27 @@
// locale options, load custom number/time formats if specified
const locale = {
number: arg.format ? load(arg.format) : null,
time: arg.timeFormat ? load(arg.timeFormat) : null
time: arg.timeFormat ? load(arg.timeFormat) : null,
};

// instantiate view and invoke headless render method
function render(vlSpec) {
const vgSpec = vegaLite.compile(vlSpec, {config}).spec;
const vgSpec = compile(vlSpec, {config}).spec;
const view = new vega.View(vega.parse(vgSpec), {
locale: locale, // set locale options
loader: vega.loader({baseURL: base}), // load files from base path
logger: vega.logger(loglevel, 'error'), // route all logging to stderr
renderer: 'none' // no primary renderer needed
renderer: 'none', // no primary renderer needed
}).finalize(); // clear any timers, etc

return (type === 'svg' ? view.toSVG(scale) : view.toCanvas(scale * ppi / 72, opt)).then(_ => callback(_, arg));
return (type === 'svg' ? view.toSVG(scale) : view.toCanvas((scale * ppi) / 72, opt)).then((_) => callback(_, arg));
}

// read input from file or stdin
read(arg._[0] || null)
.then(text => render(JSON.parse(text)))
.catch(err => { process.exitCode = 1; console.error(err); }); // eslint-disable-line no-console
};
.then((text) => render(JSON.parse(text)))
.catch((err) => {
console.error(err);

Check failure on line 70 in bin/render.js

View workflow job for this annotation

GitHub Actions / Format, Schema, and Examples

'console' is not defined
exit(1);
});
}
11 changes: 5 additions & 6 deletions bin/vl2pdf
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
#!/usr/bin/env node

// Render a Vega-Lite specification to PDF, using node canvas
const {createWriteStream} = require('fs');
const render = require('./render');
import {createWriteStream} from 'fs';
import {render} from './render';

render(
'pdf',
function (canvas, arg) {
(canvas, arg) => {
const file = arg._[1] || null;
const out = file ? createWriteStream(file) : process.stdout;
const stream = canvas.createPDFStream();
stream.on('data', chunk => {
stream.on('data', (chunk) => {
out.write(chunk);
});
},
{type: 'pdf', context: {textDrawingMode: 'glyph'}}
{type: 'pdf', context: {textDrawingMode: 'glyph'}},
);

12 changes: 7 additions & 5 deletions bin/vl2png
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#!/usr/bin/env node

// Render a Vega-Lite specification to PNG, using node canvas
const {createWriteStream} = require('fs');
const render = require('./render');
import {createWriteStream} from 'fs';
import {render} from './render';

render('png', function(canvas, arg) {
render('png', (canvas, arg) => {
const file = arg._[1] || null;
const out = file ? createWriteStream(file) : process.stdout;
const stream = canvas.createPNGStream({ resolution: arg.ppi || undefined });
stream.on('data', chunk => { out.write(chunk); });
const stream = canvas.createPNGStream({resolution: arg.ppi || undefined});
stream.on('data', (chunk) => {
out.write(chunk);
});
});
8 changes: 4 additions & 4 deletions bin/vl2svg
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
#!/usr/bin/env node

// Render a Vega-Lite specification to SVG
const {writeFile} = require('fs');
const render = require('./render');
import {writeFile} from 'fs';
import {render} from './render';

const svgHeader =
'<?xml version="1.0" encoding="utf-8"?>\n' +
'<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" ' +
'"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n';

render('svg', function (body, arg) {
render('svg', (body, arg) => {
const svg = (arg.h ? svgHeader : '') + body;
const file = arg._[1] || null;

if (file) {
// write to file
writeFile(file, svg, err => {
writeFile(file, svg, (err) => {
if (err) throw err;
});
} else {
Expand Down
13 changes: 6 additions & 7 deletions bin/vl2vg
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@

// Compile a Vega-Lite spec to Vega

const {createWriteStream} = require('fs');
const vegaLite = require('..');
const read = require('./read');
const args = require('./args');
import {createWriteStream} from 'fs';
import {compile as compileVL} from '../build/index.js';
import read from './read.js';
import args from './args.js';

const arg = args('vega');

// load spec, compile vg spec
read(arg._[0]).then(text => compile(JSON.parse(text)));
read(arg._[0]).then((text) => compile(JSON.parse(text)));

async function compile(vlSpec) {
// @ts-ignore
const vgSpec = vegaLite.compile(vlSpec).spec;
const vgSpec = compileVL(vlSpec).spec;

const file = arg._[1] || null;
const out = file ? createWriteStream(file) : process.stdout;
Expand Down
Loading
Loading