Skip to content

Commit

Permalink
feat: add ESM browser build
Browse files Browse the repository at this point in the history
  • Loading branch information
nebrelbug committed May 29, 2023
1 parent a522489 commit 5269d79
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
4 changes: 2 additions & 2 deletions browser-tests/benchmark.html
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@
<script src="https://unpkg.com/dot@1.1.3/doT.min.js"></script>
<script src="https://unpkg.com/ejs@3.1.8/ejs.min.js"></script>
<script src="https://unpkg.com/handlebars@4.7.7/dist/handlebars.min.js"></script>
<!-- This is a fallback, since ../dist/browser/eta.min.js is not uploaded to GitHub -->
<!-- This is a fallback, since ../dist/ is not uploaded to GitHub -->
<script src="https://unpkg.com/eta"></script>
<script src="../dist/browser.min.umd.js"></script>
<script src="../dist/browser.umd.js"></script>
<script src="https://unpkg.com/squirrelly@9.0.0"></script>
<script src="https://unpkg.com/mustache@4.2.0/mustache.min.js"></script>
<script src="https://pugjs.org/js/pug.js"></script>
Expand Down
21 changes: 13 additions & 8 deletions browser-tests/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"
/>
<meta name="description" content="Test out Eta templates in the browser here" />
<script src="../dist/browser.min.umd.js"></script>
<style>
body {
background-color: #f4f4f4;
Expand Down Expand Up @@ -77,6 +76,7 @@
overflow-y: auto;
}

#function,
#result {
white-space: pre;
}
Expand Down Expand Up @@ -199,10 +199,15 @@ <h4>Result</h4>
<div id="result"></div>
</div>
</div>
<script>
/* global eta */
<script type="module">
import { Eta } from "../dist/browser.module.mjs";

// TODO: add fallback using unpkg

var eta = new Eta();

window.onload = function () {
eta.templates.define("mypartial", eta.compile("This is a partial"));
eta.loadTemplate("mypartial", eta.compile("This is a partial"));

function escape(str) {
// To handle escaping for the function result
Expand Down Expand Up @@ -234,14 +239,14 @@ <h4>Result</h4>
try {
var functionResult = eta.compile(template).toString();
document.getElementById("function").innerHTML = escape(functionResult);
if (!eta.defaultConfig.async) {
document.getElementById("result").innerHTML = eta.render(template, options);
if (!eta.config.async) {
document.getElementById("result").innerHTML = eta.renderString(template, options);
} else {
eta.render(template, options).then(function (res) {
eta.renderStringAsync(template, options).then(function (res) {
document.getElementById("result").innerHTML = res;
});
}
console.log(eta.render(template, options));
console.log(eta.renderString(template, options));
} catch (ex) {
console.error(ex.message);
}
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
"type": "commonjs",
"main": "./dist/eta.umd.js",
"umd:main": "./dist/eta.umd.js",
"unpkg": "./dist/browser.min.umd.js",
"unpkg": "./dist/browser.umd.js",
"module": "./dist/eta.module.mjs",
"types": "./dist/types/index.d.ts",
"source": "src/index.ts",
"exports": {
"types": "./dist/types/index.d.ts",
"browser": "./dist/browser.min.umd.js",
"browser": "./dist/browser.umd.js",
"require": "./dist/eta.umd.js",
"import": "./dist/eta.module.mjs",
"default": "./dist/eta.umd.js"
Expand All @@ -46,7 +46,7 @@
},
"scripts": {
"build:node": "microbundle src/index.ts --target node --format esm,umd",
"build:browser": "microbundle src/browser.ts --target web --format umd --output dist/browser.min.js",
"build:browser": "microbundle src/browser.ts --target web --format umd,modern --output dist/browser.js",
"build": "npm run build:node && npm run build:browser",
"commit": "git-cz",
"format": "prettier --write '{src,test}/**/**.ts'",
Expand All @@ -63,7 +63,7 @@
},
"size-limit": [
{
"path": "dist/browser.min.umd.js",
"path": "dist/browser.umd.js",
"limit": "3 KB"
}
],
Expand Down

0 comments on commit 5269d79

Please sign in to comment.