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.

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