vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

229 lines
6.3 KiB

  1. var fs = require('fs');
  2. var async = require('async');
  3. var gulp = require('gulp');
  4. var gutil = require('gulp-util');
  5. var concat = require('gulp-concat');
  6. var minifyCSS = require('gulp-minify-css');
  7. var rename = require("gulp-rename");
  8. var webpack = require('webpack');
  9. var uglify = require('uglify-js');
  10. var rimraf = require('rimraf');
  11. var merge = require('merge-stream');
  12. var argv = require('yargs').argv;
  13. var ENTRY = './index.js';
  14. var HEADER = './lib/header.js';
  15. var DIST = './dist';
  16. var VIS_JS = 'vis.js';
  17. var VIS_MAP = 'vis.map';
  18. var VIS_MIN_JS = 'vis.min.js';
  19. var VIS_CSS = 'vis.css';
  20. var VIS_MIN_CSS = 'vis.min.css';
  21. var INDIVIDUAL_BUNDLES = [
  22. {entry: './index-timeline-graph2d.js', filename: 'vis-timeline-graph2d.min.js'},
  23. {entry: './index-network.js', filename: 'vis-network.min.js'},
  24. {entry: './index-graph3d.js', filename: 'vis-graph3d.min.js'}
  25. ];
  26. // generate banner with today's date and correct version
  27. function createBanner() {
  28. var today = gutil.date(new Date(), 'yyyy-mm-dd'); // today, formatted as yyyy-mm-dd
  29. var version = require('./package.json').version;
  30. return String(fs.readFileSync(HEADER))
  31. .replace('@@date', today)
  32. .replace('@@version', version);
  33. }
  34. var bannerPlugin = new webpack.BannerPlugin(createBanner(), {
  35. entryOnly: true,
  36. raw: true
  37. });
  38. var webpackModule = {
  39. loaders: [
  40. {
  41. test: /\.js$/,
  42. exclude: /node_modules/,
  43. loader: 'babel',
  44. query: {
  45. cacheDirectory: true,
  46. presets: ['es2015']
  47. }
  48. }
  49. ],
  50. // exclude requires of moment.js language files
  51. wrappedContextRegExp: /$^/
  52. };
  53. var webpackConfig = {
  54. entry: ENTRY,
  55. output: {
  56. library: 'vis',
  57. libraryTarget: 'umd',
  58. path: DIST,
  59. filename: VIS_JS,
  60. sourcePrefix: ' '
  61. },
  62. module: webpackModule,
  63. plugins: [ bannerPlugin ],
  64. cache: true
  65. //debug: true,
  66. //bail: true
  67. };
  68. var uglifyConfig = {
  69. outSourceMap: VIS_MAP,
  70. output: {
  71. comments: /@license/
  72. }
  73. };
  74. // create a single instance of the compiler to allow caching
  75. var compiler = webpack(webpackConfig);
  76. function handleCompilerCallback (err, stats) {
  77. if (err) {
  78. gutil.log(err.toString());
  79. }
  80. if (stats && stats.compilation && stats.compilation.errors) {
  81. // output soft errors
  82. stats.compilation.errors.forEach(function (err) {
  83. gutil.log(err.toString());
  84. });
  85. if (err || stats.compilation.errors.length > 0) {
  86. gutil.beep(); // TODO: this does not work on my system
  87. }
  88. }
  89. }
  90. // clean the dist/img directory
  91. gulp.task('clean', function (cb) {
  92. rimraf(DIST + '/img', cb);
  93. });
  94. gulp.task('bundle-js', function (cb) {
  95. // update the banner contents (has a date in it which should stay up to date)
  96. bannerPlugin.banner = createBanner();
  97. compiler.run(function (err, stats) {
  98. handleCompilerCallback(err, stats);
  99. cb();
  100. });
  101. });
  102. // create individual bundles for timeline+graph2d, network, graph3d
  103. gulp.task('bundle-js-individual', function (cb) {
  104. // update the banner contents (has a date in it which should stay up to date)
  105. bannerPlugin.banner = createBanner();
  106. async.each(INDIVIDUAL_BUNDLES, function (item, callback) {
  107. var webpackTimelineConfig = {
  108. entry: item.entry,
  109. output: {
  110. library: 'vis',
  111. libraryTarget: 'umd',
  112. path: DIST,
  113. filename: item.filename,
  114. sourcePrefix: ' '
  115. },
  116. module: webpackModule,
  117. plugins: [ bannerPlugin, new webpack.optimize.UglifyJsPlugin() ],
  118. cache: true
  119. };
  120. var compiler = webpack(webpackTimelineConfig);
  121. compiler.run(function (err, stats) {
  122. handleCompilerCallback(err, stats);
  123. callback();
  124. });
  125. }, cb);
  126. });
  127. // bundle and minify css
  128. gulp.task('bundle-css', function () {
  129. var files = [
  130. './lib/shared/activator.css',
  131. './lib/shared/bootstrap.css',
  132. './lib/shared/configuration.css',
  133. './lib/timeline/component/css/timeline.css',
  134. './lib/timeline/component/css/panel.css',
  135. './lib/timeline/component/css/labelset.css',
  136. './lib/timeline/component/css/itemset.css',
  137. './lib/timeline/component/css/item.css',
  138. './lib/timeline/component/css/timeaxis.css',
  139. './lib/timeline/component/css/currenttime.css',
  140. './lib/timeline/component/css/customtime.css',
  141. './lib/timeline/component/css/animation.css',
  142. './lib/timeline/component/css/dataaxis.css',
  143. './lib/timeline/component/css/pathStyles.css',
  144. './lib/network/css/network-manipulation.css',
  145. './lib/network/css/network-tooltip.css',
  146. './lib/network/css/network-navigation.css',
  147. './lib/network/css/network-colorpicker.css'
  148. ];
  149. return gulp.src(files)
  150. .pipe(concat(VIS_CSS))
  151. .pipe(gulp.dest(DIST))
  152. // TODO: nicer to put minifying css in a separate task?
  153. .pipe(minifyCSS())
  154. .pipe(rename(VIS_MIN_CSS))
  155. .pipe(gulp.dest(DIST));
  156. });
  157. gulp.task('copy', ['clean'], function () {
  158. var network = gulp.src('./lib/network/img/**/*')
  159. .pipe(gulp.dest(DIST + '/img/network'));
  160. var timeline = gulp.src('./lib/timeline/img/**/*')
  161. .pipe(gulp.dest(DIST + '/img/timeline'));
  162. return merge(network, timeline);
  163. });
  164. gulp.task('minify', ['bundle-js'], function (cb) {
  165. var result = uglify.minify([DIST + '/' + VIS_JS], uglifyConfig);
  166. // note: we add a newline '\n' to the end of the minified file to prevent
  167. // any issues when concatenating the file downstream (the file ends
  168. // with a comment).
  169. fs.writeFileSync(DIST + '/' + VIS_MIN_JS, result.code + '\n');
  170. fs.writeFileSync(DIST + '/' + VIS_MAP, result.map.replace(/"\.\/dist\//g, '"'));
  171. cb();
  172. });
  173. gulp.task('bundle', ['bundle-js', 'bundle-js-individual', 'bundle-css', 'copy']);
  174. // read command line arguments --bundle and --minify
  175. var bundle = 'bundle' in argv;
  176. var minify = 'minify' in argv;
  177. var watchTasks = [];
  178. if (bundle || minify) {
  179. // do bundling and/or minifying only when specified on the command line
  180. watchTasks = [];
  181. if (bundle) watchTasks.push('bundle');
  182. if (minify) watchTasks.push('minify');
  183. }
  184. else {
  185. // by default, do both bundling and minifying
  186. watchTasks = ['bundle', 'minify'];
  187. }
  188. // The watch task (to automatically rebuild when the source code changes)
  189. gulp.task('watch', watchTasks, function () {
  190. gulp.watch(['index.js', 'lib/**/*'], watchTasks);
  191. });
  192. // The default task (called when you run `gulp`)
  193. gulp.task('default', ['clean', 'bundle', 'minify']);