├── package.json ├── index.js ├── .gitignore └── README.md /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "packageify", 3 | "version": "0.2.1", 4 | "description": "inline things like \"require('package.version')\". Do not include all the package.json in your bundles.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "homepage": "https://github.com/jfromaniello/packageify", 10 | "keywords": [ 11 | "browserify", 12 | "browserify-transform", 13 | "package.json" 14 | ], 15 | "author": "Jose F. Romaniello (http://joseoncode.com)", 16 | "license": "MIT", 17 | "dependencies": { 18 | "through": "^2.3.4" 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var through = require('through'); 2 | var path = require('path'); 3 | var fs = require('fs'); 4 | 5 | function getPackageJson (inDir) { 6 | var dirname = path.dirname(inDir); 7 | var pkg_route = path.join(dirname, 'package.json'); 8 | if (fs.existsSync(pkg_route)) return pkg_route; 9 | return getPackageJson(dirname); 10 | } 11 | 12 | module.exports = function (file, opts) { 13 | return through(function write(data) { 14 | data = data.toString().replace(/require\((\'|\")package\.(.*)(\'|\")\)/ig, function (str, p1, p2) { 15 | var r = JSON.stringify(require(getPackageJson(file))[p2]); 16 | return r; 17 | }); 18 | 19 | this.emit('data', data); 20 | }, function end () { //optional 21 | this.emit('end'); 22 | }); 23 | }; -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by http://www.gitignore.io 2 | 3 | ### Node ### 4 | # Logs 5 | logs 6 | *.log 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | 13 | # Directory for instrumented libs generated by jscoverage/JSCover 14 | lib-cov 15 | 16 | # Coverage directory used by tools like istanbul 17 | coverage 18 | 19 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 20 | .grunt 21 | 22 | # Compiled binary addons (http://nodejs.org/api/addons.html) 23 | build/Release 24 | 25 | # Dependency directory 26 | # Commenting this out is preferred by some people, see 27 | # https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git 28 | node_modules 29 | 30 | # Users Environment Variables 31 | .lock-wscript 32 | 33 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Inline things from your package.json without including all the package.json. 2 | 3 | You can require `package.json` with browserify since it is a JSON as follows: 4 | 5 | ``` 6 | require('./package.json'); 7 | ``` 8 | 9 | this is often an overkill since it will include all the package.json file and most of the time you only need things like the `version` property: 10 | 11 | ``` 12 | exports.version = require('./package.json').version; 13 | ``` 14 | 15 | With this transforms you can use: 16 | 17 | ``` 18 | exports.version = require('package.version'); 19 | ``` 20 | 21 | and it will transform to: 22 | 23 | ``` 24 | exports.version = '1.0.20'; 25 | ``` 26 | 27 | Any field works for instance 28 | 29 | 30 | ``` 31 | exports.author = require('package.author'); 32 | exports.license = require('package.license'); 33 | ``` 34 | 35 | ## Usage 36 | 37 | ``` 38 | npm i packageify --save-dev 39 | ``` 40 | 41 | Then add the transform as any other transform. 42 | 43 | ## License 44 | 45 | MIT 2014 - Jose F. Romaniello 46 | --------------------------------------------------------------------------------