123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- 'use strict'
- /**
- * FUSEBOX
- *
- * Client & Server compiler / bundler / watcher
- */
- const _ = require('lodash')
- const Promise = require('bluebird')
- const colors = require('colors/safe')
- const fs = Promise.promisifyAll(require('fs-extra'))
- const fsbx = require('fuse-box')
- const nodemon = require('nodemon')
- const path = require('path')
- const uglify = require('uglify-js')
- // ======================================================
- // Parse cmd arguments
- // ======================================================
- const args = require('yargs')
- .option('d', {
- alias: 'dev',
- describe: 'Start in Developer mode',
- type: 'boolean'
- })
- .option('c', {
- alias: 'dev-configure',
- describe: 'Start in Configure Developer mode',
- type: 'boolean'
- })
- .option('i', {
- alias: 'inspect',
- describe: 'Enable Inspector for debugging',
- type: 'boolean',
- implies: 'd'
- })
- .help('h')
- .alias('h', 'help')
- .argv
- let mode = 'build'
- if (args.d) {
- console.info(colors.bgWhite.black(' Starting Fuse in DEVELOPER mode... '))
- mode = 'dev'
- } else if (args.c) {
- console.info(colors.bgWhite.black(' Starting Fuse in CONFIGURE DEVELOPER mode... '))
- mode = 'dev-configure'
- } else {
- console.info(colors.bgWhite.black(' Starting Fuse in BUILD mode... '))
- }
- // ======================================================
- // Define aliases / shims
- // ======================================================
- const ALIASES = {
- 'brace-ext-modelist': 'brace/ext/modelist.js',
- 'simplemde': 'simplemde/dist/simplemde.min.js',
- 'socket.io-client': 'socket.io-client/dist/socket.io.min.js',
- 'vue': 'vue/dist/vue.min.js'
- }
- const SHIMS = {
- _preinit: {
- source: '.build/_preinit.js',
- exports: '_preinit'
- },
- jquery: {
- source: 'node_modules/jquery/dist/jquery.js',
- exports: '$'
- },
- mathjax: {
- source: 'node_modules/mathjax/MathJax.js',
- exports: 'MathJax'
- }
- }
- // ======================================================
- // Global Tasks
- // ======================================================
- console.info(colors.white('└── ') + colors.green('Running global tasks...'))
- let preInitContent = ''
- let globalTasks = Promise.mapSeries([
- /**
- * ACE Modes
- */
- () => {
- return fs.accessAsync('./assets/js/ace').then(() => {
- console.info(colors.white(' └── ') + colors.magenta('ACE modes directory already exists. Task aborted.'))
- return true
- }).catch(err => {
- if (err.code === 'ENOENT') {
- console.info(colors.white(' └── ') + colors.green('Copy + Minify ACE modes to assets...'))
- return fs.ensureDirAsync('./assets/js/ace').then(() => {
- return fs.readdirAsync('./node_modules/brace/mode').then(modeList => {
- return Promise.map(modeList, mdFile => {
- console.info(colors.white(' mode-' + mdFile))
- let result = uglify.minify(path.join('./node_modules/brace/mode', mdFile), { output: { 'max_line_len': 1000000 } })
- return fs.writeFileAsync(path.join('./assets/js/ace', 'mode-' + mdFile), result.code)
- })
- })
- })
- } else {
- throw err
- }
- })
- },
- /**
- * MathJax
- */
- () => {
- return fs.accessAsync('./assets/js/mathjax').then(() => {
- console.info(colors.white(' └── ') + colors.magenta('MathJax directory already exists. Task aborted.'))
- return true
- }).catch(err => {
- if (err.code === 'ENOENT') {
- console.info(colors.white(' └── ') + colors.green('Copy MathJax dependencies to assets...'))
- return fs.ensureDirAsync('./assets/js/mathjax').then(() => {
- return fs.copyAsync('./node_modules/mathjax', './assets/js/mathjax', { filter: (src, dest) => {
- let srcNormalized = src.replace(/\\/g, '/')
- let shouldCopy = false
- console.log(srcNormalized)
- _.forEach([
- '/node_modules/mathjax',
- '/node_modules/mathjax/jax',
- '/node_modules/mathjax/jax/input',
- '/node_modules/mathjax/jax/output'
- ], chk => {
- if (srcNormalized.endsWith(chk)) {
- shouldCopy = true
- }
- })
- _.forEach([
- '/node_modules/mathjax/extensions',
- '/node_modules/mathjax/MathJax.js',
- '/node_modules/mathjax/jax/element',
- '/node_modules/mathjax/jax/input/MathML',
- '/node_modules/mathjax/jax/input/TeX',
- '/node_modules/mathjax/jax/output/SVG'
- ], chk => {
- if (srcNormalized.indexOf(chk) > 0) {
- shouldCopy = true
- }
- })
- if (shouldCopy && srcNormalized.indexOf('/fonts/') > 0 && srcNormalized.indexOf('/STIX-Web') <= 1) {
- shouldCopy = false
- }
- return shouldCopy
- }})
- })
- } else {
- throw err
- }
- })
- },
- /**
- * Bundle pre-init scripts
- */
- () => {
- console.info(colors.white(' └── ') + colors.green('Bundling pre-init scripts...'))
- return fs.readdirAsync('./client/js/pre-init').map(f => {
- let fPath = path.join('./client/js/pre-init/', f)
- return fs.readFileAsync(fPath, 'utf8').then(fContent => {
- preInitContent += fContent + ';\n'
- })
- }).then(() => {
- return fs.outputFileAsync('./.build/_preinit.js', preInitContent, 'utf8')
- })
- }
- ], f => { return f() })
- // ======================================================
- // Fuse Tasks
- // ======================================================
- let fuse
- globalTasks.then(() => {
- switch (mode) {
- // =============================================
- // DEVELOPER MODE
- // =============================================
- case 'dev':
- // Client
- fuse = fsbx.FuseBox.init({
- homeDir: './client',
- outFile: './assets/js/bundle.min.js',
- alias: ALIASES,
- shim: SHIMS,
- plugins: [
- [ fsbx.SassPlugin({ includePaths: ['../core'] }), fsbx.CSSPlugin() ],
- fsbx.BabelPlugin({ comments: false, presets: ['es2015'] }),
- fsbx.JSONPlugin()
- ],
- debug: false,
- log: true
- })
- fuse.devServer('>index.js', {
- port: 4444,
- httpServer: false,
- hmr: false
- })
- // Server
- _.delay(() => {
- nodemon({
- exec: (args.i) ? 'node --inspect server' : 'node server',
- ignore: ['assets/', 'client/', 'data/', 'repo/', 'tests/'],
- ext: 'js json',
- watch: [
- 'controllers',
- 'libs',
- 'locales',
- 'middlewares',
- 'models',
- 'agent.js',
- 'server.js'
- ],
- env: { 'NODE_ENV': 'development' }
- })
- }, 1000)
- break
- // =============================================
- // CONFIGURE - DEVELOPER MODE
- // =============================================
- case 'dev-configure':
- // Client
- fuse = fsbx.FuseBox.init({
- homeDir: './client',
- outFile: './assets/js/configure.min.js',
- alias: ALIASES,
- shim: SHIMS,
- plugins: [
- [ fsbx.SassPlugin({ includePaths: ['../core'] }), fsbx.CSSPlugin() ],
- fsbx.BabelPlugin({ comments: false, presets: ['es2015'] }),
- fsbx.JSONPlugin()
- ],
- debug: false,
- log: true
- })
- fuse.devServer('>configure.js', {
- port: 4444,
- httpServer: false
- })
- // Server
- _.delay(() => {
- nodemon({
- exec: 'node wiki configure',
- ignore: ['assets/', 'client/', 'data/', 'repo/', 'tests/'],
- ext: 'js json',
- watch: [
- 'configure.js'
- ],
- env: { 'NODE_ENV': 'development' }
- })
- }, 1000)
- break
- // =============================================
- // BUILD ONLY MODE
- // =============================================
- case 'build':
- fuse = fsbx.FuseBox.init({
- homeDir: './client',
- alias: ALIASES,
- shim: SHIMS,
- plugins: [
- fsbx.EnvPlugin({ NODE_ENV: 'production' }),
- fsbx.BannerPlugin(preInitContent),
- [ fsbx.SassPlugin({ outputStyle: 'compressed', includePaths: ['./node_modules/requarks-core'] }), fsbx.CSSPlugin() ],
- fsbx.BabelPlugin({
- config: {
- comments: false,
- presets: ['es2015']
- }
- }),
- fsbx.JSONPlugin(),
- fsbx.UglifyJSPlugin({
- compress: { unused: false },
- output: { 'max_line_len': 1000000 }
- })
- ],
- debug: false,
- log: true
- })
- fuse.bundle({
- './assets/js/bundle.min.js': '>index.js',
- './assets/js/configure.min.js': '>configure.js'
- }).then(() => {
- console.info(colors.green.bold('\nAssets compilation + bundling completed.'))
- }).catch(err => {
- console.error(colors.red(' X Bundle compilation failed! ' + err.message))
- process.exit(1)
- })
- break
- }
- })
|