Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Bootstrap 4 Cookbook

You're reading from   Bootstrap 4 Cookbook Solutions to common problems faced in Responsive Web Design

Arrow left icon
Product type Paperback
Published in Jun 2017
Publisher Packt
ISBN-13 9781785889295
Length 338 pages
Edition 1st Edition
Languages
Concepts
Arrow right icon
Author (1):
Arrow left icon
Ajdin Imsirovic Ajdin Imsirovic
Author Profile Icon Ajdin Imsirovic
Ajdin Imsirovic
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Installing Bootstrap 4 and Comparing Its Versions FREE CHAPTER 2. Layout Like a Boss with the Grid System 3. Power Up with the Media Object, Text, Images, and Tables 4. Diving Deep into Bootstrap 4 Components 5. Menus and Navigations 6. Extending Bootstrap 4 7. Make Your Own jQuery Plugins in Bootstrap 4 8. Bootstrap 4 Flexbox and Layouts 9. Workflow Boosters 10. Creating a Blog with Jekyll and Bootstrap 4 11. Bootstrap 4 with ASP.NET Core 12. Integrating Bootstrap 4 with React and Angular

Making custom Grunt tasks in Bootstrap 4

To understand how to better work with a build tool such as Grunt, in this recipe we will customize the available Gruntfile.js and package.json. We will perform these changes without physically deleting these important files from the default installation. That way, we will be able to play around with customization and not lose the original files.

Getting ready

To start working on this recipe, we first need to navigate to the workspace folder, and rename the original Gruntfile.js and package.json:

      cd && cd workspace
mv Gruntfile.js Gruntfile.jsORIGINAL
mv package.json package.jsonORIGINAL

Now, we are ready to create new versions of these two files and add a custom Grunt task.

How to do it...

  1. Let's create new files:
      cd && cd workspace
touch Gruntfile.js package.json
  1. Open the package.json file:
      c9 package.json
An important thing to note is that if you had the old package.json file open before, during, and after the file renaming using the mv command, using the c9 <filename> command might point to the tab that was not closed, and show the old version of the file. Feel free to close this file's tab by middle-clicking on it (this works just like browser tabs, at least on c9.io running in Chrome).
  1. The package file is completely empty, so let's add some code to it:
      {
"name": "customGrunt",
"version": "",
"devDependencies": {
"grunt": "~1.0.1"
}
}

What are we doing in the package.json file? We are giving our package just some key:value pairs. Specifically, we are giving it a name, a version, and devDependencies. Right now, only the devDependency grunt is listed.

  1. Now we will add another plugin, grunt-contrib-copy, by typing the following command in our Bash console:
      npm install grunt-contrib-copy --save-dev

Now, we can see that the grunt-contrib-copy plugin has been added to the list of devDependencies in our custom package.json:

      {
"name": "customGrunt",
"version": "",
"devDependencies": {
"grunt": "~1.0.1",
"grunt-contrib-copy": "^1.0.0"
}
}

More information about this plugin can be found at https://www.npmjs.com/package/grunt-contrib-copy. In a nutshell, this plugin copies files as we specify.

  1. Now that we have prepared our package.json file, we can tell Grunt how to use it, by coding Gruntfile.js. We will begin by opening the currently empty Gruntfile.js:
      c9 Gruntfile.js
  1. We will add the following code to our Gruntfile.js:
      'use strict';
module.exports = function (grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),

// Define the copy task
copy: {
main: {
expand: true,
src: 'dist/css/bootstrap.css',
dest: 'copy',
},
},
});

grunt.loadNpmTasks("grunt-contrib-copy");
grunt.registerTask("default", ['copy']);
};
If you need a detailed explanation of how the above Gruntfile.js code works, take a look at the How it works… section.
  1. Finally, it is time to run our default Grunt task, with verbose logging:
      grunt -v

Running the preceding command will create a new folder and will copy the bootstrap.css file in the workspace/copy/dist/css/bootstrap.css

path.

  1. Now that we have a basic understanding of just how Grunt runs its tasks, as well as how to modify its tasks to our liking, let's undo the changes we did. However, we will still keep our experimental files, just to have them handy if needed. What follows are the commands used to achieve this. The following commands will get to the root, navigate to /workspace, and make a new folder called GruntExperiment:
      cd; cd workspace; mkdir GruntExperiment
  1. Now, let's move our custom Gruntfile.js and package.json files, with the following two commands:
  1. Finally, we need to rename our original files to their original names, running the following commands (note that there are two commands here, for two files; each command was split on two rows so they can fit this page width):
      mv node_modules/bootstrap/Gruntfile.jsORIGINAL 
node_modules/bootstrap/Gruntfile.js; mv node_modules/bootstrap/package.jsonORIGINAL
node_modules/bootstrap/package.json

How it works...

In this recipe, we have provided some custom code needed for a very simple Gruntfile.js file to work in step 6 . What follows is the breakdown of what the code does.

On line 1, we use the strict mode. On line 3, we call the grunt module. Line 4 instructs grunt to read our package.json file. Lines 7-13 specify the copy task. Line 17 is the entry point that registers the 'copy' task as the default task.

You have been reading a chapter from
Bootstrap 4 Cookbook
Published in: Jun 2017
Publisher: Packt
ISBN-13: 9781785889295
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image