Skip to content

Commit

Permalink
fix(prepublish): Use babel directly
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorgerhardt committed Sep 8, 2016
1 parent 5ecbd27 commit e6a7cd4
Showing 1 changed file with 41 additions and 5 deletions.
46 changes: 41 additions & 5 deletions bin/mastarm
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,51 @@ commander
}))
})

const BABEL = path.join(__dirname, '../node_modules/.bin/babel')
commander
.command('prepublish [entries...]')
.description('Transpile JavaScript down to ES5 with Babel')
.action(function (entries, options) {
.action(function (entries) {
checkDependencies()
const exec = require('child_process').execSync
entries.forEach((entry) =>
exec(`${BABEL} ${entry} --out-dir build --source-maps`))
const babel = require('babel-core')
const fs = require('fs')
const glob = require('glob')
const mkdirp = require('mkdirp')
const outdir = path.join(process.cwd(), 'build')
const options = {
presets: [
require('babel-preset-es2015'),
require('babel-preset-react'),
require('babel-preset-stage-0')
],
plugins: [
require('babel-plugin-add-module-exports'),
require('babel-plugin-transform-runtime')
],
sourceMaps: true
}
entries.forEach((entry) => {
if (fs.statSync(entry).isDirectory()) {
transformDir(entry)
} else {
transformFile(entry)
}
})
function transformFile (filename) {
const filepath = path.join(outdir, filename)
const results = babel.transform(
fs.readFileSync(filename, 'utf8'),
Object.assign({}, options, {filename})
)
mkdirp.sync(path.dirname(filepath))
fs.writeFileSync(filepath, results.code + '\n//# sourceMappingURL=' + path.basename(filepath))
fs.writeFileSync(`${filepath}.map`, JSON.stringify(results.map))
}
function transformDir (dir) {
glob(`${dir}/**/*.js`, (err, files) => {
if (err) console.error(err)
files.forEach(transformFile)
})
}
})

commander.parse(process.argv)
Expand Down

0 comments on commit e6a7cd4

Please sign in to comment.