<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Luca</title>
    <description>The latest articles on DEV Community by Luca (@pldg).</description>
    <link>https://dev.to/pldg</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F76042%2F1b906e05-65e0-4d53-8afc-0a361b217b17.jpeg</url>
      <title>DEV Community: Luca</title>
      <link>https://dev.to/pldg</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pldg"/>
    <language>en</language>
    <item>
      <title>Learn Webpack</title>
      <dc:creator>Luca</dc:creator>
      <pubDate>Tue, 27 Sep 2022 17:39:44 +0000</pubDate>
      <link>https://dev.to/pldg/learn-webpack-2n6i</link>
      <guid>https://dev.to/pldg/learn-webpack-2n6i</guid>
      <description>&lt;p&gt;A collection of notes and examples I've taken while learning how to use &lt;a href="https://webpack.js.org/"&gt;Webpack&lt;/a&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://github.com/pldg/learn-webpack/blob/main/mode/README.md"&gt;Mode&lt;/a&gt;: Enable webpack built-in optimizations for development and production.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/pldg/learn-webpack/blob/main/multiple-configurations/README.md"&gt;Multiple Configurations&lt;/a&gt;: Use &lt;code&gt;--env&lt;/code&gt; flag to create different config based on the environment.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/pldg/learn-webpack/blob/main/bundle/README.md"&gt;Bundle&lt;/a&gt;: Merge multiple javascripts files.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/pldg/learn-webpack/blob/main/separate-runtime/README.md"&gt;Separate Runtime&lt;/a&gt;: Extract webpack runtime logic in a separate file.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/pldg/learn-webpack/blob/main/tree-shaking/README.md"&gt;Tree Shaking&lt;/a&gt;: Remove unused javascript codes from the app.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/pldg/learn-webpack/blob/main/chunks-types/README.md"&gt;Chunks Types&lt;/a&gt;: Explain what a webpack chunk is.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/pldg/learn-webpack/blob/main/dynamic-import/README.md"&gt;Dynamic Import&lt;/a&gt;: Use &lt;code&gt;import()&lt;/code&gt; to load part of an app on demand.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/pldg/learn-webpack/blob/main/code-splitting/README.md"&gt;Code Splitting&lt;/a&gt;: Divide an app into smaller files.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/pldg/learn-webpack/blob/main/loaders-plugins/README.md"&gt;Loaders and Plugins&lt;/a&gt;: Explain what webpack loaders and plugins are.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/pldg/learn-webpack/blob/main/asset-modules/README.md"&gt;Asset Modules&lt;/a&gt;: Import additional type of assets without configuring loaders.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/pldg/learn-webpack/blob/main/babel/README.md"&gt;Babel&lt;/a&gt;: Use &lt;code&gt;babel-loader&lt;/code&gt; to transpile ES2015 code into into a backwards compatible version of javascript.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/pldg/learn-webpack/blob/main/babel-polyfill/README.md"&gt;Babel Polyfill&lt;/a&gt;: Apply pollyfills to provide a backwards compatible version of a javascript feature.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/pldg/learn-webpack/blob/main/html-plugin-template/README.md"&gt;Html Plugin Template&lt;/a&gt;: Generate html files with &lt;code&gt;html-webpack-plugin&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/pldg/learn-webpack/blob/main/load-css/README.md"&gt;Load CSS&lt;/a&gt;: Use &lt;code&gt;css-loader&lt;/code&gt; and &lt;code&gt;style-loader&lt;/code&gt; to parse stylesheets and place them into html page.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/pldg/learn-webpack/blob/main/source-maps/README.md"&gt;Source Maps&lt;/a&gt;: Emit javascript and css source maps.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/pldg/learn-webpack/blob/main/publicpath/README.md"&gt;Public Path&lt;/a&gt;: Specify a base path for all the assets within your applicaion.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/pldg/learn-webpack/blob/main/separate-css/README.md"&gt;Separate CSS&lt;/a&gt;: Use &lt;code&gt;mini-css-extract-plugin&lt;/code&gt; to extract stylesheets into a separate file.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/pldg/learn-webpack/blob/main/remove-unused-css/README.md"&gt;Remove Unused CSS&lt;/a&gt;: Use &lt;code&gt;purgecss&lt;/code&gt; to remove unused part of stylesheets.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/pldg/learn-webpack/blob/main/load-images/README.md"&gt;Load Images&lt;/a&gt;: Use &lt;code&gt;asset/resource&lt;/code&gt; to parse and emit images and &lt;code&gt;asset/source&lt;/code&gt; to parse raw svg files.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/pldg/learn-webpack/blob/main/sass/README.md"&gt;SASS&lt;/a&gt;: Load sass files with &lt;code&gt;sass-loader&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/pldg/learn-webpack/blob/main/postcss/README.md"&gt;PostCSS&lt;/a&gt;: Enable postcss with &lt;code&gt;postcss-loader&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/pldg/learn-webpack/blob/main/minify/README.md"&gt;Minify&lt;/a&gt;: Enable code minification by using &lt;code&gt;mode.production&lt;/code&gt;, &lt;code&gt;css-minimizer-webpack-plugin&lt;/code&gt; and &lt;code&gt;HtmlWebpackPlugin.minify&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/pldg/learn-webpack/blob/main/caching/README.md"&gt;Caching&lt;/a&gt;: Setup client level caching by adding hash to filenames.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/pldg/learn-webpack/blob/main/context-module/README.md"&gt;Context Module&lt;/a&gt;: Run &lt;code&gt;require.context()&lt;/code&gt; at compile time to import assets.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/pldg/learn-webpack/blob/main/debug-webpack/README.md"&gt;Debug Webpack&lt;/a&gt;: Debug webpack configuration using nodejs &lt;code&gt;--inspect&lt;/code&gt; tool.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/pldg/learn-webpack/blob/main/lazy-load-image/README.md"&gt;Lazy Load Image&lt;/a&gt;: Dynamically import an image with &lt;code&gt;import()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/pldg/learn-webpack/blob/main/lazy-load-multiple-images/README.md"&gt;Lazy Load Multiple Images&lt;/a&gt;: Use &lt;code&gt;require.context()&lt;/code&gt; to include all images from a folder and dynamically load them with &lt;code&gt;import()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/pldg/learn-webpack/blob/main/lazy-load-multiple-images-folders/README.md"&gt;Lazy Load Multiple Images Folders&lt;/a&gt;: Dynamically import images inside multiple folders when a button is clicked.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/pldg/learn-webpack/blob/main/composing-configs/README.md"&gt;Composing Configurations&lt;/a&gt;: Organize webpack configs in separate files and merge them with &lt;code&gt;webpack-merge&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/pldg/learn-webpack/blob/main/static-site-generator/README.md"&gt;Static Site Generator&lt;/a&gt;: Create a simple SSG on top of &lt;code&gt;HtmlWebpackPlugin&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>tutorial</category>
      <category>javascript</category>
      <category>webpack</category>
    </item>
    <item>
      <title>Customize shell prompt</title>
      <dc:creator>Luca</dc:creator>
      <pubDate>Fri, 24 May 2019 17:37:15 +0000</pubDate>
      <link>https://dev.to/pldg/customize-shell-prompt-2bpa</link>
      <guid>https://dev.to/pldg/customize-shell-prompt-2bpa</guid>
      <description>&lt;p&gt;Change the appearance of the terminal &lt;a href="https://en.wikibooks.org/wiki/Guide_to_Unix/Explanations/Shell_Prompt"&gt;shell prompt&lt;/a&gt;: customize colors, text formatting, and dynamically display other type of information (including git status).&lt;/p&gt;

