{"id":7268,"date":"2019-07-07T19:32:34","date_gmt":"2019-07-07T19:32:34","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=7268"},"modified":"2019-07-07T19:32:34","modified_gmt":"2019-07-07T19:32:34","slug":"loading-javascript-objects-from-a-string","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/loading-javascript-objects-from-a-string\/","title":{"rendered":"Loading JavaScript objects from a string"},"content":{"rendered":"<p>As mentioned in a previous post &#8211; I&#8217;ve been working on dynamically loading TypeScript\/JavaScript as required (i.e. like a runtime plugin system) and whilst investigating the process have come across several libraries that I felt I&#8217;d document in case I need them in the future. First up is <a href=\"https:\/\/github.com\/floatdrop\/require-from-string\" rel=\"noopener noreferrer\" target=\"_blank\">require-from-string<\/a>.<\/p>\n<p>I&#8217;ll assume for this and any follow up posts, that you&#8217;ve created a basic set-up, i.e.<\/p>\n<ul>\n<li>yarn init &#8211;typescript<\/li>\n<li>tsc &#8211;init<\/li>\n<\/ul>\n<p>Although I&#8217;ve added TypeScript in the above, for simplicity we&#8217;re going to start out just using JavaScript, but we&#8217;ve covered all bases with TypeScript&#8217;s inclusion.<\/p>\n<p>Next create an index.js that looks like this<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport requireFromString from 'require-from-string';\r\n\r\nconst classA = requireFromString('class A { output() { console.log(&quot;Remote class called&quot;) } } module.exports = A;');\r\nnew classA().output();\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>As mentioned in a previous post &#8211; I&#8217;ve been working on dynamically loading TypeScript\/JavaScript as required (i.e. like a runtime plugin system) and whilst investigating the process have come across several libraries that I felt I&#8217;d document in case I need them in the future. First up is require-from-string. I&#8217;ll assume for this and any [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[45],"tags":[],"class_list":["post-7268","post","type-post","status-publish","format-standard","hentry","category-javascript"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7268","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=7268"}],"version-history":[{"count":2,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7268\/revisions"}],"predecessor-version":[{"id":7293,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7268\/revisions\/7293"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=7268"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=7268"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=7268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}