I have a code generator that I created, and I would like to have the ability to run it on the server. The generator uses pure native ECMA6 javascript to render HTML markup, but is transpiled to ES5 before runtime via Babel and WebPack.
I wish to leverage this on NodeJS, but without an entire rewrite if possible.
I have used Node with Express + EJS/Pug/HTML/JSX and some other frameworks, however, there is always middleware involved.
Here is the code I tried (runtime/transpiled version @ ES5):
var fs = require('fs');
var _typeof = typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol' ? function (obj) {
return typeof obj;
} : function (obj) {
return obj && typeof Symbol === 'function' && obj.constructor === Symbol ? 'symbol' : typeof obj;
};
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError('Cannot call a class as a function');
}
}
var JSML = function () {
'use strict';
function JSML(stdIn) {
_classCallCheck(this, JSML);
this.output = '';
this.parse(stdIn);
return this.output;
}
JSML.prototype.generateAttributeKeyValueString = function generateAttributeKeyValueString(key, value) {
return key + '=\'' + value + '\'';
};
JSML.prototype.camelCaseToDashes = function camelCaseToDashes(str) {
return str.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
};
JSML.prototype.generateDom = function generateDom(vNode) {
var self = this, selfClosingTagNames = [
'area',
'base',
'br',
'col',
'command',
'embed',
'hr',
'img',
'input',
'keygen',
'link',
'meta',
'param',
'source',
'track',
'wbr'
], elmStart = '<' + vNode.elm, elmAttrs = '', elmEnd, elmContent;
selfClosingTagNames.forEach(function (selfClosingTagName) {
if (vNode.elm === selfClosingTagName)
elmEnd = '';
else
elmEnd = '</' + vNode.elm + '>';
});
function parseInput(vNode, key) {
if (!vNode.hasOwnProperty(key))
return;
var value = vNode[key], isActualInnerValueChildContents = key === 'inner' && typeof value === 'string', isChildrenContentArr = key === 'inner' && Array.isArray(value), isSingleChildContent = key === 'inner' && !isChildrenContentArr && (typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object', isAttributeKeyValuePair = key !== 'elm' && key !== 'inner';
if (isActualInnerValueChildContents)
elmContent = value;
else if (isAttributeKeyValuePair)
elmAttrs += self.generateAttributeKeyValueString(self.camelCaseToDashes(key), value);
else if (isChildrenContentArr) {
elmContent = '';
value.forEach(function (subValue) {
elmContent += JSML.run(subValue).output;
});
} else if (isSingleChildContent)
elmContent = JSML.run(value).output;
}
for (var key in vNode){
if (vNode.hasOwnProperty(key)) parseInput(vNode, key);
}
elmStart += ' ' + elmAttrs + '>';
if (elmContent)
this.output = elmStart + elmContent + elmEnd;
else
this.output = elmStart + elmEnd;
};
JSML.prototype.parse = function parse(input) {
var self = this;
self.generateDom(input);
};
return JSML;
}();
JSML.run = function (appCode, target) {
var defaultTarget = 'body', dom = new JSML(appCode);
document.getElementsByTagName(target || defaultTarget)[0].innerHTML = dom.output;
return dom;
};
fs.writeFile("index2.html", JSML.run({
elm: 'img',
src: 'http://placehold.it/50x50'
}, function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
I keep getting "unexpected token ;" in generateView.js at like 98
The main idea is I need to run the JavaScript on my API server, generate the markup needed from my custom templating and data-binding platform in order to serve the content generated from my script to the web.
Thank you, I am open to any helpful suggestions.