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.

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