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.

215 lines
5.8 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'))
  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. if (args.d) {
  34. console.info(colors.bgWhite.black(' Starting Fuse in DEVELOPER mode... '))
  35. mode = 'dev'
  36. } else if (args.c) {
  37. console.info(colors.bgWhite.black(' Starting Fuse in CONFIGURE DEVELOPER mode... '))
  38. mode = 'dev-configure'
  39. } else {
  40. console.info(colors.bgWhite.black(' Starting Fuse in BUILD mode... '))
  41. }
  42. // ======================================================
  43. // Define aliases / shims
  44. // ======================================================
  45. const ALIASES = {
  46. 'brace-ext-modelist': 'brace/ext/modelist.js',
  47. 'simplemde': 'simplemde/dist/simplemde.min.js',
  48. 'socket.io-client': 'socket.io-client/dist/socket.io.min.js',
  49. 'vue': 'vue/dist/vue.js'
  50. }
  51. const SHIMS = {
  52. jquery: {
  53. source: 'node_modules/jquery/dist/jquery.js',
  54. exports: '$'
  55. }
  56. }
  57. // ======================================================
  58. // Global Tasks
  59. // ======================================================
  60. console.info(colors.white('└── ') + colors.green('Running global tasks...'))
  61. let globalTasks = Promise.mapSeries([
  62. () => {
  63. console.info(colors.white(' └── ') + colors.green('Copy + Minify ACE modes to assets...'))
  64. return fs.readdirAsync('./node_modules/brace/mode').then(modeList => {
  65. return Promise.map(modeList, mdFile => {
  66. console.info(colors.white(' mode-' + mdFile))
  67. let result = uglify.minify(path.join('./node_modules/brace/mode', mdFile), { output: { 'max_line_len': 1000000 } })
  68. return fs.writeFileAsync(path.join('./assets/js/ace', 'mode-' + mdFile), result.code)
  69. })
  70. })
  71. }
  72. ], f => { return f() })
  73. // ======================================================
  74. // Fuse Tasks
  75. // ======================================================
  76. let fuse
  77. globalTasks.then(() => {
  78. switch (mode) {
  79. // =============================================
  80. // DEVELOPER MODE
  81. // =============================================
  82. case 'dev':
  83. // Client
  84. fuse = fsbx.FuseBox.init({
  85. homeDir: './client',
  86. outFile: './assets/js/bundle.min.js',
  87. alias: ALIASES,
  88. shim: SHIMS,
  89. plugins: [
  90. [ fsbx.SassPlugin({ includePaths: ['../core'] }), fsbx.CSSPlugin() ],
  91. fsbx.BabelPlugin({ comments: false, presets: ['es2015'] }),
  92. fsbx.JSONPlugin()
  93. ],
  94. debug: false,
  95. log: true
  96. })
  97. fuse.devServer('>index.js', {
  98. port: 4444,
  99. httpServer: false,
  100. hmr: false
  101. })
  102. // Server
  103. _.delay(() => {
  104. nodemon({
  105. script: './server.js',
  106. args: [],
  107. ignore: ['assets/', 'client/', 'data/', 'repo/', 'tests/'],
  108. ext: 'js json',
  109. watch: [
  110. 'controllers',
  111. 'libs',
  112. 'locales',
  113. 'middlewares',
  114. 'models',
  115. 'agent.js',
  116. 'server.js'
  117. ],
  118. env: { 'NODE_ENV': 'development' }
  119. })
  120. }, 1000)
  121. break
  122. // =============================================
  123. // CONFIGURE - DEVELOPER MODE
  124. // =============================================
  125. case 'dev-configure':
  126. // Client
  127. fuse = fsbx.FuseBox.init({
  128. homeDir: './client',
  129. outFile: './assets/js/configure.min.js',
  130. alias: ALIASES,
  131. shim: SHIMS,
  132. plugins: [
  133. [ fsbx.SassPlugin({ includePaths: ['../core'] }), fsbx.CSSPlugin() ],
  134. fsbx.BabelPlugin({ comments: false, presets: ['es2015'] }),
  135. fsbx.JSONPlugin()
  136. ],
  137. debug: false,
  138. log: true
  139. })
  140. fuse.devServer('>configure.js', {
  141. port: 4444,
  142. httpServer: false
  143. })
  144. // Server
  145. _.delay(() => {
  146. nodemon({
  147. exec: 'node wiki configure',
  148. ignore: ['assets/', 'client/', 'data/', 'repo/', 'tests/'],
  149. ext: 'js json',
  150. watch: [
  151. 'configure.js'
  152. ],
  153. env: { 'NODE_ENV': 'development' }
  154. })
  155. }, 1000)
  156. break
  157. // =============================================
  158. // BUILD ONLY MODE
  159. // =============================================
  160. case 'build':
  161. fuse = fsbx.FuseBox.init({
  162. homeDir: './client',
  163. alias: ALIASES,
  164. shim: SHIMS,
  165. plugins: [
  166. [ fsbx.SassPlugin({ outputStyle: 'compressed', includePaths: ['./node_modules/requarks-core'] }), fsbx.CSSPlugin() ],
  167. fsbx.BabelPlugin({
  168. config: {
  169. comments: false,
  170. presets: ['es2015']
  171. }
  172. }),
  173. fsbx.JSONPlugin(),
  174. fsbx.UglifyJSPlugin({
  175. compress: { unused: false },
  176. output: { 'max_line_len': 1000000 }
  177. })
  178. ],
  179. debug: false,
  180. log: true
  181. })
  182. fuse.bundle({
  183. './assets/js/bundle.min.js': '>index.js',
  184. './assets/js/configure.min.js': '>configure.js'
  185. }).then(() => {
  186. console.info(colors.green.bold('\nAssets compilation + bundling completed.'))
  187. }).catch(err => {
  188. console.error(colors.red(' X Bundle compilation failed! ' + err.message))
  189. process.exit(1)
  190. })
  191. break
  192. }
  193. })