Skip to content

Commit b0ac223

Browse files
committed
init ./tasks/ :
- mv pull_css & pull_font_svg from shelly/ - add build.js , preprocess.js
1 parent d41c5b2 commit b0ac223

File tree

7 files changed

+184
-51
lines changed

7 files changed

+184
-51
lines changed

shelly/plotlyjs/static/plotlyjs/pull_font_svg.js

Lines changed: 0 additions & 33 deletions
This file was deleted.

tasks/build.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
var fs = require('fs');
2+
var browserify = require('browserify');
3+
var UglifyJS = require('uglify-js');
4+
5+
var compressAttributes = require('./util/compress_attributes');
6+
var constants = require('./util/constants');
7+
8+
9+
/*
10+
* This script takes one argument
11+
*
12+
* Run `npm run build -- dev` or `npm run build -- --dev`
13+
* to include source map in the plotly.js bundle
14+
*
15+
*/
16+
var arg = process.argv[2];
17+
var DEV = (arg === 'dev') || (arg === '--dev');
18+
19+
20+
// Check if style and font build files are there
21+
try {
22+
fs.statSync(constants.pathToCSSBuild).isFile();
23+
fs.statSync(constants.pathToFontSVGBuild).isFile();
24+
}
25+
catch(e) {
26+
throw new Error('Please run `npm run preprocess` first');
27+
}
28+
29+
30+
// Browserify plotly.js
31+
browserify(constants.pathToPlotlySrc, {
32+
debug: DEV,
33+
standalone: 'Plotly',
34+
transform: [compressAttributes]
35+
})
36+
.bundle(function(err, buf) {
37+
if(err) throw err;
38+
39+
// generate plotly.min.js
40+
if(!DEV) {
41+
fs.writeFile(
42+
constants.pathToPlotlyDistMin,
43+
UglifyJS.minify(buf.toString(), constants.uglifyOptions).code
44+
);
45+
}
46+
})
47+
.pipe(fs.createWriteStream(constants.pathToPlotlyDist));
48+
49+
50+
// Browserify the geo assets
51+
browserify(constants.pathToPlotlyGeoAssetsSrc, {
52+
standalone: 'PlotlyGeoAssets'
53+
})
54+
.bundle(function(err, buf) {
55+
if(err) throw err;
56+
})
57+
.pipe(fs.createWriteStream(constants.pathToPlotlyGeoAssetsDist));
58+
59+
60+
61+
// Browserify the plotly.js with meta
62+
browserify(constants.pathToPlotlySrc, {
63+
debug: DEV,
64+
standalone: 'Plotly'
65+
})
66+
.bundle(function(err, buf) {
67+
if(err) throw err;
68+
})
69+
.pipe(fs.createWriteStream(constants.pathToPlotlyDistWithMeta));

tasks/preprocess.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var fs = require('fs');
2+
var sass = require('node-sass');
3+
4+
var pullCSS = require('./util/pull_css');
5+
var pullFontSVG = require('./util/pull_font_svg');
6+
var constants = require('./util/constants');
7+
8+
9+
// convert scss to css
10+
sass.render({
11+
file: constants.pathToSCSS,
12+
outputStyle: 'compressed'
13+
}, function(err, result) {
14+
if(err) console.log('SASS error');
15+
16+
// css to js
17+
pullCSS(String(result.css), constants.pathToCSSBuild);
18+
});
19+
20+
// convert font svg into js
21+
fs.readFile(constants.pathToFontSVG, function(err, data) {
22+
pullFontSVG(data.toString(), constants.pathToFontSVGBuild);
23+
});

tasks/util/constants.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
var path = require('path');
2+
3+
var pathToSrc = path.join(__dirname, '../../src/');
4+
var pathToDist = path.join(__dirname, '../../dist/');
5+
6+
module.exports = {
7+
pathToPlotlySrc: path.join(pathToSrc, 'plotly.js'),
8+
pathToPlotlyDist: path.join(pathToDist, 'plotly.js'),
9+
pathToPlotlyDistMin: path.join(pathToDist, 'plotly.min.js'),
10+
pathToPlotlyDistWithMeta: path.join(pathToDist, 'plotly-with-meta.js'),
11+
12+
pathToPlotlyGeoAssetsSrc: path.join(pathToSrc, 'geo/geo-assets.js'),
13+
pathToPlotlyGeoAssetsDist: path.join(pathToDist, 'plotly-geo-assets.js'),
14+
15+
pathToFontSVG: path.join(pathToSrc, 'fonts/ploticon/ploticon.svg'),
16+
pathToFontSVGBuild: path.join(pathToSrc, 'fonts/ploticon.js'),
17+
18+
pathToSCSS: path.join(pathToSrc, 'css/scss/style.scss'),
19+
pathToCSSBuild: path.join(pathToSrc, 'css/plotcss.js'),
20+
21+
uglifyOptions: {
22+
fromString: true,
23+
mangle: true,
24+
compress: {
25+
warnings: false,
26+
screw_ie8: true
27+
},
28+
output: {
29+
beautify: false,
30+
ascii_only: true
31+
}
32+
}
33+
};
Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
var fs = require('fs');
22

