8
0

tasks.js 3.4 KB

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