Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion browser-tests/benchmark.html
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
<script src="https://unpkg.com/[email protected]/dist/handlebars.min.js"></script>
<!-- This is a fallback, since ../dist/ is not uploaded to GitHub -->
<script src="https://unpkg.com/eta"></script>
<script type="module" src="../dist/browser.js"></script>
<script type="module" src="../dist/core.js"></script>
<script src="https://unpkg.com/[email protected]"></script>
<script src="https://unpkg.com/[email protected]/mustache.min.js"></script>
<script src="https://pugjs.org/js/pug.js"></script>
Expand Down
4 changes: 2 additions & 2 deletions browser-tests/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ <h4>Template</h4>
<% if (key === 'thirdchild') { %>
<% for (var i = 0, arr = it.obj[key]; i < arr.length; i++) { %>
Salutations! Index: <%= i %>, parent key: <%= key %>

<% } %>
<% } %>
<% } %>
Expand Down Expand Up @@ -200,7 +200,7 @@ <h4>Result</h4>
</div>
</div>
<script type="module">
import { Eta } from "../dist/browser.module.mjs";
import { Eta } from "../dist/core.js";

// TODO: add fallback using unpkg

Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
"import": "./dist/index.js",
"types": "./index.d.ts"
},
"./browser": {
"import": "./dist/browser.js",
"types": "./dist/browser.d.ts"
"./core": {
"import": "./dist/core.js",
"types": "./dist/core.d.ts"
}
},
"types": "./dist/index.d.ts",
"browser": "./dist/browser.js",
"browser": "./dist/core.js",
"sideEffects": false,
"files": [
"dist"
Expand All @@ -50,7 +50,7 @@
},
"size-limit": [
{
"path": "dist/browser.js",
"path": "dist/core.js",
"limit": "3.5 KB"
}
],
Expand Down
3 changes: 0 additions & 3 deletions src/browser.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/compile-string.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Options } from "./config.ts";
import type { Eta } from "./core.ts";
import type { Eta } from "./internal.ts";
import type { AstObject } from "./parse.ts";