&lt;p&gt;We're going to use bash on Ubuntu but most concepts can also be applied in other unix-based systems (e.g. MacOS and Windows Subsystem for Linux).&lt;/p&gt;

&lt;h2&gt;
  
  
  Prompts variables
&lt;/h2&gt;

&lt;p&gt;Bash has four type of prompts controlled by those variables:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;PS1&lt;/code&gt; primary prompt.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;PS2&lt;/code&gt; displayed when you've to type more commands (multi-line commands).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;PS3&lt;/code&gt; displayed when the select command is waiting for input.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;PS4&lt;/code&gt; displayed when debugging Bash scripts.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you &lt;code&gt;echo $PS1&lt;/code&gt; you'll see a bunch of characters sequences:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you enter an interactive shell session, the shell read &lt;code&gt;PS1&lt;/code&gt; and output something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;username@hostname:directory$
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The dollar &lt;code&gt;$&lt;/code&gt; symbol at the end signifies that you're a normal user, for root user it's replaced with the hash &lt;code&gt;#&lt;/code&gt; symbol.&lt;/p&gt;

&lt;p&gt;If you &lt;code&gt;echo $PS2&lt;/code&gt; it'll display only the greater-than sign &lt;code&gt;&amp;gt;&lt;/code&gt; symbol.&lt;/p&gt;

