8
0

gulpfile.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. BUNDLE_DIR: 'build/dist',
  17. WORKSPACE_DIR: '..',
  18. // Path to the default configuration file for bundler.
  19. BUNDLE_DEFAULT_CONFIG: 'dev/bundles/build-config-standard.js',
  20. DOCUMENTATION: {
  21. // Path to the temporary documentation files.
  22. SOURCE_DIR: '.docs',
  23. // Path to the built documentation.
  24. DESTINATION_DIR: 'build/docs',
  25. // Glob pattern with samples.
  26. SAMPLES: 'docs/samples/**/*.@(md|html|js)',
  27. // Glob pattern with guides.
  28. GUIDES: 'docs/guides/**/*.md'
  29. },
  30. // Files ignored by jshint and jscs tasks. Files from .gitignore will be added automatically during tasks execution.
  31. IGNORED_FILES: [
  32. 'src/lib/**'
  33. ]
  34. };
  35. // Lint tasks. ---------------------------------------------------------------
  36. const ckeditor5Lint = require( '@ckeditor/ckeditor5-dev-lint' )( config );
  37. gulp.task( 'lint', ckeditor5Lint.lint );
  38. gulp.task( 'lint-staged', ckeditor5Lint.lintStaged );
  39. gulp.task( 'pre-commit', [ 'lint-staged' ] );
  40. // Development environment tasks. ---------------------------------------------
  41. const ckeditor5DevEnv = require( '@ckeditor/ckeditor5-dev-env' )( config );
  42. gulp.task( 'init', ckeditor5DevEnv.initRepository );
  43. gulp.task( 'create-package', ckeditor5DevEnv.createPackage );
  44. gulp.task( 'update', ckeditor5DevEnv.updateRepositories );
  45. gulp.task( 'pull', ckeditor5DevEnv.updateRepositories );
  46. gulp.task( 'status', ckeditor5DevEnv.checkStatus );
  47. gulp.task( 'st', ckeditor5DevEnv.checkStatus );
  48. gulp.task( 'relink', ckeditor5DevEnv.relink );
  49. gulp.task( 'install', ckeditor5DevEnv.installPackage );
  50. gulp.task( 'exec', ckeditor5DevEnv.execOnRepositories );
  51. // Compilation tasks. ---------------------------------------------------------
  52. const ckeditor5DevCompiler = require( '@ckeditor/ckeditor5-dev-compiler' );
  53. const compiler = ckeditor5DevCompiler.compiler( config );
  54. gulp.task( 'default', [ 'compile' ] );
  55. gulp.task( 'compile', callback => {
  56. runSequence( 'compile:clean:all', 'compile:themes', 'compile:js', callback );
  57. } );
  58. gulp.task( 'compile:bundled-sample-tests', [ 'compile:bundled-sample-tests:build-editors' ],
  59. () => compiler.compile.bundledSampleTests() );
  60. // Helpers. ---------------------------
  61. gulp.task( 'compile:clean:all', () => compiler.clean.all() );
  62. gulp.task( 'compile:clean:themes', () => compiler.clean.themes() );
  63. gulp.task( 'compile:clean:js', () => compiler.clean.js() );
  64. gulp.task( 'compile:themes', callback => {
  65. runSequence( 'compile:clean:themes', 'compile:icons', 'compile:sass', callback );
  66. } );
  67. gulp.task( 'compile:sass', () => compiler.compile.sass() );
  68. gulp.task( 'compile:icons', () => compiler.compile.icons() );
  69. gulp.task( 'compile:js', [ 'compile:clean:js' ], () => compiler.compile.js() );
  70. // Tasks specific for preparing compiled output with unmodified source files. Used by `gulp docs` or `gulp build`.
  71. gulp.task( 'compile:clean:js:esnext', () => compiler.clean.js( { formats: [ 'esnext' ] } ) );
  72. gulp.task( 'compile:clean:themes:esnext', () => compiler.clean.themes( { formats: [ 'esnext' ] } ) );
  73. gulp.task( 'compile:sass:esnext', () => compiler.compile.sass( { formats: [ 'esnext' ] } ) );
  74. gulp.task( 'compile:icons:esnext', () => compiler.compile.icons( { formats: [ 'esnext' ] } ) );
  75. gulp.task( 'compile:js:esnext', [ 'compile:clean:js:esnext' ], () => compiler.compile.js( { formats: [ 'esnext' ] } ) );
  76. gulp.task( 'compile:themes:esnext', callback => {
  77. runSequence( 'compile:clean:themes:esnext', 'compile:icons:esnext', 'compile:sass:esnext', callback );
  78. } );
  79. // Building tasks. ------------------------------------------------------------
  80. const ckeditor5DevBundler = require( '@ckeditor/ckeditor5-dev-bundler-rollup' )( config );
  81. gulp.task( 'build', callback => {
  82. runSequence(
  83. 'bundle:generate',
  84. [
  85. 'bundle:minify:js',
  86. 'bundle:minify:css'
  87. ],
  88. () => ckeditor5DevBundler.showSummaryFromConfig( callback )
  89. );
  90. } );
  91. // Helpers. ---------------------------
  92. gulp.task( 'bundle:clean', ckeditor5DevBundler.cleanFromConfig );
  93. gulp.task( 'bundle:minify:js', ckeditor5DevBundler.minify.jsFromConfig );
  94. gulp.task( 'bundle:minify:css', ckeditor5DevBundler.minify.cssFromConfig );
  95. // Generates the bundle without minifying it.
  96. gulp.task( 'bundle:generate',
  97. [
  98. 'bundle:clean',
  99. 'compile:js:esnext',
  100. 'compile:themes:esnext'
  101. ],
  102. ckeditor5DevBundler.generateFromConfig
  103. );
  104. // Task specific for building editors for testing releases.
  105. gulp.task( 'compile:bundled-sample-tests:build-editors',
  106. [
  107. 'compile:js:esnext',
  108. 'compile:themes:esnext'
  109. ],
  110. buildEditorsForSamples
  111. );
  112. // Documentation. -------------------------------------------------------------
  113. const ckeditor5DevDocs = require( '@ckeditor/ckeditor5-dev-docs' );
  114. const docsBuilder = ckeditor5DevDocs.docs( config );
  115. gulp.task( 'docs', [ 'compile:js:esnext', 'docs:prepare-files', 'docs:editors' ], docsBuilder.buildDocs );
  116. // Collect documentation files.
  117. gulp.task( 'docs:prepare-files:clean', docsBuilder.removeDocumentationFiles );
  118. gulp.task( 'docs:prepare-files', [ 'docs:prepare-files:clean' ], docsBuilder.collectDocumentationFiles );
  119. // Build editors for samples.
  120. gulp.task( 'docs:editors:clean', docsBuilder.removeBuiltEditors );
  121. gulp.task( 'docs:editors', [ 'docs:editors:clean', 'compile:js:esnext' ], docsBuilder.buildSamplesEditors );
  122. // Testing. -------------------------------------------------------------------
  123. // TODO The below code is here only temporarily. It will be extracted to a separate package
  124. // once we'll understand better where it should belong. Right now it's somewhere beyond testing
  125. // environment, compilation and documentation.
  126. /**
  127. * Prepares configurations for bundler based on sample files and builds editors
  128. * based on prepared configuration.
  129. *
  130. * You can find more details in: https://github.com/ckeditor/ckeditor5/issues/260
  131. *
  132. * @returns {Stream}
  133. */
  134. function buildEditorsForSamples() {
  135. const { stream, tools } = require( '@ckeditor/ckeditor5-dev-utils' );
  136. const { utils: docsUtils } = require( '@ckeditor/ckeditor5-dev-docs' );
  137. const gulpFilter = require( 'gulp-filter' );
  138. const gulpRename = require( 'gulp-rename' );
  139. const path = require( 'path' );
  140. const bundleDir = path.join( config.ROOT_DIR, config.BUNDLE_DIR );
  141. return docsUtils.getSamplesStream( config.ROOT_DIR, config.DOCUMENTATION.SAMPLES )
  142. .pipe( gulpFilter( ( file ) => path.extname( file.path ) === '.js' ) )
  143. .pipe( gulpRename( ( file ) => {
  144. file.dirname = file.dirname.replace( '/docs/samples', '' );
  145. } ) )
  146. .pipe( stream.noop( ( file ) => {
  147. const bundleConfig = docsUtils.getBundlerConfigFromSample( file.contents.toString( 'utf-8' ) );
  148. bundleConfig.format = 'iife';
  149. bundleConfig.path = file.path.match( /\/samples\/(.*)\.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 }.js` ),
  173. tools.copyFile( `${ builtEditorPath }.css`, `${ destinationPath }.css` )
  174. ] );
  175. } )
  176. // And clean up.
  177. .then( () => tools.clean( path.join( config.BUNDLE_DIR, packageName, '..' ), packageName ) );
  178. } ) );
  179. }