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.

192 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('./fuse_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. tsConfig: './tsconfig.json',
  81. plugins: [
  82. fsbx.EnvPlugin({ NODE_ENV: (dev) ? 'development' : 'production' }),
  83. fsbx.VuePlugin(),
  84. ['.scss', fsbx.SassPlugin({ outputStyle: (dev) ? 'nested' : 'compressed' }), fsbx.CSSPlugin()],
  85. fsbx.BabelPlugin({ comments: false, presets: ['es2015'] }),
  86. fsbx.JSONPlugin()
  87. /* !dev && fsbx.QuantumPlugin({
  88. target: 'browser',
  89. uglify: true,
  90. api: (core) => {
  91. core.solveComputed('default/js/components/editor-codeblock.vue', {
  92. mapping: '/js/ace/ace.js',
  93. fn: (statement, core) => {
  94. statement.setExpression(`'/js/ace/ace.js'`)
  95. }
  96. })
  97. core.solveComputed('default/js/components/editor.component.js', {
  98. mapping: '/js/simplemde/simplemde.min.js',
  99. fn: (statement, core) => {
  100. statement.setExpression(`'/js/simplemde/simplemde.min.js'`)
  101. }
  102. })
  103. }
  104. }) */
  105. // !dev && fsbx.UglifyESPlugin()
  106. ],
  107. debug: false,
  108. log: true
  109. })
  110. const bundleVendor = fuse.bundle('vendor').shim(SHIMS).instructions('~ index.js') // eslint-disable-line no-unused-vars
  111. const bundleApp = fuse.bundle('app').instructions('!> [index.js]')
  112. // const bundleApp = fuse.bundle('app').shim(SHIMS).instructions('> index.js')
  113. const bundleSetup = fuse.bundle('configure').instructions('> configure.js')
  114. switch (mode) {
  115. case 'dev':
  116. bundleApp.watch()
  117. break
  118. case 'dev-configure':
  119. bundleSetup.watch()
  120. break
  121. }
  122. fuse.run().then(() => {
  123. console.info(colors.green.bold('\nAssets compilation + bundling completed.'))
  124. if (dev) {
  125. nodemon({
  126. exec: (args.d) ? 'node server' : 'node wiki configure',
  127. ignore: ['assets/', 'client/', 'data/', 'repo/', 'tests/'],
  128. ext: 'js json',
  129. watch: (args.d) ? ['server'] : ['server/configure.js'],
  130. env: { 'NODE_ENV': 'development' }
  131. })
  132. } else {
  133. console.info(colors.yellow.bold('\nTranspiling vendor bundle...'))
  134. let appCode = babel.transform(fs.readFileSync('./assets/js/app.js', 'utf8'), {
  135. babelrc: false,
  136. compact: false,
  137. filename: 'app.js',
  138. plugins: ['transform-object-assign']
  139. }).code
  140. let vendorCode = babel.transform(fs.readFileSync('./assets/js/vendor.js', 'utf8'), {
  141. babelrc: false,
  142. comments: false,
  143. compact: false,
  144. filename: 'vendor.js',
  145. plugins: [
  146. 'transform-es2015-arrow-functions',
  147. 'transform-es2015-block-scoped-functions',
  148. 'transform-es2015-block-scoping',
  149. 'transform-es2015-classes',
  150. 'transform-es2015-computed-properties',
  151. 'transform-es2015-destructuring',
  152. 'transform-es2015-duplicate-keys',
  153. 'transform-es2015-for-of',
  154. 'transform-es2015-function-name',
  155. 'transform-es2015-literals',
  156. 'transform-es2015-object-super',
  157. 'transform-es2015-parameters',
  158. 'transform-es2015-shorthand-properties',
  159. 'transform-es2015-spread',
  160. 'transform-es2015-sticky-regex',
  161. 'transform-es2015-template-literals',
  162. 'transform-es2015-typeof-symbol',
  163. 'transform-es2015-unicode-regex'
  164. ]
  165. }).code
  166. console.info(colors.yellow.bold('Minifing bundles...'))
  167. fs.writeFileSync('./assets/js/vendor.js', uglify.minify(vendorCode).code, 'utf8')
  168. fs.writeFileSync('./assets/js/app.js', uglify.minify(appCode).code, 'utf8')
  169. fs.writeFileSync('./assets/js/configure.js', uglify.minify(fs.readFileSync('./assets/js/configure.js', 'utf8')).code, 'utf8')
  170. console.info(colors.green.bold('\nBUILD SUCCEEDED.'))
  171. return true
  172. }
  173. }).catch(err => {
  174. console.error(colors.red(' X Bundle compilation failed! ' + err.message))
  175. process.exit(1)
  176. })
  177. })