Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/kamiazya/jsonnet-node-loader

A loader that allows Node.js to import Jsonnet settings from an ES Module file.
https://github.com/kamiazya/jsonnet-node-loader

jsonnet node-loader node-loader-hook

Last synced: 18 days ago
JSON representation

A loader that allows Node.js to import Jsonnet settings from an ES Module file.

Awesome Lists containing this project

README

        

[![NodeCI](https://github.com/kamiazya/jsonnet-node-loader/actions/workflows/node-ci.yaml/badge.svg)](https://github.com/kamiazya/jsonnet-node-loader/actions/workflows/node-ci.yaml)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![tested with jest](https://img.shields.io/badge/tested_with-jest-99424f.svg)](https://github.com/facebook/jest)
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](http://makeapullrequest.com)
[![Open in Visual Studio Code](https://open.vscode.dev/badges/open-in-vscode.svg)](https://open.vscode.dev/kamiazya/jsonnet-node-loader)

# jsonnet-node-loader

A [loader](https://nodejs.org/api/esm.html#loaders) that allows Node.js to import [Jsonnet](https://jsonnet.org/) settings from an ESModule file.

## Features

- Support importing `.jsonnet` file

## Installation

If you're using the npm or yarn CLI, then add the plugin by:

[![NPM](https://nodei.co/npm/jsonnet-node-loader.png)](https://nodei.co/npm/jsonnet-node-loader/)

```bash
# yarn
$ yarn add -D jsonnet-node-loader
# or npm
$ npm install --save-dev jsonnet-node-loader
```

## Usage

### `loader` option

Specify jsonnet-node-loader in the loader option and execute the file.

Then, when the jsonnet file is imported, the evaluated value is returned.

```bash
$ node --experimental-loader jsonnet-node-loader example.mjs
{
person1: { name: 'Alice', welcome: 'Hello Alice!' },
person2: { name: 'Bob', welcome: 'Hello Bob!' }
}
```

example.mjs

```js
import data from './example.jsonnet';

console.log(data);
```

example.jsonnet

```jsonnet
{
person1: {
name: "Alice",
welcome: "Hello " + self.name + "!",
},
person2: self.person1 { name: "Bob" },
}
```

### Advanced usage

When using `std.extVar(x)`, query parameters can be specified and passed when importing `vars.jsonnet`.

vars.jsonnet

```jsonnet
{
vars: {
foo: std.extVar('foo'),
var: std.extVar('bar'),
},
}
```

vars.mjs

```js
import data from './vars.jsonnet?foo=1&bar=2';

console.log(data);
// { vars: { foo: '1', var: '2' } }
```

## License

This software is released under the MIT License, see [LICENSE](./LICENSE).