{"id":1720,"date":"2018-08-19T14:25:12","date_gmt":"2018-08-19T18:25:12","guid":{"rendered":"https:\/\/www.a2life.info\/w\/?p=1720"},"modified":"2018-08-21T15:09:40","modified_gmt":"2018-08-21T19:09:40","slug":"using-vue-with-webpack","status":"publish","type":"post","link":"https:\/\/www.a2life.info\/w\/?p=1720","title":{"rendered":"Using Vue with WebPack"},"content":{"rendered":"\r\n<p>I wanted to transfer my existing script to WebPack bundle.\u00a0 The application is a simple page that uses\u00a0 Vue.js.\u00a0 \u00a0Note that I only read Introduction part of Vue and started using it.\u00a0 At that time, I did not even know the concept of Vue component files .\u00a0 Thus my application was entirely in one JavaScript file.<\/p>\r\n\r\n\r\n\r\n<p>I rewrote it using typescript.\u00a0 \u00a0WebPack bundling was necessary to make it work.<\/p>\r\n\r\n\r\n\r\n<p>I had to decipher minor (but important) detail on NPM&#8217;s Vue\u00a0 module that should be used along with WebPack.\u00a0<\/p>\r\n\r\n\r\n\r\n<p>If I were to use a Vue package from node_modules\/ and bundle it with WebPack, then the default module that will be used is Runtime-only ES module build (vue.runtime.esm.js.)<\/p>\r\n\r\n\r\n\r\n<p>Now this verbiage is coming from <a href=\"http:\/\/vuejs.org\/\">VueJs.org<\/a>\u00a0V2 guide<br \/><a href=\"https:\/\/vuejs.org\/v2\/guide\/installation.html#Terms\">https:\/\/vuejs.org\/v2\/guide\/installation.html#Terms<\/a><\/p>\r\n\r\n\r\n\r\n<p><br \/>&#8221; If you need to compile templates on the client (e.g. passing a string to the\u00a0template\u00a0option, or mounting to an element using its in-DOM HTML as the template), you will need the compiler and thus the full build:&#8221;<\/p>\r\n\r\n\r\n\r\n<p>This applies to &#8216;getting started example&#8217; code where\u00a0 you use template directly on target html file, such as\u00a0<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">&lt;div id=\"app\"&gt; {{message}} &lt;\/div&gt;<\/pre>\r\n\r\n\r\n\r\n<p>According to Vue documentation, runtime only module is 30% lighter and when I write my code in such a way that it utilize component file (*.vue), then\u00a0 using vue-loader will precompile all the templates into render function (for those within component file)\u00a0 so the default settings wil be not an issue.\u00a0 \u00a0Issue is when I create a new instance\u00a0 of the view .\u00a0<br \/>Again, according to the guide<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">\/\/ this requires the compiler\r\nnew Vue({\r\ntemplate: '{{ hi }}'\r\n})\r\n\r\n\/\/ this does not\r\nnew Vue({\r\nrender (h) {\r\nreturn h('div', this.hi)\r\n}\r\n})<\/pre>\r\n\r\n\r\n\r\n<p><br \/>If I use the first method but did not configure webpack to use full version of Vue module, resulting bundle file\u00a0 is missing template compiler and I will be staring at the blank page.\u00a0 In this situation,\u00a0 looking into\u00a0 source with F12 looks something like this<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">&lt;body&gt;&lt;!-- function(a,b,c,d) {.......} --&gt;&lt;\/body&gt;<\/pre>\r\n\r\n\r\n\r\n<p><br \/>Then how do I configure Webpack\u00a0 so that it knows to bundle the full version of vue module instead of runtime-only module?\u00a0 It was spelled right in the guide above.<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">module.exports = {\r\n          \/\/ ...\r\n          resolve: \r\n                {\r\n                   \/\/.....,\r\n               alias: {'vue$': 'vue\/dist\/vue.esm.js'\u00a0}\r\n               \/\/.......\r\n      }\r\n<\/pre>\r\n\r\n\r\n\r\n<p>I needed to add\u00a0 &#8216;alias&#8217; clause to\u00a0 the &#8216;resolve&#8217; section of module.exports.\u00a0 (for webpack v2 and above)<br \/>WebPack will then bundle full version of Vue module(vue.esm.js).\u00a0<\/p>\r\n\r\n\r\n\r\n<p>Since I didn&#8217;t use .vue files for this project and put everything in a single html file, I did not\u00a0 need to use &#8216;vue-loader&#8217; either.<br \/>So this is a minimal requirement for Vue to work with WebPack.<\/p>\r\n\r\n\r\n\r\n<p>Vue.js has a very detailed documentation which is good. All I needed was to read it through and understand the meaning of it (lol).\u00a0\u00a0<\/p>\r\n\r\n\r\n\r\n<p>By the way, another way to make it work is not tell WebPack about Vue at all.<\/p>\r\n\r\n\r\n\r\n<ol class=\"wp-block-list\">\r\n<li>add script tag in your HTML writeup pointing to Vue CDN<\/li>\r\n<\/ol>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">&lt;script src=\"<a href=\"https:\/\/cdn.jsdelivr.net\/npm\/vue\/dist\/vue.js\">https:\/\/cdn.jsdelivr.net\/npm\/vue\/dist\/vue.js<\/a>\"&gt;&lt;\/script&gt;<\/pre>\r\n\r\n\r\n\r\n<p><br \/>\u00a0 \u00a0 2. Then tell Typescript\u00a0 that &#8216;Vue&#8217; is a global object<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">declare let Vue:any<\/pre>\r\n\r\n\r\n\r\n<p>Yes, use the bundler but not bundle the module. It sounds strange but it works<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>I wanted to transfer my existing script to WebPack bundle.\u00a0 The application is a simple page that uses\u00a0 Vue.js.\u00a0 \u00a0Note that I only read Introduction part of Vue and started using it.\u00a0 At that time, I did not even know the concept of Vue component files .\u00a0 Thus my application was entirely in one JavaScript file. I rewrote it using typescript.\u00a0 \u00a0WebPack bundling was necessary to make it work. I had to decipher minor (but important) detail on NPM&#8217;s Vue\u00a0 module that should be used along with WebPack.\u00a0 If I were to use a Vue package from node_modules\/ and bundle it with WebPack, then the default module that will be &hellip; <a href=\"https:\/\/www.a2life.info\/w\/?p=1720\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[38,61,62],"class_list":["post-1720","post","type-post","status-publish","format-standard","hentry","category-programming","tag-javascript","tag-typescript","tag-vuejs"],"_links":{"self":[{"href":"https:\/\/www.a2life.info\/w\/index.php?rest_route=\/wp\/v2\/posts\/1720","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.a2life.info\/w\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.a2life.info\/w\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.a2life.info\/w\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.a2life.info\/w\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1720"}],"version-history":[{"count":3,"href":"https:\/\/www.a2life.info\/w\/index.php?rest_route=\/wp\/v2\/posts\/1720\/revisions"}],"predecessor-version":[{"id":1724,"href":"https:\/\/www.a2life.info\/w\/index.php?rest_route=\/wp\/v2\/posts\/1720\/revisions\/1724"}],"wp:attachment":[{"href":"https:\/\/www.a2life.info\/w\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.a2life.info\/w\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1720"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.a2life.info\/w\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}