Skip to content

Commit 4232ab1

Browse files
authored
Merge pull request #914 from plotly/test-requirejs
Test plotly.min.js in Require.js environment
2 parents 06af109 + 3bd8241 commit 4232ab1

File tree

8 files changed

+110
-53
lines changed

8 files changed

+110
-53
lines changed

dist/plotly.min.js

+53-53
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
@@ -110,6 +110,7 @@
110110
"open": "0.0.5",
111111
"prepend-file": "^1.3.0",
112112
"prettysize": "0.0.3",
113+
"requirejs": "^2.3.1",
113114
"through2": "^2.0.0",
114115
"uglify-js": "^2.6.1",
115116
"watchify": "^3.7.0",

tasks/bundle.js

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var UglifyJS = require('uglify-js');
77
var constants = require('./util/constants');
88
var common = require('./util/common');
99
var compressAttributes = require('./util/compress_attributes');
10+
var patchMinified = require('./util/patch_minified');
1011
var doesFileExist = common.doesFileExist;
1112

1213
/*
@@ -83,6 +84,7 @@ function _bundle(pathToIndex, pathToBundle, opts) {
8384

8485
if(outputMinified) {
8586
var minifiedCode = UglifyJS.minify(buf.toString(), constants.uglifyOptions).code;
87+
minifiedCode = patchMinified(minifiedCode);
8688

8789
fs.writeFile(pathToMinBundle, minifiedCode, function(err) {
8890
if(err) throw err;

tasks/pretest.js

+11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var common = require('./util/common');
77
makeCredentialsFile();
88
makeSetPlotConfigFile();
99
makeTestImageFolders();
10+
makeRequireJSFixture();
1011

1112
// Create a credentials json file,
1213
// to be required in jasmine test suites and test dashboard
@@ -52,6 +53,16 @@ function makeTestImageFolders() {
5253
makeOne(constants.pathToTestImagesDiff, 'test image diff folder');
5354
}
5455

56+
// Make script file that define plotly in a RequireJS context
57+
function makeRequireJSFixture() {
58+
var bundle = fs.readFileSync(constants.pathToPlotlyDistMin, 'utf-8'),
59+
template = 'define(\'plotly\', function(require, exports, module) { {{bundle}} });',
60+
index = template.replace('{{bundle}}', bundle);
61+
62+
common.writeFile(constants.pathToRequireJSFixture, index);
63+
logger('make build/requirejs_fixture.js');
64+
}
65+
5566
function logger(task) {
5667
console.log('ok ' + task);
5768
}

tasks/util/constants.js

+2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ module.exports = {
6666

6767
pathToJasmineTests: path.join(pathToRoot, 'test/jasmine/tests'),
6868
pathToJasmineBundleTests: path.join(pathToRoot, 'test/jasmine/bundle_tests'),
69+
pathToRequireJS: path.join(pathToRoot, 'node_modules', 'requirejs', 'require.js'),
70+
pathToRequireJSFixture: path.join(pathToBuild, 'requirejs_fixture.js'),
6971

7072
// this mapbox access token is 'public', no need to hide it
7173
// more info: https://www.mapbox.com/help/define-access-token/

tasks/util/patch_minified.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var STR_TO_REPLACE = 'require("+a(r)+");';
2+
var STR_NEW = 'require("+ a(r) +");';
3+
4+
/* Uber hacky in-house fix to
5+
*
6+
* https://github.com/substack/webworkify/issues/29
7+
*
8+
* so that plotly.min.js loads in Jupyter NBs, more info here:
9+
*
10+
* https://github.com/plotly/plotly.py/pull/545
11+
*
12+
*/
13+
module.exports = function patchMinified(minifiedCode) {
14+
return minifiedCode.replace(STR_TO_REPLACE, STR_NEW);
15+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
describe('plotly.js + require.js', function() {
2+
'use strict';
3+
4+
it('should preserve require.js globals', function() {
5+
expect(window.requirejs).toBeDefined();
6+
expect(window.define).toBeDefined();
7+
expect(window.require).toBeDefined();
8+
});
9+
10+
it('should be able to import plotly.min.js', function(done) {
11+
require(['plotly'], function(Plotly) {
12+
expect(Plotly).toBeDefined();
13+
done();
14+
});
15+
});
16+
});

test/jasmine/karma.conf.js

+10
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@
1313
*
1414
*/
1515

16+
var constants = require('../../tasks/util/constants');
17+
1618
var arg = process.argv[4];
1719

1820
var testFileGlob = arg ? arg : 'tests/*_test.js';
1921
var isSingleSuiteRun = (arg && arg.indexOf('bundle_tests/') === -1);
22+
var isRequireJSTest = (arg && arg.indexOf('bundle_tests/requirejs') !== -1);
2023

2124
var pathToMain = '../../lib/index.js';
2225
var pathToJQuery = 'assets/jquery-1.8.3.min.js';
@@ -113,6 +116,13 @@ if(isSingleSuiteRun) {
113116
func.defaultConfig.preprocessors[pathToMain] = ['browserify'];
114117
func.defaultConfig.preprocessors[testFileGlob] = ['browserify'];
115118
}
119+
else if(isRequireJSTest) {
120+
func.defaultConfig.files = [
121+
constants.pathToRequireJS,
122+
constants.pathToRequireJSFixture,
123+
testFileGlob
124+
];
125+
}
116126
else {
117127
func.defaultConfig.files = [
118128
pathToJQuery,

0 commit comments

Comments
 (0)