{"id":342,"date":"2016-07-25T06:58:56","date_gmt":"2016-07-25T06:58:56","guid":{"rendered":"http:\/\/fluentreports.com\/blog\/?p=342"},"modified":"2016-08-08T18:46:24","modified_gmt":"2016-08-08T18:46:24","slug":"nativescript-webpacking","status":"publish","type":"post","link":"http:\/\/fluentreports.com\/blog\/?p=342","title":{"rendered":"NativeScript: WebPacking"},"content":{"rendered":"<p>The NativeScript team released a cool plugin using the \"WebPack\" code to packup the entire code base and since it does things like tree shaking; your end result is a much smaller app.<\/p>\n<p>Well I had an opportunity to test it over the last couple weeks for a friend of mine (Nathan Walker) as he is prepping to release a very very cool app.<\/p>\n<p>However, the NS-Webpack plugin is unfortunately very (very) buggy; and odds are very (very) likely if you are doing anything beyond a very simple application; it will not work.\u00a0\u00a0 You can see a stack of bug reports in the <a href=\"https:\/\/github.com\/NativeScript\/NativeScript-Dev-WebPack\/issues\" target=\"_blank\">repo <\/a>for all of these issues I mentioned and some others; so hopefully these issues will soon be just a distant memory, and it will work as intended.<\/p>\n<p>So many days later; and after much debugging, we can declare that we have smacked and cajoled webpack into shape and we were able to get it to actually webpack an application properly!<\/p>\n<p>So here is the things you need to do if you want to webpack something while it is in its current state...<\/p>\n<h2>Fonts<\/h2>\n<p>The first thing you need to be aware of; is that on iOS if you are using any custom fonts; the automatic font registration is totally broken when webpack'ed.\u00a0 The solution is to fallback to what we used to have to do on iOS; which is manually register the font.\u00a0\u00a0 The code I wrote for this project looks like this:<br \/>\n<pre>if (global.NSObject &amp;amp;&amp;amp; global.NSString) {\n\u00a0 var font = require(&#039;ui\/styling\/font&#039;);\n\u00a0 font.ios.registerFont(&#039;fontawesome-webfont.ttf&#039;);\n}<\/pre><br \/>\nBasically the if statement tests for iOS, then I manually require the font class; and finally the last line; I registered the font I needed.\u00a0 You can repeat the last line for each font you are using.<\/p>\n<h2>Pathing Issues<\/h2>\n<p>The second issue is only if you use your screens\/components in a subfolder type system the url pathing to find them breaks under webpack code.\u00a0 So when you have something like this:<br \/>\n<pre>\/app\/components\/blah\/\n-blah.js\n-blah.html\n-blah.css<\/pre><br \/>\nand inside your blah.js you have code like this:<br \/>\n<pre>Blah = _decorate([\n...\ntemplateUrl: &#039;blah.html&#039;\n...<\/pre><br \/>\nThis will fail; ALL the JS is actually bundled into the webpack'd bundle file; so when it goes to try to find this file, the JS code is running from the root directory of your app.\u00a0 Where your actual html file is not in the root.\u00a0 So you have to change the path to be:<br \/>\n<pre>templateUrl: &#039;.\/components\/blah\/blah.html&#039;<\/pre><br \/>\nPretty simple fix if you know about it; but man if you don't it starts crashing...<\/p>\n<h2>Crashing on Startup<\/h2>\n<p>The final issue is simple and complex at the same time.\u00a0 NativeScript iOS runtime has some built in short-circuits for __extends and ALL the Native objects (i.e. like NSObject, NSString...) -- well Webpacking unfortunately changes the signature of the __extends function and so the runtime doesn't detect that the function is actually a __extends...\u00a0 This causes the runtime to run code that isn't actually supported and so it, well, crashes...\u00a0\u00a0 This can be solved in one of two ways;<\/p>\n<ol>\n<li>You edit each of the plugins that are installed and manually strip out the __extends function.\u00a0 (and then let the plugin author's know to disable creating the __extends in their plugin as this code normally is just pointless and a waste of space and memory, and in the webpack case it is now deadly -- So just add \"NoEmitHelpers: true\" to the tsconfig file).<\/li>\n<li>You add a simple plugin that I wrote to your main project directory; this plugin will automatically strip out all __extends while it is webpacks so that it doesn't matter if the plugin author forgot to set that flag; the simple plugin eliminate it.<\/li>\n<\/ol>\n<p><pre>\/**********************************************************************************\n * (c) 2016, Master Technology\n * Licensed under the MIT license or contact me for a Support or Commercial License\n *\n * I do contract work in most languages, so let me solve your problems!\n *\n * Any questions please feel free to email me\n * Version 1.0.2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Nathan@master-technology.com\n *********************************************************************************\/\n&quot;use strict&quot;;\n\nfunction eliminateExtends(pre) {\n&nbsp;&nbsp;&nbsp;&nbsp;var i = pre.indexOf(&#039;var __extends = &#039;);\n&nbsp;&nbsp;&nbsp;&nbsp;if (i === -1) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return pre;\n&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;var x = pre.indexOf(&#039;};&#039;,i);\n&nbsp;&nbsp;&nbsp;&nbsp;return pre.substring(0,i) + pre.substring(x+2);\n}\nmodule.exports = eliminateExtends;<\/pre><br \/>\nYou copy this code into an eliminateextends.js file in your root folder of the project; and modify your apps webpack's config file to use this as a loader plugin.<\/p>\n<p>Please note this is not all the bugs in webpack; there are a couple others on the <a href=\"https:\/\/github.com\/NativeScript\/NativeScript-Dev-WebPack\/issues\" target=\"_blank\">https:\/\/github.com\/NativeScript\/NativeScript-Dev-WebPack\/issues<\/a> repo that we didn't run into; but these were the only ones that we ran into for a iOS NG2 project.\u00a0\u00a0 So make sure you check out the bug list if you are still having issues...<\/p>\n<p>Happy NativeScript-WebPacking!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The NativeScript team released a cool plugin using the \"WebPack\" code to packup the entire code base and since it does things like tree shaking; your end result is a much smaller app. Well I had an opportunity to test it over the last couple weeks for a friend of mine (Nathan Walker) as he&hellip; <a class=\"more-link\" href=\"http:\/\/fluentreports.com\/blog\/?p=342\">Continue reading <span class=\"screen-reader-text\">NativeScript: WebPacking<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[15,7],"tags":[16,75],"class_list":["post-342","post","type-post","status-publish","format-standard","hentry","category-nativescript","category-performance","tag-nativescript","tag-webpack","entry"],"_links":{"self":[{"href":"http:\/\/fluentreports.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/342","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/fluentreports.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/fluentreports.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/fluentreports.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/fluentreports.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=342"}],"version-history":[{"count":3,"href":"http:\/\/fluentreports.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/342\/revisions"}],"predecessor-version":[{"id":352,"href":"http:\/\/fluentreports.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/342\/revisions\/352"}],"wp:attachment":[{"href":"http:\/\/fluentreports.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/fluentreports.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=342"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/fluentreports.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}