fuse.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 Wiki.js in DEVELOPER mode... '))
  27. } else {
  28. console.info(colors.bgWhite.black(' Starting Wiki.js 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. 'vue': (dev) ? 'vue/dist/vue.js' : 'vue/dist/vue.min.js',
  37. 'vue-lodash': 'vue-lodash/dist/vue-lodash.min.js',
  38. 'vue-resource': (dev) ? 'vue-resource/dist/vue-resource.js' : 'vue-resource/dist/vue-resource.es2015.js'
  39. }
  40. const SHIMS = {
  41. diff2html: {
  42. source: '../node_modules/diff2html/dist/diff2html.min.js',
  43. exports: 'Diff2Html'
  44. },
  45. diff2htmlui: {
  46. source: '../node_modules/diff2html/dist/diff2html-ui.min.js',
  47. exports: 'Diff2HtmlUI'
  48. }
  49. }
  50. // -------------------------------------------------------
  51. // Global Tasks
  52. // -------------------------------------------------------
  53. console.info(colors.white('└── ') + colors.green('Running global tasks...'))
  54. let globalTasks = require('./fuse_tasks')
  55. // -------------------------------------------------------
  56. // FUSEBOX PRODUCER
  57. // -------------------------------------------------------
  58. const babelrc = fs.readJsonSync('.babelrc')
  59. const scssChain = [
  60. fsbx.SassPlugin({
  61. includePaths: ['node_modules'],
  62. outputStyle: dev ? 'nested' : 'compressed'
  63. }),
  64. fsbx.PostCSS([
  65. autoprefixer({
  66. remove: false,
  67. browsers: babelrc.presets[0][1].targets.browsers
  68. })
  69. ]),
  70. fsbx.CSSPlugin(dev ? {} : {
  71. group: 'bundle.css',
  72. outFile: './assets/css/bundle.css',
  73. inject: false
  74. })
  75. ]
  76. globalTasks.then(() => {
  77. let fuse = fsbx.FuseBox.init({
  78. homeDir: '../client',
  79. output: '../assets/js/$name.js',
  80. alias: ALIASES,
  81. target: 'browser',
  82. tsConfig: './tsconfig.json',
  83. plugins: [
  84. fsbx.EnvPlugin({ NODE_ENV: (dev) ? 'development' : 'production' }),
  85. fsbx.VueComponentPlugin({
  86. script: fsbx.BabelPlugin(babelrc),
  87. template: fsbx.ConsolidatePlugin({
  88. engine: 'pug'
  89. }),
  90. style: scssChain
  91. }),
  92. scssChain,
  93. fsbx.RawPlugin(['.svg']),
  94. fsbx.BabelPlugin(babelrc),
  95. fsbx.JSONPlugin()
  96. ],
  97. debug: false,
  98. log: true
  99. })
  100. // -------------------------------------------------------
  101. // FUSEBOX DEV
  102. // -------------------------------------------------------
  103. if (dev) {
  104. fuse.dev({
  105. port: 5555,
  106. httpServer: false
  107. })
  108. }
  109. // -------------------------------------------------------
  110. // FUSEBOX BUNDLES
  111. // -------------------------------------------------------
  112. if (dev) {
  113. fuse.bundle('libs').shim(SHIMS).instructions('~ index.js')
  114. fuse.bundle('app').instructions('!> [index.js]').hmr({ reload: true }).watch()
  115. } else {
  116. fuse.bundle('bundle.min.js').shim(SHIMS).instructions('> index.js')
  117. }
  118. // -------------------------------------------------------
  119. // FUSEBOX RUN
  120. // -------------------------------------------------------
  121. fuse.run().then(() => {
  122. console.info(colors.green.bold('\nAssets compilation + bundling completed.'))
  123. if (dev) {
  124. nodemon({
  125. exec: 'node server',
  126. ignore: ['assets/', 'client/', 'data/', 'repo/', 'tests/', 'tools/'],
  127. ext: 'js json graphql',
  128. watch: ['server'],
  129. env: { 'NODE_ENV': 'development' }
  130. })
  131. }
  132. return true
  133. }).catch(err => {
  134. console.error(colors.red(' X Bundle compilation failed! ' + err.message))
  135. process.exit(1)
  136. })
  137. })