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.

149 lines
4.2 KiB

  1. var fs = require('fs');
  2. var gulp = require('gulp');
  3. var gutil = require('gulp-util');
  4. var concat = require('gulp-concat');
  5. var minifyCSS = require('gulp-minify-css');
  6. var rename = require("gulp-rename");
  7. var webpack = require('webpack');
  8. var uglify = require('uglify-js');
  9. var rimraf = require('rimraf');
  10. var merge = require('merge-stream');
  11. var argv = require('yargs').argv;
  12. var ENTRY = './index.js';
  13. var HEADER = './lib/header.js';
  14. var DIST = './dist';
  15. var VIS_JS = 'vis.js';
  16. var VIS_MAP = 'vis.map';
  17. var VIS_CSS = 'vis.css';
  18. var VIS_MIN_CSS = 'vis.min.css';
  19. var DIST_VIS_MIN_JS = DIST + '/vis.min.js';
  20. var DIST_VIS_MAP = DIST + '/' + VIS_MAP;
  21. // generate banner with today's date and correct version
  22. function createBanner() {
  23. var today = gutil.date(new Date(), 'yyyy-mm-dd'); // today, formatted as yyyy-mm-dd
  24. var version = require('./package.json').version;
  25. return String(fs.readFileSync(HEADER))
  26. .replace('@@date', today)
  27. .replace('@@version', version);
  28. }
  29. var bannerPlugin = new webpack.BannerPlugin(createBanner(), {
  30. entryOnly: true,
  31. raw: true
  32. });
  33. // TODO: the moment.js language files should be excluded by default (they are quite big)
  34. var webpackConfig = {
  35. entry: ENTRY,
  36. output: {
  37. library: 'vis',
  38. libraryTarget: 'umd',
  39. path: DIST,
  40. filename: VIS_JS,
  41. sourcePrefix: ' '
  42. },
  43. plugins: [ bannerPlugin ],
  44. cache: true
  45. };
  46. var uglifyConfig = {
  47. outSourceMap: VIS_MAP,
  48. output: {
  49. comments: /@license/
  50. }
  51. };
  52. // create a single instance of the compiler to allow caching
  53. var compiler = webpack(webpackConfig);
  54. // clean the dist/img directory
  55. gulp.task('clean', function (cb) {
  56. rimraf(DIST + '/img', cb);
  57. });
  58. gulp.task('bundle-js', ['clean'], function (cb) {
  59. // update the banner contents (has a date in it which should stay up to date)
  60. bannerPlugin.banner = createBanner();
  61. compiler.run(function (err, stats) {
  62. if (err) gutil.log(err);
  63. cb();
  64. });
  65. });
  66. // bundle and minify css
  67. gulp.task('bundle-css', ['clean'], function () {
  68. var files = [
  69. './lib/timeline/component/css/timeline.css',
  70. './lib/timeline/component/css/panel.css',
  71. './lib/timeline/component/css/labelset.css',
  72. './lib/timeline/component/css/itemset.css',
  73. './lib/timeline/component/css/item.css',
  74. './lib/timeline/component/css/timeaxis.css',
  75. './lib/timeline/component/css/currenttime.css',
  76. './lib/timeline/component/css/customtime.css',
  77. './lib/timeline/component/css/animation.css',
  78. './lib/timeline/component/css/dataaxis.css',
  79. './lib/timeline/component/css/pathStyles.css',
  80. './lib/network/css/network-manipulation.css',
  81. './lib/network/css/network-navigation.css'
  82. ];
  83. return gulp.src(files)
  84. .pipe(concat(VIS_CSS))
  85. .pipe(gulp.dest(DIST))
  86. // TODO: nicer to put minifying css in a separate task?
  87. .pipe(minifyCSS())
  88. .pipe(rename(VIS_MIN_CSS))
  89. .pipe(gulp.dest(DIST));
  90. });
  91. gulp.task('copy-img', ['clean'], function () {
  92. var network = gulp.src('./lib/network/img/**/*')
  93. .pipe(gulp.dest(DIST + '/img/network'));
  94. var timeline = gulp.src('./lib/timeline/img/**/*')
  95. .pipe(gulp.dest(DIST + '/img/timeline'));
  96. return merge(network, timeline);
  97. });
  98. gulp.task('minify', ['bundle-js'], function (cb) {
  99. var result = uglify.minify([DIST + '/' + VIS_JS], uglifyConfig);
  100. fs.writeFileSync(DIST_VIS_MIN_JS, result.code);
  101. fs.writeFileSync(DIST_VIS_MAP, result.map);
  102. cb();
  103. });
  104. gulp.task('bundle', ['bundle-js', 'bundle-css', 'copy-img']);
  105. // read command line arguments --bundle and --minify
  106. var bundle = 'bundle' in argv;
  107. var minify = 'minify' in argv;
  108. var watchTasks = [];
  109. if (bundle || minify) {
  110. // do bundling and/or minifying only when specified on the command line
  111. watchTasks = [];
  112. if (bundle) watchTasks.push('bundle');
  113. if (minify) watchTasks.push('minify');
  114. }
  115. else {
  116. // by default, do both bundling and minifying
  117. watchTasks = ['bundle', 'minify'];
  118. }
  119. // The watch task (to automatically rebuild when the source code changes)
  120. gulp.task('watch', watchTasks, function () {
  121. gulp.watch(['index.js', 'lib/**/*'], watchTasks);
  122. });
  123. // The default task (called when you run `gulp`)
  124. gulp.task('default', ['clean', 'bundle', 'minify']);