babel-plugin-pkg
Advanced tools
Comparing version 2.0.0 to 2.1.0
/*! | ||
*** babel-plugin-pkg 2.0.0 | ||
*** babel-plugin-pkg 2.1.0 | ||
*** Copyright © 2020, Jaid <jaid.jsx@gmail.com> (https://github.com/Jaid) | ||
@@ -7,2 +7,2 @@ *** @license MIT | ||
!*/ | ||
var e,n;e=global,n=function(){return function(e){var n={};function t(o){if(n[o])return n[o].exports;var r=n[o]={i:o,l:!1,exports:{}};return e[o].call(r.exports,r,r.exports,t),r.l=!0,r.exports}return t.m=e,t.c=n,t.d=function(e,n,o){t.o(e,n)||Object.defineProperty(e,n,{enumerable:!0,get:o})},t.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},t.t=function(e,n){if(1&n&&(e=t(e)),8&n)return e;if(4&n&&"object"==typeof e&&e&&e.__esModule)return e;var o=Object.create(null);if(t.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:e}),2&n&&"string"!=typeof e)for(var r in e)t.d(o,r,function(n){return e[n]}.bind(null,r));return o},t.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(n,"a",n),n},t.o=function(e,n){return Object.prototype.hasOwnProperty.call(e,n)},t.p="",t(t.s=3)}([function(e,n){e.exports=require("camelcase")},function(e,n){e.exports=require("path")},function(e,n){e.exports=require("read-pkg-up")},function(e,n,t){t.r(n);var o=t(0),r=t.n(o),a=t(1),i=t.n(a),u=t(2);const p=t(4)("babel-plugin-pkg");n.default=({types:e})=>{let n;return{visitor:{MemberExpression(t,o){var a;(e=>{n||(n={cwd:e.cwd,prefix:"REPLACE_PKG_",nameFallback:!0,...e.opts},n.packageJson=Object(u.sync)(n.cwd),n.packageJson.path&&p("Got package data from %s",n.packageJson.path),n.packageJson.package&&p("Data: %o",n.packageJson.package))})(o);const c=null===(a=t.node.property)||void 0===a?void 0:a.name;if(!c)return;if(!c.startsWith(n.prefix))return;const l=t.node.object;if(null==l||!l.property)return;if("env"!==l.property.name)return;const s=l.object;if(!s)return;if("process"!==s.name)return;const f=r()(c.slice(n.prefix.length)),d=(e=>{var t,o,r,a,u,p,c;return"path"===e&&(null===(t=n.packageJson)||void 0===t?void 0:t.path)?n.packageJson.path:("title"!==e||(null===(o=n.packageJson)||void 0===o||null===(r=o.packageJson)||void 0===r?void 0:r.title)||(e="name"),!n.nameFallback||"name"!==e||null!==(a=n.packageJson)&&void 0!==a&&null!==(u=a.packageJson)&&void 0!==u&&u.name?(null===(p=n.packageJson)||void 0===p||null===(c=p.packageJson)||void 0===c?void 0:c[e])?n.packageJson.packageJson[e]:void 0:i.a.basename(n.cwd))})(f);void 0===d?p(`Requested package field ${f} in process.env.${c}, but it was not found`):(p.enabled&&p(`process.env.${c} → ${JSON.stringify(d)}`),t.replaceWith(e.valueToNode(d)))}}}}},function(e,n){e.exports=require("debug")}])},"object"==typeof exports&&"object"==typeof module?module.exports=n():"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?exports["babel-plugin-pkg"]=n():e.babelPluginPkg=n(); | ||
var e,n;e=global,n=function(){return function(e){var n={};function t(o){if(n[o])return n[o].exports;var r=n[o]={i:o,l:!1,exports:{}};return e[o].call(r.exports,r,r.exports,t),r.l=!0,r.exports}return t.m=e,t.c=n,t.d=function(e,n,o){t.o(e,n)||Object.defineProperty(e,n,{enumerable:!0,get:o})},t.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},t.t=function(e,n){if(1&n&&(e=t(e)),8&n)return e;if(4&n&&"object"==typeof e&&e&&e.__esModule)return e;var o=Object.create(null);if(t.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:e}),2&n&&"string"!=typeof e)for(var r in e)t.d(o,r,function(n){return e[n]}.bind(null,r));return o},t.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(n,"a",n),n},t.o=function(e,n){return Object.prototype.hasOwnProperty.call(e,n)},t.p="",t(t.s=3)}([function(e,n){e.exports=require("path")},function(e,n){e.exports=require("camelcase")},function(e,n){e.exports=require("read-pkg-up")},function(e,n,t){t.r(n);var o=t(1),r=t.n(o),a=t(0),i=t.n(a),p=t(2);const u=t(4)("babel-plugin-pkg");n.default=({types:e})=>{let n;return{visitor:{MemberExpression(t,o){var a;(e=>{n||(n={cwd:e.cwd,prefix:"REPLACE_PKG_",nameFallback:!0,...e.opts},n.packageJson=Object(p.sync)(n.cwd),n.packageJson.path&&u("Got package data from %s",n.packageJson.path),n.packageJson.package&&u("Data: %o",n.packageJson.package))})(o);const c=null===(a=t.node.property)||void 0===a?void 0:a.name;if(!c)return;if(!c.startsWith(n.prefix))return;const l=t.node.object;if(null==l||!l.property)return;if("env"!==l.property.name)return;const s=l.object;if(!s)return;if("process"!==s.name)return;const f=r()(c.slice(n.prefix.length)),d=(e=>{var t,o,r,a,p,u,c;return"path"===e&&(null===(t=n.packageJson)||void 0===t?void 0:t.path)?n.packageJson.path:("title"!==e||(null===(o=n.packageJson)||void 0===o||null===(r=o.packageJson)||void 0===r?void 0:r.title)||(e="name"),!n.nameFallback||"name"!==e||null!==(a=n.packageJson)&&void 0!==a&&null!==(p=a.packageJson)&&void 0!==p&&p.name?(null===(u=n.packageJson)||void 0===u||null===(c=u.packageJson)||void 0===c?void 0:c[e])?n.packageJson.packageJson[e]:void 0:i.a.basename(i.a.dirname(n.packageJson.path)))})(f);void 0===d?u(`Requested package field ${f} in process.env.${c}, but it was not found`):(u.enabled&&u(`process.env.${c} → ${JSON.stringify(d)}`),t.replaceWith(e.valueToNode(d)))}}}}},function(e,n){e.exports=require("debug")}])},"object"==typeof exports&&"object"==typeof module?module.exports=n():"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?exports["babel-plugin-pkg"]=n():e.babelPluginPkg=n(); |
@@ -1,1 +0,1 @@ | ||
{"author":{"name":"Jaid","email":"jaid.jsx@gmail.com","url":"https://github.com/Jaid"},"bugs":{"url":"https://github.com/jaid/babel-plugin-pkg/issues"},"dependencies":{"camelcase":"^6.0.0","debug":"^4.2.0","read-pkg-up":"^7.0.1"},"description":"Resolves _PKG_VERSION to version from package.json - also works with any other field!","homepage":"https://github.com/Jaid/babel-plugin-pkg","keywords":["babel","babel-plugin","convert","name","package","package-json","pkg","resolve","resolver","transform","transformation","transpile","transpiler","version"],"name":"babel-plugin-pkg","repository":"github:jaid/babel-plugin-pkg","version":"2.0.0"} | ||
{"author":{"name":"Jaid","email":"jaid.jsx@gmail.com","url":"https://github.com/Jaid"},"bugs":{"url":"https://github.com/jaid/babel-plugin-pkg/issues"},"dependencies":{"camelcase":"^6.0.0","debug":"^4.2.0","read-pkg-up":"^7.0.1"},"description":"Resolves _PKG_VERSION to version from package.json - also works with any other field!","homepage":"https://github.com/Jaid/babel-plugin-pkg","keywords":["babel","babel-plugin","convert","name","package","package-json","pkg","resolve","resolver","transform","transformation","transpile","transpiler","version"],"name":"babel-plugin-pkg","repository":"github:jaid/babel-plugin-pkg","version":"2.1.0"} |
@@ -5,3 +5,3 @@ # babel-plugin-pkg | ||
<a href="https://raw.githubusercontent.com/jaid/babel-plugin-pkg/master/license.txt"><img src="https://img.shields.io/github/license/jaid/babel-plugin-pkg?style=flat-square" alt="License"/></a> <a href="https://github.com/sponsors/jaid"><img src="https://img.shields.io/badge/<3-Sponsor-FF45F1?style=flat-square" alt="Sponsor babel-plugin-pkg"/></a> | ||
<a href="https://actions-badge.atrox.dev/jaid/babel-plugin-pkg/goto"><img src="https://img.shields.io/endpoint.svg?style=flat-square&url=https%3A%2F%2Factions-badge.atrox.dev%2Fjaid%2Fbabel-plugin-pkg%2Fbadge" alt="Build status"/></a> <a href="https://github.com/jaid/babel-plugin-pkg/commits"><img src="https://img.shields.io/github/commits-since/jaid/babel-plugin-pkg/v2.0.0?style=flat-square&logo=github" alt="Commits since v2.0.0"/></a> <a href="https://github.com/jaid/babel-plugin-pkg/commits"><img src="https://img.shields.io/github/last-commit/jaid/babel-plugin-pkg?style=flat-square&logo=github" alt="Last commit"/></a> <a href="https://github.com/jaid/babel-plugin-pkg/issues"><img src="https://img.shields.io/github/issues/jaid/babel-plugin-pkg?style=flat-square&logo=github" alt="Issues"/></a> | ||
<a href="https://actions-badge.atrox.dev/jaid/babel-plugin-pkg/goto"><img src="https://img.shields.io/endpoint.svg?style=flat-square&url=https%3A%2F%2Factions-badge.atrox.dev%2Fjaid%2Fbabel-plugin-pkg%2Fbadge" alt="Build status"/></a> <a href="https://github.com/jaid/babel-plugin-pkg/commits"><img src="https://img.shields.io/github/commits-since/jaid/babel-plugin-pkg/v2.1.0?style=flat-square&logo=github" alt="Commits since v2.1.0"/></a> <a href="https://github.com/jaid/babel-plugin-pkg/commits"><img src="https://img.shields.io/github/last-commit/jaid/babel-plugin-pkg?style=flat-square&logo=github" alt="Last commit"/></a> <a href="https://github.com/jaid/babel-plugin-pkg/issues"><img src="https://img.shields.io/github/issues/jaid/babel-plugin-pkg?style=flat-square&logo=github" alt="Issues"/></a> | ||
<a href="https://npmjs.com/package/babel-plugin-pkg"><img src="https://img.shields.io/npm/v/babel-plugin-pkg?style=flat-square&logo=npm&label=latest%20version" alt="Latest version on npm"/></a> <a href="https://github.com/jaid/babel-plugin-pkg/network/dependents"><img src="https://img.shields.io/librariesio/dependents/npm/babel-plugin-pkg?style=flat-square&logo=npm" alt="Dependents"/></a> <a href="https://npmjs.com/package/babel-plugin-pkg"><img src="https://img.shields.io/npm/dm/babel-plugin-pkg?style=flat-square&logo=npm" alt="Downloads"/></a> | ||
@@ -12,2 +12,3 @@ | ||
This plugin attempts to dynamically replace expressions starting with `process.env.REPLACE_PKG_`. | ||
@@ -21,3 +22,3 @@ | ||
```bash | ||
npm install --save babel-plugin-pkg@^2.0.0 | ||
npm install --save-dev babel-plugin-pkg@^2.1.0 | ||
``` | ||
@@ -28,3 +29,3 @@ | ||
```bash | ||
yarn add babel-plugin-pkg@^2.0.0 | ||
yarn add --dev babel-plugin-pkg@^2.1.0 | ||
``` | ||
@@ -36,3 +37,3 @@ | ||
```bash | ||
npm install --save @jaid/babel-plugin-pkg@^2.0.0 | ||
npm install --save-dev @jaid/babel-plugin-pkg@^2.1.0 | ||
``` | ||
@@ -42,20 +43,79 @@ | ||
## Example | ||
Reference fields from your `package.json` in your source files. | ||
`package.json` | ||
```json | ||
{ | ||
"name": "readable-ms", | ||
"version": "1.2.3" | ||
} | ||
``` | ||
`src/index.js` | ||
```js | ||
console.log(`This is ${process.env.REPLACE_PKG_NAME} v${process.env.REPLACE_PKG_VERSION}`) | ||
``` | ||
This will be transpiled to: | ||
`dist/index.js` | ||
```js | ||
console.log("This is readable-ms v1.2.3") | ||
``` | ||
## Usage | ||
Add to your Babel configuration. | ||
`.babelrc.js` | ||
```js | ||
module.exports = { | ||
plugins: [ | ||
"pkg" | ||
] | ||
} | ||
``` | ||
## Options | ||
<table> | ||
<tr> | ||
<th></th> | ||
<th>Default</th> | ||
<th>Info</th> | ||
</tr> | ||
<tr> | ||
<td>cwd</td> | ||
<td>(determined by Babel)</td> | ||
<td>The directory which the search for the package.json file begins in.</td> | ||
</tr> | ||
<tr> | ||
<td>nameFallback</td> | ||
<td>true</td> | ||
<td>If true and package.json does not contain a "name" field, the folder name that contains the chosen package.json file will be used as replacement.</td> | ||
</tr> | ||
<tr> | ||
<td>prefix</td> | ||
<td>REPLACE_PKG_</td> | ||
<td>Only members of process.env starting with this string will be replaced.</td> | ||
</tr> | ||
</table> | ||
## Development | ||
@@ -62,0 +122,0 @@ |
9019
138