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.

216 lines
5.9 KiB

  1. var fs = require('fs');
  2. var async = require('async');
  3. var gulp = require('gulp');
  4. var gutil = require('gulp-util');
  5. var concat = require('gulp-concat');
  6. var cleanCSS = require('gulp-clean-css');
  7. var rename = require("gulp-rename");
  8. var webpack = require('webpack');
  9. var uglify = require('uglify-js');
  10. var rimraf = require('rimraf');
  11. var merge = require('merge-stream');
  12. var argv = require('yargs').argv;
  13. var ENTRY = './index.js';
  14. var HEADER = './lib/header.js';
  15. var DIST = './dist';
  16. var VIS_JS = 'vis.js';
  17. var VIS_MAP = 'vis.map';
  18. var VIS_MIN_JS = 'vis.min.js';
  19. var VIS_CSS = 'vis.css';
  20. var VIS_MIN_CSS = 'vis.min.css';
  21. var INDIVIDUAL_JS_BUNDLES = [
  22. {entry: './index-timeline-graph2d.js', filename: 'vis-timeline-graph2d.min.js'},
  23. {entry: './index-network.js', filename: 'vis-network.min.js'},
  24. {entry: './index-graph3d.js', filename: 'vis-graph3d.min.js'}
  25. ];
  26. var INDIVIDUAL_CSS_BUNDLES = [
  27. {entry: ['./lib/shared/**/*.css', './lib/timeline/**/*.css'], filename: 'vis-timeline-graph2d.min.css'},
  28. {entry: ['./lib/shared/**/*.css', './lib/network/**/*.css'], filename: 'vis-network.min.css'}
  29. ];
  30. // generate banner with today's date and correct version
  31. function createBanner() {
  32. var today = gutil.date(new Date(), 'yyyy-mm-dd'); // today, formatted as yyyy-mm-dd
  33. var version = require('./package.json').version;
  34. return String(fs.readFileSync(HEADER))
  35. .replace('@@date', today)
  36. .replace('@@version', version);
  37. }
  38. var bannerPlugin = new webpack.BannerPlugin(createBanner(), {
  39. entryOnly: true,
  40. raw: true
  41. });
  42. var webpackModule = {
  43. loaders: [
  44. {
  45. test: /\.js$/,
  46. exclude: /node_modules/,
  47. loader: 'babel',
  48. query: {
  49. cacheDirectory: true,
  50. presets: ['es2015']
  51. }
  52. }
  53. ],
  54. // exclude requires of moment.js language files
  55. wrappedContextRegExp: /$^/
  56. };
  57. var webpackConfig = {
  58. entry: ENTRY,
  59. output: {
  60. library: 'vis',
  61. libraryTarget: 'umd',
  62. path: DIST,
  63. filename: VIS_JS,
  64. sourcePrefix: ' '
  65. },
  66. module: webpackModule,
  67. plugins: [ bannerPlugin ],
  68. cache: true
  69. //debug: true,
  70. //bail: true
  71. };
  72. var uglifyConfig = {
  73. outSourceMap: VIS_MAP,
  74. output: {
  75. comments: /@license/
  76. }
  77. };
  78. // create a single instance of the compiler to allow caching
  79. var compiler = webpack(webpackConfig);
  80. function handleCompilerCallback (err, stats) {
  81. if (err) {
  82. gutil.log(err.toString());
  83. }
  84. if (stats && stats.compilation && stats.compilation.errors) {
  85. // output soft errors
  86. stats.compilation.errors.forEach(function (err) {
  87. gutil.log(err.toString());
  88. });
  89. if (err || stats.compilation.errors.length > 0) {
  90. gutil.beep(); // TODO: this does not work on my system
  91. }
  92. }
  93. }
  94. // clean the dist/img directory
  95. gulp.task('clean', function (cb) {
  96. rimraf(DIST + '/img', cb);
  97. });
  98. gulp.task('bundle-js', function (cb) {
  99. // update the banner contents (has a date in it which should stay up to date)
  100. bannerPlugin.banner = createBanner();
  101. compiler.run(function (err, stats) {
  102. handleCompilerCallback(err, stats);
  103. cb();
  104. });
  105. });
  106. // create individual bundles for timeline+graph2d, network, graph3d
  107. gulp.task('bundle-js-individual', function (cb) {
  108. // update the banner contents (has a date in it which should stay up to date)
  109. bannerPlugin.banner = createBanner();
  110. async.each(INDIVIDUAL_JS_BUNDLES, function (item, callback) {
  111. var webpackTimelineConfig = {
  112. entry: item.entry,
  113. output: {
  114. library: 'vis',
  115. libraryTarget: 'umd',
  116. path: DIST,
  117. filename: item.filename,
  118. sourcePrefix: ' '
  119. },
  120. module: webpackModule,
  121. plugins: [ bannerPlugin, new webpack.optimize.UglifyJsPlugin() ],
  122. cache: true
  123. };
  124. var compiler = webpack(webpackTimelineConfig);
  125. compiler.run(function (err, stats) {
  126. handleCompilerCallback(err, stats);
  127. callback();
  128. });
  129. }, cb);
  130. });
  131. // bundle and minify css
  132. gulp.task('bundle-css', function () {
  133. return gulp.src('./lib/**/*.css')
  134. .pipe(concat(VIS_CSS))
  135. .pipe(gulp.dest(DIST))
  136. // TODO: nicer to put minifying css in a separate task?
  137. .pipe(cleanCSS())
  138. .pipe(rename(VIS_MIN_CSS))
  139. .pipe(gulp.dest(DIST));
  140. });
  141. // bundle and minify individual css
  142. gulp.task('bundle-css-individual', function (cb) {
  143. async.each(INDIVIDUAL_CSS_BUNDLES, function (item, callback) {
  144. return gulp.src(item.entry)
  145. .pipe(concat(item.filename))
  146. .pipe(cleanCSS())
  147. .pipe(rename(item.filename))
  148. .pipe(gulp.dest(DIST))
  149. .on('end', callback);
  150. }, cb);
  151. });
  152. gulp.task('copy', ['clean'], function () {
  153. var network = gulp.src('./lib/network/img/**/*')
  154. .pipe(gulp.dest(DIST + '/img/network'));
  155. return network;
  156. });
  157. gulp.task('minify', ['bundle-js'], function (cb) {
  158. var result = uglify.minify([DIST + '/' + VIS_JS], uglifyConfig);
  159. // note: we add a newline '\n' to the end of the minified file to prevent
  160. // any issues when concatenating the file downstream (the file ends
  161. // with a comment).
  162. fs.writeFileSync(DIST + '/' + VIS_MIN_JS, result.code + '\n');
  163. fs.writeFileSync(DIST + '/' + VIS_MAP, result.map.replace(/"\.\/dist\//g, '"'));
  164. cb();
  165. });
  166. gulp.task('bundle', ['bundle-js', 'bundle-js-individual', 'bundle-css', 'bundle-css-individual', 'copy']);
  167. // read command line arguments --bundle and --minify
  168. var bundle = 'bundle' in argv;
  169. var minify = 'minify' in argv;
  170. var watchTasks = [];
  171. if (bundle || minify) {
  172. // do bundling and/or minifying only when specified on the command line
  173. watchTasks = [];
  174. if (bundle) watchTasks.push('bundle');
  175. if (minify) watchTasks.push('minify');
  176. }
  177. else {
  178. // by default, do both bundling and minifying
  179. watchTasks = ['bundle', 'minify'];
  180. }
  181. // The watch task (to automatically rebuild when the source code changes)
  182. gulp.task('watch', watchTasks, function () {
  183. gulp.watch(['index.js', 'lib/**/*'], watchTasks);
  184. });
  185. // The default task (called when you run `gulp`)
  186. gulp.task('default', ['clean', 'bundle', 'minify']);