├── .gitignore ├── .npmignore ├── package.json ├── src └── index.js └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | lib -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | src 3 | *.log -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "babel-plugin-inline-package-json", 3 | "version": "2.0.0", 4 | "description": "Inline references to fields in package.json", 5 | "repository": "andrewimm/babel-plugin-inline-package-json", 6 | "license": "BSD-3-Clause", 7 | "main": "lib/index.js", 8 | "keywords": [ 9 | "babel-plugin" 10 | ], 11 | "devDependencies": { 12 | "babel-core": "^6.14.0", 13 | "babel-preset-es2015": "^6.14.0" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import path from 'path'; 2 | 3 | export default function({types: t}) { 4 | return { 5 | visitor: { 6 | MemberExpression: function MemberExpression(treePath) { 7 | const node = treePath.node; 8 | if (t.isCallExpression(node.object) && 9 | t.isIdentifier(node.object.callee, { name: 'require' }) && 10 | t.isLiteral(node.object.arguments[0]) && 11 | node.object.arguments[0].value.match(/package\.json$/)) { 12 | let srcPath = path.resolve(this.file.opts.filename); 13 | let pkg = require(path.join(srcPath, '..', node.object.arguments[0].value)); 14 | let value = pkg[node.property.name]; 15 | treePath.replaceWith(t.expressionStatement(t.valueToNode(value))); 16 | } 17 | } 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # babel-plugin-inline-package-json 2 | 3 | Babel plugin for inlining values from `package.json` 4 | 5 | Input: 6 | ```js 7 | var version = require('./package.json').version; 8 | ``` 9 | Output: 10 | ```js 11 | var version = '1.0.0'; 12 | ``` 13 | 14 | The plugin can be used to import any value from a `package.json` file: primitives, objects, or arrays will all be injected into your compiled source as literal expressions. 15 | 16 | ## Installation 17 | 18 | ```sh 19 | $ npm install babel-plugin-inline-package-json 20 | ``` 21 | 22 | ## Usage 23 | 24 | ### Via `.babelrc` 25 | 26 | ```json 27 | { 28 | "plugins": ["inline-package-json"] 29 | } 30 | ``` 31 | 32 | ### Via CLI 33 | 34 | ```sh 35 | $ babel --plugins inline-package-json script.js 36 | ``` 37 | 38 | ### Via Node API 39 | 40 | ```js 41 | require('babel-core').transform('code', { 42 | plugins: ['inline-package-json'] 43 | }); 44 | ``` --------------------------------------------------------------------------------