Last active
August 29, 2015 14:16
-
-
Save alanrubin/a672f5bc0cc6c673a2b4 to your computer and use it in GitHub Desktop.
Fixes for proxyquireify
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var detective = require('detective'); | |
function simpleRequire(n) { | |
// var proxy = require('proxyquireify') | |
return n.parent | |
&& n.parent.id | |
&& n.parent.id.name; | |
} | |
function requireWithImmediateCall(n) { | |
// var proxy = require('proxyquireify')(require) | |
var p = n.parent; | |
return p.parent | |
&& p.parent.id | |
&& p.parent.id.name; | |
} | |
function requireWithImmediateCallWithoutVar(n) { | |
// proxy = require('proxyquireify')(require) | |
var p = n.parent; | |
return p.parent | |
&& p.parent.left | |
&& p.parent.left.name; | |
} | |
// TODO We probably need to detect proxy = require('proxyquireify') syntax as well ? | |
function findProxyquireVars(src) { | |
return detective | |
.find(src, { nodes: true }).nodes | |
.map(function (n) { | |
var arg = n.arguments[0]; | |
return arg | |
&& arg.value === 'proxyquireify' | |
&& arg.type === 'Literal' | |
&& ( simpleRequire(n) || requireWithImmediateCall(n) || requireWithImmediateCallWithoutVar(n)); | |
}) | |
.filter(function (n) { return n; }) | |
; | |
} | |
module.exports = function(src) { | |
if (!/require\(.+proxyquireify.+\)/.test(src)) return []; | |
var hash = findProxyquireVars(src) | |
.map(function (name) { | |
return detective(src, { word: name }); | |
}) | |
.reduce(function (acc, arr) { | |
arr.forEach(function (x) { acc[x] = true;}); | |
return acc; | |
}, {}); | |
return Object.keys(hash); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var through = require('through') | |
, findDependencies = require('./find-dependencies') | |
; | |
function requireDependencies(src) { | |
var deps = findDependencies(src); | |
if (!deps.length) return ''; | |
return '/* proxyquireify injected requires to make browserify include dependencies in the bundle */;' + | |
deps.map(function (x) { return 'require(\'' + x + '\')'; }).join(';') + ';'; | |
} | |
module.exports = function (file) { | |
if (file === require.resolve('../index')) return through(); | |
// TODO Here .coffee test is hard coded but I guess the best way | |
// to deal with that is to use browserify "extensions" property to test that | |
// because a file extension could be .jsx, .coffee, .cjsx and that should work if that | |
if (!/\.js$/.test(file) && !/\.coffee$/.test(file)) return through(); | |
var data = ''; | |
return through(write, end); | |
function write (buf) { data += buf; } | |
function end() { | |
var deps = requireDependencies(data); | |
this.queue(deps); | |
this.queue(data); | |
this.queue(null); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment