From ffb3e619c81bca937cf45021c6e5f04fbe3d46e7 Mon Sep 17 00:00:00 2001 From: Simon Weidacher <17548832+sweidac@users.noreply.github.com> Date: Tue, 31 Jan 2023 14:40:16 +0100 Subject: [PATCH] Add rollup to provide distributables. --- dist/peasy.js | 77 +++++++++++++++++++++++++++++++++++++++++++++++ dist/peasy.min.js | 1 + package.json | 7 +++-- rollup.config.js | 18 +++++++++++ 4 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 dist/peasy.js create mode 100644 dist/peasy.min.js create mode 100644 rollup.config.js diff --git a/dist/peasy.js b/dist/peasy.js new file mode 100644 index 0000000..adec1b1 --- /dev/null +++ b/dist/peasy.js @@ -0,0 +1,77 @@ +(function (global, factory) { + typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : + typeof define === 'function' && define.amd ? define(factory) : + (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.html = factory()); +})(this, (function () { 'use strict'; + + const REMOVE_ANY_INDENTATION_REGEX = /[\n\t]| {2,}/g; + + const RENDERER = document.createElement('div'); + + const ID_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ", + ID_LENGTH = 10; + + function _removeIndentation(strings) { + return strings.map(htmlPart => htmlPart.replace(REMOVE_ANY_INDENTATION_REGEX, '')); + } + + function _generateId() { + let id = ""; + + for (let i = 0; i < ID_LENGTH; i++) { + id += ID_ALPHABET.charAt(Math.floor(Math.random() * ID_ALPHABET.length)); + } + + return id; + } + + function _createInterpolationPlaceholders(values) { + const interpolations = { + nodes : [] + }; + + interpolations.values = values.map(value => { + if (value instanceof Node) { + const id = _generateId(); + + interpolations.nodes.push({ + id, + node : value + }); + + return `
`; // placeholder + } else { + return value; + } + }); + + return interpolations; + } + + function html(strings, ...values){ + const interpolations = _createInterpolationPlaceholders(values); + const htmlText = String.raw({ raw : _removeIndentation(strings.raw) }, ...interpolations.values); + + RENDERER.innerHTML = htmlText; + + interpolations.nodes.forEach(({ id, node }) => { + const placeholder = RENDERER.querySelector(`#${id}`); + + placeholder.after(node); + placeholder.remove(); + }); + + const childCount = RENDERER.childElementCount; + + if (childCount === 1) { + return RENDERER.firstChild; + } else if (RENDERER.childElementCount > 1) { + return RENDERER.children; + } else { + return null; + } + } + + return html; + +})); diff --git a/dist/peasy.min.js b/dist/peasy.min.js new file mode 100644 index 0000000..71ddc74 --- /dev/null +++ b/dist/peasy.min.js @@ -0,0 +1 @@ +!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):(e="undefined"!=typeof globalThis?globalThis:e||self).html=n()}(this,(function(){"use strict";const e=/[\n\t]| {2,}/g,n=document.createElement("div"),t="ABCDEFGHIJKLMNOPQRSTUVWXYZ";function o(n){return n.map((n=>n.replace(e,"")))}function r(e){const n={nodes:[]};return n.values=e.map((e=>{if(e instanceof Node){const o=function(){let e="";for(let n=0;n<10;n++)e+=t.charAt(Math.floor(Math.random()*t.length));return e}();return n.nodes.push({id:o,node:e}),``}return e})),n}return function(e,...t){const i=r(t),d=String.raw({raw:o(e.raw)},...i.values);return n.innerHTML=d,i.nodes.forEach((({id:e,node:t})=>{const o=n.querySelector(`#${e}`);o.after(t),o.remove()})),1===n.childElementCount?n.firstChild:n.childElementCount>1?n.children:null}})); diff --git a/package.json b/package.json index 78dda3e..a7616e4 100644 --- a/package.json +++ b/package.json @@ -8,10 +8,13 @@ "private": false, "type": "module", "scripts": { - "test": "yarn node --experimental-vm-modules $(yarn bin jest)" + "test": "yarn node --experimental-vm-modules $(yarn bin jest)", + "package": "yarn rollup -c" }, "devDependencies": { "jest": "^29.3.1", - "jest-environment-jsdom": "^29.3.1" + "jest-environment-jsdom": "^29.3.1", + "rollup": "^3.9.1", + "rollup-plugin-terser": "^7.0.2" } } diff --git a/rollup.config.js b/rollup.config.js new file mode 100644 index 0000000..ef5bc68 --- /dev/null +++ b/rollup.config.js @@ -0,0 +1,18 @@ +import { terser } from "rollup-plugin-terser"; + +export default { + input : 'index.mjs', + output : [ + { + file : 'dist/peasy.js', + name : 'html', + format : 'umd' + }, + { + file : 'dist/peasy.min.js', + name : 'html', + format : 'umd', + plugins : [ terser() ] + } + ] +} \ No newline at end of file