Skip to content

Commit

Permalink
feat: add watcher to Babel transpiling
Browse files Browse the repository at this point in the history
It is no possible to watch for changes in the source directory and
get the files automatically transpiled. Run AEgir as

    aegir build --no-dist --watch
  • Loading branch information
vmx committed Apr 11, 2018
1 parent 85960f3 commit c1db1e9
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 11 deletions.
10 changes: 10 additions & 0 deletions cmds/build.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
'use strict'

const path = require('path')

const chokidar = require('chokidar')

const build = require('../src/build')
const onError = require('../src/error-handler')
const utils = require('../src/utils')

module.exports = {
command: 'build',
Expand All @@ -16,6 +21,11 @@ module.exports = {
alias: 'l',
describe: 'Transpile src to lib',
default: true
},
watch: {
alias: 'w',
describe: 'Keep watching source files for changes',
default: false
}
},
handler (argv) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"babel-preset-flow-node": "^2.0.1",
"browserify-zlib": "^0.2.0",
"chalk": "^2.3.0",
"chokidar": "^2.0.3",
"clean-documentation-theme": "^0.5.2",
"codecov": "^3.0.0",
"conventional-changelog": "^1.1.7",
Expand Down
55 changes: 44 additions & 11 deletions src/build/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
const path = require('path')

const babelCore = require('babel-core')
const chokidar = require('chokidar')
const fs = require('fs-extra')
const glob = require('glob')
const pify = require('pify')

const utils = require('../utils')

const babelConfig = {
env: {
development: {
Expand All @@ -28,7 +31,17 @@ const babelConfig = {
}
}

const transform = (src, dest) => {
/**
* Babel transpiles a file from `src` to `lib`
*
* @param {string} filename The filename relative to the `src` directory
*
* @returns {Promise}
*/
const transform = (filename) => {
const src = path.join('src', filename)
const dest = path.join('lib', filename)

// To have the right filename in the source map
babelConfig.sourceFileName = path.join('..', src)

Expand All @@ -45,17 +58,37 @@ const transform = (src, dest) => {
})
}

const babel = () => {
const src = 'src'
const dest = 'lib'
const babel = (ctx) => {
const srcDir = path.join(utils.getBasePath(), 'src')

return pify(glob)('./**/*.js', {
cwd: path.join(process.cwd(), src)
}).then((filenames) => {
return Promise.all(filenames.map((filename) => {
return transform(path.join(src, filename), path.join(dest, filename))
}))
})
if (ctx.watch) {
// The watcher code is based on the babel-cli code (MIT licensed):
// https://github.com/babel/babel/blob/6597a472b30419493f123bff1e9194e4c09e488e/packages/babel-cli/src/babel/dir.js#L164-L188`
const watcher = chokidar.watch(srcDir, {
persistent: true,
ignoreInitial: false,
awaitWriteFinish: {
stabilityThreshold: 50,
pollInterval: 10
}
})

;['add', 'change'].forEach((type) => {
watcher.on(type, (filename) => {
const relative = path.relative(srcDir, filename)
console.log('Transpile file: ' + relative)
transform(relative)
})
})
} else {
return pify(glob)('./**/*.js', {
cwd: srcDir
}).then((filenames) => {
return Promise.all(filenames.map((filename) => {
return transform(filename)
}))
})
}
}

module.exports = babel

0 comments on commit c1db1e9

Please sign in to comment.