官术网_书友最值得收藏!

  • JavaScript by Example
  • Dani Akash S
  • 791字
  • 2021-07-02 18:39:11

Bundling Bootstrap in Webpack

Time to bundle our final dependency into Webpack. Bootstrap consists of three parts. The first is Bootstrap's CSS file, followed by jQuery and Bootstrap's JavaScript file, which is dependent on jQuery. The last two files were ignored in this chapter's index.html file, since we weren't using them. But, since we are bundling our dependencies with Webpack, let's just bring all of them together. For the first step, install our dependencies (these are not dev dependencies; hence, -S is used instead of -D):

npm install -S jquery bootstrap@3

Bootstrap is written using Less instead of CSS. Less is a CSS pre-processor that extends CSS with more features, such as variables, mixins, and functions. To import Bootstrap's less file using Webpack, we need another loader:

npm install -D less less-loader

This will install the less compiler and loader into our node_modules. Now, in our rules, modify the CSS rules into:

{
test: /\.(less|css)$/,
use: [ 'style-loader', 'css-loader', 'less-loader' ]
},

This will add less-loader as the first option as a loader whenever CSS or a less file is detected by Webpack. Now, try npm run webpack. This time, you will get an error in the terminal saying "You may need an appropriate loader to handle this file type" for the fonts that are used by Bootstrap. Since Bootstrap is dependent on a lot of fonts, we need to create a separate loader to include them in our bundle. For this purpose, install the following:

npm install -D file-loader url-loader

And then include the following object in your rules array:

{
test: /\.(svg|eot|ttf|woff|woff2)$/,
loader: 'url-loader',
options: {
limit: 10000,
name: 'fonts/[name].[ext]'
}
},

This will tell Webpack if the file size is smaller than 10 KB. Then, simply inline the file into JavaScript as a data URL. Otherwise, move the file into the fonts folder and create a reference in JavaScript. This is useful to reduce a network overhead if the file is smaller than 10 KB. url-loader requires file-loader to be installed as a dependency. Once again, execute npm run webpack and, this time, your Bootstrap less file will be bundled successfully and you will be able to view your website in the browser.

This may look like a lot of work for a few CSS and JS files. But, when you are working on large-scale applications, these configurations can save hours of development work. The biggest advantage of Webpack is that you can write the configuration for one project and use it for other projects. So, most of the work we do here will be done only once. We'll simply copy and use our webpack.config.js file in other projects.

As I mentioned earlier, we didn't use Bootstrap's JS files in our application. However, we might need to use them for our applications in the future. Bootstrap requires jQuery to be available in global scope so that it's JavaScript files can be executed. However, Webpack does not expose the JavaScript variables it has bundled unless it is explicitly specified to expose them.

To make jQuery available in the global scope throughout our web application, we need to use a Webpack plugin. Plugins are different from loaders. We'll see more about plugins in a moment. For now, add the following code after the module property of Webpack:

module: {
rules: [...],
},
plugins: [
new webpack.ProvidePlugin({
jQuery: 'jquery',
$: 'jquery',
jquery: 'jquery'
}),
],

In our general.js file, include the following line to import all the Bootstrap JavaScript files into our web app:

import 'bootstrap';

This line will import Bootstrap's JavaScript files from the node_modules folder. You have now successfully bundled Bootstrap using Webpack. There is just one more loader that is commonly used - img-loader. There are scenarios when we include images in CSS and JavaScript. Using Webpack, we can automatically bundle the images while compressing the size of larger images during bundling.

To bundle images, we need to use img-loader and url-loader together. First, install img-loader:

npm install -D img-loader

Add the following object to your rules list:

{
test: /\.(png|jpg|gif)$/,
loaders: [
{
loader: 'url-loader',
options: {
limit: 10000,
name: 'images/[name].[ext]'
}
},
'img-loader'
],
},

Now, execute npm run webpack and open up the website again. You have all your dependencies bundled inside a single JavaScript file memes.js and you are ready to go.

Sometimes, the img-loader binaries might fail during building depending on your operating system. In the latest version of Ubuntu, this is due to a missing package that can be downloaded and installed from: https://packages.debian.org/jessie/amd64/libpng12-0/download. In other operating systems, you have to manually find out why the build failed. If you cannot resolve the img-loader issue, do try to use a different loader or simply remove img-loader and only use url-loader for images.
主站蜘蛛池模板: 剑川县| 永新县| 封丘县| 云霄县| 嘉兴市| 阿城市| 白山市| 江达县| 日土县| 长岛县| 巴里| 衡南县| 嘉定区| 宕昌县| 新泰市| 汉中市| 屏南县| 定西市| 高淳县| 确山县| 兴义市| 青铜峡市| 谷城县| 蒙自县| 抚宁县| 武强县| 随州市| 盐源县| 区。| 宝坻区| 饶河县| 来宾市| 镇远县| 城口县| 山东省| 乐陵市| 泾源县| 金堂县| 德庆县| 招远市| 涞源县|