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

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.
主站蜘蛛池模板: 漳州市| 涡阳县| 壤塘县| 从化市| 神池县| 华阴市| 鹤壁市| 定结县| 安丘市| 彩票| 霍林郭勒市| 吉隆县| 岳普湖县| 诏安县| 鸡西市| 化州市| 桂平市| 罗定市| 西充县| 岚皋县| 个旧市| 儋州市| 静安区| 兴和县| 读书| 丽江市| 贺州市| 沙洋县| 鹿邑县| 原平市| 梁河县| 惠安县| 格尔木市| 瑞丽市| 合肥市| 榆社县| 陕西省| 四平市| 贵德县| 乌拉特后旗| 都江堰市|