8
0

tasks.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. const cssSource = path.join( sourceBuildDir, 'theme', 'ckeditor.css' );
  55. return utils.copyFile( cssSource, bundleDir );
  56. }
  57. // Lets wait for both - JS and CSS.
  58. return Promise.all( [ bundleJS(), bundleCSS() ] );
  59. },
  60. minify: {
  61. /**
  62. * JS minification by UglifyJS.
  63. *
  64. * At this moment we don't minify JS file because there is no minifier fully sports esnext syntax.
  65. * For consistency `ckeditor.min.js` file is created, but is not minified yed.
  66. */
  67. js() {
  68. let stream = gulp.src( path.join( bundleDir, config.MAIN_FILE ) );
  69. return utils.saveFileFromStreamAsMinified( stream, bundleDir );
  70. },
  71. /**
  72. * CSS minification by cssnano.
  73. */
  74. css() {
  75. let stream = gulp.src( path.join( bundleDir, 'ckeditor.css' ) )
  76. .pipe( gulpCssnano() );
  77. return utils.saveFileFromStreamAsMinified( stream, bundleDir );
  78. }
  79. }
  80. };
  81. gulp.task( 'bundle:clean', tasks.clean );
  82. gulp.task( 'bundle:generate', [ 'bundle:clean', 'build:js:esnext', 'build:themes:esnext' ], tasks.generate );
  83. gulp.task( 'bundle:minify:js', tasks.minify.js );
  84. gulp.task( 'bundle:minify:css', tasks.minify.css );
  85. gulp.task( 'bundle:next', tasks.next );
  86. gulp.task( 'bundle', ( callback ) => {
  87. runSequence( 'bundle:generate', [ 'bundle:minify:js', 'bundle:minify:css' ], () => {
  88. // Print files size on console just before the end of the task.
  89. const files = [ 'ckeditor.js', 'ckeditor.css', 'ckeditor.min.js', 'ckeditor.min.css' ];
  90. utils.logFilesSize( files, bundleDir );
  91. // Finish the task.
  92. callback();
  93. } );
  94. } );
  95. return tasks;
  96. };