Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,32 @@

Babel plugin for inlining values from `package.json`

Using `require`:

Input:
```js

```javascript
var version = require('./package.json').version;
```

Output:
```js

```javascript
var version = '1.0.0';
```

Using `import`:

```javascript
import { version } from '../package.json';
```

Output:

```javascript
const version = '1.0.0';
```

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.

## Installation
Expand All @@ -37,8 +54,8 @@ $ babel --plugins inline-package-json script.js

### Via Node API

```js
```javascript
require('babel-core').transform('code', {
plugins: ['inline-package-json']
});
```
```
54 changes: 44 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,54 @@
import path from 'path';

export default function({types: t}) {
export default function({ types: t }) {
function getValue(src, name, key) {
const basePath = path.resolve(src);
const pkg = require(path.join(basePath, '..', name));

return pkg[key];
}

function isPackageJSON(node) {
return t.isLiteral(node) && /package\.json$/.test(node.value);
}

return {
visitor: {
ImportDeclaration: function importDeclaration(treePath) {
const node = treePath.node;
const src = this.file.opts.filename;

if (!isPackageJSON(node.source)) {
return;
}

const variables = node.specifiers.filter(t.isImportSpecifier).map(specifier => {
const value = getValue(src, node.source.value, specifier.local.name);

return t.variableDeclaration('const', [t.variableDeclarator(specifier.local, t.valueToNode(value))]);
});

if (!variables.length) {
return;
}

return treePath.replaceWithMultiple(variables);
},

MemberExpression: function MemberExpression(treePath) {
const node = treePath.node;
if (t.isCallExpression(node.object) &&
t.isIdentifier(node.object.callee, { name: 'require' }) &&
t.isLiteral(node.object.arguments[0]) &&
node.object.arguments[0].value.match(/package\.json$/)) {
let srcPath = path.resolve(this.file.opts.filename);
let pkg = require(path.join(srcPath, '..', node.object.arguments[0].value));
let value = pkg[node.property.name];
treePath.replaceWith(t.expressionStatement(t.valueToNode(value)));
const src = this.file.opts.filename;

if (!t.isCallExpression(node.object) ||
!t.isIdentifier(node.object.callee, { name: 'require' }) ||
!isPackageJSON(node.object.arguments[0])) {
return;
}

const value = getValue(src, node.object.arguments[0].value, node.property.name);

return treePath.replaceWith(t.expressionStatement(t.valueToNode(value)));
}
}
}
}
};