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: switch to ESM #9236

Draft
wants to merge 28 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions .eslintignore
@@ -1,2 +1,3 @@
build
*.cjs
*.js
File renamed without changes.
1 change: 1 addition & 0 deletions .github/workflows/check-toc.yml
Expand Up @@ -24,6 +24,7 @@ jobs:
uses: actions/setup-node@v4
with:
cache: 'yarn'
node-version: current

- name: Install Node dependencies
run: yarn --frozen-lockfile
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/check.yml
Expand Up @@ -19,6 +19,7 @@ jobs:
uses: actions/setup-node@v4
with:
cache: 'yarn'
node-version: current

- name: Install Node dependencies
run: yarn --frozen-lockfile
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/release-docs-and-schema.yml
Expand Up @@ -26,6 +26,7 @@ jobs:
uses: actions/setup-node@v4
with:
cache: 'yarn'
node-version: current

- name: Install Node dependencies
run: yarn --frozen-lockfile
Expand Down Expand Up @@ -61,6 +62,7 @@ jobs:
uses: actions/setup-node@v4
with:
cache: 'yarn'
node-version: current

- name: Setup Pages
uses: actions/configure-pages@v4
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test-docs.yml
Expand Up @@ -20,6 +20,7 @@ jobs:
uses: actions/setup-node@v4
with:
cache: 'yarn'
node-version: current

- name: Install Node dependencies
run: yarn --frozen-lockfile
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Expand Up @@ -19,6 +19,7 @@ jobs:
uses: actions/setup-node@v4
with:
cache: 'yarn'
node-version: current

- name: Install Node dependencies
run: yarn --frozen-lockfile
Expand All @@ -43,6 +44,7 @@ jobs:
uses: actions/setup-node@v4
with:
cache: 'yarn'
node-version: current

- name: Install Node dependencies
run: yarn --frozen-lockfile
Expand Down Expand Up @@ -82,6 +84,7 @@ jobs:
uses: actions/setup-node@v4
with:
cache: 'yarn'
node-version: current

