Skip to content

Commit

Permalink
Add option babelifyDeps (#422)
Browse files Browse the repository at this point in the history
  • Loading branch information
aknuds1 authored and goto-bus-stop committed Apr 13, 2018
1 parent 593eb8f commit a4306c5
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 @@ -306,7 +306,7 @@ includes IE11. And if you have different opinions on which browsers to use,
Bankai respects `.babelrc` and [`.browserslistrc`](https://github.com/ai/browserslist) 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 @@ -333,6 +333,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 @@ -106,6 +106,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 @@ -64,12 +64,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 @@ -90,6 +90,7 @@
"rimraf": "^2.6.2",
"standard": "^11.0",
"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 @@ -128,6 +129,41 @@ tape('use custom babel config for local files, but not for dependencies', functi
})
})

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()
})
})

tape('use custom browserslist config', function (assert) {
assert.plan(5)

Expand Down

0 comments on commit a4306c5

Please sign in to comment.