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.

179 lines
4.7 KiB

  1. 'use strict'
  2. /**
  3. * FUSEBOX
  4. *
  5. * Client & Server compiler / bundler / watcher
  6. */
  7. const Promise = require('bluebird')
  8. const autoprefixer = require('autoprefixer')
  9. const colors = require('colors/safe')
  10. const fsbx = require('fuse-box')
  11. const nodemon = require('nodemon')
  12. const fs = require('fs-extra')
  13. const yargs = require('yargs')
  14. const yaml = require('js-yaml')
  15. const path = require('path')
  16. // -------------------------------------------------------
  17. // PARSE CMD ARGUMENTS
  18. // -------------------------------------------------------
  19. const opts = yargs
  20. .option('d', {
  21. alias: 'dev',
  22. describe: 'Start in Developer mode',
  23. type: 'boolean'
  24. })
  25. .option('b', {
  26. alias: 'build',
  27. describe: 'Start in Build mode',
  28. type: 'boolean'
  29. })
  30. .help('h')
  31. .alias('h', 'help')
  32. .argv
  33. if (opts.dev) {
  34. console.info(colors.bgWhite.black(' Starting Wiki.js in DEVELOPER mode... '))
  35. } else if (opts.build) {
  36. console.info(colors.bgWhite.black(' Starting Wiki.js in BUILD mode... '))
  37. } else {
  38. yargs.showHelp()
  39. process.exit(0)
  40. }
  41. // -------------------------------------------------------
  42. // GET CONFIG
  43. // -------------------------------------------------------
  44. try {
  45. const config = yaml.safeLoad(fs.readFileSync(path.join(process.cwd(), 'dev/config/config.yml'), 'utf8'))
  46. global.config = config
  47. } catch (ex) {
  48. console.error(ex)
  49. process.exit(1)
  50. }
  51. // -------------------------------------------------------
  52. // BUILD VARS
  53. // -------------------------------------------------------
  54. const ALIASES = {
  55. 'brace-ext-modelist': 'brace/ext/modelist.js',
  56. 'simplemde': 'simplemde/dist/simplemde.min.js',
  57. 'vue': (opts.dev) ? 'vue/dist/vue.js' : 'vue/dist/vue.min.js',
  58. 'vue-lodash': 'vue-lodash/dist/vue-lodash.min.js',
  59. 'vue-resource': (opts.dev) ? 'vue-resource/dist/vue-resource.js' : 'vue-resource/dist/vue-resource.es2015.js'
  60. }
  61. const SHIMS = {
  62. diff2html: {
  63. source: '../../node_modules/diff2html/dist/diff2html.min.js',
  64. exports: 'Diff2Html'
  65. },
  66. diff2htmlui: {
  67. source: '../../node_modules/diff2html/dist/diff2html-ui.min.js',
  68. exports: 'Diff2HtmlUI'
  69. }
  70. }
  71. // -------------------------------------------------------
  72. // Tasks
  73. // -------------------------------------------------------
  74. console.info(colors.white('└── ') + colors.green('Running tasks...'))
  75. let tasks = require('./tasks')
  76. let tasksToRun = []
  77. tasksToRun.push(tasks.cleanFuseboxCache)
  78. tasksToRun.push(tasks.copySimpleMdeAssets)
  79. tasksToRun.push(tasks.copyAceModes)
  80. if (opts.build) {
  81. tasksToRun.push(tasks.cleanTestResults)
  82. tasksToRun.push(tasks.fetchLocalizationResources)
  83. }
  84. // -------------------------------------------------------
  85. // FUSEBOX PRODUCER
  86. // -------------------------------------------------------
  87. const babelrc = fs.readJsonSync('.babelrc')
  88. const scssChain = [
  89. fsbx.SassPlugin({
  90. includePaths: ['node_modules'],
  91. outputStyle: opts.dev ? 'nested' : 'compressed'
  92. }),
  93. fsbx.PostCSS([
  94. autoprefixer({
  95. remove: false,
  96. browsers: babelrc.presets[0][1].targets.browsers
  97. })
  98. ]),
  99. fsbx.CSSPlugin(opts.dev ? {} : {
  100. group: 'bundle.css',
  101. outFile: '../../assets/css/bundle.css',
  102. inject: false
  103. })
  104. ]
  105. Promise.mapSeries(tasksToRun, fn => fn()).then(() => {
  106. let fuse = fsbx.FuseBox.init({
  107. homeDir: '../../client',
  108. output: '../../assets/js/$name.js',
  109. alias: ALIASES,
  110. target: 'browser',
  111. tsConfig: '../config/tsconfig.json',
  112. plugins: [
  113. fsbx.EnvPlugin({ NODE_ENV: (opts.dev) ? 'development' : 'production' }),
  114. fsbx.VueComponentPlugin({
  115. script: fsbx.BabelPlugin(babelrc),
  116. template: fsbx.ConsolidatePlugin({
  117. engine: 'pug'
  118. }),
  119. style: scssChain
  120. }),
  121. scssChain,
  122. fsbx.RawPlugin(['.svg']),
  123. fsbx.BabelPlugin(babelrc),
  124. fsbx.JSONPlugin()
  125. ],
  126. debug: false,
  127. log: true
  128. })
  129. // -------------------------------------------------------
  130. // FUSEBOX DEV
  131. // -------------------------------------------------------
  132. if (opts.dev) {
  133. fuse.dev({
  134. port: 5555,
  135. httpServer: false
  136. })
  137. }
  138. // -------------------------------------------------------
  139. // FUSEBOX BUNDLES
  140. // -------------------------------------------------------
  141. if (opts.dev) {
  142. fuse.bundle('libs').shim(SHIMS).instructions('~ index.js')
  143. fuse.bundle('app').instructions('!> [index.js]').hmr({ reload: true }).watch()
  144. } else {
  145. fuse.bundle('bundle.min.js').shim(SHIMS).instructions('> index.js')
  146. }
  147. // -------------------------------------------------------
  148. // FUSEBOX RUN
  149. // -------------------------------------------------------
  150. fuse.run().then(() => {
  151. console.info(colors.green.bold('\nAssets compilation + bundling completed.'))
  152. return true
  153. }).catch(err => {
  154. console.error(colors.red(' X Bundle compilation failed! ' + err.message))
  155. process.exit(1)
  156. })
  157. })