fuse.js 9.6 KB

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