Comparing version 0.1.0-next.2 to 0.1.0
@@ -74,3 +74,3 @@ const ESCAPE = /[&"<]/g, CHARS = { | ||
} else if (extra[action]) { | ||
if (inner.length) { | ||
if (inner) { | ||
tmp = []; | ||
@@ -81,4 +81,5 @@ // parse arguments, `defer=true` -> `{ defer: true }` | ||
} | ||
tmp = (options.async) ? 'await ' : ''; | ||
wip += '${' + tmp + '$$2.' + action + '(' + inner + ')}'; | ||
inner = inner || '{}'; | ||
tmp = options.async ? 'await ' : ''; | ||
wip += '${' + tmp + '$$2.' + action + '(' + inner + ',$$2)}'; | ||
} else { | ||
@@ -85,0 +86,0 @@ throw new Error(`Unknown "${action}" block`); |
export type Args = Record<string, any>; | ||
export type Block<T extends Args = Args> = (args: T) => string; | ||
export type Compiler = <T extends Record<string, any>> (data: T) => string; | ||
export type Blocks = Record<string, Compiler>; | ||
export type Compiler = <T extends Args> (data?: T, blocks?: Blocks) => Promise<string>|string; | ||
export interface Options { | ||
props?: string[]; | ||
blocks?: Record<string, Block>; | ||
blocks?: Blocks; | ||
async?: boolean; | ||
@@ -12,4 +12,6 @@ } | ||
export function esc<T=unknown>(value: T): T|string; | ||
export function transform(input: string, options?: Options & { format?: 'esm' | 'cjs' }): string; | ||
export function compile(input: string, options?: Options & { async: true }): <T extends Args> (data: T) => Promise<string>; | ||
export function compile(input: string, options?: Options & { async: false }): <T extends Args> (data: T) => string; | ||
export function compile(input: string, options?: Options & { escape?: typeof esc; }): Compiler; | ||
export function transform(input: string, options?: Options & { format?: 'esm' | 'cjs' }): string; |
{ | ||
"name": "tempura", | ||
"version": "0.1.0-next.2", | ||
"version": "0.1.0", | ||
"repository": "lukeed/tempura", | ||
"description": "WIP", | ||
"description": "A light, crispy, and delicious template engine", | ||
"module": "dist/index.mjs", | ||
@@ -24,3 +24,2 @@ "main": "dist/index.js", | ||
"*.d.ts", | ||
"utils", | ||
"dist" | ||
@@ -38,3 +37,10 @@ ], | ||
}, | ||
"keywords": [], | ||
"keywords": [ | ||
"engine", | ||
"mustache", | ||
"handlebars", | ||
"template", | ||
"html", | ||
"hbs" | ||
], | ||
"devDependencies": { | ||
@@ -41,0 +47,0 @@ "bundt": "1.1.5", |
120
readme.md
@@ -12,7 +12,7 @@ <div align="center"> | ||
</a> | ||
<a href="https://codecov.io/gh/lukeed/TODO"> | ||
<img src="https://badgen.now.sh/codecov/c/github/lukeed/TODO" alt="codecov" /> | ||
<a href="https://codecov.io/gh/lukeed/tempura"> | ||
<img src="https://badgen.now.sh/codecov/c/github/lukeed/tempura" alt="codecov" /> | ||
</a> | ||
<a href="https://npmjs.org/package/TODO"> | ||
<img src="https://badgen.now.sh/npm/dm/TODO" alt="downloads" /> | ||
<a href="https://npmjs.org/package/tempura"> | ||
<img src="https://badgen.now.sh/npm/dm/tempura" alt="downloads" /> | ||
</a> | ||
@@ -26,3 +26,3 @@ </div> | ||
* **Extremely lightweight**<br> | ||
_Everything is `1.25 kB` (gzip) – even less with tree-shaking!_ | ||
_Everything is `1.22 kB` (gzip) – even less with tree-shaking!_ | ||
@@ -36,3 +36,3 @@ * **Super Performant**<br> | ||
* **Custom Directives**<br> | ||
_Easily define custom directives, via the [API](#API), to extend functionality._ | ||
_Easily define [custom blocks](/docs/blocks.md), via the [API](/docs/api.md), to extend functionality._ | ||
@@ -47,63 +47,91 @@ ## Install | ||
```js | ||
// TODO | ||
``` | ||
> Visit the [`/examples`](/examples) and [Syntax Cheatsheet](/docs/syntax.md) for more info! | ||
## Syntax | ||
***example.hbs*** | ||
TODO | ||
```hbs | ||
{{! expected props to receive }} | ||
{{#expect title, items }} | ||
## API | ||
{{! inline variables }} | ||
{{#var count = items.length }} | ||
{{#var suffix = count === 1 ? 'task' : 'tasks' }} | ||
> **Note:** All methods share common [Options](#options). | ||
{{#if count == 0}} | ||
<p>You're done! 🎉</p> | ||
{{#else}} | ||
<p>You have {{{ count }}} {{{ suffix }}} remaining!</p> | ||
### tempura.compile(input, options?) | ||
Returns: `(data: object) => string` | ||
{{#if count == 1}} | ||
<small>Almost there!</small> | ||
{{#elif count > 10}} | ||
<small>... you must be <em>fried</em> 😔</small> | ||
{{#else}} | ||
<small>You've got this 💪🏼</small> | ||
{{/if}} | ||
Convert a template into an executable `function` equivalent. | ||
<ul> | ||
{{#each items as todo}} | ||
<li>{{ todo.text }}</li> | ||
{{/each}} | ||
</ul> | ||
{{/if}} | ||
``` | ||
This function will produce a `string` output based on the data you provide. The `data` key names should match the `#expect`ed variable names. | ||
***render.js*** | ||
#### input | ||
Type: `string` | ||
```js | ||
import { readFile } from 'fs/promises'; | ||
import { transform, compile } from 'tempura'; | ||
The template to be converted. | ||
const template = await readFile('example.hbs', 'utf8'); | ||
#### options.escape | ||
Type: `(value: any) => string` | ||
// Transform the template into a function | ||
// NOTE: Produces a string; ideal for build/bundlers | ||
// --- | ||
todo | ||
let toESM = transform(template); | ||
console.log(typeof toESM); //=> "string" | ||
console.log(toESM); | ||
//=> `import{esc as $$1}from"tempura";export default function($$3,$$2){...}` | ||
### tempura.transform(input, options?) | ||
Returns: `string` | ||
let toCJS = transform(template, { format: 'cjs' }); | ||
console.log(typeof toCJS); //=> "string" | ||
console.log(toCJS); | ||
//=> `var $$1=require("tempura").esc;module.exports=function($$3,$$2){...}` | ||
Transforms the `input` source template into JavaScript code. Unlike `tempura.compile()`, this generates a `string` instead of a `function`, which means that this method is likely only suitable for generating and/or replacing code at build-time. | ||
#### input | ||
Type: `string` | ||
// Convert the template into a live function | ||
// NOTE: Produces a `Function`; ideal for runtime/servers | ||
// --- | ||
The template to be converted. | ||
let render = compile(template); | ||
console.log(typeof render); //=> "function" | ||
render({ | ||
title: 'Reminders', | ||
items: [ | ||
{ id: 1, text: 'Feed the doggo' }, | ||
{ id: 2, text: 'Buy groceries' }, | ||
{ id: 3, text: 'Exercise, ok' }, | ||
] | ||
}); | ||
//=> "<p>You have 3 tasks remaining!</p>\n" | ||
//=> + "<small>You've got this 💪🏼</small>\n\n" | ||
//=> + "<ul>\n" | ||
//=> + " <li>Feed the doggo</li>\n" | ||
//=> + " <li>Buy groceries</li>\n" | ||
//=> + " <li>Exercise, ok</li>\n" | ||
//=> + "</ul>" | ||
``` | ||
#### options.format | ||
Type: `"esm"` or `"cjs"`<br> | ||
Default: `"esm"` | ||
## Syntax | ||
Modify the generated output to be compliant with the CommonJS or ES Module format. | ||
Please refer to the [Syntax Cheatsheet](/docs/syntax.md). | ||
> **Note:** Most bundlers and/or upstream consumers can understand (and prefer) the ESM format. | ||
## Options | ||
## API | ||
#### options.props | ||
Type: `string[]` | ||
Visit the [API](/docs/api.md) and [Custom Blocks](/docs/blocks.md) for documentation. | ||
The _names_ of variables that will be provided to a view. | ||
> **NOTE:** Declaring `options.props` names _could_ take the place of `{{#expect ...}}` declarataions – and vice versa. In other words, `options.props` is a programmatic way to define (or skip) `{{#expect}}` blocks. Declaring a variable name in both locations has no effect. | ||
#### options.blocks | ||
Type: `{ ... }` | ||
TODO: extra actions/blocks | ||
## Benchmarks | ||
@@ -115,3 +143,3 @@ | ||
The following is a subset of the full results, presented without context. Again, please visit [`/bench`](/bench) for explanations and/or comparisons. | ||
The following is a subset of the full results, presented without context. Again, please visit [`/bench`](/bench) for explanations, comparisons, and/or differences. | ||
@@ -118,0 +146,0 @@ ``` |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
15334
259
162
0