- Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathbundle_wrapper.mjs
91 lines (75 loc) · 2.4 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
85
86
87
88
89
90
91
importfsfrom'fs';
importfsExtrafrom'fs-extra';
importprependFilefrom'prepend-file';
import{build}from'esbuild';
importesbuildConfigfrom'../../esbuild-config.js';
importbrowserifyAdapterfrom'esbuild-plugin-browserify-adapter';
importtransformfrom'../../tasks/compress_attributes.js';
importcommonfrom'./common.js';
varbasePlugins=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.
*/
exportdefaultasyncfunction_bundle(pathToIndex,pathToBundle,opts,cb){
opts=opts||{};
varconfig={...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=[];
awaitbuild(config);
addWrapper(pathToBundle);
if(pathToBundle.endsWith('.js')){
varlen=pathToBundle.length;
varcssOutput=pathToBundle.slice(0,len-3)+'.css';
// remove unwanted css file
if(fs.existsSync(cssOutput)){
fs.unlinkSync(cssOutput);
}
}
if(cb)cb();
}
// Until https://github.com/evanw/esbuild/pull/513 is merged
// Thanks to https://github.com/prantlf and https://github.com/birkskyum
functionaddWrapper(path){
prependFile.sync(
path,
[
'(',
' function(root, factory) {',
' 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,
[
'',
'window.Plotly = Plotly;',
'return Plotly;',
'}));',
].join('\n'),
common.throwOnError
);
}