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.

193 lines
6.3 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.hmr().watch()
  117. fuse.dev({ httpServer: false })
  118. break
  119. case 'dev-configure':
  120. bundleSetup.watch()
  121. break
  122. }
  123. fuse.run().then(() => {
  124. console.info(colors.green.bold('\nAssets compilation + bundling completed.'))
  125. if (dev) {
  126. nodemon({
  127. exec: (args.d) ? 'node server' : 'node wiki configure',
  128. ignore: ['assets/', 'client/', 'data/', 'repo/', 'tests/'],
  129. ext: 'js json graphql',
  130. watch: (args.d) ? ['server'] : ['server/configure.js'],
  131. env: { 'NODE_ENV': 'development' }
  132. })
  133. } else {
  134. console.info(colors.yellow.bold('\nTranspiling vendor bundle...'))
  135. let appCode = babel.transform(fs.readFileSync('./assets/js/app.js', 'utf8'), {
  136. babelrc: false,
  137. compact: false,
  138. filename: 'app.js',
  139. plugins: ['transform-object-assign']
  140. }).code
  141. let vendorCode = babel.transform(fs.readFileSync('./assets/js/vendor.js', 'utf8'), {
  142. babelrc: false,
  143. comments: false,
  144. compact: false,
  145. filename: 'vendor.js',
  146. plugins: [
  147. 'transform-es2015-arrow-functions',
  148. 'transform-es2015-block-scoped-functions',
  149. 'transform-es2015-block-scoping',
  150. 'transform-es2015-classes',
  151. 'transform-es2015-computed-properties',
  152. 'transform-es2015-destructuring',
  153. 'transform-es2015-duplicate-keys',
  154. 'transform-es2015-for-of',
  155. 'transform-es2015-function-name',
  156. 'transform-es2015-literals',
  157. 'transform-es2015-object-super',
  158. 'transform-es2015-parameters',
  159. 'transform-es2015-shorthand-properties',
  160. 'transform-es2015-spread',
  161. 'transform-es2015-sticky-regex',
  162. 'transform-es2015-template-literals',
  163. 'transform-es2015-typeof-symbol',
  164. 'transform-es2015-unicode-regex'
  165. ]
  166. }).code
  167. console.info(colors.yellow.bold('Minifing bundles...'))
  168. fs.writeFileSync('./assets/js/vendor.js', uglify.minify(vendorCode).code, 'utf8')
  169. fs.writeFileSync('./assets/js/app.js', uglify.minify(appCode).code, 'utf8')
  170. fs.writeFileSync('./assets/js/configure.js', uglify.minify(fs.readFileSync('./assets/js/configure.js', 'utf8')).code, 'utf8')
  171. console.info(colors.green.bold('\nBUILD SUCCEEDED.'))
  172. return true
  173. }
  174. }).catch(err => {
  175. console.error(colors.red(' X Bundle compilation failed! ' + err.message))
  176. process.exit(1)
  177. })
  178. })