3-
fs.readFile(__dirname + '/build/plotlyjs-style.css', {encoding: 'utf8'}, function(err, data) {
4-
'use strict';
5-
if(err) throw err;
63

4+
module.exports = function pullCSS(data, pathOut) {
75
var rules = {};
86

97
data.split(/\s*\}\s*/).forEach(function(chunk) {
@@ -21,6 +19,7 @@ fs.readFile(__dirname + '/build/plotlyjs-style.css', {encoding: 'utf8'}, functio
2119
'found: ' + selectorList);
2220
}
2321
});
22+
2423
selectorList = selectorList
2524
.replace(/[\.]js-plotly-plot [\.]plotly/g, 'X')
2625
.replace(/[\.]plotly-notifier/g, 'Y');
@@ -34,18 +33,24 @@ fs.readFile(__dirname + '/build/plotlyjs-style.css', {encoding: 'utf8'}, functio
3433
rules[selectorList] = rules[selectorList] || '' + rule;
3534
});
3635

37-
var rulesStr = JSON.stringify(rules, null, 4)
38-
.replace(/\"(\w+)\":/g, '$1:');
39-
40-
fs.writeFile(__dirname + '/build/plotcss.js',
41-
'\'use strict\';\n\nvar Plotly = require(\'../src/plotly\');\nvar rules = ' + rulesStr + ';\n\n' +
42-
'for(var selector in rules) {\n' +
43-
' var fullSelector = selector.replace(/^,/,\' ,\')\n' +
44-
' .replace(/X/g, \'.js-plotly-plot .plotly\')\n' +
45-
' .replace(/Y/g, \'.plotly-notifier\');\n' +
46-
' Plotly.Lib.addStyleRule(fullSelector, rules[selector]);\n' +
47-
'}\n',
48-
function(err2) { if(err2) throw err2; }
49-
);
50-
51-
});
36+
var rulesStr = JSON.stringify(rules, null, 4).replace(/\"(\w+)\":/g, '$1:');
37+
38+
var outStr = [
39+
'\'use strict\';',
40+
'',
41+
'var Plotly = require(\'../plotly\');',
42+
'var rules = ' + rulesStr + ';',
43+
'',
44+
'for(var selector in rules) {',
45+
' var fullSelector = selector.replace(/^,/,\' ,\')',
46+
' .replace(/X/g, \'.js-plotly-plot .plotly\')',
47+
' .replace(/Y/g, \'.plotly-notifier\');',
48+
' Plotly.Lib.addStyleRule(fullSelector, rules[selector]);',
49+
'}',
50+
''
51+
].join('\n');
52+
53+
fs.writeFile(pathOut, outStr, function(err) {
54+
if(err) throw err;
55+
});
56+
};

tasks/util/pull_font_svg.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
var fs = require('fs');
2+
var xml2js = require('xml2js');
3+
4+
var parser = new xml2js.Parser();
5+
6+
7+
module.exports = function pullFontSVG(data, pathOut) {
8+
parser.parseString(data, function(err, result) {
9+
if(err) throw err;
10+
11+
var font_obj = result.svg.defs[0].font[0],
12+
default_width = Number(font_obj.$['horiz-adv-x']),
13+
chars = {
14+
ascent: Number(font_obj['font-face'][0].$.ascent),
15+
descent: Number(font_obj['font-face'][0].$.descent)
16+
};
17+
18+
font_obj.glyph.forEach(function(glyph) {
19+
chars[glyph.$['glyph-name']] = {
20+
width: Number(glyph.$['horiz-adv-x']) || default_width,
21+
path: glyph.$.d
22+
};
23+
});
24+
25+
// turn remaining double quotes into single
26+
var charStr = JSON.stringify(chars, null, 4).replace(/\"/g, '\'');
27+
28+
var outStr = '/*jshint quotmark:true */\n\'use strict\';\n\n' +
29+
'module.exports = ' + charStr + ';\n';
30+
31+
fs.writeFile(pathOut, outStr, function(err) {
32+
if(err) throw err;
33+
});
34+
});
35+
};
36+

0 commit comments

Comments
 (0)