tasks.js 5.5 KB

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