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 8fae4f4
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 12 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
57 changes: 45 additions & 12 deletions src/build/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
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: {
Expand All @@ -28,7 +29,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,16 +56,38 @@ const transform = (src, dest) => {
})
}

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

return new Promise((resolve, reject) => {
// 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: ctx.watch,
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)
})
})

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))
}))
watcher
.on('ready', () => {
// Finish the task after the initial scan. If files are watched, the
// task will keep running though.
resolve()
})
.on('error', (err) => {
reject(err)
})
})
}

Expand Down

0 comments on commit 8fae4f4

Please sign in to comment.