8
0

tasks.js 5.6 KB

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