Skip to content

Commit a4306c5

Browse files
aknuds1goto-bus-stop
authored andcommitted
Add option babelifyDeps (#422)
1 parent 593eb8f commit a4306c5

File tree

5 files changed

+49
-8
lines changed

5 files changed

+49
-8
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ includes IE11. And if you have different opinions on which browsers to use,
306306
Bankai respects `.babelrc` and [`.browserslistrc`](https://github.com/ai/browserslist) files.
307307

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

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

337338
### `compiler.documents(routename, [opts], done(err, { buffer, hash }))`
338339
Output an HTML bundle for a route. Routes are determined based on the project's

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ function Bankai (entry, opts) {
106106
this.graph.start({
107107
dirname: this.dirname,
108108
watch: opts.watch !== false,
109+
babelifyDeps: opts.babelifyDeps !== false,
109110
fullPaths: opts.fullPaths,
110111
reload: Boolean(opts.reload),
111112
log: this.log,

lib/graph-script.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,14 @@ function node (state, createEdge) {
6464
b.ignore('sheetify/insert')
6565
b.transform(sheetify)
6666
b.transform(glslify)
67-
// Dependencies should be transformed, but their .babelrc should be ignored.
68-
b.transform(tfilter(babelify, { include: /node_modules/ }), {
69-
global: true,
70-
babelrc: false,
71-
presets: babelPresets
72-
})
67+
if (state.metadata.babelifyDeps) {
68+
// Dependencies should be transformed, but their .babelrc should be ignored.
69+
b.transform(tfilter(babelify, { include: /node_modules/ }), {
70+
global: true,
71+
babelrc: false,
72+
presets: babelPresets
73+
})
74+
}
7375
// In our own code, .babelrc files should be used.
7476
b.transform(tfilter(babelify, { exclude: /node_modules/ }), {
7577
global: true,

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
"rimraf": "^2.6.2",
9191
"standard": "^11.0",
9292
"tachyons": "^4.9.1",
93-
"tape": "^4.8.0"
93+
"tape": "^4.8.0",
94+
"tmp": "0.0.33"
9495
}
9596
}

test/script.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var path = require('path')
55
var tape = require('tape')
66
var fs = require('fs')
77
var os = require('os')
8+
var tmp = require('tmp')
89

910
var bankai = require('../')
1011

@@ -128,6 +129,41 @@ tape('use custom babel config for local files, but not for dependencies', functi
128129
})
129130
})
130131

132+
tape('skip babel for dependencies if babelifyDeps is false', function (assert) {
133+
assert.plan(4)
134+
var file = dedent`
135+
const depFunc = require('mydep').depFunc
136+
depFunc(1)
137+
`
138+
var depFile = dedent`
139+
const depFunc = (arg) => {
140+
console.log(arg)
141+
}
142+
module.exports = {
143+
depFunc
144+
}
145+
`
146+
147+
var filename = 'js-pipeline-' + (Math.random() * 1e4).toFixed() + '.js'
148+
const outputDir = tmp.dirSync({unsafeCleanup: true})
149+
var tmpFilename = path.join(outputDir.name, filename)
150+
fs.writeFileSync(tmpFilename, file)
151+
const nodeModulesDir = path.join(outputDir.name, 'node_modules')
152+
mkdirp.sync(nodeModulesDir)
153+
fs.writeFileSync(path.join(nodeModulesDir, 'mydep.js'), depFile)
154+
155+
var compiler = bankai(tmpFilename, { watch: false, babelifyDeps: false })
156+
compiler.scripts('bundle.js', function (err, node) {
157+
assert.error(err, 'no error writing script')
158+
assert.ok(node, 'output exists')
159+
assert.ok(node.buffer, 'output buffer exists')
160+
161+
const compiledJs = node.buffer.toString('utf8')
162+
assert.notOk(/['"]use strict['"]/.test(compiledJs))
163+
outputDir.removeCallback()
164+
})
165+
})
166+
131167
tape('use custom browserslist config', function (assert) {
132168
assert.plan(5)
133169

0 commit comments

Comments
 (0)