├── .babelrc ├── .gitignore ├── .npmignore ├── LICENSE ├── README.md ├── package.json └── src └── index.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["es2015"] 3 | } 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | lib/ 3 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | src/ 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Gabriel Andretta 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 | # babel-plugin-version-inline 2 | 3 | Turn `__VERSION__` into the package's version string. 4 | 5 | ## Example 6 | 7 | Given the following _package.json_. 8 | 9 | ```json 10 | { 11 | "name": "package-using-babel-plugin-version-inline", 12 | "version": "1.0.0" 13 | } 14 | ``` 15 | 16 | #### in 17 | 18 | ```js 19 | __VERSION__ 20 | ``` 21 | 22 | #### out 23 | 24 | ```js 25 | "1.0.0" 26 | ``` 27 | 28 | 29 | ## Installation 30 | 31 | ```sh 32 | $ npm install babel-plugin-version-inline 33 | ``` 34 | 35 | ## Usage 36 | 37 | ### Via `.babelrc` (Recommended) 38 | 39 | **.babelrc** 40 | 41 | ```json 42 | { 43 | "plugins": ["version-inline"] 44 | } 45 | ``` 46 | 47 | ### Via CLI 48 | 49 | ```sh 50 | $ babel --plugins version-inline script.js 51 | ``` 52 | 53 | ### Via Node API 54 | 55 | ```javascript 56 | require("babel-core").transform("code", { 57 | plugins: ["version-inline"] 58 | }); 59 | ``` 60 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "babel-plugin-version-inline", 3 | "version": "1.0.0", 4 | "description": "Babel plugin for turning __VERSION__ into a package version string", 5 | "main": "lib/index.js", 6 | "scripts": { 7 | "build": "babel src --out-dir lib --copy-files", 8 | "test": "echo \"Error: no test specified\" && exit 1" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git+https://github.com/gnandretta/babel-plugin-version-inline.git" 13 | }, 14 | "keywords": [ 15 | "babel-plugin" 16 | ], 17 | "author": "Gabriel Andretta", 18 | "license": "MIT", 19 | "bugs": { 20 | "url": "https://github.com/gnandretta/babel-plugin-version-inline/issues" 21 | }, 22 | "homepage": "https://github.com/gnandretta/babel-plugin-version-inline#readme", 23 | "devDependencies": { 24 | "babel-cli": "^6.1.18", 25 | "babel-preset-es2015": "^6.1.18" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import fs from 'fs'; 2 | 3 | const version = JSON.parse(fs.readFileSync('package.json', 'utf8')).version; 4 | 5 | export default function ({ types: t }) { 6 | return { 7 | visitor: { 8 | ReferencedIdentifier(path) { 9 | if (path.node.name === "__VERSION__") { 10 | path.replaceWith(t.valueToNode(version)); 11 | } 12 | } 13 | } 14 | }; 15 | } 16 | --------------------------------------------------------------------------------