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.

218 lines
6.0 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. .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.min.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.ensureDirAsync('./assets/js/ace').then(() => {
  65. return fs.readdirAsync('./node_modules/brace/mode').then(modeList => {
  66. return Promise.map(modeList, mdFile => {
  67. console.info(colors.white(' mode-' + mdFile))
  68. let result = uglify.minify(path.join('./node_modules/brace/mode', mdFile), { output: { 'max_line_len': 1000000 } })
  69. return fs.writeFileAsync(path.join('./assets/js/ace', 'mode-' + mdFile), result.code)
  70. })
  71. })
  72. })
  73. }
  74. ], f => { return f() })
  75. // ======================================================
  76. // Fuse Tasks
  77. // ======================================================
  78. let fuse
  79. globalTasks.then(() => {
  80. switch (mode) {
  81. // =============================================
  82. // DEVELOPER MODE
  83. // =============================================
  84. case 'dev':
  85. // Client
  86. fuse = fsbx.FuseBox.init({
  87. homeDir: './client',
  88. outFile: './assets/js/bundle.min.js',
  89. alias: ALIASES,
  90. shim: SHIMS,
  91. plugins: [
  92. [ fsbx.SassPlugin({ includePaths: ['../core'] }), fsbx.CSSPlugin() ],
  93. fsbx.BabelPlugin({ comments: false, presets: ['es2015'] }),
  94. fsbx.JSONPlugin()
  95. ],
  96. debug: false,
  97. log: true
  98. })
  99. fuse.devServer('>index.js', {
  100. port: 4444,
  101. httpServer: false,
  102. hmr: false
  103. })
  104. // Server
  105. _.delay(() => {
  106. nodemon({
  107. script: './server.js',
  108. args: [],
  109. ignore: ['assets/', 'client/', 'data/', 'repo/', 'tests/'],
  110. ext: 'js json',
  111. watch: [
  112. 'controllers',
  113. 'libs',
  114. 'locales',
  115. 'middlewares',
  116. 'models',
  117. 'agent.js',
  118. 'server.js'
  119. ],
  120. env: { 'NODE_ENV': 'development' }
  121. })
  122. }, 1000)
  123. break
  124. // =============================================
  125. // CONFIGURE - DEVELOPER MODE
  126. // =============================================
  127. case 'dev-configure':
  128. // Client
  129. fuse = fsbx.FuseBox.init({
  130. homeDir: './client',
  131. outFile: './assets/js/configure.min.js',
  132. alias: ALIASES,
  133. shim: SHIMS,
  134. plugins: [
  135. [ fsbx.SassPlugin({ includePaths: ['../core'] }), fsbx.CSSPlugin() ],
  136. fsbx.BabelPlugin({ comments: false, presets: ['es2015'] }),
  137. fsbx.JSONPlugin()
  138. ],
  139. debug: false,
  140. log: true
  141. })
  142. fuse.devServer('>configure.js', {
  143. port: 4444,
  144. httpServer: false
  145. })
  146. // Server
  147. _.delay(() => {
  148. nodemon({
  149. exec: 'node wiki configure',
  150. ignore: ['assets/', 'client/', 'data/', 'repo/', 'tests/'],
  151. ext: 'js json',
  152. watch: [
  153. 'configure.js'
  154. ],
  155. env: { 'NODE_ENV': 'development' }
  156. })
  157. }, 1000)
  158. break
  159. // =============================================
  160. // BUILD ONLY MODE
  161. // =============================================
  162. case 'build':
  163. fuse = fsbx.FuseBox.init({
  164. homeDir: './client',
  165. alias: ALIASES,
  166. shim: SHIMS,
  167. plugins: [
  168. fsbx.EnvPlugin({ NODE_ENV: 'production' }),
  169. [ fsbx.SassPlugin({ outputStyle: 'compressed', includePaths: ['./node_modules/requarks-core'] }), fsbx.CSSPlugin() ],
  170. fsbx.BabelPlugin({
  171. config: {
  172. comments: false,
  173. presets: ['es2015']
  174. }
  175. }),
  176. fsbx.JSONPlugin(),
  177. fsbx.UglifyJSPlugin({
  178. compress: { unused: false },
  179. output: { 'max_line_len': 1000000 }
  180. })
  181. ],
  182. debug: false,
  183. log: true
  184. })
  185. fuse.bundle({
  186. './assets/js/bundle.min.js': '>index.js',
  187. './assets/js/configure.min.js': '>configure.js'
  188. }).then(() => {
  189. console.info(colors.green.bold('\nAssets compilation + bundling completed.'))
  190. }).catch(err => {
  191. console.error(colors.red(' X Bundle compilation failed! ' + err.message))
  192. process.exit(1)
  193. })
  194. break
  195. }
  196. })