Skip to content
This repository was archived by the owner on Aug 18, 2021. It is now read-only.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
offline/*
!offline/index.js
dist
node_modules
dist/
77 changes: 77 additions & 0 deletions bin/go-offline.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/usr/bin/env node
/**
* @file
* Runtime for downloading appropriate deps for offline us
*/

const cheerio = require('cheerio');
const Axios = require('axios');
const execa = require('execa');
const tmp = require('tmp');
const npm = require('npm');
const pacote = require('pacote');
const { readFileSync, writeFileSync, createWriteStream } = require('fs');

(async () => {
try {
console.info('Checking d3-bootloader version...');
const index = readFileSync(`${process.cwd()}/index.html`, 'utf-8');
const $ = cheerio.load(index);
const bootloaderTag = $(
$('script')
.toArray()
.find(d => /d3-bootloader/.test($(d).attr('src'))),
);
let version;
const bootloaderUrl = bootloaderTag.attr('src');

if (!bootloaderUrl) {
throw new Error(
'Unable to locate D3 bootloader; have you run `offline` in a directory with an index.html file?',
);
} else if (/unpkg.com\/@financial-times\/d3-bootloader@/.test(bootloaderUrl)) {
console.error('Using a specified version of d3-bootloader deps is current unsupported.');
console.info('Falling back to using `latest` release.');
version = 'latest';
// @TODO write version handling logic
} else {
version = 'latest';
console.info('Using `latest` release dependencies.');
}

console.info('Downloading dependencies...');

// Create a tmp file
const tmpFile = tmp.fileSync();

// Download tarball to tmpfile
await pacote.tarball.toFile(`@financial-times/d3-bootloader@${version}`, tmpFile.name);

console.info('Download complete.');
console.info('Extracting...');

// Create tmp dir.
const tmpDir = tmp.dirSync();

// Extract to tmp dir
await execa('tar', ['-C', tmpDir.name, '--strip=1', '-xzvf', tmpFile.name]);

// Move tmpDir/offline to $CWD
await execa('mv', [`${tmpDir.name}/offline`, process.cwd()]);

// Remove tmp dir.
tmpDir.removeCallback();

// Remove the tarball
tmpFile.removeCallback();

// Write updated index.html to $CWD
console.info('Updating index.html...');
bootloaderTag.attr('src', './offline/index.js');
writeFileSync(`${process.cwd()}/index.html`, $.html());

console.info('Done!');
} catch (e) {
console.error(e);
}
})();
44 changes: 44 additions & 0 deletions get-offline.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Where is this file called?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@tlfrd It's set as bin.offline in package.json so users can either install @financial-times/d3-bootloader globally or run npx @financial-times/d3-bootloader and it will:

  1. Download all dependencies for offline use to the local directory
  2. Rewrite the d3-bootloader script tag to use the offline version.

* @file
* Offline support
*/

import Axios from "axios";
import path from "path";
import { createWriteStream } from "fs";
import modules from "./modules";

Promise.all(Object.entries(modules).map(item => downloadDependency(...item)))
.then(() => console.log("Done!"))
.catch(console.error);

async function downloadDependency(pkg, url) {
const pkgName = /d3js\.org\/d3\./.test(url)
? "d3"
: /^@/.test(pkg)
? pkg.slice(pkg.indexOf("/") + 1)
: pkg;
const outFile = path.resolve(__dirname, "offline", `${pkgName}.js`);
console.log(pkgName, url);

// axios image download with response type "stream"
const response = await Axios({
method: "GET",
url: url,
responseType: "stream"
});

// pipe the result stream into a file on disc
response.data.pipe(createWriteStream(outFile));

// return a promise and resolve when download finishes
return new Promise((resolve, reject) => {
response.data.on("end", () => {
resolve();
});

response.data.on("error", () => {
reject();
});
});
}
65 changes: 6 additions & 59 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,68 +9,15 @@
* in-browser tool for creating static print visualisations).
*/

import SystemJS from 'systemjs';

