avatarTrevor-Indrek Lasn

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

4958

Abstract

github.com/NekR/offline-plugin/blob/master/docs/typescript.md">here</a>.</p><h1 id="a22a">webpack-pwa-manifest</h1><p id="17fc"><code>webpack-pwa-manifest</code> describes itself as a “Progressive Web App Manifest Generator for Webpack, with auto icon resizing and fingerprinting support.”</p><figure id="8113"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*dMUrTdYGh-FHs_eTNE4EYw.png"><figcaption>webpack-pwa-manifest — <a href="https://github.com/arthurbergmz/webpack-pwa-manifest">https://github.com/arthurbergmz/webpack-pwa-manifest</a></figcaption></figure><p id="e9ed"><code>webpack-pwa-manifest</code> is a webpack plugin that generates a <code>manifest.json</code> for your progressive web application.</p><p id="36a5">If you’re using <code>inject</code> on your configuration, ensure that <a href="https://github.com/jantimon/html-webpack-plugin"><code>HtmlWebpackPlu</code>gin</a> appears <i>before</i> <code>WebpackPwaManifest</code> in the <code>plugins</code> array.</p><h2 id="232b">Features</h2><ul><li>Autoicon resizing</li><li>Icon fingerprinting</li><li>Manifest fingerprinting</li><li>Automanifest injection on HTML</li><li>Hot-reload support</li></ul><h2 id="9c4e">Installation</h2><div id="8aab"><pre>npm install --<span class="hljs-built_in">save</span>-<span class="hljs-built_in">dev</span> webpack-pwa-manifest</pre></div><h2 id="ef30">Usage</h2><p id="f89c">In your <code>webpack.config.js</code>:</p><div id="c153"><pre><span class="hljs-keyword">import</span> WebpackPwaManifest <span class="hljs-keyword">from</span> <span class="hljs-string">'webpack-pwa-manifest'</span></pre></div><div id="6404"><pre><span class="hljs-meta prompt_">...</span></pre></div><div id="90e8"><pre>plugins: [ new <span class="hljs-built_in">WebpackPwaManifest</span>({ name: <span class="hljs-string">'My Progressive Web App'</span>, short_name: <span class="hljs-string">'MyPWA'</span>, description: <span class="hljs-string">'My awesome Progressive Web App!'</span>, background_color: <span class="hljs-string">'#ffffff'</span>, crossorigin: <span class="hljs-string">'use-credentials'</span>, //can be null, use-credentials or anonymous icons: [ { <span class="hljs-attribute">src</span>: path.<span class="hljs-built_in">resolve</span>(<span class="hljs-string">'src/assets/icon.png'</span>), sizes: [<span class="hljs-number">96</span>, <span class="hljs-number">128</span>, <span class="hljs-number">192</span>, <span class="hljs-number">256</span>, <span class="hljs-number">384</span>, <span class="hljs-number">512</span>] // multiple sizes }, { <span class="hljs-attribute">src</span>: path.<span class="hljs-built_in">resolve</span>(<span class="hljs-string">'src/assets/large-icon.png'</span>), size: <span class="hljs-string">'1024x1024'</span> // you can also use the specifications pattern } ] }) ]</pre></div><h1 id="d6ae">imagemin-webpack-plugin</h1><p id="a405"><code>imagemin-webpack-plugin</code> is a webpack plugin to compress images with <a href="https://github.com/imagemin/imagemin">imagemin</a>.</p><figure id="c525"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*HoGiJt7VZv4WUxz3AmajFw.png"><figcaption>imagemin-webpack-plugin — <a href="https://github.com/Klathmon/imagemin-webpack-plugin">https://github.com/Klathmon/imagemin-webpack-plugin</a></figcaption></figure><h2 id="d17f">Install</h2><div id="2346"><pre>npm <span class="hljs-keyword">install</span> imagemin-webpack-plugin</pre></div><h2 id="44a4">Usage</h2><div id="98d3"><pre><span class="hljs-keyword">import</span> ImageminPlugin <span class="hljs-keyword">from</span> <span class="hljs-string">'imagemin-webpack-plugin'</span></pre></div><div id="e37a"><pre><span class="hljs-keyword">module</span>.<span class="hljs-keyword">exports</span> = { plugins: [ <span class="hljs-comment">// Make sure that the plugin is after any plugins that add images</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ImageminPlugin</span>({ disable: process.env.NODE_ENV !== <span class="hljs-string">'production'</span>, <span class="hljs-comment">// Disable during development</span> pngquant: { quality: <span class="hljs-string">'95-100'</span> } }) ] }</pre></div><h1 id="558e">prerender-spa-plugin</h1><p id="2e3e"><code>prerender-spa-plugin</code> prerenders static HTML into a single-page application.</p><figure id="5ad6"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*QsD_l7m6IM2glbCaESjc8Q.png"><figcaption>prerender-spa-plugin — <a href="https://github.com/chrisvfritz/prerender-spa-plugin">https://github.com/chrisvfritz/prerender-spa-plugin</a></figcaption></figure><p id="f6c6">The goal of this plugin is to provide a simple prerendering solution that’s easily extensible and usable for any site or single-page app built with webpack.</p><h2 id="

