fuse.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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-es')
  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. // BUILD VARS
  45. // ======================================================
  46. const ALIASES = {
  47. 'brace-ext-modelist': 'brace/ext/modelist.js',
  48. 'simplemde': 'simplemde/dist/simplemde.min.js',
  49. 'socket-io-client': 'socket.io-client/dist/socket.io.js',
  50. 'vue': (dev) ? 'vue/dist/vue.js' : 'vue/dist/vue.min.js',
  51. 'vue-lodash': 'vue-lodash/dist/vue-lodash.min.js'
  52. }
  53. const SHIMS = {
  54. _preinit: {
  55. source: '.build/_preinit.js',
  56. exports: '_preinit'
  57. },
  58. jquery: {
  59. source: 'node_modules/jquery/dist/jquery.js',
  60. exports: '$'
  61. },
  62. mathjax: {
  63. source: 'node_modules/mathjax/MathJax.js',
  64. exports: 'MathJax'
  65. }
  66. }
  67. // ======================================================
  68. // Global Tasks
  69. // ======================================================
  70. console.info(colors.white('└── ') + colors.green('Running global tasks...'))
  71. let globalTasks = Promise.mapSeries([
  72. /**
  73. * ACE Modes
  74. */
  75. () => {
  76. return fs.accessAsync('./assets/js/ace').then(() => {
  77. console.info(colors.white(' └── ') + colors.magenta('ACE modes directory already exists. Task aborted.'))
  78. return true
  79. }).catch(err => {
  80. if (err.code === 'ENOENT') {
  81. console.info(colors.white(' └── ') + colors.green('Copy + Minify ACE modes to assets...'))
  82. return fs.ensureDirAsync('./assets/js/ace').then(() => {
  83. return Promise.join(
  84. // Core
  85. Promise.all([
  86. fs.readFileAsync('./node_modules/brace/index.js', 'utf8'),
  87. fs.readFileAsync('./node_modules/brace/theme/dawn.js', 'utf8'),
  88. fs.readFileAsync('./node_modules/brace/mode/markdown.js', 'utf8')
  89. ]).then(items => {
  90. console.info(colors.white(' source-view.js'))
  91. let result = uglify.minify(items.join(';\n'), { output: { 'max_line_len': 1000000 } })
  92. return fs.writeFileAsync('./assets/js/ace/source-view.js', result.code)
  93. }),
  94. // Modes
  95. fs.readdirAsync('./node_modules/brace/mode').then(modeList => {
  96. return Promise.map(modeList, mdFile => {
  97. return fs.readFileAsync(path.join('./node_modules/brace/mode', mdFile), 'utf8').then(modeCode => {
  98. console.info(colors.white(' mode-' + mdFile))
  99. let result = uglify.minify(modeCode, { output: { 'max_line_len': 1000000 } })
  100. return fs.writeFileAsync(path.join('./assets/js/ace', 'mode-' + mdFile), result.code)
  101. })
  102. }, { concurrency: 3 })
  103. })
  104. )
  105. })
  106. } else {
  107. throw err
  108. }
  109. })
  110. },
  111. /**
  112. * MathJax
  113. */
  114. () => {
  115. return fs.accessAsync('./assets/js/mathjax').then(() => {
  116. console.info(colors.white(' └── ') + colors.magenta('MathJax directory already exists. Task aborted.'))
  117. return true
  118. }).catch(err => {
  119. if (err.code === 'ENOENT') {
  120. console.info(colors.white(' └── ') + colors.green('Copy MathJax dependencies to assets...'))
  121. return fs.ensureDirAsync('./assets/js/mathjax').then(() => {
  122. return fs.copyAsync('./node_modules/mathjax', './assets/js/mathjax', {
  123. filter: (src, dest) => {
  124. let srcNormalized = src.replace(/\\/g, '/')
  125. let shouldCopy = false
  126. console.info(colors.white(' ' + srcNormalized))
  127. _.forEach([
  128. '/node_modules/mathjax',
  129. '/node_modules/mathjax/jax',
  130. '/node_modules/mathjax/jax/input',
  131. '/node_modules/mathjax/jax/output'
  132. ], chk => {
  133. if (srcNormalized.endsWith(chk)) {
  134. shouldCopy = true
  135. }
  136. })
  137. _.forEach([
  138. '/node_modules/mathjax/extensions',
  139. '/node_modules/mathjax/MathJax.js',
  140. '/node_modules/mathjax/jax/element',
  141. '/node_modules/mathjax/jax/input/MathML',
  142. '/node_modules/mathjax/jax/input/TeX',
  143. '/node_modules/mathjax/jax/output/SVG'
  144. ], chk => {
  145. if (srcNormalized.indexOf(chk) > 0) {
  146. shouldCopy = true
  147. }
  148. })
  149. if (shouldCopy && srcNormalized.indexOf('/fonts/') > 0 && srcNormalized.indexOf('/STIX-Web') <= 1) {
  150. shouldCopy = false
  151. }
  152. return shouldCopy
  153. }
  154. })
  155. })
  156. } else {
  157. throw err
  158. }
  159. })
  160. },
  161. /**
  162. * i18n
  163. */
  164. () => {
  165. console.info(colors.white(' └── ') + colors.green('Copying i18n client files...'))
  166. return fs.ensureDirAsync('./assets/js/i18n').then(() => {
  167. return fs.readJsonAsync('./server/locales/en/browser.json').then(enContent => {
  168. return fs.readdirAsync('./server/locales').then(langs => {
  169. return Promise.map(langs, lang => {
  170. console.info(colors.white(' ' + lang + '.json'))
  171. let outputPath = path.join('./assets/js/i18n', lang + '.json')
  172. return fs.readJsonAsync(path.join('./server/locales', lang + '.json'), 'utf8').then((content) => {
  173. return fs.outputJsonAsync(outputPath, _.defaultsDeep(content, enContent))
  174. }).catch(err => { // eslint-disable-line handle-callback-err
  175. return fs.outputJsonAsync(outputPath, enContent)
  176. })
  177. })
  178. })
  179. })
  180. })
  181. },
  182. /**
  183. * Bundle pre-init scripts
  184. */
  185. () => {
  186. console.info(colors.white(' └── ') + colors.green('Bundling pre-init scripts...'))
  187. let preInitContent = ''
  188. return fs.readdirAsync('./client/js/pre-init').map(f => {
  189. let fPath = path.join('./client/js/pre-init/', f)
  190. return fs.readFileAsync(fPath, 'utf8').then(fContent => {
  191. preInitContent += fContent + ';\n'
  192. })
  193. }).then(() => {
  194. return fs.outputFileAsync('./.build/_preinit.js', preInitContent, 'utf8')
  195. })
  196. },
  197. /**
  198. * Delete Fusebox cache
  199. */
  200. () => {
  201. console.info(colors.white(' └── ') + colors.green('Clearing fuse-box cache...'))
  202. return fs.emptyDirAsync('./.fusebox')
  203. }
  204. ], f => { return f() })
  205. // ======================================================
  206. // Fuse Tasks
  207. // ======================================================
  208. globalTasks.then(() => {
  209. let fuse = fsbx.FuseBox.init({
  210. homeDir: './client',
  211. output: './assets/js/$name.min.js',
  212. alias: ALIASES,
  213. shim: SHIMS,
  214. plugins: [
  215. fsbx.EnvPlugin({ NODE_ENV: (dev) ? 'development' : 'production' }),
  216. fsbx.VuePlugin(),
  217. ['.scss', fsbx.SassPlugin({ outputStyle: (dev) ? 'nested' : 'compressed' }), fsbx.CSSPlugin()],
  218. dev && fsbx.BabelPlugin({ comments: false, presets: ['es2015'] }),
  219. fsbx.JSONPlugin(),
  220. !dev && fsbx.UglifyESPlugin({
  221. compress: { unused: false },
  222. output: { 'max_line_len': 1000000 }
  223. })
  224. ],
  225. debug: false,
  226. log: true
  227. })
  228. if (dev) {
  229. fuse.dev({
  230. port: 4444,
  231. httpServer: false
  232. })
  233. }
  234. // const bundleLibs = fuse.bundle('libs').instructions('~ index.js - brace')
  235. // const bundleApp = fuse.bundle('app').instructions('!> [index.js]')
  236. const bundleApp = fuse.bundle('app').instructions('> index.js')
  237. const bundleSetup = fuse.bundle('configure').instructions('> configure.js')
  238. switch (mode) {
  239. case 'dev':
  240. // bundleLibs.watch()
  241. bundleApp.watch()
  242. break
  243. case 'dev-configure':
  244. bundleSetup.watch()
  245. break
  246. }
  247. fuse.run().then(() => {
  248. console.info(colors.green.bold('\nAssets compilation + bundling completed.'))
  249. if (dev) {
  250. nodemon({
  251. exec: (args.d) ? 'node server' : 'node wiki configure',
  252. ignore: ['assets/', 'client/', 'data/', 'repo/', 'tests/'],
  253. ext: 'js json',
  254. watch: (args.d) ? ['server'] : ['server/configure.js'],
  255. env: { 'NODE_ENV': 'development' }
  256. })
  257. }
  258. }).catch(err => {
  259. console.error(colors.red(' X Bundle compilation failed! ' + err.message))
  260. process.exit(1)
  261. })
  262. })