tasks.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. const mkdirp = require( 'mkdirp' );
  17. module.exports = ( config ) => {
  18. const args = utils.parseArguments();
  19. const sourceBuildDir = path.join( config.ROOT_DIR, config.BUILD_DIR, 'esnext' );
  20. const bundleDir = path.join( config.ROOT_DIR, config.BUNDLE_DIR );
  21. const bundleTmpDir = path.join( bundleDir, 'tmp' );
  22. const temporaryEntryFilePath = path.join( bundleTmpDir, 'entryfile.js' );
  23. const tasks = {
  24. /**
  25. * Remove all files from bundle directory.
  26. */
  27. clean() {
  28. return utils.clean( bundleDir, '*.*' );
  29. },
  30. /**
  31. * Combine whole editor files into two files `ckeditor.js` and `ckeditor.css`.
  32. *
  33. * For JS bundle is required to pass configuration file `gulp bundle --config path/to/file.js` where
  34. * we need to specify which editor and features we want to include in our bundle.
  35. *
  36. * example-config-file.js:
  37. * module.exports = {
  38. * // Name of CKEditor instance exposed as global variable by a bundle.
  39. * moduleName: 'MyCKEditor',
  40. *
  41. * // Path to specified editor.
  42. * // It could be a path relative to `build/esnext/ckeditor5` directory e.g. `classic-editor/classic.js`
  43. * // or path relative to directory where build task will be executed `./full/path/to/custom/editor.js`.
  44. * editor: 'classic-editor/classic.js',
  45. *
  46. * // List of features.
  47. * features: [
  48. * // It could be a plugin name only if plugin is a default CKEditor plugin.
  49. * 'delete',
  50. *
  51. * // It could be a path relative to `build/esnext/ckeditor5` directory.
  52. * `typing/typing.js`
  53. *
  54. * // Or it could be a path relative to directory where build task will be executed.
  55. * './path/to/some/custom/feature.js',
  56. * ]
  57. * }
  58. *
  59. * @param {String} configFilePath Path to the bundle configuration file.
  60. * @returns {Promise} Promise that resolve bundling for CSS and JS.
  61. */
  62. generate( configFilePath ) {
  63. // When config file is not specified.
  64. if ( !configFilePath ) {
  65. // Then log error.
  66. gutil.log( gutil.colors.red( `Bundle Error: Path to the config file is required. 'gulp bundle --config path/to/file.js'` ) );
  67. // And stop task as failed.
  68. return Promise.reject();
  69. }
  70. // Get configuration from the configuration file.
  71. const config = require( path.resolve( '.', configFilePath ) );
  72. // Create a temporary entry file with proper directory structure if not exist.
  73. mkdirp.sync( bundleTmpDir );
  74. fs.writeFileSync( temporaryEntryFilePath, utils.renderEntryFileContent( bundleTmpDir, config ) );
  75. /**
  76. * Bundling JS by Rollup.
  77. */
  78. function bundleJS() {
  79. const outputFile = path.join( bundleDir, 'ckeditor.js' );
  80. return rollup( {
  81. entry: temporaryEntryFilePath,
  82. plugins: [
  83. rollupBabel( {
  84. presets: [ 'es2015-rollup' ]
  85. } )
  86. ]
  87. } ).then( ( bundle ) => {
  88. return bundle.write( {
  89. dest: outputFile,
  90. format: 'iife',
  91. moduleName: config.moduleName
  92. } );
  93. } ).then( () => {
  94. // If everything went well then remove tmp directory.
  95. utils.clean( bundleTmpDir, path.join( '' ) );
  96. } ).catch( ( err ) => {
  97. // If something went wrong then log error.
  98. gutil.log( gutil.colors.red( `Bundle Error` ) );
  99. gutil.log( gutil.colors.red( err.stack ) );
  100. // And remove tmp directory.
  101. utils.clean( bundleTmpDir, path.join( '' ) );
  102. } );
  103. }
  104. /**
  105. * CSS is already bundled by a build task, so we need only to copy it.
  106. */
  107. function bundleCSS() {
  108. const cssSource = path.join( sourceBuildDir, 'theme', 'ckeditor.css' );
  109. return utils.copyFile( cssSource, bundleDir );
  110. }
  111. // Lets wait for both - JS and CSS.
  112. return Promise.all( [ bundleJS(), bundleCSS() ] );
  113. },
  114. minify: {
  115. /**
  116. * JS minification by UglifyJS.
  117. */
  118. js() {
  119. let stream = gulp.src( path.join( bundleDir, 'ckeditor.js' ) )
  120. .pipe( gulpUglify() );
  121. return utils.saveFileFromStreamAsMinified( stream, bundleDir );
  122. },
  123. /**
  124. * CSS minification by cssnano.
  125. */
  126. css() {
  127. let stream = gulp.src( path.join( bundleDir, 'ckeditor.css' ) )
  128. .pipe( gulpCssnano() );
  129. return utils.saveFileFromStreamAsMinified( stream, bundleDir );
  130. }
  131. },
  132. register() {
  133. gulp.task( 'bundle:clean', tasks.clean );
  134. gulp.task( 'bundle:generate',
  135. [
  136. 'bundle:clean',
  137. 'build:js:esnext',
  138. 'build:themes:esnext'
  139. ],
  140. () => tasks.generate( args.config )
  141. );
  142. gulp.task( 'bundle:minify:js', tasks.minify.js );
  143. gulp.task( 'bundle:minify:css', tasks.minify.css );
  144. gulp.task( 'bundle', ( callback ) => {
  145. runSequence( 'bundle:generate', [ 'bundle:minify:js', 'bundle:minify:css' ], () => {
  146. const files = [ 'ckeditor.js', 'ckeditor.css', 'ckeditor.min.js', 'ckeditor.min.css' ];
  147. const filesStats = utils.getFilesSizeStats( files, bundleDir );
  148. // Show bundle summary on console.
  149. utils.showFilesSummary( 'Bundle summary', filesStats );
  150. // Finish the task.
  151. callback();
  152. } );
  153. } );
  154. }
  155. };
  156. return tasks;
  157. };