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.

220 lines
6.0 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. // generate details sourcempas of webpack modules
  70. devtool: 'source-map'
  71. //debug: true,
  72. //bail: true
  73. };
  74. var uglifyConfig = {
  75. outSourceMap: VIS_MAP,
  76. output: {
  77. comments: /@license/
  78. }
  79. };
  80. // create a single instance of the compiler to allow caching
  81. var compiler = webpack(webpackConfig);
  82. function handleCompilerCallback (err, stats) {
  83. if (err) {
  84. gutil.log(err.toString());
  85. }
  86. if (stats && stats.compilation && stats.compilation.errors) {
  87. // output soft errors
  88. stats.compilation.errors.forEach(function (err) {
  89. gutil.log(err.toString());
  90. });
  91. if (err || stats.compilation.errors.length > 0) {
  92. gutil.beep(); // TODO: this does not work on my system
  93. }
  94. }
  95. }
  96. // clean the dist/img directory
  97. gulp.task('clean', function (cb) {
  98. rimraf(DIST + '/img', cb);
  99. });
  100. gulp.task('bundle-js', function (cb) {
  101. // update the banner contents (has a date in it which should stay up to date)
  102. bannerPlugin.banner = createBanner();
  103. compiler.run(function (err, stats) {
  104. handleCompilerCallback(err, stats);
  105. cb();
  106. });
  107. });
  108. // create individual bundles for timeline+graph2d, network, graph3d
  109. gulp.task('bundle-js-individual', function (cb) {
  110. // update the banner contents (has a date in it which should stay up to date)
  111. bannerPlugin.banner = createBanner();
  112. async.each(INDIVIDUAL_JS_BUNDLES, function (item, callback) {
  113. var webpackTimelineConfig = {
  114. entry: item.entry,
  115. output: {
  116. library: 'vis',
  117. libraryTarget: 'umd',
  118. path: DIST,
  119. filename: item.filename,
  120. sourcePrefix: ' '
  121. },
  122. module: webpackModule,
  123. plugins: [ bannerPlugin, new webpack.optimize.UglifyJsPlugin() ],
  124. cache: true
  125. };
  126. var compiler = webpack(webpackTimelineConfig);
  127. compiler.run(function (err, stats) {
  128. handleCompilerCallback(err, stats);
  129. callback();
  130. });
  131. }, cb);
  132. });
  133. // bundle and minify css
  134. gulp.task('bundle-css', function () {
  135. return gulp.src('./lib/**/*.css')
  136. .pipe(concat(VIS_CSS))
  137. .pipe(gulp.dest(DIST))
  138. // TODO: nicer to put minifying css in a separate task?
  139. .pipe(cleanCSS())
  140. .pipe(rename(VIS_MIN_CSS))
  141. .pipe(gulp.dest(DIST));
  142. });
  143. // bundle and minify individual css
  144. gulp.task('bundle-css-individual', function (cb) {
  145. async.each(INDIVIDUAL_CSS_BUNDLES, function (item, callback) {
  146. return gulp.src(item.entry)
  147. .pipe(concat(item.filename))
  148. .pipe(cleanCSS())
  149. .pipe(rename(item.filename))
  150. .pipe(gulp.dest(DIST))
  151. .on('end', callback);
  152. }, cb);
  153. });
  154. gulp.task('copy', ['clean'], function () {
  155. var network = gulp.src('./lib/network/img/**/*')
  156. .pipe(gulp.dest(DIST + '/img/network'));
  157. return network;
  158. });
  159. gulp.task('minify', ['bundle-js'], function (cb) {
  160. var result = uglify.minify([DIST + '/' + VIS_JS], uglifyConfig);
  161. // note: we add a newline '\n' to the end of the minified file to prevent
  162. // any issues when concatenating the file downstream (the file ends
  163. // with a comment).
  164. fs.writeFileSync(DIST + '/' + VIS_MIN_JS, result.code + '\n');
  165. fs.writeFileSync(DIST + '/' + VIS_MAP, result.map.replace(/"\.\/dist\//g, '"'));
  166. cb();
  167. });
  168. gulp.task('bundle', ['bundle-js', 'bundle-js-individual', 'bundle-css', 'bundle-css-individual', 'copy']);
  169. // read command line arguments --bundle and --minify
  170. var bundle = 'bundle' in argv;
  171. var minify = 'minify' in argv;
  172. var watchTasks = [];
  173. if (bundle || minify) {
  174. // do bundling and/or minifying only when specified on the command line
  175. watchTasks = [];
  176. if (bundle) watchTasks.push('bundle');
  177. if (minify) watchTasks.push('minify');
  178. }
  179. else {
  180. // by default, do both bundling and minifying
  181. watchTasks = ['bundle', 'minify'];
  182. }
  183. // The watch task (to automatically rebuild when the source code changes)
  184. gulp.task('watch', watchTasks, function () {
  185. gulp.watch(['index.js', 'lib/**/*'], watchTasks);
  186. });
  187. // The default task (called when you run `gulp`)
  188. gulp.task('default', ['clean', 'bundle', 'minify']);