gulpfile.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. var gulp = require('gulp');
  2. var eslint = require('gulp-eslint');
  3. var file = require('gulp-file');
  4. var replace = require('gulp-replace');
  5. var size = require('gulp-size');
  6. var streamify = require('gulp-streamify');
  7. var terser = require('gulp-terser');
  8. var zip = require('gulp-zip');
  9. var exec = require('child_process').exec;
  10. var karma = require('karma');
  11. var merge = require('merge-stream');
  12. var yargs = require('yargs');
  13. var path = require('path');
  14. var htmllint = require('gulp-htmllint');
  15. var pkg = require('./package.json');
  16. var argv = yargs
  17. .option('verbose', {default: false})
  18. .argv;
  19. var srcDir = './src/';
  20. var outDir = './dist/';
  21. gulp.task('bower', bowerTask);
  22. gulp.task('build', buildTask);
  23. gulp.task('package', packageTask);
  24. gulp.task('lint-html', lintHtmlTask);
  25. gulp.task('lint-js', lintJsTask);
  26. gulp.task('lint', gulp.parallel('lint-html', 'lint-js'));
  27. gulp.task('docs', docsTask);
  28. gulp.task('unittest', unittestTask);
  29. gulp.task('test', gulp.parallel('lint', 'unittest'));
  30. gulp.task('library-size', librarySizeTask);
  31. gulp.task('module-sizes', moduleSizesTask);
  32. gulp.task('size', gulp.parallel('library-size', 'module-sizes'));
  33. gulp.task('default', gulp.parallel('build'));
  34. function run(bin, args, done) {
  35. return new Promise(function(resolve, reject) {
  36. var exe = '"' + process.execPath + '"';
  37. var src = require.resolve(bin);
  38. var ps = exec([exe, src].concat(args || []).join(' '));
  39. ps.stdout.pipe(process.stdout);
  40. ps.stderr.pipe(process.stderr);
  41. ps.on('close', function(error) {
  42. if (error) {
  43. reject(error);
  44. } else {
  45. resolve();
  46. }
  47. });
  48. });
  49. }
  50. /**
  51. * Generates the bower.json manifest file which will be pushed along release tags.
  52. * Specs: https://github.com/bower/spec/blob/master/json.md
  53. */
  54. function bowerTask() {
  55. var json = JSON.stringify({
  56. name: pkg.name,
  57. description: pkg.description,
  58. homepage: pkg.homepage,
  59. license: pkg.license,
  60. version: pkg.version,
  61. main: outDir + 'Chart.js',
  62. ignore: [
  63. '.github',
  64. '.codeclimate.yml',
  65. '.gitignore',
  66. '.npmignore',
  67. '.travis.yml',
  68. 'scripts'
  69. ]
  70. }, null, 2);
  71. return file('bower.json', json, { src: true })
  72. .pipe(gulp.dest('./'));
  73. }
  74. function buildTask() {
  75. return run('rollup/bin/rollup', ['-c', argv.watch ? '--watch' : '']);
  76. }
  77. function packageTask() {
  78. return merge(
  79. // gather "regular" files landing in the package root
  80. gulp.src([outDir + '*.js', outDir + '*.css', 'LICENSE.md']),
  81. // since we moved the dist files one folder up (package root), we need to rewrite
  82. // samples src="../dist/ to src="../ and then copy them in the /samples directory.
  83. gulp.src('./samples/**/*', { base: '.' })
  84. .pipe(streamify(replace(/src="((?:\.\.\/)+)dist\//g, 'src="$1')))
  85. )
  86. // finally, create the zip archive
  87. .pipe(zip('Chart.js.zip'))
  88. .pipe(gulp.dest(outDir));
  89. }
  90. function lintJsTask() {
  91. var files = [
  92. 'samples/**/*.html',
  93. 'samples/**/*.js',
  94. 'src/**/*.js',
  95. 'test/**/*.js'
  96. ];
  97. // NOTE(SB) codeclimate has 'complexity' and 'max-statements' eslint rules way too strict
  98. // compare to what the current codebase can support, and since it's not straightforward
  99. // to fix, let's turn them as warnings and rewrite code later progressively.
  100. var options = {
  101. rules: {
  102. 'complexity': [1, 10],
  103. 'max-statements': [1, 30]
  104. }
  105. };
  106. return gulp.src(files)
  107. .pipe(eslint(options))
  108. .pipe(eslint.format())
  109. .pipe(eslint.failAfterError());
  110. }
  111. function lintHtmlTask() {
  112. return gulp.src('samples/**/*.html')
  113. .pipe(htmllint({
  114. failOnError: true,
  115. }));
  116. }
  117. function docsTask() {
  118. var bin = 'gitbook-cli/bin/gitbook.js';
  119. var cmd = argv.watch ? 'serve' : 'build';
  120. return run(bin, ['install', './'])
  121. .then(() => run(bin, [cmd, './', './dist/docs']));
  122. }
  123. function unittestTask(done) {
  124. new karma.Server({
  125. configFile: path.join(__dirname, 'karma.conf.js'),
  126. singleRun: !argv.watch,
  127. args: {
  128. coverage: !!argv.coverage,
  129. inputs: (argv.inputs || 'test/specs/**/*.js').split(';'),
  130. watch: argv.watch
  131. }
  132. },
  133. // https://github.com/karma-runner/gulp-karma/issues/18
  134. function(error) {
  135. error = error ? new Error('Karma returned with the error code: ' + error) : undefined;
  136. done(error);
  137. }).start();
  138. }
  139. function librarySizeTask() {
  140. return gulp.src('dist/Chart.bundle.min.js')
  141. .pipe(size({
  142. gzip: true
  143. }));
  144. }
  145. function moduleSizesTask() {
  146. return gulp.src(srcDir + '**/*.js')
  147. .pipe(terser())
  148. .pipe(size({
  149. showFiles: true,
  150. gzip: true
  151. }));
  152. }