tasks.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. const fs = require( 'fs' );
  7. const path = require( 'path' );
  8. const gulp = require( 'gulp' );
  9. const gulpCssnano = require( 'gulp-cssnano' );
  10. const gulpUglify = require( 'gulp-uglify' );
  11. const gutil = require( 'gulp-util' );
  12. const runSequence = require( 'run-sequence' );
  13. const utils = require( './utils' );
  14. const rollup = require( 'rollup' ).rollup;
  15. const rollupBabel = require( 'rollup-plugin-babel' );
  16. module.exports = ( config ) => {
  17. const args = utils.parseArguments();
  18. const sourceBuildDir = path.join( config.ROOT_DIR, config.BUILD_DIR, 'esnext' );
  19. const bundleDir = path.join( config.ROOT_DIR, config.BUNDLE_DIR );
  20. const temporaryEntryFilePath = './tmp/entryfile.js';
  21. const tasks = {
  22. /**
  23. * Remove all files from bundle directory.
  24. */
  25. clean() {
  26. return utils.clean( bundleDir, '*.*' );
  27. },
  28. /**
  29. * Combine whole editor files into two files `ckeditor.js` and `ckeditor.css`.
  30. *
  31. * @param {String} configFilePath Path to the bundle configuration file.
  32. * @return {Promise} Promise that resolve bundling for CSS anf JS.
  33. */
  34. generate( configFilePath ) {
  35. // When config file is not specified.
  36. if ( !configFilePath ) {
  37. // Then log error.
  38. gutil.log( gutil.colors.red( `Bundle Error: Path to the config file is required. 'gulp bundle --config path/to/file.js'` ) );
  39. // And stop process as failed.
  40. process.exit( 1 );
  41. }
  42. // Get configuration from the configuration file.
  43. const config = require( path.resolve( '.', configFilePath ) );
  44. // Create temporary entry file.
  45. fs.writeFileSync( temporaryEntryFilePath, utils.renderEntryFileContent( config ) );
  46. /**
  47. * Bundling JS by Rollup.
  48. */
  49. function bundleJS() {
  50. const outputFile = path.join( bundleDir, 'ckeditor.js' );
  51. return rollup( {
  52. entry: temporaryEntryFilePath,
  53. plugins: [
  54. rollupBabel( {
  55. presets: [ 'es2015-rollup' ]
  56. } )
  57. ]
  58. } ).then( ( bundle ) => {
  59. return bundle.write( {
  60. dest: outputFile,
  61. format: 'iife',
  62. moduleName: config.moduleName
  63. } );
  64. } ).then( () => {
  65. // If everything went well then remove temporary entry file.
  66. utils.clean( '', temporaryEntryFilePath );
  67. } ).catch( ( err ) => {
  68. // If something went wrong then log error.
  69. gutil.log( gutil.colors.red( `Bundle Error` ) );
  70. gutil.log( gutil.colors.red( err.stack ) );
  71. // And remove temporary entry file.
  72. utils.clean( '', temporaryEntryFilePath );
  73. } );
  74. }
  75. /**
  76. * CSS is already bundled by a build task, so we need only to copy it.
  77. */
  78. function bundleCSS() {
  79. const cssSource = path.join( sourceBuildDir, 'theme', 'ckeditor.css' );
  80. return utils.copyFile( cssSource, bundleDir );
  81. }
  82. // Lets wait for both - JS and CSS.
  83. return Promise.all( [ bundleJS(), bundleCSS() ] );
  84. },
  85. minify: {
  86. /**
  87. * JS minification by UglifyJS.
  88. */
  89. js() {
  90. let stream = gulp.src( path.join( bundleDir, 'ckeditor.js' ) )
  91. .pipe( gulpUglify() );
  92. return utils.saveFileFromStreamAsMinified( stream, bundleDir );
  93. },
  94. /**
  95. * CSS minification by cssnano.
  96. */
  97. css() {
  98. let stream = gulp.src( path.join( bundleDir, 'ckeditor.css' ) )
  99. .pipe( gulpCssnano() );
  100. return utils.saveFileFromStreamAsMinified( stream, bundleDir );
  101. }
  102. },
  103. register() {
  104. gulp.task( 'bundle:clean', tasks.clean );
  105. gulp.task( 'bundle:generate',
  106. [
  107. 'bundle:clean',
  108. 'build:js:esnext',
  109. 'build:themes:esnext'
  110. ],
  111. () => tasks.generate( args.config )
  112. );
  113. gulp.task( 'bundle:minify:js', tasks.minify.js );
  114. gulp.task( 'bundle:minify:css', tasks.minify.css );
  115. gulp.task( 'bundle:next', tasks.next );
  116. gulp.task( 'bundle', ( callback ) => {
  117. runSequence( 'bundle:generate', [ 'bundle:minify:js', 'bundle:minify:css' ], () => {
  118. const files = [ 'ckeditor.js', 'ckeditor.css', 'ckeditor.min.js', 'ckeditor.min.css' ];
  119. const filesStats = utils.getFilesSizeStats( files, bundleDir );
  120. // Show bundle summary on console.
  121. utils.showFilesSummary( 'Bundle summary', filesStats );
  122. // Finish the task.
  123. callback();
  124. } );
  125. } );
  126. }
  127. };
  128. return tasks;
  129. };