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.

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