tasks.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 path = require( 'path' );
  7. const gulp = require( 'gulp' );
  8. const gulpCssnano = require( 'gulp-cssnano' );
  9. const gulpUglify = require( 'gulp-uglify' );
  10. const runSequence = require( 'run-sequence' );
  11. const utils = require( './utils' );
  12. const rollup = require( 'rollup' ).rollup;
  13. const rollupBabel = require( 'rollup-plugin-babel' );
  14. module.exports = ( config ) => {
  15. const sourceBuildDir = path.join( config.ROOT_DIR, config.BUILD_DIR, 'esnext' );
  16. const bundleDir = path.join( config.ROOT_DIR, config.BUNDLE_DIR );
  17. const entryFilePath = path.join( config.ROOT_DIR, 'dev', 'tasks', 'bundle', 'buildclassiceditor.js' );
  18. const tasks = {
  19. /**
  20. * Remove all files from bundle directory.
  21. */
  22. clean() {
  23. return utils.clean( bundleDir, '*.*' );
  24. },
  25. /**
  26. * Combine whole editor files into two files `ckeditor.js` and `ckeditor.css`.
  27. */
  28. generate() {
  29. /**
  30. * Bundling JS by Rollup.
  31. *
  32. * At this moment we don't know a list of every dependency needed in the bundle. It is because
  33. * editor features load automatically during initialization process. To work around this problem
  34. * we have created a custom entry file where we defined some of imports with features
  35. * needed to initialize editor.
  36. *
  37. * For more details see `buildclassiceditor.js`.
  38. */
  39. function bundleJS() {
  40. const outputFile = path.join( bundleDir, 'ckeditor.js' );
  41. return rollup( {
  42. entry: entryFilePath,
  43. plugins: [
  44. rollupBabel( {
  45. presets: [ 'es2015-rollup' ]
  46. } )
  47. ]
  48. } ).then( ( bundle ) => {
  49. return bundle.write( {
  50. dest: outputFile,
  51. format: 'iife',
  52. moduleName: 'ClassicEditor'
  53. } );
  54. } );
  55. }
  56. /**
  57. * CSS is already bundled by a build task, so we need only to copy it.
  58. */
  59. function bundleCSS() {
  60. const cssSource = path.join( sourceBuildDir, 'theme', 'ckeditor.css' );
  61. return utils.copyFile( cssSource, bundleDir );
  62. }
  63. // Lets wait for both - JS and CSS.
  64. return Promise.all( [ bundleJS(), bundleCSS() ] );
  65. },
  66. minify: {
  67. /**
  68. * JS minification by UglifyJS.
  69. */
  70. js() {
  71. let stream = gulp.src( path.join( bundleDir, 'ckeditor.js' ) )
  72. .pipe( gulpUglify() );
  73. return utils.saveFileFromStreamAsMinified( stream, bundleDir );
  74. },
  75. /**
  76. * CSS minification by cssnano.
  77. */
  78. css() {
  79. let stream = gulp.src( path.join( bundleDir, 'ckeditor.css' ) )
  80. .pipe( gulpCssnano() );
  81. return utils.saveFileFromStreamAsMinified( stream, bundleDir );
  82. }
  83. }
  84. };
  85. gulp.task( 'bundle:clean', tasks.clean );
  86. gulp.task( 'bundle:generate', [ 'bundle:clean', 'build:js:esnext', 'build:themes:esnext' ], tasks.generate );
  87. gulp.task( 'bundle:minify:js', tasks.minify.js );
  88. gulp.task( 'bundle:minify:css', tasks.minify.css );
  89. gulp.task( 'bundle:next', tasks.next );
  90. gulp.task( 'bundle', ( callback ) => {
  91. runSequence( 'bundle:generate', [ 'bundle:minify:js', 'bundle:minify:css' ], () => {
  92. // Print files size on console just before the end of the task.
  93. const files = [ 'ckeditor.js', 'ckeditor.min.js', 'ckeditor.css', 'ckeditor.min.css' ];
  94. utils.logFilesSize( files, bundleDir );
  95. // Finish the task.
  96. callback();
  97. } );
  98. } );
  99. return tasks;
  100. };