├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── index.js ├── package.json └── test ├── in.js ├── index.js └── out.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | coverage/ 3 | tmp/ 4 | npm-debug.log* 5 | .DS_Store 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | node_js: 2 | - "0.12" 3 | - "4" 4 | sudo: false 5 | language: node_js 6 | script: "npm run test:cov" 7 | after_script: "npm i -g codecov.io && cat ./coverage/lcov.info | codecov" 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Yoshua Wuyts 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # bindingify [![stability][0]][1] 2 | [![npm version][2]][3] [![build status][4]][5] [![test coverage][6]][7] 3 | [![downloads][8]][9] [![js-standard-style][10]][11] 4 | 5 | Transform native bindings to be relative to their file 6 | 7 | ## Usage 8 | ```sh 9 | $ browserify -t bindingify index.js > bundle.js 10 | ``` 11 | 12 | Transforms: 13 | ```js 14 | require('bindings')('binding.node') 15 | ``` 16 | Into: 17 | ```js 18 | require('bindings')({bindings: 'binding.node', module_root: __dirname}) 19 | ``` 20 | 21 | ## Installation 22 | ```sh 23 | $ npm install bindingify 24 | ``` 25 | 26 | ## License 27 | [MIT](https://tldrlegal.com/license/mit-license) 28 | 29 | [0]: https://img.shields.io/badge/stability-experimental-orange.svg?style=flat-square 30 | [1]: https://nodejs.org/api/documentation.html#documentation_stability_index 31 | [2]: https://img.shields.io/npm/v/bindingify.svg?style=flat-square 32 | [3]: https://npmjs.org/package/bindingify 33 | [4]: https://img.shields.io/travis/yoshuawuyts/bindingify/master.svg?style=flat-square 34 | [5]: https://travis-ci.org/yoshuawuyts/bindingify 35 | [6]: https://img.shields.io/codecov/c/github/yoshuawuyts/bindingify/master.svg?style=flat-square 36 | [7]: https://codecov.io/github/yoshuawuyts/bindingify 37 | [8]: http://img.shields.io/npm/dm/bindingify.svg?style=flat-square 38 | [9]: https://npmjs.org/package/bindingify 39 | [10]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square 40 | [11]: https://github.com/feross/standard 41 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var StringDecoder = require('string_decoder').StringDecoder 2 | var through = require('through2') 3 | var falafel = require('falafel') 4 | 5 | module.exports = bindingify 6 | 7 | function bindingify (file) { 8 | if (!/\.js$/.test(file)) return through() 9 | 10 | var decoder = new StringDecoder('utf8') 11 | var src = '' 12 | 13 | return through(write, flush) 14 | 15 | function write (chunk, _, cb) { 16 | src += decoder.write(chunk) 17 | cb() 18 | } 19 | 20 | function flush (cb) { 21 | src += decoder.end() 22 | 23 | var output = falafel(src, function (node) { 24 | if (node.type !== 'CallExpression') return 25 | if (!node.arguments.length) return 26 | if (!node.callee || !node.callee.arguments) return 27 | if (!node.callee.arguments.length) return 28 | 29 | var requireLiteral = node.callee.arguments[0] 30 | if (requireLiteral.value !== 'bindings') return 31 | 32 | var val = node.arguments[0].value 33 | var str = "{bindings: '" + val + "', module_root: __dirname}" 34 | node.arguments[0].update(str) 35 | }) 36 | 37 | this.push(output.toString()) 38 | cb() 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bindingify", 3 | "version": "1.0.1", 4 | "description": "Transform native bindings to be relative to their file", 5 | "main": "index.js", 6 | "scripts": { 7 | "deps": "dependency-check . && dependency-check . --extra --no-dev", 8 | "test": "standard && npm run deps && NODE_ENV=test node test", 9 | "test:cov": "standard && npm run deps && NODE_ENV=test istanbul cover test" 10 | }, 11 | "repository": "yoshuawuyts/bindingify", 12 | "keywords": [ 13 | "browserify", 14 | "transform", 15 | "electron" 16 | ], 17 | "license": "MIT", 18 | "dependencies": { 19 | "falafel": "^2.0.0", 20 | "through2": "^2.0.3" 21 | }, 22 | "devDependencies": { 23 | "browserify": "^13.1.1", 24 | "dependency-check": "^2.6.0", 25 | "istanbul": "^0.4.5", 26 | "standard": "^8.6.0", 27 | "tape": "^4.6.2" 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /test/in.js: -------------------------------------------------------------------------------- 1 | require('bindings')('binding.node') 2 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | var StringDecoder = require('string_decoder').StringDecoder 2 | var browserify = require('browserify') 3 | var through = require('through2') 4 | var test = require('tape') 5 | var path = require('path') 6 | var fs = require('fs') 7 | 8 | var outfile = fs.readFileSync(path.join(__dirname, 'out.js'), 'utf8') 9 | 10 | test('should compile bindings', function (t) { 11 | t.plan(2) 12 | 13 | var b = browserify(path.join(__dirname, 'in.js')) 14 | var res = '' 15 | 16 | b.ignore('bindings') 17 | b.transform(path.join(__dirname, '../index')) 18 | b.transform(function (file) { 19 | var decoder = new StringDecoder('utf8') 20 | return through(write, function (cb) { 21 | decoder.end() 22 | cb(null, res) 23 | }) 24 | 25 | function write (chunk, _, cb) { 26 | res += decoder.write(chunk) 27 | cb() 28 | } 29 | }) 30 | 31 | b.bundle(function (err) { 32 | t.ifError(err, 'no compile error') 33 | t.equal(res, outfile) 34 | }) 35 | }) 36 | -------------------------------------------------------------------------------- /test/out.js: -------------------------------------------------------------------------------- 1 | require('bindings')({bindings: 'binding.node', module_root: __dirname}) 2 | --------------------------------------------------------------------------------