&lt;p&gt;You can see both &lt;code&gt;PS1&lt;/code&gt; and &lt;code&gt;PS2&lt;/code&gt; in the screenshot below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--e2STSGdo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/24630716/54070240-cff76700-425d-11e9-963c-6ad444de4146.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--e2STSGdo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/24630716/54070240-cff76700-425d-11e9-963c-6ad444de4146.png" alt="" width="880" height="125"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;PS3&lt;/code&gt; and &lt;code&gt;PS4&lt;/code&gt; prompts are not very common. In this guide we'll focus on the primary prompt.&lt;/p&gt;

&lt;h2&gt;
  
  
  Change the prompt
&lt;/h2&gt;

&lt;p&gt;To control the output of your primary prompt, edit the &lt;code&gt;PS1&lt;/code&gt; variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;PS1&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'my_prompt: '&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XUe_MVKN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/24630716/54074429-bf151880-4292-11e9-97bf-8a4a2cf98308.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XUe_MVKN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/24630716/54074429-bf151880-4292-11e9-97bf-8a4a2cf98308.png" alt="" width="880" height="125"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Because &lt;code&gt;PS1&lt;/code&gt; was replaced directly in Bash, changes will disappear on shell &lt;code&gt;exit&lt;/code&gt;. Later in this guide we'll learn how to make this changes permanent.&lt;/p&gt;

&lt;h2&gt;
  
  
  Embedding commands
&lt;/h2&gt;

&lt;p&gt;You can run commands inside &lt;code&gt;PS1&lt;/code&gt; variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;PS1&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'$(exit_status=$? ; if test $exit_status -ne 0 ; then echo "(err)" ; fi)my_prompt: '&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If exit code is not equal to &lt;code&gt;0&lt;/code&gt; it'll display &lt;code&gt;(err)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mXCEOHzM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/24630716/54114732-555c5200-43eb-11e9-9e7b-f5cb70b0e96e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mXCEOHzM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/24630716/54114732-555c5200-43eb-11e9-9e7b-f5cb70b0e96e.png" alt="" width="880" height="170"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Backslash-escaped characters
&lt;/h2&gt;

&lt;p&gt;There are some backslash-escaped special characters you can use to dynamically display useful information in the prompt.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;PS1&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'\t \u \h \w \$ '&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;\t&lt;/code&gt; display time.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\u&lt;/code&gt; display user name.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\h&lt;/code&gt; display hostname.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\w&lt;/code&gt; display current working directory.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\$&lt;/code&gt; display dollar &lt;em&gt;$&lt;/em&gt; symbol if you're normal user; display hash &lt;em&gt;#&lt;/em&gt; symbol if you're root user.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lIriI6cr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/24630716/54074447-dce27d80-4292-11e9-9792-800e95b857f6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lIriI6cr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/24630716/54074447-dce27d80-4292-11e9-9792-800e95b857f6.png" alt="" width="880" height="125"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is a complete list of &lt;a href="https://ss64.com/bash/syntax-prompt.html"&gt;backslash-escaped characters&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  ANSI escape sequences
&lt;/h2&gt;

&lt;p&gt;Bash allows the user to call a series of &lt;a href="https://en.wikipedia.org/wiki/ANSI_escape_code"&gt;ANSI escape sequences&lt;/a&gt; to change colors, text formatting, cursor location and other options of the terminal window. Those sequences are a set of non-printable &lt;a href="https://en.wikipedia.org/wiki/ASCII#Control_characters"&gt;control characters&lt;/a&gt; which the shell interprets as commands.&lt;/p&gt;

