https://github.com/jaid/babel-plugin-pkg
Resolves _PKG_VERSION to version from package.json - also works with any other field!
https://github.com/jaid/babel-plugin-pkg
babel babel-plugin convert name package package-json pkg resolve resolver transform transformation transpile transpiler version
Last synced: about 1 month ago
JSON representation
Resolves _PKG_VERSION to version from package.json - also works with any other field!
- Host: GitHub
- URL: https://github.com/jaid/babel-plugin-pkg
- Owner: Jaid
- License: mit
- Created: 2019-02-22T06:16:16.000Z (over 7 years ago)
- Default Branch: main
- Last Pushed: 2022-12-11T05:08:58.000Z (over 3 years ago)
- Last Synced: 2025-08-09T02:17:10.042Z (10 months ago)
- Topics: babel, babel-plugin, convert, name, package, package-json, pkg, resolve, resolver, transform, transformation, transpile, transpiler, version
- Language: JavaScript
- Homepage: https://github.com/Jaid/babel-plugin-pkg
- Size: 2.67 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 15
-
Metadata Files:
- Readme: readme.md
- Funding: .github/funding.yml
- License: license.txt
Awesome Lists containing this project
README
# babel-plugin-pkg
**Resolves _PKG_VERSION to version from package.json - also works with any other field!**
This plugin attempts to dynamically replace expressions starting with `process.env.REPLACE_PKG_`.
## Installation
```bash
npm install --save-dev babel-plugin-pkg@^2.1.0
```
```bash
yarn add --dev babel-plugin-pkg@^2.1.0
```
(if [configured properly](https://help.github.com/en/github/managing-packages-with-github-packages/configuring-npm-for-use-with-github-packages))
```bash
npm install --save-dev @jaid/babel-plugin-pkg@^2.1.0
```
## 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
Default
Info
cwd
(determined by Babel)
The directory which the search for the package.json file begins in.
nameFallback
true
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.
prefix
REPLACE_PKG_
Only members of process.env starting with this string will be replaced.
## Development
Setting up:
```bash
git clone git@github.com:jaid/babel-plugin-pkg.git
cd babel-plugin-pkg
npm install
```
Testing:
```bash
npm run test:dev
```
Testing in production environment:
```bash
npm run test
```
## License
[MIT License](https://raw.githubusercontent.com/jaid/babel-plugin-pkg/master/license.txt)
Copyright © 2020, Jaid \ (https://github.com/jaid)