build.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* jshint node: true, esnext: true */
  2. 'use strict';
  3. const KNOWN_OPTIONS = {
  4. build: {
  5. string: [
  6. 'formats'
  7. ],
  8. boolean: [
  9. 'watch'
  10. ],
  11. default: {
  12. formats: 'amd',
  13. watch: false
  14. }
  15. }
  16. };
  17. const path = require( 'path' );
  18. const gulp = require( 'gulp' );
  19. const del = require( 'del' );
  20. const merge = require( 'merge-stream' );
  21. const gulpMirror = require( 'gulp-mirror' );
  22. const gutil = require( 'gulp-util' );
  23. const minimist = require( 'minimist' );
  24. const utils = require( './utils' );
  25. const sep = path.sep;
  26. const options = minimist( process.argv.slice( 2 ), KNOWN_OPTIONS[ process.argv[ 2 ] ] );
  27. module.exports = ( config ) => {
  28. const distDir = path.join( config.ROOT_DIR, config.DIST_DIR );
  29. const tasks = {
  30. /**
  31. * Removes the dist directory.
  32. */
  33. clean() {
  34. return del( distDir );
  35. },
  36. src: {
  37. /**
  38. * Returns a stream of all source files.
  39. *
  40. * @param {Boolean} [watch] Whether the files should be watched.
  41. * @returns {Stream}
  42. */
  43. all( watch ) {
  44. return merge( tasks.src.main( watch ), tasks.src.ckeditor5( watch ), tasks.src.modules( watch ) );
  45. },
  46. /**
  47. * Returns a stream with just the main file (`ckeditor5/ckeditor.js`).
  48. *
  49. * @param {Boolean} [watch] Whether the files should be watched.
  50. * @returns {Stream}
  51. */
  52. main( watch ) {
  53. return utils.src( config.ROOT_DIR, 'ckeditor.js', watch );
  54. },
  55. /**
  56. * Returns a stream of all source files from CKEditor 5.
  57. *
  58. * @param {Boolean} [watch] Whether the files should be watched.
  59. * @returns {Stream}
  60. */
  61. ckeditor5( watch ) {
  62. return utils.src( config.ROOT_DIR, 'src/**/*.js', watch )
  63. .pipe( utils.wrapCKEditor5Module() );
  64. },
  65. /**
  66. * Returns a stream of all source files from CKEditor 5 dependencies.
  67. *
  68. * @param {Boolean} [watch] Whether the files should be watched.
  69. * @returns {Stream}
  70. */
  71. modules( watch ) {
  72. const modulePathPattern = new RegExp( `node_modules${ sep }(ckeditor5-[^${ sep }]+)${ sep }src` );
  73. return utils.src( config.ROOT_DIR, 'node_modules/ckeditor5-*/src/**/*.js', watch )
  74. .pipe( utils.unpackModules( modulePathPattern ) );
  75. }
  76. }
  77. };
  78. gulp.task( 'build:clean', tasks.clean );
  79. gulp.task( 'build', [ 'build:clean' ], () => {
  80. const formats = options.formats.split( ',' );
  81. const codeStream = tasks.src.all( options.watch )
  82. .on( 'data', ( file ) => {
  83. gutil.log( `Processing '${ gutil.colors.cyan( file.path ) }'...` );
  84. } );
  85. const formatPipes = formats.reduce( utils.addFormat( distDir ), [] );
  86. return codeStream
  87. .pipe( gulpMirror.apply( null, formatPipes ) );
  88. } );
  89. return tasks;
  90. };