# extendx

> Extend brings the syntax you want to the language of your choice. Write Python list comprehensions, Rust `if let`, Nim ranges, or Zig loops inside JavaScript — Extend compiles them to plain JS.

Latest version **0.3.14** (published 2026-05-25) · 0 weekly downloads

## Install

```sh
npm install extendx
pnpm add extendx
yarn add extendx
bun add extendx
```

Provides the command `extendx`.

## Health

**Score 45/100 (D)** — status: active.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.3.14 |
| Published | 2026-05-25 |
| First published | 2019-11-23 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 6 |
| Unpacked size | 126.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Hassan |
| Maintainers | squaredev |

## Links

- npm: https://www.npmjs.com/package/extendx
- Repository: https://github.com/hassanmodev/Extend
- Homepage: https://github.com/hassanmodev/Extend#readme
- Issues: https://github.com/hassanmodev/Extend/issues
- npm.io page: https://npm.io/package/extendx

## Dependencies (6)

- [chokidar](https://npm.io/package/chokidar.md) ^3.3.0
- [fs-extra](https://npm.io/package/fs-extra.md) ^8.1.0
- [decomment](https://npm.io/package/decomment.md) ^0.9.2
- [js-beautify](https://npm.io/package/js-beautify.md) ^1.14.6
- [command-line-args](https://npm.io/package/command-line-args.md) ^5.1.1
- [@types/js-beautify](https://npm.io/package/@types/js-beautify.md) ^1.13.3

## Recent versions

- 0.3.14 (latest) — 2026-05-25
- 0.3.13 — 2026-05-25
- 0.3.12 — 2022-09-27
- 0.3.11 — 2022-09-27
- 0.3.10 — 2022-09-27
- 0.3.9 — 2022-09-27
- 0.3.8 — 2022-09-27
- 0.3.6 — 2022-09-27
- 0.3.5 — 2022-09-26
- 0.3.4 — 2022-09-26
- 0.3.3 — 2022-09-26
- 0.3.2 — 2020-05-04
- 0.3.1 — 2020-05-02
- 0.2.1 — 2020-04-18
- 0.1.2 — 2020-04-15
- … 9 more at https://npm.io/package/extendx/versions

## README

# Extend

Extend brings the syntax you want to the language of your choice. Write Python list comprehensions, Rust `if let`, Nim ranges, or Zig loops inside JavaScript — Extend compiles them to plain JS.

## Installation

Install Extend globally:

```
npm install -g extendx
```

The CLI is published as **extendx** for now.

**Use this repo locally:** from the repo root run `npm link`. That installs the **`extendx`** command from your checkout (replacing any global npm install until you unlink). After changing TypeScript sources, rebuild with `npm run t`; the symlinked CLI loads `build/watch.js`.

To depend on **extendx** from another project without publishing, run **`npm link extendx`** inside that project's folder (the dependency name is **`extendx`**, matching `"name"` in this package).

Syntax highlighting is partially supported in VS Code. Open the command palette (`CTRL`+`P`) and run:

```
ext install fabiospampinato.vscode-highlight
```

This installs the [vscode-highlight](https://marketplace.visualstudio.com/items?itemName=fabiospampinato.vscode-highlight) extension. It writes to `.vscode/settings.json` in your workspace, so you may want to back that file up first.

## Usage

Create a starter project:

```
extendx --start
```

This scaffolds `_extend.js` (your rules) and `src/demo.xt.js` (a polyglot demo).

The compiler reads files from `src/` with extensions `.xt` or `.xt.js`, finds blocks marked with your code delimiters, applies matching rules, and writes output to `dist/`.

By default, code blocks use backtick-wrapped mustache syntax:

```
const salaries = `{{ [e.salary for e in employees] }}`
```

You can choose any opening/closing markers in `_extend.js` — comment syntax from your target language works well so your IDE does not flag extensions as errors.

## Polyglot demo

The starter project includes rules for idioms from several languages. Source (`src/demo.xt.js`):

```javascript
// Python
const salaries = `{{ [e.salary for e in employees] }}`
const topTwo = `{{ salaries[0:2] }}`
const lastSalary = `{{ salaries[-1] }}`
const bonus = `{{ 500 if lastSalary > 1500 else 200 }}`

// Rust
`{{ if let name = maybeName { console.log(name) } }}`
const add = `{{ fn add(a, b){ return a + b } }}`

// Nim
const nimRange = `{{ 1..5 }}`
`{{ echo nimRange.join('-') }}`

// Zig
`{{ for (tags) | tag |{ print(tag) } }}`
const fallback = `{{ null orelse 'default' }}`
```

Compiled output (`dist/demo.js`):

```javascript
const salaries = employees.map(e => e.salary)
const topTwo = salaries.slice(0, 2)
const lastSalary = salaries[salaries.length - 1]
const bonus = (lastSalary > 1500 ? 500 : 200)

if (maybeName != null) { const name = maybeName; console.log(name) }
const add = function add(a, b) { return a + b }

const nimRange = [...Array(5 - 1 + 1).keys()].map(i => i + 1)
console.log(nimRange.join('-'))

for (const tag of tags) { print(tag) }
const fallback = (null ?? 'default')
```

Run the watcher from your project root:

```
extendx
```

Or compile once from the Extend repo against the bundled `polyglot-js/` example:

```
cd polyglot-js && node ../extend
```

## Working with rules

Each rule is a plain object with `template` and `output`.

`template` describes what to match. Variables use `{variableName}`. Escape special characters with `#` (the default escape character).

Python slicing example:

```
{array} #[{start}:{end}#]
```

`output` is a function that receives the captured variables and returns the replacement string. Return `false` to skip a match (e.g. when a variable is invalid or another rule should win).

```javascript
{
  id: 'py slice',
  template: '{array} #[{start}:{end}#]',
  output: ({ array, start, end }) => {
    start = (start || '0').trim()
    end = end.trim()
    if (!end) return `${array.trim()}.slice(${start})`
    return `${array.trim()}.slice(${start}, ${end})`
  },
}
```

Rules can optionally include an `id` for debugging. See `polyglot-js/_extend.js` for a full ruleset covering Python, Rust, Nim, and Zig.

## `_extend.js` file

`_extend.js` lives in your project root and exports `rules`, `settings`, and `types`.

### Settings

Default settings:

```javascript
module.exports.settings = {
  srcFolder: 'src',
  distFolder: 'dist',
  codeOpening: '`{{',
  codeClosing: '}}`',
  variableOpening: '{',
  variableClosing: '}',
  arrayOpening: '[',
  arrayClosing: ']',
  escapeCharacter: '#',
  vscodeHighlighting: true
}
```

- `codeOpening` / `codeClosing` — delimiters in your source files
- `variableOpening` / `variableClosing` — variable syntax in rule templates
- `arrayOpening` / `arrayClosing` — array repetition syntax in templates
- `escapeCharacter` — escapes literal characters in templates (default `#`)

### Types

Types validate or transform captured variables. Each type is a name mapped to a `RegExp` or a function that returns `true`/`false` (or a replacement string).

```javascript
const ident = /^[a-zA-Z_$][\w$]*$/

module.exports.types = {
  int: /^\d+$/,
  ident,
  any: () => true,
}
```

Use a type in a template: `{ident myVar}` — only values matching `ident` are accepted.

[experimental]: Type functions also receive the full variable block and the current variable name. Returning a string replaces the captured value.

## What's new

### 0.3
Syntax highlighting on VSCode is here! (kind of)

### 0.2
Added variable types using regex or js functions.

### 0.1.0
Added array support.

Added rules settings.

---
_Source: https://npm.io/package/extendx · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
