-
Notifications
You must be signed in to change notification settings - Fork 29
/
bundle.js
63 lines (56 loc) · 1.8 KB
/
bundle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { rollup } from 'rollup/dist/rollup.browser';
import buble from 'rollup-plugin-buble';
import hypothetical from './hypothetical';
import { d3Packages } from './d3Packages';
const transformFilesToObject = files =>
files
.filter(file => file.name.endsWith('.js'))
.reduce((accumulator, file) => {
accumulator['./' + file.name] = file.text;
return accumulator;
}, {});
const outputOptions = {
format: 'iife',
name: 'bundle',
sourcemap: 'inline',
globals: d3Packages.reduce((accumulator, packageName) => {
accumulator[packageName] = 'd3';
return accumulator;
}, {
react: 'React'
})
};
export const bundle = async (files) => {
const inputOptions = {
input: './index.js',
plugins: [
hypothetical({
files: transformFilesToObject(files)
}),
buble({
// Disable most ES6 transforms,
// use Buble mainly for its JSX transform.
target: {
chrome: 71
}
})
],
external: d3Packages.concat('react')
};
const rollupBundle = await rollup(inputOptions);
const { code, map } = await rollupBundle.generate(outputOptions);
// Monkey patch magic-string internals
// to support characters outside of the Latin1 range, e.g. Cyrillic.
//
// Related reading:
// - https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/btoa#Unicode_strings
// - https://github.com/Rich-Harris/magic-string/blob/3466b0230dddc95eb378ed3e0d199e36fbd1f572/src/SourceMap.js#L3
const toString = map.toString.bind(map);
map.toString = () => unescape(encodeURIComponent(toString()));
// Inspired by https://github.com/rollup/rollup/issues/121
const codeWithSourceMap = code + '\n//# sourceMappingURL=' + map.toUrl();
return [{
name: 'bundle.js',
text: codeWithSourceMap
}];
};