Skip to content

Commit

Permalink
Add option babelifyDeps
Browse files Browse the repository at this point in the history
  • Loading branch information
aknuds1 committed Feb 14, 2018
1 parent bdebb61 commit f7e2e7c
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 8 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ includes IE11. And if you have different opinions on which browsers to use,
Bankai respects `.babelrc` files.

Some newer JavaScript features require loading an extra library; `async/await`
being the clearest example. To enable this features, the `babel-polyfill`
being the clearest example. To enable such features, the `babel-polyfill`
library needs to be included in your application's root (e.g. `index.js`).

```js
Expand All @@ -277,6 +277,7 @@ argument. The following options are available:
if you have your own logging system.
- __opts.watch:__ Defaults to `true`. Watch for changes in the source files and
rebuild. Set to `false` to get optimized bundles.
- __babelifyDeps:__ Defaults to true. Transform dependencies with babelify.

### `compiler.documents(routename, [opts], done(err, { buffer, hash }))`
Output an HTML bundle for a route. Routes are determined based on the project's
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ function Bankai (entry, opts) {
this.graph.start({
dirname: this.dirname,
watch: opts.watch !== false,
babelifyDeps: opts.babelifyDeps !== false,
fullPaths: opts.fullPaths,
reload: Boolean(opts.reload),
log: this.log,
Expand Down
14 changes: 8 additions & 6 deletions lib/graph-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ function node (state, createEdge) {
b.ignore('sheetify/insert')
b.transform(sheetify)
b.transform(glslify)
// Dependencies should be transformed, but their .babelrc should be ignored.
b.transform(tfilter(babelify, { include: /node_modules/ }), {
global: true,
babelrc: false,
presets: babelPresets
})
if (state.metadata.babelifyDeps) {
// Dependencies should be transformed, but their .babelrc should be ignored.
b.transform(tfilter(babelify, { include: /node_modules/ }), {
global: true,
babelrc: false,
presets: babelPresets
})
}
// In our own code, .babelrc files should be used.
b.transform(tfilter(babelify, { exclude: /node_modules/ }), {
global: true,
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"rimraf": "^2.6.2",
"standard": "^10.0.3",
"tachyons": "^4.9.1",
"tape": "^4.8.0"
"tape": "^4.8.0",
"tmp": "0.0.33"
}
}
36 changes: 36 additions & 0 deletions test/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var path = require('path')
var tape = require('tape')
var fs = require('fs')
var os = require('os')
var tmp = require('tmp')

var bankai = require('../')

Expand Down Expand Up @@ -107,3 +108,38 @@ tape('use custom babel config for local files, but not for dependencies', functi
assert.pass('should build')
})
})

tape('skip babel for dependencies if babelifyDeps is false', function (assert) {
assert.plan(4)
var file = dedent`
const depFunc = require('mydep').depFunc
depFunc(1)
`
var depFile = dedent`
const depFunc = (arg) => {
console.log(arg)
}
module.exports = {
depFunc
}
`

var filename = 'js-pipeline-' + (Math.random() * 1e4).toFixed() + '.js'
const outputDir = tmp.dirSync({unsafeCleanup: true})
var tmpFilename = path.join(outputDir.name, filename)
fs.writeFileSync(tmpFilename, file)
const nodeModulesDir = path.join(outputDir.name, 'node_modules')
mkdirp.sync(nodeModulesDir)
fs.writeFileSync(path.join(nodeModulesDir, 'mydep.js'), depFile)

var compiler = bankai(tmpFilename, { watch: false, babelifyDeps: false })
compiler.scripts('bundle.js', function (err, node) {
assert.error(err, 'no error writing script')
assert.ok(node, 'output exists')
assert.ok(node.buffer, 'output buffer exists')

const compiledJs = node.buffer.toString('utf8')
assert.notOk(/['"]use strict['"]/.test(compiledJs))
outputDir.removeCallback()
})
})

0 comments on commit f7e2e7c

Please sign in to comment.