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.

179 lines
5.0 KiB

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