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.

191 lines
6.2 KiB

  1. 'use strict'
  2. /**
  3. * FUSEBOX
  4. *
  5. * Client & Server compiler / bundler / watcher
  6. */
  7. const colors = require('colors/safe')
  8. const fsbx = require('fuse-box')
  9. const nodemon = require('nodemon')
  10. const babel = require('babel-core')
  11. const uglify = require('uglify-es')
  12. const fs = require('fs-extra')
  13. // ======================================================
  14. // Parse cmd arguments
  15. // ======================================================
  16. const args = require('yargs')
  17. .option('d', {
  18. alias: 'dev',
  19. describe: 'Start in Developer mode',
  20. type: 'boolean'
  21. })
  22. .option('c', {
  23. alias: 'dev-configure',
  24. describe: 'Start in Configure Developer mode',
  25. type: 'boolean'
  26. })
  27. .help('h')
  28. .alias('h', 'help')
  29. .argv
  30. let mode = 'build'
  31. const dev = args.d || args.c
  32. if (args.d) {
  33. console.info(colors.bgWhite.black(' Starting Fuse in DEVELOPER mode... '))
  34. mode = 'dev'
  35. } else if (args.c) {
  36. console.info(colors.bgWhite.black(' Starting Fuse in CONFIGURE DEVELOPER mode... '))
  37. mode = 'dev-configure'
  38. } else {
  39. console.info(colors.bgWhite.black(' Starting Fuse in BUILD mode... '))
  40. }
  41. // ======================================================
  42. // BUILD VARS
  43. // ======================================================
  44. const ALIASES = {
  45. 'brace-ext-modelist': 'brace/ext/modelist.js',
  46. 'simplemde': 'simplemde/dist/simplemde.min.js',
  47. 'socket-io-client': 'socket.io-client/dist/socket.io.js',
  48. 'vue': (dev) ? 'vue/dist/vue.js' : 'vue/dist/vue.min.js',
  49. 'vue-lodash': 'vue-lodash/dist/vue-lodash.min.js',
  50. 'vue-resource': (dev) ? 'vue-resource/dist/vue-resource.js' : 'vue-resource/dist/vue-resource.es2015.js'
  51. }
  52. const SHIMS = {
  53. jquery: {
  54. source: 'node_modules/jquery/dist/jquery.js',
  55. exports: '$'
  56. },
  57. diff2html: {
  58. source: 'node_modules/diff2html/dist/diff2html.min.js',
  59. exports: 'Diff2Html'
  60. },
  61. diff2htmlui: {
  62. source: 'node_modules/diff2html/dist/diff2html-ui.min.js',
  63. exports: 'Diff2HtmlUI'
  64. }
  65. }
  66. // ======================================================
  67. // Global Tasks
  68. // ======================================================
  69. console.info(colors.white('└── ') + colors.green('Running global tasks...'))
  70. let globalTasks = require('./.build/_tasks')
  71. // ======================================================
  72. // Fuse Tasks
  73. // ======================================================
  74. globalTasks.then(() => {
  75. let fuse = fsbx.FuseBox.init({
  76. homeDir: './client',
  77. output: './assets/js/$name.js',
  78. alias: ALIASES,
  79. target: 'browser',
  80. plugins: [
  81. fsbx.EnvPlugin({ NODE_ENV: (dev) ? 'development' : 'production' }),
  82. fsbx.VuePlugin(),
  83. ['.scss', fsbx.SassPlugin({ outputStyle: (dev) ? 'nested' : 'compressed' }), fsbx.CSSPlugin()],
  84. fsbx.BabelPlugin({ comments: false, presets: ['es2015'] }),
  85. fsbx.JSONPlugin()
  86. /* !dev && fsbx.QuantumPlugin({
  87. target: 'browser',
  88. uglify: true,
  89. api: (core) => {
  90. core.solveComputed('default/js/components/editor-codeblock.vue', {
  91. mapping: '/js/ace/ace.js',
  92. fn: (statement, core) => {
  93. statement.setExpression(`'/js/ace/ace.js'`)
  94. }
  95. })
  96. core.solveComputed('default/js/components/editor.component.js', {
  97. mapping: '/js/simplemde/simplemde.min.js',
  98. fn: (statement, core) => {
  99. statement.setExpression(`'/js/simplemde/simplemde.min.js'`)
  100. }
  101. })
  102. }
  103. }) */
  104. // !dev && fsbx.UglifyESPlugin()
  105. ],
  106. debug: false,
  107. log: true
  108. })
  109. const bundleVendor = fuse.bundle('vendor').shim(SHIMS).instructions('~ index.js') // eslint-disable-line no-unused-vars
  110. const bundleApp = fuse.bundle('app').instructions('!> [index.js]')
  111. // const bundleApp = fuse.bundle('app').shim(SHIMS).instructions('> index.js')
  112. const bundleSetup = fuse.bundle('configure').instructions('> configure.js')
  113. switch (mode) {
  114. case 'dev':
  115. bundleApp.watch()
  116. break
  117. case 'dev-configure':
  118. bundleSetup.watch()
  119. break
  120. }
  121. fuse.run().then(() => {
  122. console.info(colors.green.bold('\nAssets compilation + bundling completed.'))
  123. if (dev) {
  124. nodemon({
  125. exec: (args.d) ? 'node server' : 'node wiki configure',
  126. ignore: ['assets/', 'client/', 'data/', 'repo/', 'tests/'],
  127. ext: 'js json',
  128. watch: (args.d) ? ['server'] : ['server/configure.js'],
  129. env: { 'NODE_ENV': 'development' }
  130. })
  131. } else {
  132. console.info(colors.yellow.bold('\nTranspiling vendor bundle...'))
  133. let appCode = babel.transform(fs.readFileSync('./assets/js/app.js', 'utf8'), {
  134. babelrc: false,
  135. compact: false,
  136. filename: 'app.js',
  137. plugins: ['transform-object-assign']
  138. }).code
  139. let vendorCode = babel.transform(fs.readFileSync('./assets/js/vendor.js', 'utf8'), {
  140. babelrc: false,
  141. comments: false,
  142. compact: false,
  143. filename: 'vendor.js',
  144. plugins: [
  145. 'transform-es2015-arrow-functions',
  146. 'transform-es2015-block-scoped-functions',
  147. 'transform-es2015-block-scoping',
  148. 'transform-es2015-classes',
  149. 'transform-es2015-computed-properties',
  150. 'transform-es2015-destructuring',
  151. 'transform-es2015-duplicate-keys',
  152. 'transform-es2015-for-of',
  153. 'transform-es2015-function-name',
  154. 'transform-es2015-literals',
  155. 'transform-es2015-object-super',
  156. 'transform-es2015-parameters',
  157. 'transform-es2015-shorthand-properties',
  158. 'transform-es2015-spread',
  159. 'transform-es2015-sticky-regex',
  160. 'transform-es2015-template-literals',
  161. 'transform-es2015-typeof-symbol',
  162. 'transform-es2015-unicode-regex'
  163. ]
  164. }).code
  165. console.info(colors.yellow.bold('Minifing bundles...'))
  166. fs.writeFileSync('./assets/js/vendor.js', uglify.minify(vendorCode).code, 'utf8')
  167. fs.writeFileSync('./assets/js/app.js', uglify.minify(appCode).code, 'utf8')
  168. fs.writeFileSync('./assets/js/configure.js', uglify.minify(fs.readFileSync('./assets/js/configure.js', 'utf8')).code, 'utf8')
  169. console.info(colors.green.bold('\nBUILD SUCCEEDED.'))
  170. return true
  171. }
  172. }).catch(err => {
  173. console.error(colors.red(' X Bundle compilation failed! ' + err.message))
  174. process.exit(1)
  175. })
  176. })