gulpfile.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* jshint browser: false, node: true, strict: true */
  6. 'use strict';
  7. const gulp = require( 'gulp' );
  8. const runSequence = require( 'run-sequence' );
  9. const config = {
  10. ROOT_DIR: '.',
  11. MODULE_DIR: {
  12. amd: 'build/modules/amd',
  13. cjs: 'build/modules/cjs',
  14. esnext: 'build/modules/esnext'
  15. },
  16. DOCS_DIR: 'build/docs',
  17. BUNDLE_DIR: 'build/dist',
  18. WORKSPACE_DIR: '..',
  19. // Path to the default configuration file for bundler.
  20. BUNDLE_DEFAULT_CONFIG: 'dev/bundles/build-config-standard.js',
  21. DOCUMENTATION: {
  22. // Glob pattern with samples.
  23. SAMPLES: 'docs/samples/**/*.@(md|html|js)',
  24. // Glob pattern with guides.
  25. GUIDES: 'docs/guides/**/*.md'
  26. },
  27. // Files ignored by jshint and jscs tasks. Files from .gitignore will be added automatically during tasks execution.
  28. IGNORED_FILES: [
  29. 'src/lib/**'
  30. ]
  31. };
  32. // Lint tasks. ---------------------------------------------------------------
  33. const ckeditor5Lint = require( '@ckeditor/ckeditor5-dev-lint' )( config );
  34. gulp.task( 'lint', ckeditor5Lint.lint );
  35. gulp.task( 'lint-staged', ckeditor5Lint.lintStaged );
  36. gulp.task( 'pre-commit', [ 'lint-staged' ] );
  37. // Development environment tasks. ---------------------------------------------
  38. const ckeditor5DevEnv = require( '@ckeditor/ckeditor5-dev-env' )( config );
  39. gulp.task( 'init', ckeditor5DevEnv.initRepository );
  40. gulp.task( 'create-package', ckeditor5DevEnv.createPackage );
  41. gulp.task( 'update', ckeditor5DevEnv.updateRepositories );
  42. gulp.task( 'pull', ckeditor5DevEnv.updateRepositories );
  43. gulp.task( 'status', ckeditor5DevEnv.checkStatus );
  44. gulp.task( 'st', ckeditor5DevEnv.checkStatus );
  45. gulp.task( 'relink', ckeditor5DevEnv.relink );
  46. gulp.task( 'install', ckeditor5DevEnv.installPackage );
  47. gulp.task( 'exec', ckeditor5DevEnv.execOnRepositories );
  48. // Compilation tasks. ---------------------------------------------------------
  49. const ckeditor5DevCompiler = require( '@ckeditor/ckeditor5-dev-compiler' );
  50. const compiler = ckeditor5DevCompiler.compiler( config );
  51. gulp.task( 'default', [ 'compile' ] );
  52. gulp.task( 'compile', callback => {
  53. runSequence( 'compile:clean:all', 'compile:themes', 'compile:js', callback );
  54. } );
  55. gulp.task( 'compile:bundled-sample-tests', [ 'compile:bundled-sample-tests:build-editors' ],
  56. () => compiler.compile.bundledSampleTests() );
  57. // Helpers. ---------------------------
  58. gulp.task( 'compile:clean:all', () => compiler.clean.all() );
  59. gulp.task( 'compile:clean:themes', () => compiler.clean.themes() );
  60. gulp.task( 'compile:clean:js', () => compiler.clean.js() );
  61. gulp.task( 'compile:themes', callback => {
  62. runSequence( 'compile:clean:themes', 'compile:icons', 'compile:sass', callback );
  63. } );
  64. gulp.task( 'compile:sass', () => compiler.compile.sass() );
  65. gulp.task( 'compile:icons', () => compiler.compile.icons() );
  66. gulp.task( 'compile:js', [ 'compile:clean:js' ], () => compiler.compile.js() );
  67. // Tasks specific for preparing compiled output with unmodified source files. Used by `gulp docs` or `gulp build`.
  68. gulp.task( 'compile:clean:js:esnext', () => compiler.clean.js( { formats: [ 'esnext' ] } ) );
  69. gulp.task( 'compile:clean:themes:esnext', () => compiler.clean.themes( { formats: [ 'esnext' ] } ) );
  70. gulp.task( 'compile:sass:esnext', () => compiler.compile.sass( { formats: [ 'esnext' ] } ) );
  71. gulp.task( 'compile:icons:esnext', () => compiler.compile.icons( { formats: [ 'esnext' ] } ) );
  72. gulp.task( 'compile:js:esnext', [ 'compile:clean:js:esnext' ], () => compiler.compile.js( { formats: [ 'esnext' ] } ) );
  73. gulp.task( 'compile:themes:esnext', callback => {
  74. runSequence( 'compile:clean:themes:esnext', 'compile:icons:esnext', 'compile:sass:esnext', callback );
  75. } );
  76. // Building tasks. ------------------------------------------------------------
  77. const ckeditor5DevBundler = require( '@ckeditor/ckeditor5-dev-bundler-rollup' )( config );
  78. gulp.task( 'build', callback => {
  79. runSequence(
  80. 'bundle:generate',
  81. [
  82. 'bundle:minify:js',
  83. 'bundle:minify:css'
  84. ],
  85. () => ckeditor5DevBundler.showSummaryFromConfig( callback )
  86. );
  87. } );
  88. // Helpers. ---------------------------
  89. gulp.task( 'bundle:clean', ckeditor5DevBundler.cleanFromConfig );
  90. gulp.task( 'bundle:minify:js', ckeditor5DevBundler.minify.jsFromConfig );
  91. gulp.task( 'bundle:minify:css', ckeditor5DevBundler.minify.cssFromConfig );
  92. // Generates the bundle without minifying it.
  93. gulp.task( 'bundle:generate',
  94. [
  95. 'bundle:clean',
  96. 'compile:js:esnext',
  97. 'compile:themes:esnext'
  98. ],
  99. ckeditor5DevBundler.generateFromConfig
  100. );
  101. // Task specific for building editors for testing releases.
  102. gulp.task( 'compile:bundled-sample-tests:build-editors',
  103. [
  104. 'compile:js:esnext',
  105. 'compile:themes:esnext'
  106. ],
  107. buildEditorsForSamples
  108. );
  109. // Documentation. -------------------------------------------------------------
  110. const docsBuilder = ckeditor5DevCompiler.docs( config );
  111. gulp.task( 'docs', [ 'compile:js:esnext' ], docsBuilder.buildDocs );
  112. // Testing. -------------------------------------------------------------------
  113. // TODO The below code is here only temporarily. It will be extracted to a separate package
  114. // once we'll understand better where it should belong. Right now it's somewhere beyond testing
  115. // environment, compilation and documentation.
  116. /**
  117. * Prepares configurations for bundler based on sample files and builds editors
  118. * based on prepared configuration.
  119. *
  120. * You can find more details in: https://github.com/ckeditor/ckeditor5/issues/260
  121. *
  122. * @returns {Stream}
  123. */
  124. function buildEditorsForSamples() {
  125. const { utils: compilerUtils } = require( '@ckeditor/ckeditor5-dev-compiler' );
  126. const { stream, tools } = require( '@ckeditor/ckeditor5-dev-utils' );
  127. const gulpFilter = require( 'gulp-filter' );
  128. const gulpRename = require( 'gulp-rename' );
  129. const path = require( 'path' );
  130. const bundleDir = path.join( config.ROOT_DIR, config.BUNDLE_DIR );
  131. return compilerUtils.getFilesStream( config.ROOT_DIR, config.DOCUMENTATION.SAMPLES )
  132. .pipe( gulpFilter( ( file ) => path.extname( file.path ) === '.js' ) )
  133. .pipe( gulpRename( ( file ) => {
  134. file.dirname = file.dirname.replace( '/docs/samples', '' );
  135. } ) )
  136. .pipe( stream.noop( ( file ) => {
  137. const contents = file.contents.toString( 'utf-8' );
  138. const bundleConfig = {};
  139. // Prepare the config based on sample.
  140. bundleConfig.format = 'iife';
  141. // Find `moduleName` from line which ends with "// editor:name".
  142. bundleConfig.moduleName = contents.match( /([a-z]+)\.create(.*)\/\/ ?editor:name/i )[ 1 ];
  143. // Find `editor` from line which ends with "// editor:module".
  144. bundleConfig.editor = contents.match( /([-a-z0-9]+\/[-a-z0-9]+)(\.js)?'; ?\/\/ ?editor:module/i )[ 1 ];
  145. // Find `features` from line which ends with "// editor:features".
  146. bundleConfig.features = contents.match( /(\[[^\]]+\]),? ?\/\/ ?(.*)editor:features/i )[ 1 ]
  147. .match( /([a-z-\/]+)/gi );
  148. // Extract `path` from Sample's path.
  149. bundleConfig.path = file.path.match( /ckeditor5-(.*)\.js$/ )[ 1 ];
  150. const splitPath = bundleConfig.path.split( path.sep );
  151. const packageName = splitPath[ 0 ];
  152. // Clean the output paths.
  153. return ckeditor5DevBundler.clean( bundleConfig )
  154. // Then bundle a editor.
  155. .then( () => ckeditor5DevBundler.generate( bundleConfig ) )
  156. // Then copy created files.
  157. .then( () => {
  158. const beginPath = splitPath.slice( 1, -1 ).join( path.sep ) || '.';
  159. const fileName = splitPath.slice( -1 ).join();
  160. const builtEditorPath = path.join( bundleDir, bundleConfig.path, bundleConfig.moduleName );
  161. const destinationPath = path.join.apply( null, [
  162. config.MODULE_DIR.amd,
  163. 'tests',
  164. packageName,
  165. 'samples',
  166. beginPath,
  167. '_assets',
  168. fileName
  169. ] );
  170. // Copy editor builds to proper directory.
  171. return Promise.all( [
  172. tools.copyFile( `${ builtEditorPath }.js`, destinationPath ),
  173. tools.copyFile( `${ builtEditorPath }.css`, destinationPath )
  174. ] );
  175. } )
  176. // And clean up.
  177. .then( () => tools.clean( path.join( config.BUNDLE_DIR, packageName, '..' ), packageName ) );
  178. } ) );
  179. }