UNPKG

1.36 kBJavaScriptView Raw
1const emoji = require('node-emoji')
2
3const chalk = require('chalk')
4const rimrafSync = require('rimraf').sync
5
6const babelConfig = require('../config/babel')
7const paths = require('../utils/paths')
8const BabelCompiler = require('../utils/babelCompiler')
9
10module.exports = cmd => {
11 console.log(`Cleaning directory ${paths.lib}`)
12 rimrafSync(`${paths.lib}/*`)
13
14 const compiler = new BabelCompiler(paths.src, paths.lib, {
15 babel: babelConfig,
16 watch: cmd.watch,
17 sourceMaps: cmd.sourceMaps
18 })
19
20 let ready = false
21 let errors = false
22
23 if (cmd.watch) {
24 console.log(emoji.get('eyes'), ' Starting watch...\n')
25 } else {
26 console.log(emoji.get('package'), ' Compiling files...\n')
27 }
28
29 compiler.on('copy', filepath => {
30 if (ready) console.log(`File '${filepath}' copied to build dir`)
31 })
32 compiler.on('success', filepath => {
33 if (ready) console.log(`File '${filepath}' compiled successfully`)
34 })
35 compiler.on('error', (filepath, error) => {
36 if (!ready) errors = true
37
38 console.log()
39 console.log(error.toString())
40 if (error.codeFrame) console.log(`\n${error.codeFrame}\n`)
41 })
42 compiler.on('ready', () => {
43 ready = true
44 if (errors) {
45 console.log(chalk.red('Compiled with errors.\n'))
46 } else {
47 console.log(chalk.green('Compiled successfully.\n'))
48 }
49 if (!cmd.watch) {
50 compiler.close()
51 process.exit(errors ? 1 : 0)
52 }
53 })
54}