{"id":4197,"date":"2017-08-24T16:50:22","date_gmt":"2017-08-24T14:50:22","guid":{"rendered":"https:\/\/florianbrinkmann.com\/en\/?p=4197"},"modified":"2020-02-09T10:59:45","modified_gmt":"2020-02-09T09:59:45","slug":"webpack-and-babel","status":"publish","type":"post","link":"https:\/\/florianbrinkmann.com\/en\/webpack-and-babel-4197\/","title":{"rendered":"Setting up Webpack and Babel"},"content":{"rendered":"\n<p>Webpack is a bundler with the main purpose of bundling JS files for the usage in browsers (it also can, for example, compile SASS to CSS). This post shows you how to setup Webpack and Babel \u2013 a tool for compiling ES6 JavaScript code to ES5 code.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>After trying the setup for <a href=\"https:\/\/webpack.js.org\/\">Webpack<\/a> and <a href=\"https:\/\/babeljs.io\/\">Babel<\/a> a few days ago without success, I get it working quick today (just needed to follow the <a href=\"https:\/\/webpack.js.org\/guides\/\">various guides from the Webpack docs<\/a> \ud83d\ude00 ). This tutorial assumes that you already have a package.json inside the project.<\/p>\n\n\n\n<p>To install Webpack, we run the following code:<\/p>\n\n\n<pre class=\"wp-block-code lang-bash\"><span><code class=\"hljs\">npm install --save-dev webpack<\/code><\/span><\/pre>\n\n\n<p>For the Babel installation, we need the following command:<\/p>\n\n\n<pre class=\"wp-block-code lang-bash\"><span><code class=\"hljs\">npm install --save-dev babel-loader babel-core babel-preset-env<\/code><\/span><\/pre>\n\n\n<p>Now we have to create a <code class=\"lang-markup\">webpack.config.js<\/code> to configure Webpack. This file looks like that in our example:<\/p>\n\n\n<pre class=\"wp-block-code lang-js\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">const<\/span> path = <span class=\"hljs-built_in\">require<\/span>(<span class=\"hljs-string\">'path'<\/span>);\n\n<span class=\"hljs-built_in\">module<\/span>.exports = {\n\t<span class=\"hljs-attr\">entry<\/span>: <span class=\"hljs-string\">'.\/assets\/js\/src\/functions.js'<\/span>,\n\t<span class=\"hljs-attr\">output<\/span>: {\n\t\t<span class=\"hljs-attr\">filename<\/span>: <span class=\"hljs-string\">'bundle.js'<\/span>,\n\t\t<span class=\"hljs-attr\">path<\/span>: path.resolve(__dirname, <span class=\"hljs-string\">'assets\/js'<\/span>)\n\t},\n\t<span class=\"hljs-attr\">module<\/span>: {\n\t\t<span class=\"hljs-attr\">rules<\/span>: &#91;\n\t\t\t{\n\t\t\t\t<span class=\"hljs-attr\">test<\/span>: <span class=\"hljs-regexp\">\/\\.js$\/<\/span>,\n\t\t\t\t<span class=\"hljs-attr\">exclude<\/span>: <span class=\"hljs-regexp\">\/node_modules\/<\/span>,\n\t\t\t\t<span class=\"hljs-attr\">use<\/span>: {\n\t\t\t\t\t<span class=\"hljs-attr\">loader<\/span>: <span class=\"hljs-string\">'babel-loader'<\/span>,\n\t\t\t\t\t<span class=\"hljs-attr\">options<\/span>: {\n\t\t\t\t\t\t<span class=\"hljs-attr\">presets<\/span>: &#91;<span class=\"hljs-string\">'env'<\/span>]\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t]\n\t}\n};<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>At first, we define the <code class=\"lang-js\">entry<\/code> file, which should be handled by Webpack, and with <code class=\"lang-js\">output<\/code> the output file. That was the basic configuration of Webpack \u2013 the <code class=\"lang-js\">module<\/code> part is Babel related.<\/p>\n\n\n\n<p>With <code class=\"lang-js\">module.rules<\/code> we set Babel as a <a href=\"https:\/\/webpack.js.org\/concepts\/loaders\/\">Webpack loader<\/a>. It should match for all JS files excluding the <code class=\"lang-markup\">node_modules<\/code> directory. We use the just installed <code class=\"lang-js\">babel-loader<\/code> as a loader with the <code class=\"lang-js\">env<\/code> <a href=\"https:\/\/babeljs.io\/docs\/plugins\/#presets\">preset<\/a>.<\/p>\n\n\n\n<p>To run Webpack easier, we do not use its CLI but NPM scripts. The related part of my <code class=\"lang-markup\">package.json<\/code> looks like that:<\/p>\n\n\n<pre class=\"wp-block-code lang-bash\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-string\">\"scripts\"<\/span>: {\n\t<span class=\"hljs-string\">\"watch\"<\/span>: <span class=\"hljs-string\">\"webpack --watch\"<\/span>,\n\t<span class=\"hljs-string\">\"production\"<\/span>: <span class=\"hljs-string\">\"webpack -p\"<\/span>,\n\t<span class=\"hljs-string\">\"build\"<\/span>: <span class=\"hljs-string\">\"webpack\"<\/span>\n},<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>With <code class=\"lang-bash\">npm run watch<\/code>, we can now run the <code class=\"lang-bash\">webpack --watch<\/code> command, which will start Webpack and let it watch files for changes. The <code class=\"lang-bash\">webpack -p<\/code> command is for <a href=\"https:\/\/webpack.js.org\/guides\/production\/\">production<\/a> \u2013 it minifies the output, runs the <a href=\"https:\/\/webpack.js.org\/plugins\/loader-options-plugin\/\">LoaderOptionsPlugin<\/a> (which will be removed in a later version of Webpack) and sets the NodeJS environment variable, so that certain packages can compile differently.<\/p>\n\n\n\n<p>Do you use Webpack? If so, for what?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Webpack is a bundler with the main purpose of bundling JS files for the usage in browsers (it also can, for example, compile SASS to CSS). This post shows you how to setup Webpack and Babel \u2013 a tool for compiling ES6 JavaScript code to ES5 code.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"wpf_show_in_dewp_planet_feed":false,"flobn_post_versions":"","webmentions_disabled_pings":false,"webmentions_disabled":false,"lazy_load_responsive_images_disabled":false,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-4197","post","type-post","status-publish","format-standard","hentry","category-web-development"],"wp-worthy-pixel":{"ignored":false,"public":"5b67b39b3d6443c0aadfc97a33abbf39","server":"vg07.met.vgwort.de","url":"https:\/\/vg07.met.vgwort.de\/na\/5b67b39b3d6443c0aadfc97a33abbf39"},"wp-worthy-type":"normal","_links":{"self":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/4197","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/comments?post=4197"}],"version-history":[{"count":3,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/4197\/revisions"}],"predecessor-version":[{"id":5857,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/4197\/revisions\/5857"}],"wp:attachment":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/media?parent=4197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/categories?post=4197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/tags?post=4197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}