/**
Expand Down
2 changes: 1 addition & 1 deletion src/compile.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { EtaConfig, Options } from "./config.ts";
import type { Eta } from "./core.ts";
import { EtaParseError } from "./err.ts";
import type { Eta } from "./internal.ts";

export type TemplateFunction = (
this: Eta,
Expand Down
84 changes: 2 additions & 82 deletions src/core.ts
Original file line number Diff line number Diff line change
@@ -1,83 +1,3 @@
import type { TemplateFunction } from "./compile.ts";
import { compile } from "./compile.ts";
import { compileBody, compileToString } from "./compile-string.ts";
import type { EtaConfig, Options } from "./config.ts";
import { defaultConfig } from "./config.ts";
import { EtaError, RuntimeErr } from "./err.ts";
import { parse } from "./parse.ts";
import {
render,
renderAsync,
renderString,
renderStringAsync,
} from "./render.ts";
import { Cacher } from "./storage.ts";
import { Eta as EtaCore } from "./internal.ts";

export class Eta {
constructor(customConfig?: Partial<EtaConfig>) {
if (customConfig) {
this.config = { ...defaultConfig, ...customConfig };
} else {
this.config = { ...defaultConfig };
}
}

config: EtaConfig;

RuntimeErr = RuntimeErr;

compile = compile;
compileToString = compileToString;
compileBody = compileBody;
parse = parse;
render = render;
renderAsync = renderAsync;
renderString = renderString;
renderStringAsync = renderStringAsync;

filepathCache: Record<string, string> = {};
templatesSync: Cacher<TemplateFunction> = new Cacher<TemplateFunction>({});
templatesAsync: Cacher<TemplateFunction> = new Cacher<TemplateFunction>({});

// resolvePath takes a relative path from the "views" directory
resolvePath:
| null
| ((this: Eta, template: string, options?: Partial<Options>) => string) =
null;
readFile: null | ((this: Eta, path: string) => string) = null;

// METHODS

configure(customConfig: Partial<EtaConfig>) {
this.config = { ...this.config, ...customConfig };
}

withConfig(customConfig: Partial<EtaConfig>): this & { config: EtaConfig } {
return { ...this, config: { ...this.config, ...customConfig } };
}

loadTemplate(
name: string,
template: string | TemplateFunction, // template string or template function
options?: { async: boolean },
): void {
if (typeof template === "string") {
const templates = options?.async
? this.templatesAsync
: this.templatesSync;

templates.define(name, this.compile(template, options));
} else {
let templates = this.templatesSync;

if (template.constructor.name === "AsyncFunction" || options?.async) {
templates = this.templatesAsync;
}

templates.define(name, template);
}
}
}

// for instance checking against thrown errors
export { EtaError };
export class Eta extends EtaCore {}
2 changes: 1 addition & 1 deletion src/file-handling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import * as fs from "node:fs";
import * as path from "node:path";

import type { Options } from "./config.ts";
import type { Eta as EtaCore } from "./core.ts";
import { EtaFileResolutionError } from "./err.ts";
import type { Eta as EtaCore } from "./internal.ts";

export function readFile(this: EtaCore, path: string): string {
let res = "";
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Eta as EtaCore } from "./core.ts";
import { readFile, resolvePath } from "./file-handling.ts";
import { Eta as EtaCore } from "./internal.ts";

export type { EtaConfig, Options } from "./config.ts";
export {
Expand Down
83 changes: 83 additions & 0 deletions src/internal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import type { TemplateFunction } from "./compile.ts";
import { compile } from "./compile.ts";
import { compileBody, compileToString } from "./compile-string.ts";
import type { EtaConfig, Options } from "./config.ts";
import { defaultConfig } from "./config.ts";
import { EtaError, RuntimeErr } from "./err.ts";
import { parse } from "./parse.ts";
import {
render,
renderAsync,
renderString,
renderStringAsync,
} from "./render.ts";
import { Cacher } from "./storage.ts";

export class Eta {
constructor(customConfig?: Partial<EtaConfig>) {
if (customConfig) {
this.config = { ...defaultConfig, ...customConfig };
} else {
this.config = { ...defaultConfig };
}
}

config: EtaConfig;

RuntimeErr = RuntimeErr;

compile = compile;
compileToString = compileToString;
compileBody = compileBody;
parse = parse;
render = render;
renderAsync = renderAsync;
renderString = renderString;
renderStringAsync = renderStringAsync;

filepathCache: Record<string, string> = {};
templatesSync: Cacher<TemplateFunction> = new Cacher<TemplateFunction>({});
templatesAsync: Cacher<TemplateFunction> = new Cacher<TemplateFunction>({});

// resolvePath takes a relative path from the "views" directory
resolvePath:
| null
| ((this: Eta, template: string, options?: Partial<Options>) => string) =
null;
readFile: null | ((this: Eta, path: string) => string) = null;

// METHODS

configure(customConfig: Partial<EtaConfig>) {
this.config = { ...this.config, ...customConfig };
}

withConfig(customConfig: Partial<EtaConfig>): this & { config: EtaConfig } {
return { ...this, config: { ...this.config, ...customConfig } };
}

loadTemplate(
name: string,
template: string | TemplateFunction, // template string or template function
options?: { async: boolean },
): void {
if (typeof template === "string") {
const templates = options?.async
? this.templatesAsync
: this.templatesSync;

templates.define(name, this.compile(template, options));
} else {
let templates = this.templatesSync;

if (template.constructor.name === "AsyncFunction" || options?.async) {
templates = this.templatesAsync;
}

templates.define(name, template);
}
}
}

// for instance checking against thrown errors
export { EtaError };
2 changes: 1 addition & 1 deletion src/parse.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Eta } from "./core.ts";
import { ParseErr } from "./err.ts";
import type { Eta } from "./internal.ts";
import { trimWS } from "./utils.ts";

export type TagType = "r" | "e" | "i" | "";
Expand Down
2 changes: 1 addition & 1 deletion src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import type { TemplateFunction } from "./compile.ts";

/* TYPES */
import type { Options } from "./config.ts";
import type { Eta } from "./core.ts";
import { EtaNameResolutionError } from "./err.ts";
import type { Eta } from "./internal.ts";

/* END TYPES */

Expand Down
2 changes: 1 addition & 1 deletion tsdown.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default defineConfig([
sourcemap: true,
},
{
entry: ["./src/browser.ts"],
entry: ["./src/core.ts"],
platform: "browser",
dts: true,
minify: true,
Expand Down