8
0

tasks.js 5.4 KB

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