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.

296 lines
8.7 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. .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 globalTasks = Promise.mapSeries([
  76. /**
  77. * ACE Modes
  78. */
  79. () => {
  80. return fs.accessAsync('./assets/js/ace').then(() => {
  81. console.info(colors.white(' └── ') + colors.magenta('ACE modes directory already exists. Task aborted.'))
  82. return true
  83. }).catch(err => {
  84. if (err.code === 'ENOENT') {
  85. console.info(colors.white(' └── ') + colors.green('Copy + Minify ACE modes to assets...'))
  86. return fs.ensureDirAsync('./assets/js/ace').then(() => {
  87. return fs.readdirAsync('./node_modules/brace/mode').then(modeList => {
  88. return Promise.map(modeList, mdFile => {
  89. console.info(colors.white(' mode-' + mdFile))
  90. let result = uglify.minify(path.join('./node_modules/brace/mode', mdFile), { output: { 'max_line_len': 1000000 } })
  91. return fs.writeFileAsync(path.join('./assets/js/ace', 'mode-' + mdFile), result.code)
  92. })
  93. })
  94. })
  95. } else {
  96. throw err
  97. }
  98. })
  99. },
  100. /**
  101. * MathJax
  102. */
  103. () => {
  104. return fs.accessAsync('./assets/js/mathjax').then(() => {
  105. console.info(colors.white(' └── ') + colors.magenta('MathJax directory already exists. Task aborted.'))
  106. return true
  107. }).catch(err => {
  108. if (err.code === 'ENOENT') {
  109. console.info(colors.white(' └── ') + colors.green('Copy MathJax dependencies to assets...'))
  110. return fs.ensureDirAsync('./assets/js/mathjax').then(() => {
  111. return fs.copyAsync('./node_modules/mathjax', './assets/js/mathjax', { filter: (src, dest) => {
  112. let srcNormalized = src.replace(/\\/g, '/')
  113. let shouldCopy = false
  114. console.log(srcNormalized)
  115. _.forEach([
  116. '/node_modules/mathjax',
  117. '/node_modules/mathjax/jax',
  118. '/node_modules/mathjax/jax/input',
  119. '/node_modules/mathjax/jax/output'
  120. ], chk => {
  121. if (srcNormalized.endsWith(chk)) {
  122. shouldCopy = true
  123. }
  124. })
  125. _.forEach([
  126. '/node_modules/mathjax/extensions',
  127. '/node_modules/mathjax/MathJax.js',
  128. '/node_modules/mathjax/jax/element',
  129. '/node_modules/mathjax/jax/input/MathML',
  130. '/node_modules/mathjax/jax/input/TeX',
  131. '/node_modules/mathjax/jax/output/SVG'
  132. ], chk => {
  133. if (srcNormalized.indexOf(chk) > 0) {
  134. shouldCopy = true
  135. }
  136. })
  137. if (shouldCopy && srcNormalized.indexOf('/fonts/') > 0 && srcNormalized.indexOf('/STIX-Web') <= 1) {
  138. shouldCopy = false
  139. }
  140. return shouldCopy
  141. }})
  142. })
  143. } else {
  144. throw err
  145. }
  146. })
  147. },
  148. /**
  149. * Bundle pre-init scripts
  150. */
  151. () => {
  152. console.info(colors.white(' └── ') + colors.green('Bundling pre-init scripts...'))
  153. let preInitContent = ''
  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: ['server'],
  200. env: { 'NODE_ENV': 'development' }
  201. })
  202. }, 1000)
  203. break
  204. // =============================================
  205. // CONFIGURE - DEVELOPER MODE
  206. // =============================================
  207. case 'dev-configure':
  208. // Client
  209. fuse = fsbx.FuseBox.init({
  210. homeDir: './client',
  211. outFile: './assets/js/configure.min.js',
  212. alias: ALIASES,
  213. shim: SHIMS,
  214. plugins: [
  215. [ fsbx.SassPlugin({ includePaths: ['../core'] }), fsbx.CSSPlugin() ],
  216. fsbx.BabelPlugin({ comments: false, presets: ['es2015'] }),
  217. fsbx.JSONPlugin()
  218. ],
  219. debug: false,
  220. log: true
  221. })
  222. fuse.devServer('>configure.js', {
  223. port: 4444,
  224. httpServer: false
  225. })
  226. // Server
  227. _.delay(() => {
  228. nodemon({
  229. exec: 'node wiki configure',
  230. ignore: ['assets/', 'client/', 'data/', 'repo/', 'tests/'],
  231. ext: 'js json',
  232. watch: ['server/configure.js'],
  233. env: { 'NODE_ENV': 'development' }
  234. })
  235. }, 1000)
  236. break
  237. // =============================================
  238. // BUILD ONLY MODE
  239. // =============================================
  240. case 'build':
  241. fuse = fsbx.FuseBox.init({
  242. homeDir: './client',
  243. alias: ALIASES,
  244. shim: SHIMS,
  245. plugins: [
  246. fsbx.EnvPlugin({ NODE_ENV: 'production' }),
  247. [ fsbx.SassPlugin({ outputStyle: 'compressed', includePaths: ['./node_modules/requarks-core'] }), fsbx.CSSPlugin() ],
  248. fsbx.BabelPlugin({
  249. config: {
  250. comments: false,
  251. presets: ['es2015']
  252. }
  253. }),
  254. fsbx.JSONPlugin(),
  255. fsbx.UglifyJSPlugin({
  256. compress: { unused: false },
  257. output: { 'max_line_len': 1000000 }
  258. })
  259. ],
  260. debug: false,
  261. log: true
  262. })
  263. fuse.bundle({
  264. './assets/js/bundle.min.js': '>index.js',
  265. './assets/js/configure.min.js': '>configure.js'
  266. }).then(() => {
  267. console.info(colors.green.bold('\nAssets compilation + bundling completed.'))
  268. }).catch(err => {
  269. console.error(colors.red(' X Bundle compilation failed! ' + err.message))
  270. process.exit(1)
  271. })
  272. break
  273. }
  274. })