J6Pack.js is a JavaScript library that compiles JSX to JavaScriptand can then render the JSX to HTML or XML. You can use it to add JSX support to Node.js without build tools. Its HTML/XML rendering is also compatible with any web framework, though it's got builtin support for Express.js. You can even use it client-side as the HTML/XML rendering is ECMAScript 5 compatible. There's even a Browserify plugin handy for bundling.
J6Pack does not depend on React nor implements a virtual DOM. The HTML/XML it renders is just text, though with interpolated values securely escaped.
J6Pack.js defaults to using Acorn for parsing JSX — supporting ECMAScript 14 (2023) and beyond — but you're welcome to use any ESTree compatible parser and invoke J6Pack.js's compiler directly.
For pedantics, the JavaScript it renders preserves all the whitespace and newlines in your JSX source and is therefore very human readable.
J6Pack.js follows semantic versioning, so feel free to depend on its major version with something like >= 1 < 2 (a.k.a ^1).
Rendering HTML or XML
For rendering HTML, import J6Pack.js and call it in the style of React.createElement or equivalent JSX-functions, with the additional requirement that children always be in an array. Generally that's how a JSX compiler/transpiler would render the function calls.
For example, setting the factory function to Jsx via the @jsx pragma (most JSX compilers support this), using J6Pack.js would look like:
The html variable in both examples is an instance of Jsx.Html with valueOf and toString methods that return the HTML for the entire tree. The use of a value object over a string is mostly an implementation requirement of the way JSX compilers work. It does however permit you to differentiate between unescaped strings and escaped HTML via instanceof should you need to:
To use the JSX syntax (<p>Hello, {name}!</p>) on Node.js without an external compiler tool, see the section on "Using JSX with Node.js".
XML
J6Pack.js by default renders HTML5-compatible HTML. If you'd like to use it to render generic XML, for example to render an Atom feed, you can require the XML variant from j6pack/xml:
The HTML and XML serializers render slightly differently:
HTML is rendered with explicit closing-elements for empty tags, except for the void elements in the HTML standard (like <input>).
XML has no void elements.
J6Pack.js renders HTML with minimal transformations, so use HTML attribute names, not DOM properties. That is, to set the tag's class attribute, use <p class="greeting">Hello, world!</p> rather than className as you would with React. Same goes for elements' onclick, <label>s' for, <input>s' readonly and so on.
Interpolating HTML (like innerHTML)
Should you need to interpolate HTML into the output, you can't use the innerHTML property (or React's dangerouslySetInnerHTML) as those live only in the DOM world, not in HTML. Instead, use the Jsx.html function to inject your HTML into otherwise escaped values:
var Jsx = require("j6pack")var html = <p>Hello, {Jsx.html("<em>world</em>")}!</p>String(html) // => "<p>Hello, <em>world</em>!</p>"
When you're rendering XML, use Jsx.xml:
var Jsx = require("j6pack/xml")var atom = <feed xmlns="http://www.w3.org/2005/Atom"> <id>http://example.com</id> <title>My Blog</title> {Jsx.xml("<entry>…</entry>")}</feed>
Custom Elements (Functional Components)
Just like React or other virtual DOM implementations, J6Pack.js supports custom elements. Behind the scenes a custom element is really just a function that gets invoked with two arguments — an object of attributes and an array of children. Both can be null/undefined if they were not given, so beware.
function Page(attrs, children) { return <html> <head><title>{attrs.title}</title></head> <body>{children}</body> </html>}var html = <Page title="Test Page"> <p>Hello, world!</p></Page>
If you use another JSX compiler/transpiler than J6Pack.js, configure it to call component functions directly, rather than passing them to the factory function.
Returning Multiple Elements (Fragments)
Occasionally you may want a custom element to return multiple elements without wrapping them in yet another element. You can do that in two ways.
Then use it as you would any other custom element:
var html = <div> <Input label="Name" value="John" /> <br /> <Input label="Age" type="number" value={42} /></div>
If you need to configure your external JSX compiler/transpiler for fragments, you can set it to emit fragments either as JavaScript arrays or as invocations of Jsx.Fragment like regular components (children in the 2nd argument as an array).
A sufficiently new version of ESLint React plugin seems to also have a pragma setting to set the factory function once for the entire project.
Compiling JSX to JavaScript
In addition to rendering HTML, J6Pack.js also contains a compiler/transpiler for JSX syntax. This section covers the use of the standalone compiler, so if you're looking to use JSX while writing your app (for Node.js), see the section on "Using JSX with Node.js".
To compile a string of JavaScript with JSX into plain JavaScript, import the J6Pack.js compiler and invoke it with the source code
var compile = require("j6pack/compiler")var js = compile(` var Jsx = require("j6pack") var html = <Greeting name="John" /> function Greeting(attrs) { return <h1 class="greeting">Hello, {attrs.name}!</h1> }`)
The js variable should then include the compiled/transpiled output as a string:
var Jsx = require("j6pack")var html = Greeting({name: "John"})function Greetng(attrs) { return Jsx("h1", {class: "greeting"}, ["Hello, ", attrs.name, "!"])}
Compiler Options
The compiler has a few options to customize its output.
Option
Description
factory
The function called for creating elements. Called with the tag name, attributes object or null, and an array of children. Defaults to Jsx.
fragmentFactory
The function called for <>…</> JSX syntax. Called with null and an array of children. Set to null if want a plain JavaScript array instead of a function call. If it starts with ., it's considered to be a property of factory and appended to it. Defaults to null.
componentFactory
The function called for component element (tags whose name starts with a capitalized letter). Called with the component variable, attributes object or null, and an array of children. Set to null if you want the component variable called directly with attributes and children. If it starts with ., it's considered to be a property of factory and appended to it. Defaults to null.
assign
The function used for merging spread attributes (<div {...attrs} class="foo" />). If it starts with ., it's considered to be a property of factory and appended to it. Defaults to Object.assign.
ecmaVersion
Set or limit the JavaScript version for parsing. Useful for ensuring you don't accidentally use newer JavaScript syntax in your source files. Acorn, the parser J6Pack.js uses by default, supports versions from 3–14 and beyond. Defaults to "latest".
sourceType
Set to script or module to configure support for import and export declarations. Defaults to script if ecmaVersion is 3 or 5 and module otherwise.
You can pass the compiler options to compile as a second argument:
var compile = require("j6pack/compiler")var js = compile(` var Jaysex = require("j6pack") var html = <Greeting name="John" /> function Greeting(attrs) { return <h1 class="greeting">Hello, {attrs.name}!</h1> }`, {factory: "Jaysex"})
Renaming the Jsx Function
By default the compiled JavaScript expects the name of the JSX-render (factory) function to be Jsx. That's why all the examples here assign require("j6pack") to Jsx. If you want to use a different function name, you've got two options:
If you wish to not depend on Object.assign, you can use the assign compiler option to replace Object.assign with a function name of your own.
Both the HTML (require("j6pack/html")) and XML (require("j6pack/xml")) renderers have an assign export with a helper function that shallow-merges objects. If you can't depend on Object.assign being available (e.g. in old browsers) or you dislike its ignoring of inherited properties, you're welcome to set the assign option to .assign. A leading period indicates it's a property of the factory function.
Executable
J6Pack.js comes with a simple executable, j6pack that you can use to precompile JSX, perhaps for testing or just seeing what JSX compiles down to. After installing J6Pack.js, invoke it with ./node_modules/.bin/j6pack or from bin/j6pack from this repository:
J6Pack.js defaults to using Acorn for parsing JSX — supporting ECMAScript 14 (2023) and beyond — but you're welcome to use any ESTree compatible parser that also supports JSX AST nodes.
For example, use of Esprima should be something like:
var parse = require("esprima").parsevar compile = require("j6pack/compiler").compilevar js = ` var Jsx = require("j6pack") var html = <h1 class="greeting">Hello, John!</h1>`compile({factory: "Jaysex"}, parse(js), js)
Using JSX with Node.js
To add transparent support for importing JSX source files to Node.js, require j6pack/register before starting the app:
node --require j6pack/register app.js
Or require it in your main entry file before you load .jsx files:
require("j6pack/register")
Then, combined with J6Pack.js's HTML rendering, you can use JSX to respond with HTML from within your request handlers:
The same example code is in examples/server.jsx which you can run via make examples/server.jsx and view on http://localhost:3000 by default.
Express.js
J6Pack.js comes with explicit support for the Express.js web framework templates. To add .jsx template support to Express.js, set it as an engine:
var Express = require("express")var app = Express()app.engine(".jsx", require("j6pack/express"))
You can then render a JSX component through res.render. As its attributes it gets the sum of global app.locals, request's res.locals and the attributes you pass to res.render, much just like a Jade/Pug template would:
var Jsx = require("j6pack")module.exports = function(attrs) { return <html> <head><title>J6Pack Test Express Template Server</title></head> <body><p>{attrs.greeting}</p></body> </html>}
Note that you don't need to wrap the JSX in String as you did elsewhere. That's handled for you by J6Pack.js's integration with Express.js.
If you'd rather have your routes render HTML directly without extracting templates to a separate file, you can do the same as with the plain HTTP server above. You don't even need to set the view engine parameter as this will be bypassing Express.js's templating entirely:
To set the JSX compiler options, use the Browserify transform option syntax. For example, to limit J6Pack.js's JavaScript parser to ECMAScript 5 (to prevent accidentally using newer JavaScript syntax in your source files):