xastscript
xastscript is a utility library providing a hyperscript-like interface for programmatically creating xast (XML Abstract Syntax Tree) nodes. Similar to React's `createElement` or `hastscript` for HTML, it simplifies the creation of XML syntax trees by allowing nested function calls instead of verbose object literals. The current stable version is 4.0.0, which requires Node.js 16 or later and is exclusively ESM. It's part of the unified ecosystem and is primarily used when generating XML structures within code. Unlike `unist-builder` which handles generic unist nodes, `xastscript` is specifically tailored for XML elements, providing convenience for defining attributes and children in a more declarative way. Its release cadence typically involves minor and patch updates every few months, with major versions occurring less frequently, driven by breaking changes or significant feature additions.
Common errors
-
ERR_REQUIRE_ESM
cause Attempting to use `require()` to import `xastscript`, which is an ES Module.fixChange `const { x } = require('xastscript');` to `import { x } from 'xastscript';`. Ensure your `package.json` includes `"type": "module"` if this is a Node.js project or use `.mjs` file extensions. -
TypeError: xastscript_1.x is not a function
cause This error typically occurs when a CommonJS consumer attempts to use an ESM package that has been incorrectly transpiled or bundled, or when trying to default import a named export.fixVerify your build setup handles ES Modules correctly. Always use named imports for `x` (e.g., `import { x } from 'xastscript';`). If using TypeScript, check `tsconfig.json` for `moduleResolution` and `module` settings. -
TypeError: Cannot read properties of undefined (reading 'x')
cause This can happen if the `xastscript` module isn't resolved correctly, or if a global `x` is expected when it's not available, often due to a failed or incorrect import.fixDouble-check your `import { x } from 'xastscript';` statement for typos. Ensure `xastscript` is correctly installed in `node_modules`. If running in a browser, ensure `esm.sh` or similar CDN imports are correct and bundled if necessary.
Warnings
- breaking xastscript v4.0.0 requires Node.js 16 or higher. Older Node.js versions are no longer supported.
- breaking xastscript transitioned to an ESM-only package starting with v3.0.0. Direct CommonJS `require()` calls are no longer supported.
- breaking TypeScript types are now shipped directly with the package since v2.0.0. If you were using `@types/xastscript`, you should remove it. V4.0.0 specifically updated `@types/xast`, potentially requiring updates in related type definitions.
- gotcha The `x` function can serve as a JSX pragma, allowing XML-like syntax directly in your code. This requires specific configuration in your TypeScript or Babel setup.
Install
-
npm install xastscript -
yarn add xastscript -
pnpm add xastscript
Imports
- x
const { x } = require('xastscript')import { x } from 'xastscript' - Attributes
import { Attributes } from 'xastscript'import type { Attributes } from 'xastscript' - Result
import { Result } from 'xastscript'import type { Result } from 'xastscript'
Quickstart
import { u } from 'unist-builder';
import { x } from 'xastscript';
// Children as an array:
console.log(
x('album', {id: 123}, [
x('name', 'Born in the U.S.A.'),
x('artist', 'Bruce Springsteen'),
x('releasedate', '1984-04-06')
])
);
// Children as arguments:
console.log(
x(
'album',
{id: 456},
x('name', 'Exile in Guyville'),
x('artist', 'Liz Phair'),
x('releasedate', '1993-06-22')
)
);
// Combining with unist-builder for other xast nodes (comments, instructions, cdata):
console.log(
x(null, [
u('instruction', {name: 'xml'}, 'version="1.0" encoding="UTF-8"'),
x('album', [
u('comment', 'A fantastic rock album!'),
x('name', 'Born in the U.S.A.'),
x('description', [u('cdata', '3 < 5 & 8 > 13')])
])
])
);