fuse.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. .option('i', {
  30. alias: 'inspect',
  31. describe: 'Enable Inspector for debugging',
  32. type: 'boolean',
  33. implies: 'd'
  34. })
  35. .help('h')
  36. .alias('h', 'help')
  37. .argv
  38. let mode = 'build'
  39. if (args.d) {
  40. console.info(colors.bgWhite.black(' Starting Fuse in DEVELOPER mode... '))
  41. mode = 'dev'
  42. } else if (args.c) {
  43. console.info(colors.bgWhite.black(' Starting Fuse in CONFIGURE DEVELOPER mode... '))
  44. mode = 'dev-configure'
  45. } else {
  46. console.info(colors.bgWhite.black(' Starting Fuse in BUILD mode... '))
  47. }
  48. // ======================================================
  49. // Define aliases / shims
  50. // ======================================================
  51. const ALIASES = {
  52. 'brace-ext-modelist': 'brace/ext/modelist.js',
  53. 'simplemde': 'simplemde/dist/simplemde.min.js',
  54. 'socket.io-client': 'socket.io-client/dist/socket.io.min.js',
  55. 'vue': 'vue/dist/vue.min.js'
  56. }
  57. const SHIMS = {
  58. _preinit: {
  59. source: '.build/_preinit.js',
  60. exports: '_preinit'
  61. },
  62. jquery: {
  63. source: 'node_modules/jquery/dist/jquery.js',
  64. exports: '$'
  65. },
  66. mathjax: {
  67. source: 'node_modules/mathjax/MathJax.js',
  68. exports: 'MathJax'
  69. }
  70. }
  71. // ======================================================
  72. // Global Tasks
  73. // ======================================================
  74. console.info(colors.white('└── ') + colors.green('Running global tasks...'))
  75. let preInitContent = ''
  76. let globalTasks = Promise.mapSeries([
  77. /**
  78. * ACE Modes
  79. */
  80. () => {
  81. return fs.accessAsync('./assets/js/ace').then(() => {
  82. console.info(colors.white(' └── ') + colors.magenta('ACE modes directory already exists. Task aborted.'))
  83. return true
  84. }).catch(err => {
  85. if (err.code === 'ENOENT') {
  86. console.info(colors.white(' └── ') + colors.green('Copy + Minify ACE modes to assets...'))
  87. return fs.ensureDirAsync('./assets/js/ace').then(() => {
  88. return fs.readdirAsync('./node_modules/brace/mode').then(modeList => {
  89. return Promise.map(modeList, mdFile => {
  90. console.info(colors.white(' mode-' + mdFile))
  91. let result = uglify.minify(path.join('./node_modules/brace/mode', mdFile), { output: { 'max_line_len': 1000000 } })
  92. return fs.writeFileAsync(path.join('./assets/js/ace', 'mode-' + mdFile), result.code)
  93. })
  94. })
  95. })
  96. } else {
  97. throw err
  98. }
  99. })
  100. },
  101. /**
  102. * MathJax
  103. */
  104. () => {
  105. return fs.accessAsync('./assets/js/mathjax').then(() => {
  106. console.info(colors.white(' └── ') + colors.magenta('MathJax directory already exists. Task aborted.'))
  107. return true
  108. }).catch(err => {
  109. if (err.code === 'ENOENT') {
  110. console.info(colors.white(' └── ') + colors.green('Copy MathJax dependencies to assets...'))
  111. return fs.ensureDirAsync('./assets/js/mathjax').then(() => {
  112. return fs.copyAsync('./node_modules/mathjax', './assets/js/mathjax', { filter: (src, dest) => {
  113. let srcNormalized = src.replace(/\\/g, '/')
  114. let shouldCopy = false
  115. console.log(srcNormalized)
  116. _.forEach([
  117. '/node_modules/mathjax',
  118. '/node_modules/mathjax/jax',
  119. '/node_modules/mathjax/jax/input',
  120. '/node_modules/mathjax/jax/output'
  121. ], chk => {
  122. if (srcNormalized.endsWith(chk)) {
  123. shouldCopy = true
  124. }
  125. })
  126. _.forEach([
  127. '/node_modules/mathjax/extensions',
  128. '/node_modules/mathjax/MathJax.js',
  129. '/node_modules/mathjax/jax/element',
  130. '/node_modules/mathjax/jax/input/MathML',
  131. '/node_modules/mathjax/jax/input/TeX',
  132. '/node_modules/mathjax/jax/output/SVG'
  133. ], chk => {
  134. if (srcNormalized.indexOf(chk) > 0) {
  135. shouldCopy = true
  136. }
  137. })
  138. if (shouldCopy && srcNormalized.indexOf('/fonts/') > 0 && srcNormalized.indexOf('/STIX-Web') <= 1) {
  139. shouldCopy = false
  140. }
  141. return shouldCopy
  142. }})
  143. })
  144. } else {
  145. throw err
  146. }
  147. })
  148. },
  149. /**
  150. * Bundle pre-init scripts
  151. */
  152. () => {
  153. console.info(colors.white(' └── ') + colors.green('Bundling pre-init scripts...'))
  154. return fs.readdirAsync('./client/js/pre-init').map(f => {
  155. let fPath = path.join('./client/js/pre-init/', f)
  156. return fs.readFileAsync(fPath, 'utf8').then(fContent => {
  157. preInitContent += fContent + ';\n'
  158. })
  159. }).then(() => {
  160. return fs.outputFileAsync('./.build/_preinit.js', preInitContent, 'utf8')
  161. })
  162. }
  163. ], f => { return f() })
  164. // ======================================================
  165. // Fuse Tasks
  166. // ======================================================
  167. let fuse
  168. globalTasks.then(() => {
  169. switch (mode) {
  170. // =============================================
  171. // DEVELOPER MODE
  172. // =============================================
  173. case 'dev':
  174. // Client
  175. fuse = fsbx.FuseBox.init({
  176. homeDir: './client',
  177. outFile: './assets/js/bundle.min.js',
  178. alias: ALIASES,
  179. shim: SHIMS,
  180. plugins: [
  181. [ fsbx.SassPlugin({ includePaths: ['../core'] }), fsbx.CSSPlugin() ],
  182. fsbx.BabelPlugin({ comments: false, presets: ['es2015'] }),
  183. fsbx.JSONPlugin()
  184. ],
  185. debug: false,
  186. log: true
  187. })
  188. fuse.devServer('>index.js', {
  189. port: 4444,
  190. httpServer: false,
  191. hmr: false
  192. })
  193. // Server
  194. _.delay(() => {
  195. nodemon({
  196. exec: (args.i) ? 'node --inspect server' : 'node server',
  197. ignore: ['assets/', 'client/', 'data/', 'repo/', 'tests/'],
  198. ext: 'js json',
  199. watch: [
  200. 'controllers',
  201. 'libs',
  202. 'locales',
  203. 'middlewares',
  204. 'models',
  205. 'agent.js',
  206. 'server.js'
  207. ],
  208. env: { 'NODE_ENV': 'development' }
  209. })
  210. }, 1000)
  211. break
  212. // =============================================
  213. // CONFIGURE - DEVELOPER MODE
  214. // =============================================
  215. case 'dev-configure':
  216. // Client
  217. fuse = fsbx.FuseBox.init({
  218. homeDir: './client',
  219. outFile: './assets/js/configure.min.js',
  220. alias: ALIASES,
  221. shim: SHIMS,
  222. plugins: [
  223. [ fsbx.SassPlugin({ includePaths: ['../core'] }), fsbx.CSSPlugin() ],
  224. fsbx.BabelPlugin({ comments: false, presets: ['es2015'] }),
  225. fsbx.JSONPlugin()
  226. ],
  227. debug: false,
  228. log: true
  229. })
  230. fuse.devServer('>configure.js', {
  231. port: 4444,
  232. httpServer: false
  233. })
  234. // Server
  235. _.delay(() => {
  236. nodemon({
  237. exec: 'node wiki configure',
  238. ignore: ['assets/', 'client/', 'data/', 'repo/', 'tests/'],
  239. ext: 'js json',
  240. watch: [
  241. 'configure.js'
  242. ],
  243. env: { 'NODE_ENV': 'development' }
  244. })
  245. }, 1000)
  246. break
  247. // =============================================
  248. // BUILD ONLY MODE
  249. // =============================================
  250. case 'build':
  251. fuse = fsbx.FuseBox.init({
  252. homeDir: './client',
  253. alias: ALIASES,
  254. shim: SHIMS,
  255. plugins: [
  256. fsbx.EnvPlugin({ NODE_ENV: 'production' }),
  257. fsbx.BannerPlugin(preInitContent),
  258. [ fsbx.SassPlugin({ outputStyle: 'compressed', includePaths: ['./node_modules/requarks-core'] }), fsbx.CSSPlugin() ],
  259. fsbx.BabelPlugin({
  260. config: {
  261. comments: false,
  262. presets: ['es2015']
  263. }
  264. }),
  265. fsbx.JSONPlugin(),
  266. fsbx.UglifyJSPlugin({
  267. compress: { unused: false },
  268. output: { 'max_line_len': 1000000 }
  269. })
  270. ],
  271. debug: false,
  272. log: true
  273. })
  274. fuse.bundle({
  275. './assets/js/bundle.min.js': '>index.js',
  276. './assets/js/configure.min.js': '>configure.js'
  277. }).then(() => {
  278. console.info(colors.green.bold('\nAssets compilation + bundling completed.'))
  279. }).catch(err => {
  280. console.error(colors.red(' X Bundle compilation failed! ' + err.message))
  281. process.exit(1)
  282. })
  283. break
  284. }
  285. })