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.

158 lines
4.5 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_CSS = 'vis.css';
  19. var VIS_MIN_CSS = 'vis.min.css';
  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. // exclude requires of moment.js language files
  43. module: {
  44. wrappedContextRegExp: /$^/
  45. },
  46. plugins: [ bannerPlugin ],
  47. cache: true
  48. };
  49. var uglifyConfig = {
  50. outSourceMap: VIS_MAP,
  51. output: {
  52. comments: /@license/
  53. }
  54. };
  55. // create a single instance of the compiler to allow caching
  56. var compiler = webpack(webpackConfig);
  57. // clean the dist/img directory
  58. gulp.task('clean', function (cb) {
  59. rimraf(DIST + '/img', cb);
  60. });
  61. gulp.task('bundle-js', ['clean'], function (cb) {
  62. // update the banner contents (has a date in it which should stay up to date)
  63. bannerPlugin.banner = createBanner();
  64. compiler.run(function (err, stats) {
  65. if (err) gutil.log(err);
  66. cb();
  67. });
  68. });
  69. // bundle and minify css
  70. gulp.task('bundle-css', ['clean'], function () {
  71. var files = [
  72. './lib/shared/activator.css',
  73. './lib/shared/bootstrap.css',
  74. './lib/timeline/component/css/timeline.css',
  75. './lib/timeline/component/css/panel.css',
  76. './lib/timeline/component/css/labelset.css',
  77. './lib/timeline/component/css/itemset.css',
  78. './lib/timeline/component/css/item.css',
  79. './lib/timeline/component/css/timeaxis.css',
  80. './lib/timeline/component/css/currenttime.css',
  81. './lib/timeline/component/css/customtime.css',
  82. './lib/timeline/component/css/animation.css',
  83. './lib/timeline/component/css/dataaxis.css',
  84. './lib/timeline/component/css/pathStyles.css',
  85. './lib/network/css/network-manipulation.css',
  86. './lib/network/css/network-navigation.css'
  87. ];
  88. return gulp.src(files)
  89. .pipe(concat(VIS_CSS))
  90. .pipe(gulp.dest(DIST))
  91. // TODO: nicer to put minifying css in a separate task?
  92. .pipe(minifyCSS())
  93. .pipe(rename(VIS_MIN_CSS))
  94. .pipe(gulp.dest(DIST));
  95. });
  96. gulp.task('copy', ['clean'], function () {
  97. var network = gulp.src('./lib/network/img/**/*')
  98. .pipe(gulp.dest(DIST + '/img/network'));
  99. var timeline = gulp.src('./lib/timeline/img/**/*')
  100. .pipe(gulp.dest(DIST + '/img/timeline'));
  101. return merge(network, timeline);
  102. });
  103. gulp.task('minify', ['bundle-js'], function (cb) {
  104. var result = uglify.minify([DIST + '/' + VIS_JS], uglifyConfig);
  105. // note: we add a newline '\n' to the end of the minified file to prevent
  106. // any issues when concatenating the file downstream (the file ends
  107. // with a comment).
  108. fs.writeFileSync(DIST + '/' + VIS_MIN_JS, result.code + '\n');
  109. fs.writeFileSync(DIST + '/' + VIS_MAP, result.map);
  110. cb();
  111. });
  112. gulp.task('bundle', ['bundle-js', 'bundle-css', 'copy']);
  113. // read command line arguments --bundle and --minify
  114. var bundle = 'bundle' in argv;
  115. var minify = 'minify' in argv;
  116. var watchTasks = [];
  117. if (bundle || minify) {
  118. // do bundling and/or minifying only when specified on the command line
  119. watchTasks = [];
  120. if (bundle) watchTasks.push('bundle');
  121. if (minify) watchTasks.push('minify');
  122. }
  123. else {
  124. // by default, do both bundling and minifying
  125. watchTasks = ['bundle', 'minify'];
  126. }
  127. // The watch task (to automatically rebuild when the source code changes)
  128. gulp.task('watch', watchTasks, function () {
  129. gulp.watch(['index.js', 'lib/**/*'], watchTasks);
  130. });
  131. // The default task (called when you run `gulp`)
  132. gulp.task('default', ['clean', 'bundle', 'minify']);