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.

188 lines
5.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_MIN_JS = 'vis.min.js';
  18. var VIS_LIGHT_JS = 'vis-light.js';
  19. var VIS_LIGHT_MAP = 'vis-light.map';
  20. var VIS_LIGHT_MIN_JS = 'vis-light.min.js';
  21. var VIS_CSS = 'vis.css';
  22. var VIS_MIN_CSS = 'vis.min.css';
  23. // generate banner with today's date and correct version
  24. function createBanner() {
  25. var today = gutil.date(new Date(), 'yyyy-mm-dd'); // today, formatted as yyyy-mm-dd
  26. var version = require('./package.json').version;
  27. return String(fs.readFileSync(HEADER))
  28. .replace('@@date', today)
  29. .replace('@@version', version);
  30. }
  31. var bannerPlugin = new webpack.BannerPlugin(createBanner(), {
  32. entryOnly: true,
  33. raw: true
  34. });
  35. // TODO: the moment.js language files should be excluded by default (they are quite big)
  36. var webpackConfig = {
  37. entry: ENTRY,
  38. output: {
  39. library: 'vis',
  40. libraryTarget: 'umd',
  41. path: DIST,
  42. filename: VIS_JS,
  43. sourcePrefix: ' '
  44. },
  45. // exclude requires of moment.js language files
  46. module: {
  47. wrappedContextRegExp: /$^/
  48. },
  49. plugins: [ bannerPlugin ],
  50. cache: true
  51. };
  52. var webpackConfigLight = {
  53. entry: ENTRY,
  54. output: {
  55. library: 'vis',
  56. libraryTarget: 'umd',
  57. path: DIST,
  58. filename: VIS_LIGHT_JS,
  59. sourcePrefix: ' '
  60. },
  61. externals: [
  62. 'hammerjs',
  63. 'moment'
  64. ],
  65. plugins: [ bannerPlugin ],
  66. cache: 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. var compilerLight = webpack(webpackConfigLight);
  77. // clean the dist/img directory
  78. gulp.task('clean', function (cb) {
  79. rimraf(DIST + '/img', cb);
  80. });
  81. gulp.task('bundle-js', ['clean'], function (cb) {
  82. // update the banner contents (has a date in it which should stay up to date)
  83. bannerPlugin.banner = createBanner();
  84. compiler.run(function (err, stats) {
  85. if (err) gutil.log(err);
  86. cb();
  87. });
  88. });
  89. gulp.task('bundle-js-light', ['clean'], function (cb) {
  90. // update the banner contents (has a date in it which should stay up to date)
  91. bannerPlugin.banner = createBanner();
  92. compilerLight.run(function (err, stats) {
  93. if (err) gutil.log(err);
  94. cb();
  95. });
  96. });
  97. // bundle and minify css
  98. gulp.task('bundle-css', ['clean'], function () {
  99. var files = [
  100. './lib/timeline/component/css/timeline.css',
  101. './lib/timeline/component/css/panel.css',
  102. './lib/timeline/component/css/labelset.css',
  103. './lib/timeline/component/css/itemset.css',
  104. './lib/timeline/component/css/item.css',
  105. './lib/timeline/component/css/timeaxis.css',
  106. './lib/timeline/component/css/currenttime.css',
  107. './lib/timeline/component/css/customtime.css',
  108. './lib/timeline/component/css/animation.css',
  109. './lib/timeline/component/css/dataaxis.css',
  110. './lib/timeline/component/css/pathStyles.css',
  111. './lib/network/css/network-manipulation.css',
  112. './lib/network/css/network-navigation.css'
  113. ];
  114. return gulp.src(files)
  115. .pipe(concat(VIS_CSS))
  116. .pipe(gulp.dest(DIST))
  117. // TODO: nicer to put minifying css in a separate task?
  118. .pipe(minifyCSS())
  119. .pipe(rename(VIS_MIN_CSS))
  120. .pipe(gulp.dest(DIST));
  121. });
  122. gulp.task('copy', ['clean'], function () {
  123. var network = gulp.src('./lib/network/img/**/*')
  124. .pipe(gulp.dest(DIST + '/img/network'));
  125. var timeline = gulp.src('./lib/timeline/img/**/*')
  126. .pipe(gulp.dest(DIST + '/img/timeline'));
  127. return merge(network, timeline);
  128. });
  129. gulp.task('minify', ['bundle-js'], function (cb) {
  130. // minify full version of vis.js
  131. var result = uglify.minify([DIST + '/' + VIS_JS], uglifyConfig);
  132. fs.writeFileSync(DIST + '/' + VIS_MIN_JS, result.code);
  133. fs.writeFileSync(DIST + '/' + VIS_MAP, result.map);
  134. // minify light version of vis.js (external dependencies excluded)
  135. var result = uglify.minify([DIST + '/' + VIS_LIGHT_JS], uglifyConfig);
  136. fs.writeFileSync(DIST + '/' + VIS_LIGHT_MIN_JS, result.code);
  137. fs.writeFileSync(DIST + '/' + VIS_LIGHT_MAP, result.map);
  138. cb();
  139. });
  140. gulp.task('bundle', ['bundle-js', 'bundle-js-light', 'bundle-css', 'copy']);
  141. // read command line arguments --bundle and --minify
  142. var bundle = 'bundle' in argv;
  143. var minify = 'minify' in argv;
  144. var watchTasks = [];
  145. if (bundle || minify) {
  146. // do bundling and/or minifying only when specified on the command line
  147. watchTasks = [];
  148. if (bundle) watchTasks.push('bundle');
  149. if (minify) watchTasks.push('minify');
  150. }
  151. else {
  152. // by default, do both bundling and minifying
  153. watchTasks = ['bundle', 'minify'];
  154. }
  155. // The watch task (to automatically rebuild when the source code changes)
  156. gulp.task('watch', watchTasks, function () {
  157. gulp.watch(['index.js', 'lib/**/*'], watchTasks);
  158. });
  159. // The default task (called when you run `gulp`)
  160. gulp.task('default', ['clean', 'bundle', 'minify']);