Options

7a42">What is prerendering?</h2><p id="907a">Recently, server-side rendering (SSR) has taken the JavaScript front-end world by storm. The fact that you can now render your sites and apps on the server before sending them to your clients is an absolutely revolutionary idea (and totally <i>not</i> what everyone was doing before JS client-side apps got popular in the first place).</p><p id="accc">However, the same criticisms that were valid for PHP, ASP, JSP, (and such) sites are valid for server-side rendering today. It’s slow, breaks fairly easily, and is difficult to implement properly.</p><p id="a2a7">Thing is, despite what everyone might be telling you, you probably don’t <i>need</i> SSR. You can get almost all the advantages of it (without the disadvantages) by using prerendering. Prerendering is basically firing up a headless browser, loading your app’s routes, and saving the results to a static HTML file. You can then serve it with whatever static-file-serving solution you were using previously. It just works with HTML5 navigation and the like. No need to change your code or add server-side rendering workarounds.</p><h2 id="3cd3">Install</h2><div id="7afa"><pre>yarn <span class="hljs-built_in">add</span> -D prerender-spa-plugin</pre></div><h2 id="dbc3">Basic usage</h2><div id="f9c6"><pre><span class="hljs-variable">const</span> <span class="hljs-variable">path</span> = <span class="hljs-function"><span class="hljs-title">require</span>(<span class="hljs-string">'path'</span>)</span> <span class="hljs-variable">const</span> <span class="hljs-variable">PrerenderSPAPlugin</span> = <span class="hljs-function"><span class="hljs-title">require</span>(<span class="hljs-string">'prerender-spa-plugin'</span>)</span></pre></div><div id="eaee"><pre><span class="hljs-keyword">module</span>.<span class="hljs-keyword">exports</span> = { plugins: [ ... <span class="hljs-keyword">new</span> <span class="hljs-title class_">PrerenderSPAPlugin</span>({ <span class="hljs-comment">// Required - The path to the webpack-outputted app to prerender.</span> staticDir: path.join(__dirname, <span class="hljs-string">'dist'</span>), <span class="hljs-comment">// Required - Routes to render.</span> routes: [ <span class="hljs-string">'/'</span>, <span class="hljs-string">'/about'</span>, <span class="hljs-string">'/some/deep/nested/route'</span> ], }) ] }</pre></div><h1 id="ca55">duplicate-package-checker-webpack-plugin</h1><p id="eeb9">This is a webpack plugin that warns when your bundle contains multiple versions of the same package.</p><figure id="0937"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*y8HjpkGW1a3lHIlJRITvvQ.png"><figcaption>duplicate-package-checker-webpack-plugin — <a href="https://github.com/darrenscerri/duplicate-package-checker-webpack-plugin">https://github.com/darrenscerri/duplicate-package-checker-webpack-plugin</a></figcaption></figure><h2 id="3103">Why?</h2><p id="ac36">It might be possible that a single package gets included multiple times in a webpack bundle due to different package versions. This situation may happen without any warning, resulting in extra bloat in your bundle, and it may lead to hard-to-find bugs.</p><p id="c419">This plugin will warn you of such cases to minimize bundle size and avoid bugs caused by unintended duplicate packages.</p><p id="b4a7">Motivation: <a href="https://github.com/webpack/webpack/issues/385">webpack/webpack/issues/385</a> and <a href="https://github.com/webpack/webpack/issues/646">webpack/webpack/issues/646</a>.</p><h2 id="c3ed">Install</h2><div id="bd2c"><pre>npm install duplicate-<span class="hljs-keyword">package</span>-checker-webpack-plugin <span class="hljs-comment">--save-dev</span></pre></div><h2 id="0848">Usage</h2><p id="0389">Add the plugin to your webpack config:</p><div id="688f"><pre>const DuplicatePackageCheckerPlugin <span class="hljs-operator">=</span> require(<span class="hljs-string">"duplicate-package-checker-webpack-plugin"</span>)<span class="hljs-comment">;</span></pre></div><div id="5815"><pre><span class="hljs-keyword">module</span>.<span class="hljs-keyword">exports</span> = { plugins: [<span class="hljs-keyword">new</span> <span class="hljs-title class_">DuplicatePackageCheckerPlugin</span>()] };</pre></div><h1 id="42d3">Conclusion</h1><p id="f76c">Thanks for reading. I hope you learned something new. webpack is a great tool for us web developers — make use of all of its advantages. Happy coding!</p><p id="b99e">If you’re new to JavaScript and want to learn the language, I recommend starting with reading books combined with building things. Start with the “<a href="https://amzn.to/2LOkzjj"><i>A Smarter Way to Learn JavaScript</i></a>” book and <a href="https://readmedium.com/the-secret-to-being-a-top-developer-is-building-things-heres-a-list-of-fun-apps-to-build-aac61ac0736c">here’s a list of fun apps to build</a>.r</p></article></body>

