browser.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. 'use strict';
  2. let _ = require('lodash');
  3. let bluebird = require('bluebird');
  4. let readFile = bluebird.promisify(require('fs').readFile);
  5. let path = require('path');
  6. let registry = require('./tasks');
  7. let utility = require('./utility');
  8. let directory;
  9. function templateAllFunc(blobs) {
  10. const name = path.join('demo', 'index.html');
  11. blobs = _.compact(blobs);
  12. return bluebird.join(
  13. readFile(name),
  14. utility.getStyleNames(),
  15. (template, styles) => ({ template, path, blobs, styles })
  16. );
  17. }
  18. function copyDocs() {
  19. const input = path.join(directory.root, 'docs', '*.rst'),
  20. output = path.join(directory.build, 'docs');
  21. return {
  22. startLog: { task: ['log', 'Copying documentation.'] },
  23. read: { requires: 'startLog', task: ['glob', utility.glob(input)] },
  24. writeLog: { requires: 'read', task: ['log', 'Writing documentation.'] },
  25. write: { requires: 'writeLog', task: ['dest', output] }
  26. };
  27. }
  28. function generateDemo(filterCB, readArgs) {
  29. let styleDir = path.join('src', 'styles'),
  30. staticArgs = utility.glob(path.join('demo', '*.min.{js,css}')),
  31. imageArgs = utility.glob(path.join(styleDir, '*.{png,jpg}'),
  32. 'binary'),
  33. stylesArgs = utility.glob(path.join(styleDir, '*.css')),
  34. demoRoot = path.join(directory.build, 'demo'),
  35. templateArgs = { callback: templateAllFunc },
  36. destArgs = {
  37. dir: path.join(demoRoot, 'styles'),
  38. encoding: 'binary'
  39. };
  40. return {
  41. logStart: { task: ['log', 'Generating demo.'] },
  42. readLanguages: { requires: 'logStart', task: ['glob', readArgs] },
  43. filterSnippets: { requires: 'readLanguages', task: ['filter', filterCB] },
  44. readSnippet: { requires: 'filterSnippets', task: 'readSnippet' },
  45. template: {
  46. requires: 'readSnippet',
  47. task: ['templateAll', templateArgs]
  48. },
  49. write: {
  50. requires: 'template',
  51. task: ['write', path.join(demoRoot, 'index.html')]
  52. },
  53. readStatic: { requires: 'logStart', task: ['glob', staticArgs] },
  54. writeStatic: { requires: 'readStatic', task: ['dest', demoRoot] },
  55. readStyles: { requires: 'logStart', task: ['glob', stylesArgs] },
  56. compressStyles: { requires: 'readStyles', task: 'cssminify' },
  57. writeStyles: { requires: 'compressStyles', task: ['dest', destArgs] },
  58. readImages: { requires: 'logStart', task: ['glob', imageArgs] },
  59. writeImages: { requires:'readImages', task: ['dest', destArgs] },
  60. readDemoJS: {
  61. requires: 'logStart',
  62. task: ['read', path.join('demo', 'demo.js')]
  63. },
  64. minifyDemoJS: { requires: 'readDemoJS', task: 'jsminify' },
  65. writeDemoJS: { requires: 'minifyDemoJS', task: ['dest', demoRoot] },
  66. readDemoCSS: {
  67. requires: 'logStart',
  68. task: ['read', path.join('demo', 'style.css')]
  69. },
  70. minifyDemoCSS: { requires: 'readDemoCSS', task: 'cssminify' },
  71. writeDemoCSS: { requires: 'minifyDemoCSS', task: ['dest', demoRoot] }
  72. };
  73. }
  74. module.exports = function(commander, dir) {
  75. directory = dir;
  76. let hljsExt, output, requiresTask, tasks,
  77. replace = utility.replace,
  78. regex = utility.regex,
  79. replaceClassNames = utility.replaceClassNames,
  80. coreFile = path.join('src', 'highlight.js'),
  81. languages = utility.glob(path.join('src', 'languages', '*.js')),
  82. filterCB = utility.buildFilterCallback(commander.args),
  83. replaceArgs = replace(regex.header, ''),
  84. templateArgs =
  85. 'hljs.registerLanguage(\'<%= name %>\', <%= content %>);\n';
  86. tasks = {
  87. startLog: { task: ['log', 'Building highlight.js pack file.'] },
  88. readCore: { requires: 'startLog', task: ['read', coreFile] },
  89. read: { requires: 'startLog', task: ['glob', languages] },
  90. filter: { requires: 'read', task: ['filter', filterCB] },
  91. reorder: { requires: 'filter', task: 'reorderDeps' },
  92. replace: { requires: 'reorder', task: ['replace', replaceArgs] },
  93. template: { requires: 'replace', task: ['template', templateArgs] },
  94. packageFiles: {
  95. requires: ['readCore', 'template'],
  96. task: 'packageFiles'
  97. }
  98. };
  99. requiresTask = 'packageFiles';
  100. if(commander.compress || commander.target === 'cdn') {
  101. tasks.compresslog = {
  102. requires: requiresTask,
  103. task: ['log', 'Compressing highlight.js pack file.']
  104. };
  105. tasks.replace2 = {
  106. requires: 'compresslog',
  107. task: [ 'replaceSkippingStrings'
  108. , replace(regex.replaces, replaceClassNames)
  109. ]
  110. };
  111. tasks.replace3 = {
  112. requires: 'replace2',
  113. task: ['replace', replace(regex.classname, '$1.className')]
  114. };
  115. tasks.minify = { requires: 'replace3', task: 'jsminify' };
  116. requiresTask = 'minify';
  117. }
  118. tasks.insertLicenseTag = {
  119. requires: requiresTask,
  120. task: 'insertLicenseTag'
  121. };
  122. tasks.writelog = {
  123. requires: 'insertLicenseTag',
  124. task: ['log', 'Writing highlight.js pack file.']
  125. };
  126. hljsExt = commander.target === 'cdn' ? 'min' : 'pack';
  127. output = path.join(directory.build, `highlight.${hljsExt}.js`);
  128. tasks.write = {
  129. requires: 'writelog',
  130. task: ['write', output]
  131. };
  132. tasks = (commander.target === 'browser')
  133. ? [copyDocs(), generateDemo(filterCB, languages), tasks]
  134. : [tasks];
  135. return utility.toQueue(tasks, registry);
  136. };