var defaultMap = {
'@financial-times/load-data':
'https://unpkg.com/@financial-times/load-data@^1.0.2/dist/loadData.dist.min.js',
'bertha-client': 'https://unpkg.com/bertha-client@4.0.1/dist/index.js',
d3: 'https://d3js.org/d3.v4.js',
'd3-array': 'https://d3js.org/d3.v4.js',
'd3-axis': 'https://d3js.org/d3.v4.js',
'd3-brush': 'https://d3js.org/d3.v4.js',
'd3-chord': 'https://d3js.org/d3.v4.js',
'd3-collection': 'https://d3js.org/d3.v4.js',
'd3-color': 'https://d3js.org/d3.v4.js',
'd3-dispatch': 'https://d3js.org/d3.v4.js',
'd3-drag': 'https://d3js.org/d3.v4.js',
'd3-dsv': 'https://d3js.org/d3.v4.js',
'd3-ease': 'https://d3js.org/d3.v4.js',
'd3-force': 'https://d3js.org/d3.v4.js',
'd3-format': 'https://d3js.org/d3.v4.js',
'd3-geo': 'https://d3js.org/d3.v4.js',
'd3-hierarchy': 'https://d3js.org/d3.v4.js',
'd3-interpolate': 'https://d3js.org/d3.v4.js',
'd3-path': 'https://d3js.org/d3.v4.js',
'd3-polygon': 'https://d3js.org/d3.v4.js',
'd3-quadtree': 'https://d3js.org/d3.v4.js',
'd3-queue': 'https://d3js.org/d3.v4.js',
'd3-random': 'https://d3js.org/d3.v4.js',
'd3-request': 'https://d3js.org/d3.v4.js',
'd3-sankey': 'https://unpkg.com/d3-sankey@0.7.1/build/d3-sankey.js',
'd3-scale': 'https://d3js.org/d3.v4.js',
'd3-selection-multi': 'https://unpkg.com/d3-selection-multi@1.0.1/build/d3-selection-multi.js',
'd3-selection': 'https://d3js.org/d3.v4.js',
'd3-shape': 'https://d3js.org/d3.v4.js',
'd3-time-format': 'https://d3js.org/d3.v4.js',
'd3-time': 'https://d3js.org/d3.v4.js',
'd3-timer': 'https://d3js.org/d3.v4.js',
'd3-transition': 'https://d3js.org/d3.v4.js',
'd3-voronoi': 'https://d3js.org/d3.v4.js',
'd3-zoom': 'https://d3js.org/d3.v4.js',
'g-annotations': 'https://unpkg.com/g-annotations@^1/build/g-annotations.js',
'g-axis': 'https://unpkg.com/g-axis@^1/build/g-axis.js',
'g-chartcolour': 'https://unpkg.com/g-chartcolour@0.8.26/build/g-chartcolour.js',
'g-chartframe': 'https://unpkg.com/g-chartframe@^5/build/g-chartframe.js',
'g-legend': 'https://unpkg.com/g-legend@^1/build/g-legend.js',
'g-xaxisdate': 'https://unpkg.com/g-axis@^1/build/g-axis.js',
'g-yaxislinear': 'https://unpkg.com/g-axis@^1/build/g-axis.js',
'plugin-babel': 'https://unpkg.com/systemjs-plugin-babel/plugin-babel.js',
'plugin-json': 'https://unpkg.com/systemjs-plugin-json@0.3.0/json.js',
'save-svg-as-png': 'https://unpkg.com/save-svg-as-png@1.4.6/lib/saveSvgAsPng.js',
'simple-statistics': 'https://unpkg.com/simple-statistics@5.4.0/dist/simple-statistics.js',
'systemjs-babel-build':
'https://unpkg.com/systemjs-plugin-babel@0.0.25/systemjs-babel-browser.js',
'topojson-client': 'https://unpkg.com/topojson-client@3.0.0/dist/topojson-client.js',
chai: 'https://www.chaijs.com/chai.js',
};
import SystemJS from "systemjs";
import modules from "./modules.js";

var config = {
map: Object.assign({}, defaultMap, window.D3_BOOTLOADER_MODULES),
meta: { '*.json': { loader: 'plugin-json' } },
transpiler: 'plugin-babel',
map: Object.assign({}, modules, window.D3_BOOTLOADER_MODULES),
meta: { "*.json": { loader: "plugin-json" } },
transpiler: "plugin-babel"
};

SystemJS.config(config);

