gulpfile.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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' )( config );
  50. const compiler = ckeditor5DevCompiler.compiler;
  51. gulp.task( 'default', [ 'compile' ] );
  52. gulp.task( 'compile', callback => {
  53. runSequence( 'compile:clean:all', 'compile:themes', 'compile:js', callback );
  54. } );
  55. // TODO This task is temporary: https://github.com/ckeditor/ckeditor5-dev-compiler/issues/24
  56. gulp.task( 'compile:sample-tests', [ 'compile' ], () => compiler.compile.sampleTests.source() );
  57. gulp.task( 'compile:bundled-sample-tests', [ 'compile:bundled-sample-tests:build-editors' ],
  58. () => compiler.compile.sampleTests.bundled() );
  59. // Helpers. ---------------------------
  60. gulp.task( 'compile:clean:all', () => compiler.clean.all() );
  61. gulp.task( 'compile:clean:themes', () => compiler.clean.themes() );
  62. gulp.task( 'compile:clean:js', () => compiler.clean.js() );
  63. gulp.task( 'compile:themes', callback => {
  64. runSequence( 'compile:clean:themes', 'compile:icons', 'compile:sass', callback );
  65. } );
  66. gulp.task( 'compile:sass', () => compiler.compile.sass() );
  67. gulp.task( 'compile:icons', () => compiler.compile.icons() );
  68. gulp.task( 'compile:js', [ 'compile:clean:js' ], () => compiler.compile.js() );
  69. // Tasks specific for preparing compiled output with unmodified source files. Used by `gulp docs` or `gulp build`.
  70. gulp.task( 'compile:clean:js:esnext', () => compiler.clean.js( { formats: [ 'esnext' ] } ) );
  71. gulp.task( 'compile:clean:themes:esnext', () => compiler.clean.themes( { formats: [ 'esnext' ] } ) );
  72. gulp.task( 'compile:sass:esnext', () => compiler.compile.sass( { formats: [ 'esnext' ] } ) );
  73. gulp.task( 'compile:icons:esnext', () => compiler.compile.icons( { formats: [ 'esnext' ] } ) );
  74. gulp.task( 'compile:js:esnext', [ 'compile:clean:js:esnext' ], () => compiler.compile.js( { formats: [ 'esnext' ] } ) );
  75. gulp.task( 'compile:themes:esnext', callback => {
  76. runSequence( 'compile:clean:themes:esnext', 'compile:icons:esnext', 'compile:sass:esnext', callback );
  77. } );
  78. // Building tasks. ------------------------------------------------------------
  79. const ckeditor5DevBundler = require( '@ckeditor/ckeditor5-dev-bundler-rollup' )( config );
  80. gulp.task( 'build', callback => {
  81. runSequence(
  82. 'bundle:generate',
  83. [
  84. 'bundle:minify:js',
  85. 'bundle:minify:css'
  86. ],
  87. () => ckeditor5DevBundler.showSummaryFromConfig( callback )
  88. );
  89. } );
  90. // Helpers. ---------------------------
  91. gulp.task( 'bundle:clean', ckeditor5DevBundler.cleanFromConfig );
  92. gulp.task( 'bundle:minify:js', ckeditor5DevBundler.minify.jsFromConfig );
  93. gulp.task( 'bundle:minify:css', ckeditor5DevBundler.minify.cssFromConfig );
  94. // Generates the bundle without minifying it.
  95. gulp.task( 'bundle:generate',
  96. [
  97. 'bundle:clean',
  98. 'compile:js:esnext',
  99. 'compile:themes:esnext'
  100. ],
  101. ckeditor5DevBundler.generateFromConfig
  102. );
  103. // Task specific for building editors for testing releases.
  104. gulp.task( 'compile:bundled-sample-tests:build-editors',
  105. [
  106. 'compile:js:esnext',
  107. 'compile:themes:esnext'
  108. ],
  109. buildEditorsForSamples
  110. );
  111. // Documentation. -------------------------------------------------------------
  112. const docsBuilder = ckeditor5DevCompiler.docs;
  113. gulp.task( 'docs', [ 'compile:js:esnext' ], docsBuilder.buildDocs );
  114. // Testing. -------------------------------------------------------------------
  115. // TODO The below code is here only temporarily. It will be extracted to a separat package
  116. // once we'll understand better where it should belong. Right now it's somewhere beyond testing
  117. // environment, compilation and documentation.
  118. /**
  119. * Prepares configurations for bundler based on sample files and builds editors
  120. * based on prepared configuration.
  121. *
  122. * You can find more details in: https://github.com/ckeditor/ckeditor5/issues/260
  123. *
  124. * @returns {Stream}
  125. */
  126. function buildEditorsForSamples() {
  127. const { utils: compilerUtils } = require( '@ckeditor/ckeditor5-dev-compiler' )( config );
  128. const { stream, tools: tools } = require( '@ckeditor/ckeditor5-dev-utils' );
  129. const gulpFilter = require( 'gulp-filter' );
  130. const gulpRename = require( 'gulp-rename' );
  131. const path = require( 'path' );
  132. const bundleDir = path.join( config.ROOT_DIR, config.BUNDLE_DIR );
  133. return compilerUtils.getFilesStream( config.ROOT_DIR, config.DOCUMENTATION.SAMPLES )
  134. .pipe( gulpFilter( ( file ) => path.extname( file.path ) === '.js' ) )
  135. .pipe( gulpRename( ( file ) => {
  136. file.dirname = file.dirname.replace( '/docs/samples', '' );
  137. } ) )
  138. .pipe( stream.noop( ( file ) => {
  139. const contents = file.contents.toString( 'utf-8' );
  140. const bundleConfig = {};
  141. // Prepare the config based on sample.
  142. bundleConfig.format = 'iife';
  143. // Find `moduleName` from line which ends with "// editor:name".
  144. bundleConfig.moduleName = contents.match( /([a-z]+)\.create(.*)\/\/ ?editor:name/i )[ 1 ];
  145. // Find `editor` from line which ends with "// editor:module".
  146. bundleConfig.editor = contents.match( /([-a-z0-9]+\/[-a-z0-9]+)(\.js)?'; ?\/\/ ?editor:module/i )[ 1 ];
  147. // Find `features` from line which ends with "// editor:features".
  148. bundleConfig.features = contents.match( /(\[[^\]]+\]),? ?\/\/ ?(.*)editor:features/i )[ 1 ]
  149. .match( /([a-z-\/]+)/gi );
  150. // Extract `path` from Sample's path.
  151. bundleConfig.path = file.path.match( /ckeditor5-(.*)\.js$/ )[ 1 ];
  152. const splitPath = bundleConfig.path.split( path.sep );
  153. const packageName = splitPath[ 0 ];
  154. // Clean the output paths.
  155. return ckeditor5DevBundler.clean( bundleConfig )
  156. // Then bundle a editor.
  157. .then( () => ckeditor5DevBundler.generate( bundleConfig ) )
  158. // Then copy created files.
  159. .then( () => {
  160. const beginPath = splitPath.slice( 1, -1 ).join( path.sep ) || '.';
  161. const fileName = splitPath.slice( -1 ).join();
  162. const builtEditorPath = path.join( bundleDir, bundleConfig.path, bundleConfig.moduleName );
  163. const destinationPath = path.join.apply( null, [
  164. config.MODULE_DIR.amd,
  165. 'tests',
  166. packageName,
  167. 'samples',
  168. beginPath,
  169. '_assets',
  170. fileName
  171. ] );
  172. // Copy editor builds to proper directory.
  173. return Promise.all( [
  174. tools.copyFile( `${ builtEditorPath }.js`, destinationPath ),
  175. tools.copyFile( `${ builtEditorPath }.css`, destinationPath )
  176. ] );
  177. } )
  178. // And clean up.
  179. .then( () => tools.clean( path.join( config.BUNDLE_DIR, packageName, '..' ), packageName ) );
  180. } ) );
  181. }