All Projects → mattstrom → typesafe-templates

mattstrom / typesafe-templates

Licence: other
Template engine that leverages JSX to generate JavaScript code from TypeScript code files rather than text templates.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to typesafe-templates

vscode-ecsstractor
Extracting selectors from HTML / JSX / TSX and generate CSS file.
Stars: ✭ 45 (+66.67%)
Mutual labels:  jsx, tsx
ios-scriptable-tsx
在 vscode 上使用 typescript 和 jsx 开发 ios 小组件的小框架.基于 Scriptable app.
Stars: ✭ 113 (+318.52%)
Mutual labels:  jsx, tsx
lowcode
React Lowcode - prototype, develop and maintain internal apps easier
Stars: ✭ 32 (+18.52%)
Mutual labels:  jsx, tsx
njsx
A customizable and declarative interface for creating React and React Native components without JSX syntax.
Stars: ✭ 29 (+7.41%)
Mutual labels:  jsx, tsx
element
Fast and simple custom elements.
Stars: ✭ 65 (+140.74%)
Mutual labels:  jsx, tsx
vue-typescript-tsx
🤓vue typeScript jsx 工程示例
Stars: ✭ 32 (+18.52%)
Mutual labels:  jsx, tsx
Dependency Cruiser
Validate and visualize dependencies. Your rules. JavaScript, TypeScript, CoffeeScript. ES6, CommonJS, AMD.
Stars: ✭ 2,326 (+8514.81%)
Mutual labels:  jsx, tsx
harshhhdev.github.io
Harsh Singh's personal blog and portfolio ⚡ built with Next.js
Stars: ✭ 23 (-14.81%)
Mutual labels:  jsx, tsx
nornj
More exciting JS/JSX based on Template Engine, support control flow tags, custom directives, two-way binding, filters and custom operators.
Stars: ✭ 97 (+259.26%)
Mutual labels:  template-engine, jsx
vue3-jd-h5
🔥 Based on vue3.0.0, vant3.0.0, vue-router v4.0.0-0, vuex^4.0.0-0, vue-cli3, mockjs, imitating Jingdong Taobao, mobile H5 e-commerce platform! 基于vue3.0.0 ,vant3.0.0,vue-router v4.0.0-0, vuex^4.0.0-0,vue-cli3,mockjs,仿京东淘宝的,移动端H5电商平台!
Stars: ✭ 660 (+2344.44%)
Mutual labels:  jsx, tsx
indent.js
Pure code indentation for jsx, tsx, ts, js, html, css, less, scss.
Stars: ✭ 55 (+103.7%)
Mutual labels:  jsx, tsx
core
Light weight feature rich UI Framework for JavaScript for Browser with Dependency Injection, Mocking and Unit Testing
Stars: ✭ 18 (-33.33%)
Mutual labels:  jsx, tsx
Esbuild
An extremely fast JavaScript and CSS bundler and minifier
Stars: ✭ 29,374 (+108692.59%)
Mutual labels:  jsx, tsx
vuetify-tsx
Vuetify TSX is just a wrapper lib around vuetify components.
Stars: ✭ 20 (-25.93%)
Mutual labels:  jsx, tsx
md-editor-v3
Markdown editor for vue3, developed in jsx and typescript, dark theme、beautify content by prettier、render articles directly、paste or clip the picture and upload it...
Stars: ✭ 326 (+1107.41%)
Mutual labels:  jsx, tsx
vite-vue-admin
🎉🎉使用Vite + Vue3 + TypeScript + Element-plus + Mock开发的后台管理系统🎉🎉
Stars: ✭ 97 (+259.26%)
Mutual labels:  jsx, tsx
engine
A pragmatic approach to templating for PHP 7.x+
Stars: ✭ 31 (+14.81%)
Mutual labels:  template-engine
sendight-frontend
P2P File sharing
Stars: ✭ 53 (+96.3%)
Mutual labels:  jsx
react-with-observable
Use Observables with React declaratively!
Stars: ✭ 108 (+300%)
Mutual labels:  jsx
render.rs
🔏 A safe and simple template engine with the ergonomics of JSX
Stars: ✭ 158 (+485.19%)
Mutual labels:  jsx

typesafe-templates

CircleCI npm version semantic-release

Template engine for writing compiler-checked templates in TypeScript by leveraging JSX to generate JavaScript code from TypeScript code files rather than text templates.

Under the hood, typesafe-templates uses Babel to build an AST, which is then traversed to find and replace JSX nodes. Such a tool can be useful for pre-generating customized Javascript files that vary by user or for creating HTML templates using JSX rather than a syntax like Pug (some limitations apply).

Example

template.tsx

interface Message {
    name: string;
    lang: 'en' | 'es';
}

(function() {
    <$repeat items={$.messages}>
        {($: Message) => {
            <$if test={$.lang === 'en'}>
                console.log('Good morning' + <$string value={$.name} />);
            </$if>;
        	<$if test={$.lang === 'es'}>
                console.log('Bueños dias' + <$string value={$.name} />);
            </$if>;
        }}
    </$repeat>
})();

script.ts

import { renderFile } from 'typesafe-templates';

async function main() {
    const data: { messages: Message[] } = {
        messages: [
            { name: 'Alice', lang: 'en' },
            { name: 'Bob', lang: 'en' },
            { name: 'Dora', lang: 'es' },
            { name: 'Diego', lang: 'es' }
        ]
    };
    
    const { code } = await renderFile('./template.tsx', data);
    console.log(code);
}

main();

Output

(function() {
    console.log('Good morning, Alice');
    console.log('Good morning, Bob');
    console.log('Bueños dias, Dora');
    console.log('Bueños dias, Diego');
})();

Installation

npm install typesafe-templates

Use the --save-dev flag if you will be generating templates as part of a development task.

Usage

To work properly you will need to include a type definition for JSX elements that assigns JSX.Element to any.

declare namespace JSX {
    type Element = any;
}

As a convenience, the $ symbol is used to represent a parent type of a scope. However, any name can be used. Presently the library does not support deep properties. A reference like $.level1.level2.level3 may type-check correctly, but the proper value will not be injected into the template.

Elements

Control Elements

Control elements are similar to JavaScript control blocks. They wrap a block of code and control its output into the rendered code. In JSX terms, they require props.children to be defined.

Because JSX is used here as substitute JavaScript expressions, you will often need to include a semicolon after the tag as you would with a normal statement.

<$if test={} />

Controls whether the wrapped contents will appear in the output.

<$repeat items={} />

Repeats the wrapped contents for each item in items. For each copy, a new data scope is created and set to value of the current item.

The $repeat element expects the children prop to be a function or arrow function expression; however, this surrounding function will be removed in the final output.

Example:

<$repeat items={$.messages}>
    {($: Message) => {
    	console.log(<$string value={$.name}>)
    }}
</$repeat>

Injection Elements

<$boolean value={} />

Outputs a boolean literal.

<$expr code={} />

Takes in a string representing an expression, parses it into AST, and adds the resultant node into the output.

<$number value={} />

Outputs a numeric literal.

<$string value={} />

Outputs a string literal.

Limitations

Currently TypeScript treats all JSX elements as the same type (which can be changed but only to one collective type). Therefore elements, when used as values, are treated as any and will not show type errors unless you manually typecast the element. Refactoring signatures, however, will work when using TS tooling to rearrange arguments.

Example:

function print(str: string, num: number, bool: boolean) {}

print(<$string />, <$string />, <$string />); // Will not report type errors

print(<$string /> as string, <$string /> as string, <$string /> as string); // Will report type errors
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].