archive.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. const gulp = require('gulp')
  2. const rename = require('gulp-rename')
  3. const filter = require('gulp-filter')
  4. const modify = require('gulp-modify-file')
  5. const zip = require('gulp-zip')
  6. // determines the name of the ZIP file
  7. const packageConfig = require('../package.json')
  8. const packageId = packageConfig.name + '-' + packageConfig.version
  9. gulp.task('archive', [
  10. 'archive:dist',
  11. 'archive:misc',
  12. 'archive:deps',
  13. 'archive:demos'
  14. ], function() {
  15. // make the zip, with a single root directory of a similar name
  16. return gulp.src('tmp/' + packageId + '/**/*', { base: 'tmp/' })
  17. .pipe(zip(packageId + '.zip'))
  18. .pipe(gulp.dest('dist/'))
  19. })
  20. gulp.task('archive:dist', [ 'dist' ], function() {
  21. return gulp.src([
  22. 'dist/*.{js,css}',
  23. 'dist/locale-all.js',
  24. 'dist/locale/*.js'
  25. ], {
  26. base: 'dist/'
  27. })
  28. .pipe(gulp.dest('tmp/' + packageId + '/'))
  29. })
  30. gulp.task('archive:misc', function() {
  31. return gulp.src([
  32. 'LICENSE.*',
  33. 'CHANGELOG.*',
  34. 'CONTRIBUTING.*'
  35. ])
  36. .pipe(rename({ extname: '.txt' }))
  37. .pipe(gulp.dest('tmp/' + packageId + '/'))
  38. })
  39. gulp.task('archive:deps', function() {
  40. return gulp.src([
  41. 'node_modules/moment/min/moment.min.js',
  42. 'node_modules/jquery/dist/jquery.min.js',
  43. 'node_modules/components-jqueryui/jquery-ui.min.js'
  44. ])
  45. .pipe(gulp.dest('tmp/' + packageId + '/lib/'))
  46. })
  47. // transfers demo files, transforming their paths to dependencies
  48. gulp.task('archive:demos', function() {
  49. return gulp.src('**/*', { cwd: 'demos/', base: 'demos/' })
  50. .pipe(htmlFileFilter)
  51. .pipe(demoPathModify)
  52. .pipe(htmlFileFilter.restore) // pipe thru files that didn't match htmlFileFilter
  53. .pipe(gulp.dest('tmp/' + packageId + '/demos/'))
  54. })
  55. const htmlFileFilter = filter('*.html', { restore: true })
  56. const demoPathModify = modify(function(content) {
  57. return content.replace(
  58. /((?:src|href)=['"])([^'"]*)(['"])/g,
  59. function(m0, m1, m2, m3) {
  60. return m1 + transformDemoPath(m2) + m3
  61. }
  62. )
  63. })
  64. function transformDemoPath(path) {
  65. // reroot 3rd party libs
  66. path = path.replace('../node_modules/moment/', '../lib/')
  67. path = path.replace('../node_modules/jquery/dist/', '../lib/')
  68. path = path.replace('../node_modules/components-jqueryui/', '../lib/')
  69. // reroot dist files to archive root
  70. path = path.replace('../dist/', '../')
  71. if (
  72. !/\.min\.(js|css)$/.test(path) && // not already minified
  73. !/^\w/.test(path) && // reference to demo util js/css file
  74. path !== '../locale-all.js' // this file is already minified
  75. ) {
  76. // use minified
  77. path = path.replace(/\/([^/]*)\.(js|css)$/, '/$1.min.$2')
  78. }
  79. return path
  80. }