fuse.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. 'use strict'
  2. /**
  3. * FUSEBOX
  4. *
  5. * Client & Server compiler / bundler / watcher
  6. */
  7. const autoprefixer = require('autoprefixer')
  8. const colors = require('colors/safe')
  9. const fsbx = require('fuse-box')
  10. const nodemon = require('nodemon')
  11. const fs = require('fs-extra')
  12. // -------------------------------------------------------
  13. // Parse cmd arguments
  14. // -------------------------------------------------------
  15. const args = require('yargs')
  16. .option('d', {
  17. alias: 'dev',
  18. describe: 'Start in Developer mode',
  19. type: 'boolean'
  20. })
  21. .help('h')
  22. .alias('h', 'help')
  23. .argv
  24. const dev = args.dev
  25. if (dev) {
  26. console.info(colors.bgWhite.black(' Starting Fuse in DEVELOPER mode... '))
  27. } else {
  28. console.info(colors.bgWhite.black(' Starting Fuse in BUILD mode... '))
  29. }
  30. // -------------------------------------------------------
  31. // BUILD VARS
  32. // -------------------------------------------------------
  33. const ALIASES = {
  34. 'brace-ext-modelist': 'brace/ext/modelist.js',
  35. 'simplemde': 'simplemde/dist/simplemde.min.js',
  36. 'socket-io-client': 'socket.io-client/dist/socket.io.js',
  37. 'vue': (dev) ? 'vue/dist/vue.js' : 'vue/dist/vue.min.js',
  38. 'vue-lodash': 'vue-lodash/dist/vue-lodash.min.js',
  39. 'vue-resource': (dev) ? 'vue-resource/dist/vue-resource.js' : 'vue-resource/dist/vue-resource.es2015.js'
  40. }
  41. const SHIMS = {
  42. diff2html: {
  43. source: '../node_modules/diff2html/dist/diff2html.min.js',
  44. exports: 'Diff2Html'
  45. },
  46. diff2htmlui: {
  47. source: '../node_modules/diff2html/dist/diff2html-ui.min.js',
  48. exports: 'Diff2HtmlUI'
  49. }
  50. }
  51. // -------------------------------------------------------
  52. // Global Tasks
  53. // -------------------------------------------------------
  54. console.info(colors.white('└── ') + colors.green('Running global tasks...'))
  55. let globalTasks = require('./fuse_tasks')
  56. // -------------------------------------------------------
  57. // FUSEBOX PRODUCER
  58. // -------------------------------------------------------
  59. const babelrc = fs.readJsonSync('.babelrc')
  60. const scssChain = [
  61. fsbx.SassPlugin({
  62. includePaths: ['node_modules'],
  63. outputStyle: dev ? 'nested' : 'compressed'
  64. }),
  65. fsbx.PostCSS([
  66. autoprefixer({
  67. remove: false,
  68. browsers: babelrc.presets[0][1].targets.browsers
  69. })
  70. ]),
  71. fsbx.CSSPlugin()
  72. ]
  73. globalTasks.then(() => {
  74. let fuse = fsbx.FuseBox.init({
  75. homeDir: '../client',
  76. output: '../assets/js/$name.js',
  77. alias: ALIASES,
  78. target: 'browser',
  79. tsConfig: './tsconfig.json',
  80. plugins: [
  81. fsbx.EnvPlugin({ NODE_ENV: (dev) ? 'development' : 'production' }),
  82. fsbx.VueComponentPlugin({
  83. script: fsbx.BabelPlugin(babelrc),
  84. template: fsbx.ConsolidatePlugin({
  85. engine: 'pug'
  86. }),
  87. style: scssChain
  88. }),
  89. scssChain,
  90. fsbx.RawPlugin(['.svg']),
  91. fsbx.BabelPlugin(babelrc),
  92. fsbx.JSONPlugin()
  93. ],
  94. debug: false,
  95. log: true
  96. })
  97. // -------------------------------------------------------
  98. // FUSEBOX DEV
  99. // -------------------------------------------------------
  100. if (dev) {
  101. fuse.dev({
  102. port: 5555,
  103. httpServer: false
  104. })
  105. }
  106. // -------------------------------------------------------
  107. // FUSEBOX BUNDLES
  108. // -------------------------------------------------------
  109. if (dev) {
  110. fuse.bundle('libs').shim(SHIMS).instructions('~ index.js')
  111. fuse.bundle('app').instructions('!> [index.js]').hmr({ reload: true }).watch()
  112. } else {
  113. fuse.bundle('bundle.min.js').shim(SHIMS).instructions('> index.js')
  114. }
  115. // -------------------------------------------------------
  116. // FUSEBOX RUN
  117. // -------------------------------------------------------
  118. fuse.run().then(() => {
  119. console.info(colors.green.bold('\nAssets compilation + bundling completed.'))
  120. if (dev) {
  121. nodemon({
  122. exec: 'node server',
  123. ignore: ['assets/', 'client/', 'data/', 'repo/', 'tests/', 'tools/'],
  124. ext: 'js json graphql',
  125. watch: ['server'],
  126. env: { 'NODE_ENV': 'development' }
  127. })
  128. }
  129. return true
  130. }).catch(err => {
  131. console.error(colors.red(' X Bundle compilation failed! ' + err.message))
  132. process.exit(1)
  133. })
  134. })