You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

234 lines
6.9 KiB

  1. 'use strict'
  2. /**
  3. * FUSEBOX
  4. *
  5. * Client & Server compiler / bundler / watcher
  6. */
  7. const _ = require('lodash')
  8. const Promise = require('bluebird')
  9. const colors = require('colors/safe')
  10. const fs = Promise.promisifyAll(require('fs-extra'))
  11. const fsbx = require('fuse-box')
  12. const nodemon = require('nodemon')
  13. const path = require('path')
  14. const uglify = require('uglify-js')
  15. // ======================================================
  16. // Parse cmd arguments
  17. // ======================================================
  18. const args = require('yargs')
  19. .option('d', {
  20. alias: 'dev',
  21. describe: 'Start in Developer mode',
  22. type: 'boolean'
  23. })
  24. .option('c', {
  25. alias: 'dev-configure',
  26. describe: 'Start in Configure Developer mode',
  27. type: 'boolean'
  28. })
  29. .help('h')
  30. .alias('h', 'help')
  31. .argv
  32. let mode = 'build'
  33. const dev = args.d || args.c
  34. if (args.d) {
  35. console.info(colors.bgWhite.black(' Starting Fuse in DEVELOPER mode... '))
  36. mode = 'dev'
  37. } else if (args.c) {
  38. console.info(colors.bgWhite.black(' Starting Fuse in CONFIGURE DEVELOPER mode... '))
  39. mode = 'dev-configure'
  40. } else {
  41. console.info(colors.bgWhite.black(' Starting Fuse in BUILD mode... '))
  42. }
  43. // ======================================================
  44. // Global Tasks
  45. // ======================================================
  46. console.info(colors.white('└── ') + colors.green('Running global tasks...'))
  47. let globalTasks = Promise.mapSeries([
  48. /**
  49. * ACE Modes
  50. */
  51. () => {
  52. return fs.accessAsync('./assets/js/ace').then(() => {
  53. console.info(colors.white(' └── ') + colors.magenta('ACE modes directory already exists. Task aborted.'))
  54. return true
  55. }).catch(err => {
  56. if (err.code === 'ENOENT') {
  57. console.info(colors.white(' └── ') + colors.green('Copy + Minify ACE modes to assets...'))
  58. return fs.ensureDirAsync('./assets/js/ace').then(() => {
  59. return fs.readdirAsync('./node_modules/brace/mode').then(modeList => {
  60. return Promise.map(modeList, mdFile => {
  61. console.info(colors.white(' mode-' + mdFile))
  62. let result = uglify.minify(path.join('./node_modules/brace/mode', mdFile), { output: { 'max_line_len': 1000000 } })
  63. return fs.writeFileAsync(path.join('./assets/js/ace', 'mode-' + mdFile), result.code)
  64. })
  65. })
  66. })
  67. } else {
  68. throw err
  69. }
  70. })
  71. },
  72. /**
  73. * MathJax
  74. */
  75. () => {
  76. return fs.accessAsync('./assets/js/mathjax').then(() => {
  77. console.info(colors.white(' └── ') + colors.magenta('MathJax directory already exists. Task aborted.'))
  78. return true
  79. }).catch(err => {
  80. if (err.code === 'ENOENT') {
  81. console.info(colors.white(' └── ') + colors.green('Copy MathJax dependencies to assets...'))
  82. return fs.ensureDirAsync('./assets/js/mathjax').then(() => {
  83. return fs.copyAsync('./node_modules/mathjax', './assets/js/mathjax', { filter: (src, dest) => {
  84. let srcNormalized = src.replace(/\\/g, '/')
  85. let shouldCopy = false
  86. console.log(srcNormalized)
  87. _.forEach([
  88. '/node_modules/mathjax',
  89. '/node_modules/mathjax/jax',
  90. '/node_modules/mathjax/jax/input',
  91. '/node_modules/mathjax/jax/output'
  92. ], chk => {
  93. if (srcNormalized.endsWith(chk)) {
  94. shouldCopy = true
  95. }
  96. })
  97. _.forEach([
  98. '/node_modules/mathjax/extensions',
  99. '/node_modules/mathjax/MathJax.js',
  100. '/node_modules/mathjax/jax/element',
  101. '/node_modules/mathjax/jax/input/MathML',
  102. '/node_modules/mathjax/jax/input/TeX',
  103. '/node_modules/mathjax/jax/output/SVG'
  104. ], chk => {
  105. if (srcNormalized.indexOf(chk) > 0) {
  106. shouldCopy = true
  107. }
  108. })
  109. if (shouldCopy && srcNormalized.indexOf('/fonts/') > 0 && srcNormalized.indexOf('/STIX-Web') <= 1) {
  110. shouldCopy = false
  111. }
  112. return shouldCopy
  113. }})
  114. })
  115. } else {
  116. throw err
  117. }
  118. })
  119. },
  120. /**
  121. * Bundle pre-init scripts
  122. */
  123. () => {
  124. console.info(colors.white(' └── ') + colors.green('Bundling pre-init scripts...'))
  125. let preInitContent = ''
  126. return fs.readdirAsync('./client/js/pre-init').map(f => {
  127. let fPath = path.join('./client/js/pre-init/', f)
  128. return fs.readFileAsync(fPath, 'utf8').then(fContent => {
  129. preInitContent += fContent + ';\n'
  130. })
  131. }).then(() => {
  132. return fs.outputFileAsync('./.build/_preinit.js', preInitContent, 'utf8')
  133. })
  134. },
  135. /**
  136. * Delete Fusebox cache
  137. */
  138. () => {
  139. return fs.emptyDirAsync('./.fusebox')
  140. }
  141. ], f => { return f() })
  142. // ======================================================
  143. // Fuse Tasks
  144. // ======================================================
  145. const ALIASES = {
  146. 'brace-ext-modelist': 'brace/ext/modelist.js',
  147. 'simplemde': 'simplemde/dist/simplemde.min.js',
  148. 'socket.io-client': 'socket.io-client/dist/socket.io.js',
  149. 'vue': 'vue/dist/vue.min.js'
  150. }
  151. const SHIMS = {
  152. _preinit: {
  153. source: '.build/_preinit.js',
  154. exports: '_preinit'
  155. },
  156. jquery: {
  157. source: 'node_modules/jquery/dist/jquery.js',
  158. exports: '$'
  159. },
  160. mathjax: {
  161. source: 'node_modules/mathjax/MathJax.js',
  162. exports: 'MathJax'
  163. }
  164. }
  165. globalTasks.then(() => {
  166. let fuse = fsbx.FuseBox.init({
  167. homeDir: './client',
  168. output: './assets/js/$name.min.js',
  169. alias: ALIASES,
  170. shim: SHIMS,
  171. plugins: [
  172. fsbx.EnvPlugin({ NODE_ENV: (dev) ? 'development' : 'production' }),
  173. fsbx.VuePlugin(),
  174. [ '.scss', fsbx.SassPlugin({ outputStyle: (dev) ? 'nested' : 'compressed' }), fsbx.CSSPlugin() ],
  175. fsbx.BabelPlugin({ comments: false, presets: ['es2015'] }),
  176. fsbx.JSONPlugin(),
  177. !dev && fsbx.UglifyJSPlugin({
  178. compress: { unused: false },
  179. output: { 'max_line_len': 1000000 }
  180. })
  181. ],
  182. debug: false,
  183. log: true
  184. })
  185. if (dev) {
  186. fuse.dev({
  187. port: 4444,
  188. httpServer: false
  189. })
  190. }
  191. const bundleLibs = fuse.bundle('libs').instructions('~ index.js')
  192. const bundleApp = fuse.bundle('app').instructions('!> index.js')
  193. const bundleSetup = fuse.bundle('configure').instructions('> configure.js')
  194. switch (mode) {
  195. case 'dev':
  196. bundleLibs.watch()
  197. bundleApp.watch()
  198. break
  199. case 'dev-configure':
  200. bundleSetup.watch()
  201. break
  202. }
  203. fuse.run().then(() => {
  204. console.info(colors.green.bold('\nAssets compilation + bundling completed.'))
  205. if (dev) {
  206. nodemon({
  207. exec: (args.d) ? 'node server' : 'node wiki configure',
  208. ignore: ['assets/', 'client/', 'data/', 'repo/', 'tests/'],
  209. ext: 'js json',
  210. watch: (args.d) ? ['server'] : ['server/configure.js'],
  211. env: { 'NODE_ENV': 'development' }
  212. })
  213. }
  214. }).catch(err => {
  215. console.error(colors.red(' X Bundle compilation failed! ' + err.message))
  216. process.exit(1)
  217. })
  218. })