You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
typesetting/pitfall/pdfkit/node_modules/browserify-optional/index.js

66 lines
1.8 KiB
JavaScript

var astTransform = require('ast-transform');
var astTypes = require('ast-types');
var resolve = require('browser-resolve');
module.exports = astTransform(function (file) {
return function(ast, next) {
astTypes.visit(ast, {
visitCallExpression: function(path) {
var node = path.value;
if (node.callee.type !== 'Identifier'
|| node.callee.name !== 'require'
|| node.arguments.length < 1
|| node.arguments[0].type !== 'Literal')
return this.traverse(path);
var module = node.arguments[0].value;
var p = path;
while (p = p.parent) {
var parent = p.value;
if (parent.type === 'CatchClause')
break;
if (parent.type === 'TryStatement') {
try {
resolve.sync(module, { filename: file });
} catch (e) {
path.replace({
type: 'CallExpression',
arguments: [],
callee: {
type: 'FunctionExpression',
params: [],
body: {
type: 'BlockStatement',
body: [{
type: 'ThrowStatement',
argument: {
type: 'NewExpression',
callee: {
type: 'Identifier',
name: 'Error'
},
arguments: [{
type: 'Literal',
value: e.message
}]
}
}]
}
}
});
}
break;
}
}
return false;
}
});
next(null, ast);
}
});