Boost Your Productivity With These 6 Awesome webpack Plugins

Plugins are the backbone of webpack

webpack logo

webpack plugins are used for performing a wider range of tasks like bundle optimization, asset management, and injection of environment variables.

webpack itself is built on the same plugin system that you use in your webpack configuration. Depending on how you’re using webpack, there are multiple ways to use plugins.

Without further ado, here are six awesome webpack plugins.

Webpack Bundle Analyzer

Visualize the size of webpack output files with an interactive, zoomable tree map.

Webpack Bundle Analyzer — https://github.com/webpack-contrib/webpack-bundle-analyzer

This plugin will help you do the following:

  1. Realize what’s really inside your bundle
  2. Find out what modules make up the most of the bundle’s size
  3. Find modules that got there by mistake
  4. Optimize your webpack bundle

Getting started

# NPM
npm install --save-dev webpack-bundle-analyzer
# Yarn
yarn add -D webpack-bundle-analyzer

Usage

const BundleAnalyzerPlugin = require('webpack-bundle analyzer');
module.exports = {
  plugins: [
    new BundleAnalyzerPlugin()
  ]
}

offline-plugin

The offline-plugin is intended to provide an offline experience for webpack projects.

offline-plugin — https://github.com/NekR/offline-plugin

The plugin uses ServiceWorker and AppCache as a fallback under the hood. Simply include this plugin in your webpack.config and the accompanying run time in your client script, and your project will become offline-ready by caching all (or some) of the webpack output assets.

Installation

npm install offline-plugin [--save-dev]

Setup

First, instantiate the plugin in your webpack.config:

// webpack.config.js example
var OfflinePlugin = require('offline-plugin');
module.exports = {
  // ...
  plugins: [
    // ... other plugins
    // it's always better if OfflinePlugin is the last plugin added
    new OfflinePlugin()
  ]
  // ...
}

(And, optionally, you can configure it with options.)

Then, add the run time into your entry file (typically the main entry):

require('offline-plugin/runtime').install();

ES6/Babel/TypeScript

import * as OfflinePluginRuntime from 'offline-plugin/runtime';
OfflinePluginRuntime.install();

For more details of usage with TypeScript, see here.

webpack-pwa-manifest

webpack-pwa-manifest describes itself as a “Progressive Web App Manifest Generator for Webpack, with auto icon resizing and fingerprinting support.”

webpack-pwa-manifest — https://github.com/arthurbergmz/webpack-pwa-manifest

webpack-pwa-manifest is a webpack plugin that generates a manifest.json for your progressive web application.

If you’re using inject on your configuration, ensure that HtmlWebpackPlugin appears before WebpackPwaManifest in the plugins array.

Features

  • Autoicon resizing
  • Icon fingerprinting
  • Manifest fingerprinting
  • Automanifest injection on HTML
  • Hot-reload support

Installation

