Skip to content

A Bootstrap 5 boilerplate, using Webpack 5, Babel, SCSS, etc.

License

Notifications You must be signed in to change notification settings

noreading/bootstrap5-webpack-boilerplate

Repository files navigation

Bootstrap 5 + WebPack 5 = Boilerplate

This is a professional front-end template for building web apps and sites faster, without having to create the basic setup on your own, every time you start a new project.

The template is based on the Bootstrap Framework in version 5 and uses Webpack in version 5 as a flexible and modern module bundler. All common features for front-end projects (like SCSS compilation, minifying of Assets, etc.) are included out of the box.

In development the DevServer plugin is used, to serve the page with hot reloading enabled, so that you can see all changes in your code immediately. When you're done, just run the build command and upload all files from the public folder.

In addition to the basic front-end project setup, I added some cool features like a configurable image resizing command to make generating responsive images a breeze.

Table of Contents

  1. Requirements
  2. Quick Start
  3. Environment Configuration
  4. Adding Google Fonts
  5. Adding Responsive Images
  6. Image Credits

1. Requirements

The boilerplate needs Node.js to be installed on your system. It was tested with version 12 and newer.

2. Quick Start

  1. Clone the repository into a new folder for your new project.

    git clone [email protected]:noreading/bootstrap5-webpack-boilerplate.git my-project
  2. Remove the .git directory to add your own CVS later.

    rm -rf .git
  3. Update the package.json.

       {
         "name": "my-project",
         "description": "A description of my new project",
         "author": "Your Name",
         "license": "MIT"
       }
  4. Enable / Disable bootstrap features in main.js.

    initBootstrap({
      tooltip: true,
      popover: true,
      toasts: true,
    });
  5. Install needed dependencies

    npm install
  6. Run webpack

    The dev command will start a dev server and watch for code changes in JS and SCSS files. Hot reloading is enabled, so that any change will be visible in your browser as you type.

    npm run dev

    For production usage, run the build command and everything you need gets packed together into the public directory. You can upload the content to any hosting provider, without further modifications.

    npm run build

3. Environment Configuration

If you use sensitive information in your code, like API keys or encryption tokens, you should never store those in your code repository. This could lead to a security issue, especially if the repository is public.

Therefore, I included the dotenv-webpack plugin in this boilerplate, that enables you to store all your sensitive information in a .env file, that is ignored by git.

The .env.default file should contain all the variables that your application needs, but without the real data and should contain either empty variables or default values that can be used by everyone. The variables will get replaced during asset compilation so that only those variables are added, that are referenced in your code.

It is a common scheme to use an uppercase syntax for environment variables, as you can see in the example below. Comments inside of .env files start with a hash.

# GOOGLE APIs

GOOGLE_MAPS_API_KEY=vEVmihkWZ2fqedyHQT***************
YOUTUBE_API_KEY=TnJ8YOfVuL9bbFH83T13N****************

# CACHING
CACHE_ENABLED=false
CACHE_TIMEOUT=3600

You can test the usage of environment variables by editing the .envt file and changing the value of HELLO. After re-compiling the assets you should see a message in the developer console, as soon as you visit the demo page.

Important:

After each change of the .env file you need to reload Webpack, as the environment is only loaded once per runtime. If you've got an active npm run dev command, you need to stop and re-run it, for the changes to take effect.

4. Adding Google Fonts

If you want to add fonts from fonts.google.com, you should follow a few easy steps. The boilerplate uses the Roboto as an example.

  1. Download the required font families as ZIP files from fonts.google.com.
  2. Extract the files and copy the folders to /fonts/ in the theme directory.
  3. Generate the formats "eot", "woff" and "woff2" using transfonter.org and save them.
  4. Open the file src/scss/_fonts.scss and use the mixin googleFont() to add the fonts.

The mixin has 4 parameters.

Name Type Description
name String The name of the font, used as value for font-family properties.
folder String The name of the folder, that is extracted from the ZIP file.
files String The first part of font filenames.
weights List The list of weights, that should be loaded.

Example:

@include googleFont((
    "name": "IBM Plex Sans",
    "folder": "IBM_Plex_Sans",
    "files": "IBMPlexSans",
    "weights": (300, 400, 700)
));

There is also a second mixin, that can handle multiple fonts at once. This reduces the amount of code needed, if your website or application requires multiple fonts to be loaded.

Example:

@include googleFonts((
    (
        "name": "Roboto",
        "folder": "Roboto",
        "files": "Roboto",
        "weights": (300, 400, 700)
    ),
    (
        "name": "Besley",
        "folder": "Besley",
        "files": "Besley",
        "weights": (300, 400, 700)
    ),
));

5. Adding Responsive Images

5.1 What is it?

This boilerplate includes a command to resize images based on a configuration file, to get rid of the hassle to care about the responsive image sizes manually. One of the benefits of this process is that it works on all major operating systems, without the need to do any manual installations.

