Skip to content

Commit acff993

Browse files
committed
add UMD wrapper in bundle_wrapper
1 parent d1a806a commit acff993

File tree

5 files changed

+47
-24
lines changed

5 files changed

+47
-24
lines changed

esbuild-config.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
var glsl = require('esbuild-plugin-glsl').glsl;
22
var environmentPlugin = require('esbuild-plugin-environment').environmentPlugin;
3-
var umdWrapper = require('esbuild-plugin-umd-wrapper').umdWrapper;
43

54
module.exports = {
65
entryPoints: ['./lib/index.js'],
7-
format: 'cjs',
6+
format: 'iife',
87
globalName: 'Plotly',
98
bundle: true,
109
minify: false,
@@ -16,9 +15,6 @@ module.exports = {
1615
environmentPlugin({
1716
NODE_DEBUG: false,
1817
}),
19-
umdWrapper({
20-
libraryName: 'Plotly'
21-
})
2218
],
2319
alias: {
2420
stream: 'stream-browserify',

package-lock.json

-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@
137137
"esbuild-plugin-browserify-adapter": "^0.1.4",
138138
"esbuild-plugin-environment": "^0.3.0",
139139
"esbuild-plugin-glsl": "^1.2.2",
140-
"esbuild-plugin-umd-wrapper": "^2.0.0",
141140
"extra-iterable": "^2.5.22",
142141
"falafel": "^2.2.5",
143142
"fs-extra": "^10.1.0",

tasks/bundle.mjs

+6-11
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import prependFile from 'prepend-file';
44
import constants from './util/constants.js';
55
import common from './util/common.js';
66
import _bundle from './util/bundle_wrapper.mjs';
7-
import fsExtra from 'fs-extra';
87

98
var header = constants.licenseDist + '\n';
109
var pathToPlotlyDist = constants.pathToPlotlyDist;
@@ -28,11 +27,6 @@ if(!doesFileExist(constants.pathToCSSBuild)) {
2827
].join('\n'));
2928
}
3029

31-
function amdWrapper(path){
32-
prependFile.sync(path, "define(function (require, exports, module) {", common.throwOnError)
33-
fsExtra.appendFile(path, "})", common.throwOnError)
34-
}
35-
3630
// list of tasks to pass to run-series to not blow up
3731
// memory consumption.
3832
var tasks = [];
@@ -42,7 +36,7 @@ tasks.push(function(done) {
4236
_bundle(pathToPlotlyIndex, pathToPlotlyDist, {
4337
}, function() {
4438
prependFile.sync(pathToPlotlyDist, header, common.throwOnError);
45-
amdWrapper(pathToPlotlyDist)
39+
4640
done();
4741
});
4842
});
@@ -53,7 +47,7 @@ tasks.push(function(done) {
5347
minify: true,
5448
}, function() {
5549
prependFile.sync(pathToPlotlyDistMin, header, common.throwOnError);
56-
amdWrapper(pathToPlotlyDistMin)
50+
5751
done();
5852
});
5953
});
@@ -63,7 +57,7 @@ tasks.push(function(done) {
6357
_bundle(pathToPlotlyStrict, pathToPlotlyStrictDist, {
6458
}, function() {
6559
prependFile.sync(pathToPlotlyStrictDist, header.replace('plotly.js', 'plotly.js (strict)'), common.throwOnError);
66-
amdWrapper(pathToPlotlyStrictDist)
60+
6761
done();
6862
});
6963
});
@@ -74,7 +68,7 @@ tasks.push(function(done) {
7468
minify: true,
7569
}, function() {
7670
prependFile.sync(pathToPlotlyStrictDistMin, header.replace('plotly.js', 'plotly.js (strict - minified)'), common.throwOnError);
77-
amdWrapper(pathToPlotlyStrictDistMin)
71+
7872
done();
7973
});
8074
});
@@ -86,7 +80,7 @@ tasks.push(function(done) {
8680
standalone: 'PlotlyGeoAssets'
8781
}, function() {
8882
prependFile.sync(pathToPlotlyGeoAssetsDist, header, common.throwOnError);
89-
amdWrapper(pathToPlotlyGeoAssetsDist)
83+
9084
done();
9185
});
9286
});
@@ -97,6 +91,7 @@ tasks.push(function(done) {
9791
noCompressAttributes: true
9892
}, function() {
9993
prependFile.sync(pathToPlotlyDistWithMeta, header, common.throwOnError);
94+
10095
done();
10196
});
10297
});

tasks/util/bundle_wrapper.mjs

+40
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
import fsExtra from 'fs-extra';
2+
import prependFile from 'prepend-file';
3+
14
import { build } from 'esbuild';
25

36
import esbuildConfig from '../../esbuild-config.js';
47
import browserifyAdapter from 'esbuild-plugin-browserify-adapter';
58

69
import transform from '../../tasks/compress_attributes.js';
10+
import common from './common.js';
711

812
var basePlugins = esbuildConfig.plugins;
913

@@ -38,5 +42,41 @@ export default async function _bundle(pathToIndex, pathToBundle, opts, cb) {
3842
if(opts.noPlugins) config.plugins = [];
3943

4044
await build(config);
45+
46+
addWrapper(pathToBundle)
47+
4148
if(cb) cb();
4249
}
50+
51+
// Until https://github.com/evanw/esbuild/pull/513 is merged
52+
// Thanks to https://github.com/prantlf and https://github.com/birkskyum
53+
function addWrapper(path){
54+
prependFile.sync(
55+
path,
56+
[
57+
'(',
58+
' function(root, factory) {',
59+
' if (typeof define === "function" && define.amd) {',
60+
' define(factory);',
61+
' } else if (typeof module === "object" && module.exports) {',
62+
' module.exports = factory();',
63+
' } else {',
64+
' root.moduleName = factory();',
65+
' }',
66+
'} (typeof self !== "undefined" ? self : this, () => {',
67+
''
68+
].join('\n'),
69+
common.throwOnError
70+
);
71+
72+
fsExtra.appendFile(
73+
path,
74+
[
75+
'',
76+
'window.Plotly = Plotly;',
77+
'return Plotly;',
78+
'}));',
79+
].join('\n'),
80+
common.throwOnError
81+
);
82+
}

0 commit comments

Comments
 (0)