npm install --save-dev webpack-pwa-manifest

Usage

In your webpack.config.js:

import WebpackPwaManifest from 'webpack-pwa-manifest'
...
plugins: [
  new WebpackPwaManifest({
    name: 'My Progressive Web App',
    short_name: 'MyPWA',
    description: 'My awesome Progressive Web App!',
    background_color: '#ffffff',
    crossorigin: 'use-credentials', //can be null, use-credentials or anonymous
    icons: [
      {
        src: path.resolve('src/assets/icon.png'),
        sizes: [96, 128, 192, 256, 384, 512] // multiple sizes
      },
      {
        src: path.resolve('src/assets/large-icon.png'),
        size: '1024x1024' // you can also use the specifications pattern
      }
    ]
  })
]

imagemin-webpack-plugin

imagemin-webpack-plugin is a webpack plugin to compress images with imagemin.

imagemin-webpack-plugin — https://github.com/Klathmon/imagemin-webpack-plugin

Install

npm install imagemin-webpack-plugin

Usage

import ImageminPlugin from 'imagemin-webpack-plugin'
module.exports = {
  plugins: [
    // Make sure that the plugin is after any plugins that add images
    new ImageminPlugin({
      disable: process.env.NODE_ENV !== 'production', // Disable during development
      pngquant: {
        quality: '95-100'
      }
    })
  ]
}

prerender-spa-plugin

prerender-spa-plugin prerenders static HTML into a single-page application.

prerender-spa-plugin — https://github.com/chrisvfritz/prerender-spa-plugin

The goal of this plugin is to provide a simple prerendering solution that’s easily extensible and usable for any site or single-page app built with webpack.

What is prerendering?

Recently, server-side rendering (SSR) has taken the JavaScript front-end world by storm. The fact that you can now render your sites and apps on the server before sending them to your clients is an absolutely revolutionary idea (and totally not what everyone was doing before JS client-side apps got popular in the first place).

However, the same criticisms that were valid for PHP, ASP, JSP, (and such) sites are valid for server-side rendering today. It’s slow, breaks fairly easily, and is difficult to implement properly.

Thing is, despite what everyone might be telling you, you probably don’t need SSR. You can get almost all the advantages of it (without the disadvantages) by using prerendering. Prerendering is basically firing up a headless browser, loading your app’s routes, and saving the results to a static HTML file. You can then serve it with whatever static-file-serving solution you were using previously. It just works with HTML5 navigation and the like. No need to change your code or add server-side rendering workarounds.

Install

yarn add -D prerender-spa-plugin

Basic usage

const path = require('path')
const PrerenderSPAPlugin = require('prerender-spa-plugin')
module.exports = {
  plugins: [
    ...
    new PrerenderSPAPlugin({
      // Required - The path to the webpack-outputted app to prerender.
      staticDir: path.join(__dirname, 'dist'),
      // Required - Routes to render.
      routes: [ '/', '/about', '/some/deep/nested/route' ],
    })
  ]
}

duplicate-package-checker-webpack-plugin

This is a webpack plugin that warns when your bundle contains multiple versions of the same package.

duplicate-package-checker-webpack-plugin — https://github.com/darrenscerri/duplicate-package-checker-webpack-plugin

Why?

It might be possible that a single package gets included multiple times in a webpack bundle due to different package versions. This situation may happen without any warning, resulting in extra bloat in your bundle, and it may lead to hard-to-find bugs.

This plugin will warn you of such cases to minimize bundle size and avoid bugs caused by unintended duplicate packages.

Motivation: webpack/webpack/issues/385 and webpack/webpack/issues/646.

Install

npm install duplicate-package-checker-webpack-plugin --save-dev

Usage

Add the plugin to your webpack config:

const DuplicatePackageCheckerPlugin = require("duplicate-package-checker-webpack-plugin");
module.exports = {
  plugins: [new DuplicatePackageCheckerPlugin()]
};

Conclusion

Thanks for reading. I hope you learned something new. webpack is a great tool for us web developers — make use of all of its advantages. Happy coding!

If you’re new to JavaScript and want to learn the language, I recommend starting with reading books combined with building things. Start with the “A Smarter Way to Learn JavaScript” book and here’s a list of fun apps to build.r

JavaScript
Webpack
Web Development
Software Development
Programming
Recommended from ReadMedium