If you want to use the resizing feature, please edit the file images.config.js in the root directory and change all settings to your needs. You can add multiple collections with different configurations for greatest flexibility.

In order for this command to work properly you need to have "clean" filenames for your images, that don't match the patterns used to create the resized filenames automatically. The filenames get a postfix, based on the resizing settings for the images width and height.

Filenames, that will be recognized as original images, are as follows.

Allowed Filename Description
my-image.jpg Simple filenames
my-image-1982-to-2018.jpg Filenames including numbers, also at the end.
my-image-400x200-tablet.jpg Filenames including dimensions, but not at the end.
my-image_400x200.jpg Filenames including dimensions, but using an underscore.

Filenames, that will not be recognized as original images, are as follows.

Prohibited Filename Description Pattern
your-image-w200.jpg Resized using a fixed width only {filename}-w{width}.{extension}
your-image-h400.jpg Resized using a fixed height only {filename}-h{height}.{extension}
your-image-200x400.jpg Resized using a fixed width and height {filename}-{width}x{height}.{extension}

You can use a test tool to check if your filenames will work correctly, by adding one filename per line into the "Test Strings" field. This helps to ensure that none of your images will be deleted.

You can use the regular expression to test files on your local machine, too. On Linux and Mac operating systems you can check if any image in a folder would conflict with the resizing tool by using the following command:

find ./ | grep -E ".*\-([0-9]+x[0-9]+|w[0-9]+|h[0-9]+)\.[a-z]+$"

All files that are listed should get renamed, following the rules you can see in the tables above.


5.2 The Configuration

The responsive image configuration is saved in the images.config.js file, located in the root directory of the project.

5.2.1 Global Settings

The configuration has some global settings, that you should set to your personal preferences.

Option Description Default
useTrash Moves files to the trash instead of deleting them directly, when using the "recreate" or "remove" argument. false

5.2.2 Collections

The configuration uses collections which include a set of configuration options to resize images. This allows you to define different resizing rules for multiple directories.

Each collection has the following options.

Option Description Required Default
name The name of the collection, to identify it in error messages, etc. yes -
source The source directory of the image files that should get resized. yes -
recursive Resize images in subdirectories, too? no true
sizes The configurations for image sizes that get created. yes -

5.2.3 Sizes

Each collection has the option "sizes" which includes a set of configurations for different image sizes that will be generated. Width and height are optional, if at least one of them is set.

Each size has the following options.

Option Type Description Required Default
name {string} The name of the size, to identify it in error messages, etc. yes -
width {number} The width of the resized image. no -
height {number} The height of the resized image. no -
fit {string} The method by which the image should fit.

cover
Crop to cover both provided dimensions.

contain
Embed within both provided dimensions.

file
Ignore the aspect ratio of the input and stretch to both provided dimensions.

inside
Preserving aspect ratio, resize the image to be as large as possible while ensuring its dimensions are less than or equal to both those specified.

outside
Preserving aspect ratio, resize the image to be as small as possible while ensuring its dimensions are greater than or equal to both those specified.
no cover
position {string} The position When using a fit of "cover" or "contain"

left, right, top, bottom, center, left top, right top, left bottom, right bottom
no center

5.3 The Command Line Arguments

The resizing command supports different arguments to remove resized images, recreate all images, etc.

Command Description
npm run images Creates all resized versions of a file that are missing.
npm run images recreate Removes the resized versions of all files and recreates them.
npm run images remove Removes the resized versions of all files.

Important:

The recreation and removal arguments will force the command to remove all images it detects as being resized versions ( by their filename). If you use other tools for your images that add postfixes to the filenames, this might lead to false positives, so please backup your files before you run this.

npm run images remove

6. Image Credits

All placeholder images used in the index.html file are downloaded from pexels.com, and pixabay.com. Those are two fantastic collections of free stock photos from photographers around the globe.

If you're one of the photographers and would like to change the linked website or get an image removed from the boilerplate, please shoot me an email to [email protected] and I'll update it as soon as possible.

Carousel Image Photographer Website Image Source
1 kailash kumar facebook page pexels.com
2 Cleverpix cleverpix.com.au pixabay.com
3 GregMontani - pixabay.com

Album Photo Photographer Website Image Source
1 skeeze - pixabay.com
2 shottrotter shottrotter.be pexels.com
3 sasint pixartasia.com pixabay.com
4 Walkerssk walkers.sk pixabay.com
5 Marcocarli - pixabay.com
6 Jaymantri jaymantri.com pexels.com
7 veeterzy instagram profile pexels.com
8 Pok Rie - pexels.com

About

A Bootstrap 5 boilerplate, using Webpack 5, Babel, SCSS, etc.

Resources

License

Stars

Watchers

Forks

Packages

No packages published