export default SystemJS.import('./index.js');
export default SystemJS.import("./index.js");
53 changes: 53 additions & 0 deletions modules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
export default {
'@financial-times/load-data':
'https://unpkg.com/@financial-times/load-data@^1.0.2/dist/loadData.dist.min.js',
'bertha-client': 'https://unpkg.com/bertha-client@4.0.1/dist/index.js',
d3: 'https://d3js.org/d3.v4.js',
'd3-array': 'https://d3js.org/d3.v4.js',
'd3-axis': 'https://d3js.org/d3.v4.js',
'd3-brush': 'https://d3js.org/d3.v4.js',
'd3-chord': 'https://d3js.org/d3.v4.js',
'd3-collection': 'https://d3js.org/d3.v4.js',
'd3-color': 'https://d3js.org/d3.v4.js',
'd3-dispatch': 'https://d3js.org/d3.v4.js',
'd3-drag': 'https://d3js.org/d3.v4.js',
'd3-dsv': 'https://d3js.org/d3.v4.js',
'd3-ease': 'https://d3js.org/d3.v4.js',
'd3-force': 'https://d3js.org/d3.v4.js',
'd3-format': 'https://d3js.org/d3.v4.js',
'd3-geo': 'https://d3js.org/d3.v4.js',
'd3-hierarchy': 'https://d3js.org/d3.v4.js',
'd3-interpolate': 'https://d3js.org/d3.v4.js',
'd3-path': 'https://d3js.org/d3.v4.js',
'd3-polygon': 'https://d3js.org/d3.v4.js',
'd3-quadtree': 'https://d3js.org/d3.v4.js',
'd3-queue': 'https://d3js.org/d3.v4.js',
'd3-random': 'https://d3js.org/d3.v4.js',
'd3-request': 'https://d3js.org/d3.v4.js',
'd3-sankey': 'https://unpkg.com/d3-sankey@0.7.1/build/d3-sankey.js',
'd3-scale': 'https://d3js.org/d3.v4.js',
'd3-selection-multi': 'https://unpkg.com/d3-selection-multi@1.0.1/build/d3-selection-multi.js',
'd3-selection': 'https://d3js.org/d3.v4.js',
'd3-shape': 'https://d3js.org/d3.v4.js',
'd3-time-format': 'https://d3js.org/d3.v4.js',
'd3-time': 'https://d3js.org/d3.v4.js',
'd3-timer': 'https://d3js.org/d3.v4.js',
'd3-transition': 'https://d3js.org/d3.v4.js',
'd3-voronoi': 'https://d3js.org/d3.v4.js',
'd3-zoom': 'https://d3js.org/d3.v4.js',
'g-annotations': 'https://unpkg.com/g-annotations@^1/build/g-annotations.js',
'g-axis': 'https://unpkg.com/g-axis@^1/build/g-axis.js',
'g-chartcolour': 'https://unpkg.com/g-chartcolour@0.8.26/build/g-chartcolour.js',
'g-chartframe': 'https://unpkg.com/g-chartframe@^5/build/g-chartframe.js',
'g-legend': 'https://unpkg.com/g-legend@^1/build/g-legend.js',
'g-xaxisdate': 'https://unpkg.com/g-axis@^1/build/g-axis.js',
'g-yaxislinear': 'https://unpkg.com/g-axis@^1/build/g-axis.js',
'plugin-babel': 'https://unpkg.com/systemjs-plugin-babel/plugin-babel.js',
'plugin-json': 'https://unpkg.com/systemjs-plugin-json@0.3.0/json.js',
'save-svg-as-png': 'https://unpkg.com/save-svg-as-png@1.4.6/lib/saveSvgAsPng.js',
'simple-statistics': 'https://unpkg.com/simple-statistics@5.4.0/dist/simple-statistics.js',
'systemjs-babel-build':
'https://unpkg.com/systemjs-plugin-babel@0.0.25/systemjs-babel-browser.js',
'topojson-client': 'https://unpkg.com/topojson-client@3.0.0/dist/topojson-client.js',
chai: 'https://www.chaijs.com/chai.js',
};
42 changes: 42 additions & 0 deletions offline/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* d3-bootloader
* 2017 Ændrew Rininsland <@aendrew>, Financial Times
*
* Allows the use of D3 using ES6 modules via SystemJS.
* This does Babel transpilation in the browser. It's probably
* not very performant — at all — and is intended for scenarios
* such as with Visual Vocabulary (I.e., D3 is being used as an
* in-browser tool for creating static print visualisations).
*/

import SystemJS from "systemjs";
import modules from "../modules.js";

const offlineModules = Object.entries(modules).reduce(
(a, [k, v]) =>
Object.assign({}, a, {
[k]: `./${
/d3js\.org\/d3\./.test(v)
? "d3"
: /^@/.test(pkg)
? pkg.slice(pkg.indexOf("/") + 1)
: pkg
}.js`
}),
{}
);

const config = {
map: offlineModules,
packages: {
"plugin-babel": {
main: "plugin-babel.js"
}
},
meta: { "*.json": { loader: "plugin-json" } },
transpiler: "plugin-babel"
};

SystemJS.config(config);

export default SystemJS.import("../index.js");
Loading