&lt;p&gt;An ANSI escape sequence always start with an escape character and a left-bracket character, followed by one or more control characters:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ESC[COMMAND
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The escape character &lt;code&gt;ESC&lt;/code&gt; can be written as &lt;code&gt;\033&lt;/code&gt; or &lt;code&gt;\e&lt;/code&gt; or &lt;code&gt;\x1b&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;COMMAND&lt;/code&gt; is the control character.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;See the &lt;a href="https://bluesock.org/~willkg/dev/ansi.html#sequences"&gt;list of ANSI sequences&lt;/a&gt; for all available commands (some terminals may have partial support for ANSI sequences).&lt;/p&gt;

&lt;h2&gt;
  
  
  Colors and text formatting
&lt;/h2&gt;

&lt;p&gt;To colorize the output of your text terminal use the following ANSI sequence:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ESC[CODEm
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Where &lt;code&gt;CODE&lt;/code&gt; is a series of one or more semicolon-separated color codes.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\0&lt;/span&gt;&lt;span class="s2"&gt;33[44mHello World&lt;/span&gt;&lt;span class="se"&gt;\0&lt;/span&gt;&lt;span class="s2"&gt;33[0m"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-e&lt;/code&gt; enable &lt;code&gt;echo&lt;/code&gt; to parse escape sequences.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\033[&lt;/code&gt; mark the start of an ANSI sequence.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;44&lt;/code&gt; is the code for background color blue.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;m&lt;/code&gt; mark the end of color codes.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;0&lt;/code&gt; removes all text attributes (formatting and colors). It's important to reset attributes, otherwise the styles will be applied to all texts after &lt;em&gt;Hello World&lt;/em&gt; (including the prompt and the text we type).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KGj8brWX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/24630716/54041392-e7841080-41c7-11e9-8d1c-52ac7bc0e168.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KGj8brWX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/24630716/54041392-e7841080-41c7-11e9-8d1c-52ac7bc0e168.png" alt="" width="880" height="125"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can also modify a color by setting an "attribute" before its base value, separated by a &lt;code&gt;;&lt;/code&gt; semi-colon.&lt;/p&gt;

&lt;p&gt;So if you want a green background (&lt;code&gt;46&lt;/code&gt;) with underline text (&lt;code&gt;4&lt;/code&gt;), the sequence is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\0&lt;/span&gt;&lt;span class="s2"&gt;33[4;46mHello World&lt;/span&gt;&lt;span class="se"&gt;\0&lt;/span&gt;&lt;span class="s2"&gt;33[0m"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MvecnW4Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/24630716/54275170-4069f500-458a-11e9-81fc-562e18ebabef.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MvecnW4Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/24630716/54275170-4069f500-458a-11e9-81fc-562e18ebabef.png" alt="" width="880" height="132"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can combine multiple sequences together:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\0&lt;/span&gt;&lt;span class="s2"&gt;33[41m&lt;/span&gt;&lt;span class="se"&gt;\0&lt;/span&gt;&lt;span class="s2"&gt;33[4m&lt;/span&gt;&lt;span class="se"&gt;\0&lt;/span&gt;&lt;span class="s2"&gt;33[1;33mHello World&lt;/span&gt;&lt;span class="se"&gt;\0&lt;/span&gt;&lt;span class="s2"&gt;33[0m"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iX67Bki6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/24630716/54275994-698b8500-458c-11e9-9177-f966510776b5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iX67Bki6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/24630716/54275994-698b8500-458c-11e9-9177-f966510776b5.png" alt="" width="880" height="131"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here you can find a list of all &lt;a href="https://stackoverflow.com/a/33206814/"&gt;color codes&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Save prompt changes permanently
&lt;/h2&gt;

&lt;p&gt;To make the changes permanent you can modify the default &lt;code&gt;PS1&lt;/code&gt; variable or add a new one at the end of &lt;em&gt;~/.bashrc&lt;/em&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;PS1&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'$(exit_status=$? ; if test $exit_status -ne 0 ; then echo -e "(\[\033[31m\]err\[\033[0m\])${debian_chroot:+($debian_chroot)}" ; else echo ${debian_chroot:+($debian_chroot)} ; fi)\[\033[1;33m\]\u@\h\[\033[0m\]:\[\033[1;36m\]\w\[\033[0m\]\$ '&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Non-printing characters must be surrounded with escaped square brackets &lt;code&gt;\[&lt;/code&gt; (start of non-printing characters) and &lt;code&gt;\]&lt;/code&gt; (end of non-printing characters). For example &lt;code&gt;\[\033[1;33m\]&lt;/code&gt;. Otherwise Bash think they're printing characters and use them to calculate its size (cause the text to wraps badly before it gets to the edge of the terminal).&lt;/p&gt;

&lt;p&gt;This line &lt;code&gt;${debian_chroot:+($debian_chroot)}&lt;/code&gt; display to your prompt an indication of which &lt;a href="https://unix.stackexchange.com/a/3174/"&gt;chroot&lt;/a&gt; you're in.&lt;/p&gt;

&lt;p&gt;You can use &lt;code&gt;source ~/.bashrc&lt;/code&gt; to refresh the changes instead of exit and re-enter the shell.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---FGmzD6a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/24630716/54270455-c2085580-457f-11e9-9e7e-16d9a5ad0b3e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---FGmzD6a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/24630716/54270455-c2085580-457f-11e9-9e7e-16d9a5ad0b3e.png" alt="" width="880" height="134"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Display git repository status
&lt;/h2&gt;

&lt;p&gt;Git provide a script that allows to see repository status in your prompt.&lt;/p&gt;

&lt;p&gt;Download &lt;a href="https://github.com/git/git/blob/8976500cbbb13270398d3b3e07a17b8cc7bff43f/contrib/completion/git-prompt.sh"&gt;git-prompt.sh&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;-o&lt;/span&gt; ~/.git-prompt.sh https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The script comes with a function &lt;code&gt;__git_ps1&lt;/code&gt; that can be used in two ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can call it inside &lt;code&gt;PS1&lt;/code&gt; (this way you can't see colored hints).&lt;/li&gt;
&lt;li&gt;Or you can call it inside &lt;a href="https://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x264.html"&gt;&lt;code&gt;PROMPT_COMMAND&lt;/code&gt;&lt;/a&gt; (with this method you can enable colored hints).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By default &lt;code&gt;__git_ps1&lt;/code&gt; will show only the branch you're in, you can also enable git status through a series of variables whose names start with &lt;code&gt;GIT_PS1_SHOW*&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you don't want colored hints you can simply use &lt;code&gt;$(__git_ps1 "(%s)")&lt;/code&gt; inside &lt;code&gt;PS1&lt;/code&gt; and sets &lt;code&gt;GIT_PS1_SHOW*&lt;/code&gt; variables as you prefer.&lt;/p&gt;

&lt;p&gt;To show colored hints you cannot edit &lt;code&gt;PS1&lt;/code&gt; directly. Instead, you have to call &lt;code&gt;__git_ps1&lt;/code&gt; function inside &lt;code&gt;PROMPT_COMMAND&lt;/code&gt; variable. If &lt;code&gt;PROMPT_COMMAND&lt;/code&gt; is set, the value is interpreted as a command to execute before the printing of primary prompt. In this mode you can request colored hints using &lt;code&gt;GIT_PS1_SHOWCOLORHINTS=true&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Add the following code at the end of &lt;em&gt;~/.bashrc&lt;/em&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;source&lt;/span&gt; ~/.git-prompt.sh

&lt;span class="nv"&gt;GIT_PS1_SHOWDIRTYSTATE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"true"&lt;/span&gt;
&lt;span class="nv"&gt;GIT_PS1_SHOWSTASHSTATE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"true"&lt;/span&gt;
&lt;span class="nv"&gt;GIT_PS1_SHOWUNTRACKEDFILES&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"true"&lt;/span&gt;
&lt;span class="nv"&gt;GIT_PS1_SHOWUPSTREAM&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"auto"&lt;/span&gt;
&lt;span class="c"&gt;# Colored hints work only if __git_ps1 is called inside PROMPT_COMMAND&lt;/span&gt;
&lt;span class="nv"&gt;GIT_PS1_SHOWCOLORHINTS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;true

&lt;/span&gt;&lt;span class="nv"&gt;PROMPT_COMMAND&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'__git_ps1 "$(exit_status=$? ; if test $exit_status -ne 0 ; then echo -e "(\[\033[31m\]err\[\033[0m\])${debian_chroot:+($debian_chroot)}" ; else echo ${debian_chroot:+($debian_chroot)} ; fi)\[\033[1;33m\]\u@\h\[\033[0m\]:\[\033[1;36m\]\w\[\033[0m\]" "\$ " "(%s)"'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;source ~/.git-prompt.sh&lt;/code&gt; will load &lt;em&gt;git-prompt.sh&lt;/em&gt; script.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;GIT_PS1_SHOW*&lt;/code&gt; variables are used to add additional features.&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;__git_ps1&lt;/code&gt; is used inside &lt;code&gt;PROMPT_COMMAND&lt;/code&gt; it must be called with at least two arguments, the first is prepended and the second appended to the state string when assigned to &lt;code&gt;PS1&lt;/code&gt;. There is an optional argument used as &lt;a href="https://ss64.com/bash/printf.html"&gt;printf&lt;/a&gt; format string to further customize the output (&lt;code&gt;%s&lt;/code&gt; is the output of &lt;code&gt;__git_ps1&lt;/code&gt; which in this case is wrapped in parenthesis).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_mGWRHYA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/24630716/54270548-ec5a1300-457f-11e9-9dfb-dbe7e58bd569.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_mGWRHYA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/24630716/54270548-ec5a1300-457f-11e9-9dfb-dbe7e58bd569.png" alt="" width="880" height="134"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For more info read comments inside &lt;em&gt;~/.git-prompt.sh&lt;/em&gt; file.&lt;/p&gt;

&lt;h2&gt;
  
  
  External resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.digitalocean.com/community/tutorials/how-to-customize-your-bash-prompt-on-a-linux-vps"&gt;https://www.digitalocean.com/community/tutorials/how-to-customize-your-bash-prompt-on-a-linux-vps&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://misc.flogisoft.com/bash/home"&gt;https://misc.flogisoft.com/bash/home&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://wiki.archlinux.org/index.php/Bash/Prompt_customization"&gt;https://wiki.archlinux.org/index.php/Bash/Prompt_customization&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.freecodecamp.org/how-you-can-style-your-terminal-like-medium-freecodecamp-or-any-way-you-want-f499234d48bc"&gt;https://medium.freecodecamp.org/how-you-can-style-your-terminal-like-medium-freecodecamp-or-any-way-you-want-f499234d48bc&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>linux</category>
      <category>bash</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Lazy load images with Webpack</title>
      <dc:creator>Luca</dc:creator>
      <pubDate>Sat, 15 Sep 2018 13:05:04 +0000</pubDate>
      <link>https://dev.to/pldg/lazy-load-images-with-webpack-5e80</link>
      <guid>https://dev.to/pldg/lazy-load-images-with-webpack-5e80</guid>
      <description>&lt;p&gt;Automatically include all images from a folder with &lt;a href="https://github.com/pldg/learn-webpack/tree/master/require-context"&gt;require.context&lt;/a&gt; and load them only when needed using &lt;a href="https://github.com/pldg/learn-webpack/tree/master/dynamic-import"&gt;dynamic import&lt;/a&gt;. A great way to optimize your site (or application) bandwidth, loading time and caching.&lt;/p&gt;

&lt;h2&gt;
  
  
  Source code
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/pldg/learn-webpack/tree/master/lazy-load-multiple-images"&gt;learn-webpack/lazy-load-multiple-images&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How to
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Lazy import an image
&lt;/h3&gt;

&lt;p&gt;We can import an image as module thanks to &lt;a href="https://github.com/webpack-contrib/file-loader"&gt;file-loader&lt;/a&gt; which parse it and return its resolved path.&lt;/p&gt;

&lt;p&gt;Create a function to dynamic &lt;a href="https://webpack.js.org/guides/code-splitting/#dynamic-imports"&gt;import()&lt;/a&gt; an image, returns its parsed path and set it to &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt; &lt;code&gt;src&lt;/code&gt; attribute.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;lazyLoadImage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;imageName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;img&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="cm"&gt;/* webpackMode: "lazy-once" */&lt;/span&gt;
    &lt;span class="s2"&gt;`./images/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;imageName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;src&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;img&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;src&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;lazyLoadImage&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By default dynamic import create a new chunk for each &lt;code&gt;imageName&lt;/code&gt;, instead we want to create a single chunk for all images. To do so we use &lt;code&gt;/* webpackMode: "lazy-once" */&lt;/code&gt; which generates a single lazy-loadable chunk that can satisfy all calls to &lt;code&gt;import()&lt;/code&gt;. This is useful to prevent additional and unnecessary network requests when images are called. Read the &lt;a href="https://webpack.js.org/api/module-methods/#import-"&gt;list of webpackMode&lt;/a&gt; for more information.&lt;/p&gt;

&lt;h3&gt;
  
  
  Generate an image element
&lt;/h3&gt;

&lt;p&gt;Create a function which generate an &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt; element, append it to a dom container, and run &lt;code&gt;lazyLoadImage&lt;/code&gt; function to set its &lt;code&gt;src&lt;/code&gt; attribute.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;lazyLoadImage&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./lazyLoadImage&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;generateImage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;container&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;imageName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;img&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;img&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;container&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;img&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;lazyLoadImage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;imageName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;img&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;generateImage&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Get images names
&lt;/h3&gt;

&lt;p&gt;We need to get each &lt;code&gt;imageName&lt;/code&gt; to use them in &lt;code&gt;lazyLoadImage&lt;/code&gt; function. Use &lt;a href="https://webpack.js.org/guides/dependency-management/#context-module-api"&gt;require.contex&lt;/a&gt; to import all &lt;code&gt;.jpg&lt;/code&gt; images from &lt;code&gt;./images&lt;/code&gt; folder and match only their name with extension.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getImagesNames&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./images&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;\.&lt;/span&gt;&lt;span class="sr"&gt;jpg$/&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// return an array list of filenames (with extension)&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;importAll&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;[^\/]&lt;/span&gt;&lt;span class="sr"&gt;+$/&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;importAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;getImagesNames&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Generate all images
&lt;/h3&gt;

&lt;p&gt;Create a javascript file called &lt;em&gt;images.js&lt;/em&gt; that, when imported, will lazy-load and generate all images.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;getImagesNames&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./getImagesNames&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;generateImage&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./generateImage&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;images&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#images&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;imagesNames&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getImagesNames&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// generate &amp;lt;img&amp;gt; element&lt;/span&gt;
&lt;span class="c1"&gt;// lazy-load each image and set its src attribute&lt;/span&gt;
&lt;span class="c1"&gt;// append &amp;lt;img&amp;gt; to #images container&lt;/span&gt;
&lt;span class="nx"&gt;imagesNames&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;generateImage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;images&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Lazy load all images
&lt;/h3&gt;

&lt;p&gt;You can now lazy-import &lt;em&gt;images.js&lt;/em&gt; to generate all the images at once when needed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;somethingHappen&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="cm"&gt;/* webpackPrefetch: true */&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./images&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By using &lt;code&gt;webpackPrefetch: true&lt;/code&gt;, a link to the lazy-chunk will be placed in the &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; tag. A prefetched chunk is downloaded while browser is idle, and stored in the browser cache for later use.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"prefetch"&lt;/span&gt; &lt;span class="na"&gt;as=&lt;/span&gt;&lt;span class="s"&gt;"script"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"0.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;More information in &lt;a href="https://webpack.js.org/guides/code-splitting/#prefetching-preloading-modules"&gt;prefetching-preloading-modules&lt;/a&gt; and &lt;a href="https://medium.com/webpack/link-rel-prefetch-preload-in-webpack-51a52358f84c"&gt;link-rel-prefetch-preload-in-webpack&lt;/a&gt; and &lt;a href="https://medium.com/reloading/preload-prefetch-and-priorities-in-chrome-776165961bbf"&gt;preload-prefetch-and-priorities-in-chrome&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Notes
&lt;/h2&gt;

&lt;p&gt;Inspect devtools network tab while viewing the app. Make sure to not disable cache in devtools, otherwise the prefetched chunk will not be served from the cache.&lt;/p&gt;

&lt;p&gt;You can add &lt;code&gt;webpackMode: "eager"&lt;/code&gt; instead of &lt;code&gt;webpackPrefetch: true&lt;/code&gt; if you want to include &lt;em&gt;images.js&lt;/em&gt; inside the main bundle.&lt;/p&gt;

&lt;p&gt;This article is part of my &lt;a href="https://github.com/pldg/learn-webpack"&gt;learn-webpack&lt;/a&gt; repositories collection.&lt;/p&gt;

&lt;h2&gt;
  
  
  Further reading
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://survivejs.com/webpack/building/code-splitting/"&gt;code-splitting&lt;/a&gt; by Juho Vepsäläinen&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://hackernoon.com/the-100-correct-way-to-split-your-chunks-with-webpack-f8a9df5b7758"&gt;the-100-correct-way-to-split-your-chunks-with-webpack&lt;/a&gt; by David Gilbertson&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://medium.com/front-end-hacking/webpack-and-dynamic-imports-doing-it-right-72549ff49234"&gt;webpack-and-dynamic-imports-doing-it-right&lt;/a&gt; by Rubens Pinheiro Gonçalves Cavalcante&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webpack</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