- name: Install Node dependencies
run: yarn --frozen-lockfile
Expand Down
24 changes: 17 additions & 7 deletions .vscode/launch.json
Expand Up @@ -6,20 +6,30 @@
"name": "vscode-jest-tests",
"request": "launch",
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
"args": [
"--runInBand"
],
"args": ["--runInBand"],
"cwd": "${workspaceFolder}",
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true
"internalConsoleOptions": "neverOpen"
},
{
"type": "node",
"name": "Runtime Tests",
"request": "launch",
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
"env": {
"NODE_OPTIONS": "--experimental-vm-modules",
"TZ": "America/Los_Angeles"
},
"args": ["--runInBand", "--config", "test-runtime/jest-config.json"],
"cwd": "${workspaceFolder}",
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
},
{
"type": "node",
"request": "attach",
"name": "Attach",
"port": 9229,
"protocol": "inspector"
"port": 9229
}
]
}
3 changes: 2 additions & 1 deletion babel.config.js → babel.config.cjs
Expand Up @@ -10,5 +10,6 @@ module.exports = {
}
],
'@babel/preset-typescript'
]
],
plugins: ['@babel/plugin-syntax-import-attributes']
};
9 changes: 5 additions & 4 deletions bin/args.js
@@ -1,4 +1,6 @@
module.exports = type => {
import yargs from 'yargs/yargs';

export default type => {
const helpText = `${type === 'vega' ? 'Compile' : 'Render'} a Vega-Lite specification to ${
type === 'vega' ? 'Vega' : type.toUpperCase()
}.
Expand All @@ -10,7 +12,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(process.argv.slice(2)).usage(helpText).demand(0);

args
.string('b')
Expand Down Expand Up @@ -50,8 +52,7 @@ 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.');
}

Expand Down
4 changes: 2 additions & 2 deletions bin/read.js
@@ -1,8 +1,8 @@
// from vega-cli

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

module.exports = file => {
export default file => {
return new Promise((resolve, reject) => {
const input = file ? createReadStream(file) : process.stdin;
let text = '';
Expand Down
19 changes: 11 additions & 8 deletions bin/render.js
@@ -1,10 +1,10 @@
// 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 * as vegaLite from '../build/src/index.js';

function load(file) {
return require(path.resolve(file));
Expand All @@ -17,7 +17,7 @@ const Levels = {
debug: vega.Debug
};

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

Expand Down Expand Up @@ -58,11 +58,14 @@ module.exports = (type, callback, opt) => {
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
.catch(err => {
process.exitCode = 1;
console.error(err);
}); // eslint-disable-line no-console
};
7 changes: 3 additions & 4 deletions bin/vl2pdf
@@ -1,12 +1,12 @@
#!/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.js';

render(
'pdf',
function (canvas, arg) {
(canvas, arg) => {
const file = arg._[1] || null;
const out = file ? createWriteStream(file) : process.stdout;
const stream = canvas.createPDFStream();
Expand All @@ -16,4 +16,3 @@ render(
},
{type: 'pdf', context: {textDrawingMode: 'glyph'}}
);

12 changes: 7 additions & 5 deletions bin/vl2png
@@ -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.js';

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);
});
});
4 changes: 2 additions & 2 deletions bin/vl2svg
@@ -1,8 +1,8 @@
#!/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.js';

const svgHeader =
'<?xml version="1.0" encoding="utf-8"?>\n' +
Expand Down
10 changes: 5 additions & 5 deletions bin/vl2vg
Expand Up @@ -2,11 +2,11 @@

// Compile a Vega-Lite spec to Vega

const {createWriteStream} = require('fs');
const vegaLite = require('..');
const compactStringify = require('json-stringify-pretty-compact');
const read = require('./read');
const args = require('./args');
import {createWriteStream} from 'fs';
import * as vegaLite from '../build/src/index.js';
import compactStringify from 'json-stringify-pretty-compact';
import read from './read.js';
import args from './args.js';

const arg = args('vega');

Expand Down
16 changes: 10 additions & 6 deletions examples/examples.test.ts
@@ -1,15 +1,19 @@
import Ajv, {ErrorObject} from 'ajv';
import addFormats from 'ajv-formats';
import _Ajv, {ErrorObject} from 'ajv';
import _addFormats from 'ajv-formats';
import draft6Schema from 'ajv/lib/refs/json-schema-draft-06.json';
import fs from 'fs';
import path from 'path';
import {Spec as VgSpec} from 'vega';
import vgSchema from 'vega/build/vega-schema.json';
import vlSchema from '../build/vega-lite-schema.json';
import {compile} from '../src/compile/compile';
import * as log from '../src/log';
import {TopLevelSpec} from '../src/spec';
import {duplicate} from '../src/util';
import {compile} from '../src/compile/compile.js';
import * as log from '../src/log/index.js';
import {TopLevelSpec} from '../src/spec/index.js';
import {duplicate} from '../src/util.js';

// https://github.com/ajv-validator/ajv/issues/2132
const Ajv = _Ajv as unknown as typeof _Ajv.default;
const addFormats = _addFormats as unknown as typeof _addFormats.default;

// import {inspect} from 'util';

Expand Down
5 changes: 4 additions & 1 deletion examples/schema.test.ts
@@ -1,7 +1,10 @@
import Ajv from 'ajv';
import _Ajv from 'ajv';
import {inspect} from 'util';
import specSchema from '../build/vega-lite-schema.json';

// https://github.com/ajv-validator/ajv/issues/2132
const Ajv = _Ajv as unknown as typeof _Ajv.default;

describe('Schema', () => {
it('should be valid', () => {
const ajv = new Ajv();
Expand Down
20 changes: 12 additions & 8 deletions jest.config.js
@@ -1,16 +1,20 @@
/** @type {import('jest').Config} */
const config = {
export default {
preset: 'ts-jest/presets/default-esm',
testPathIgnorePatterns: ['<rootDir>/node_modules', '<rootDir>/build', '<rootDir>/_site', '<rootDir>/src'],
coverageDirectory: './coverage/',
collectCoverage: false,
setupFiles: ['./test/jest.overrides.ts'],
globals: {
'ts-jest': {
diagnostics: false,
useESM: true
}
transform: {
'^.+\\.ts$': [
'ts-jest',
{
diagnostics: false,
useESM: true
}
]
},
moduleNameMapper: {
'(.+)\\.js': '$1'
}
};

module.exports = config;