-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathbundle_wrapper.mjs
84 lines (70 loc) · 2.3 KB
/
bundle_wrapper.mjs
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import fsExtra from 'fs-extra';
import prependFile from 'prepend-file';
import { build } from 'esbuild';
import esbuildConfig from '../../esbuild-config.js';
import browserifyAdapter from 'esbuild-plugin-browserify-adapter';
import transform from '../../tasks/compress_attributes.js';
import common from './common.js';
var basePlugins = esbuildConfig.plugins;
/** Convenience bundle wrapper
*
* @param {string} pathToIndex path to index file to bundle
* @param {string} pathToBunlde path to destination bundle
* @param {object} opts
* Bundle options:
* - standalone {string}
* Additional option:
* - noCompressAttributes {boolean} skip attribute meta compression?
* @param {function} cb callback
*
* Otherwise outputs two file: one un-minified bundle and one minified bundle.
*
* Logs basename of bundle when completed.
*/
export default async function _bundle(pathToIndex, pathToBundle, opts, cb) {
opts = opts || {};
var config = {...esbuildConfig};
config.entryPoints = [pathToIndex];
config.outfile = pathToBundle;
config.minify = !!opts.minify;
if(!opts.noCompressAttributes) {
config.plugins = basePlugins.concat([browserifyAdapter(transform)]);
}
if(opts.noPlugins) config.plugins = [];
await build(config);
addWrapper(pathToBundle)
if(cb) cb();
}
// Until https://github.com/evanw/esbuild/pull/513 is merged
// Thanks to https://github.com/prantlf and https://github.com/birkskyum
function addWrapper(path){
prependFile.sync(
path,
[
'(',
' function(root, factory) {',
' if (typeof define === "function" && define.amd) {',
' define(factory);',
' } else if (typeof module === "object" && module.exports) {',
' module.exports = factory();',
' } else {',
' root.moduleName = factory();',
' }',
'} (typeof self !== "undefined" ? self : this, () => {',
''
].join('\n'),
common.throwOnError
);
fsExtra.appendFile(
path,
[
'',
'if (!(typeof define === "function" && define.amd)) {',
' window.Plotly = Plotly;',
'}',
'return Plotly;',
'}));',
].join('\n'),
common.throwOnError
);
}