From 3222264c4469577867120dbf36cd32ca39c994c0 Mon Sep 17 00:00:00 2001 From: Dominik Moritz Date: Mon, 18 Nov 2024 20:00:18 -0500 Subject: [PATCH] use dynamic import --- bin/vl2vg | 4 ++-- scripts/build-normalized-examples | 21 +++++++++++---------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/bin/vl2vg b/bin/vl2vg index d15cc1f3ef..52f93ab7d0 100755 --- a/bin/vl2vg +++ b/bin/vl2vg @@ -4,7 +4,6 @@ const {createWriteStream} = require('fs'); const vegaLite = require('..'); -const compactStringify = require('json-stringify-pretty-compact'); const read = require('./read'); const args = require('./args'); @@ -13,13 +12,14 @@ const arg = args('vega'); // load spec, compile vg spec read(arg._[0]).then(text => compile(JSON.parse(text))); -function compile(vlSpec) { +async function compile(vlSpec) { // @ts-ignore const vgSpec = vegaLite.compile(vlSpec).spec; const file = arg._[1] || null; const out = file ? createWriteStream(file) : process.stdout; if (arg.p) { + const compactStringify = (await import('json-stringify-pretty-compact')).default; out.write(compactStringify(vgSpec) + '\n'); } else { out.write(JSON.stringify(vgSpec) + '\n'); diff --git a/scripts/build-normalized-examples b/scripts/build-normalized-examples index 61eed3b968..b2663828d9 100755 --- a/scripts/build-normalized-examples +++ b/scripts/build-normalized-examples @@ -12,15 +12,17 @@ const fs = require('fs'); const vl = require('../build/vega-lite'); const stableStringify = require('fast-json-stable-stringify'); -const compactStringify = require('json-stringify-pretty-compact'); const DIR = __dirname + '/..'; const SPECS = '/examples/specs'; const examples = fs.readdirSync(DIR + SPECS); -examples.forEach(example => { - if (example && example.includes('.vl.json')) { - const path = DIR + SPECS + '/' + example; +async function main() { + const {default: compactStringify} = await import('json-stringify-pretty-compact'); + + const vlExamples = examples.filter(example => example.includes('.vl.json')); + for (const example of vlExamples) { + const path = `${DIR + SPECS}/${example}`; const spec = JSON.parse(fs.readFileSync(path)); const preNormalized = stableStringify(spec); @@ -32,11 +34,10 @@ examples.forEach(example => { const postNormalizedOutput = compactStringify(fullSpec, null, 2); // -8 is cutting .vl.json const newFilename = example.slice(0, -8) + '_normalized.vl.json'; - const newFilenameAndPath = DIR + SPECS + '/normalized/' + newFilename; - fs.writeFile(newFilenameAndPath, postNormalizedOutput, err => { - if (err) return console.log(err); - console.log('Built:', newFilename); - }); + const newFilenameAndPath = `${DIR + SPECS}/normalized/${newFilename}`; + fs.writeFileSync(newFilenameAndPath, postNormalizedOutput); } } -}); +} + +main();