utils.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* jshint node: true, esnext: true */
  2. 'use strict';
  3. const path = require( 'path' );
  4. const gulp = require( 'gulp' );
  5. const rename = require( 'gulp-rename' );
  6. const babel = require( 'gulp-babel' );
  7. const stream = require( 'stream' );
  8. const sep = path.sep;
  9. const utils = {
  10. fork( sourceStream ) {
  11. const fork = new stream.PassThrough( { objectMode: true } );
  12. return sourceStream.pipe( fork );
  13. },
  14. dist( distDir, format ) {
  15. const destDir = path.join( distDir, format );
  16. return gulp.dest( destDir );
  17. },
  18. transpile( format ) {
  19. const babelModuleTranspilers = {
  20. amd: 'amd',
  21. cjs: 'commonjs'
  22. };
  23. const babelModuleTranspiler = babelModuleTranspilers[ format ];
  24. return new stream.PassThrough( { objectMode: true } )
  25. .pipe( utils.pickVersionedFile( format ) )
  26. .pipe( babel( {
  27. plugins: [ `transform-es2015-modules-${ babelModuleTranspiler }` ]
  28. } ) );
  29. },
  30. pickVersionedFile( format ) {
  31. return rename( ( path ) => {
  32. const regexp = new RegExp( `__${ format }$` );
  33. path.basename = path.basename.replace( regexp, '' );
  34. } );
  35. },
  36. /**
  37. * Move files out of `node_modules/ckeditor5-xxx/src/*` directories to `ckeditor5-xxx/*`.
  38. *
  39. * @returns {Stream}
  40. */
  41. unpackModules( modulePathPattern ) {
  42. return rename( ( filePath ) => {
  43. filePath.dirname = filePath.dirname.replace( modulePathPattern, `${ sep }$1${ sep }` );
  44. // Remove now empty src/ dirs.
  45. if ( !filePath.extname && filePath.basename == 'src' ) {
  46. filePath.basename = '';
  47. }
  48. } );
  49. },
  50. wrapCKEditor5Module() {
  51. return rename( ( filePath ) => {
  52. filePath.dirname = path.join( filePath.dirname, 'ckeditor5' );
  53. } );
  54. }
  55. };
  56. module.exports = utils;