Skip to content

Commit cee3689

Browse files
Disable Babel modules transform for dependencies (#455)
The Babel modules transform turns top level `this` into undefined. This causes issues with some libraries that expect `this` to refer to the global object (or at least expect it to exist). See #423 for an example. Because browserify looks for the package.json `main` key, which should not contain ES modules code, it should be safe to just disable the modules transform.
1 parent a4306c5 commit cee3689

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

lib/graph-script.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,6 @@ function node (state, createEdge) {
4646
var browsers = browserslist(null, { path: entry })
4747
if (!browsers.length) browsers = defaultBrowsers
4848

49-
var babelPresets = [
50-
[babelPresetEnv, {
51-
targets: { browsers: browsers }
52-
}]
53-
]
54-
5549
if (state.metadata.watch) {
5650
b = watchify(b)
5751
debug('watching ' + entry)
@@ -64,20 +58,34 @@ function node (state, createEdge) {
6458
b.ignore('sheetify/insert')
6559
b.transform(sheetify)
6660
b.transform(glslify)
61+
6762
if (state.metadata.babelifyDeps) {
6863
// Dependencies should be transformed, but their .babelrc should be ignored.
6964
b.transform(tfilter(babelify, { include: /node_modules/ }), {
7065
global: true,
7166
babelrc: false,
72-
presets: babelPresets
67+
presets: [
68+
[babelPresetEnv, {
69+
// browserify resolves the commonjs version of modules anyway,
70+
// and the modules transform in babel-preset-env rewrites top level `this`
71+
// to `undefined` which breaks some modules (underscore, cuid, ...)
72+
modules: false,
73+
targets: { browsers: browsers }
74+
}]
75+
]
7376
})
7477
}
7578
// In our own code, .babelrc files should be used.
7679
b.transform(tfilter(babelify, { exclude: /node_modules/ }), {
7780
global: true,
7881
babelrc: true,
79-
presets: babelPresets
82+
presets: [
83+
[babelPresetEnv, {
84+
targets: { browsers: browsers }
85+
}]
86+
]
8087
})
88+
8189
b.transform(brfs, { global: true })
8290
b.transform(yoyoify, { global: true })
8391

test/script.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var mkdirp = require('mkdirp')
44
var path = require('path')
55
var tape = require('tape')
66
var fs = require('fs')
7+
var vm = require('vm')
78
var os = require('os')
89
var tmp = require('tmp')
910

@@ -194,6 +195,33 @@ tape('use custom browserslist config', function (assert) {
194195
})
195196
})
196197

198+
tape('does not transform top level `this` in dependencies', function (assert) {
199+
assert.plan(4)
200+
var file = `
201+
T.equal(require('a')(), 10)
202+
`
203+
var dependency = `
204+
module.exports = this.number || (() => 10)
205+
`
206+
207+
var tmpDirname = path.join(__dirname, '../tmp', 'js-pipeline-' + (Math.random() * 1e4).toFixed())
208+
mkdirp.sync(path.join(tmpDirname, 'node_modules'))
209+
fs.writeFileSync(path.join(tmpDirname, 'app.js'), file)
210+
fs.writeFileSync(path.join(tmpDirname, 'node_modules', 'a.js'), dependency)
211+
212+
var compiler = bankai(path.join(tmpDirname, 'app.js'), { watch: false })
213+
compiler.on('error', assert.error)
214+
compiler.scripts('bundle.js', function (err, res) {
215+
assert.error(err, 'no error writing script')
216+
var content = res.buffer.toString('utf8')
217+
218+
assert.ok(/this\.number/.test(content), 'did not rewrite `this`')
219+
assert.ok(/return 10/.test(content), 'did rewrite arrow function')
220+
221+
vm.runInNewContext(content, { T: assert })
222+
})
223+
})
224+
197225
tape('envify in watch mode', function (assert) {
198226
assert.plan(5)
199227

0 commit comments

Comments
 (0)