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

Bundling files

Let's take a look at how Webpack combines our JavaScript files together. Go through the following steps to add a second JavaScript file:

  1. In our src folder, create another file. Let’s call it index2.js, for lack of creativity.
  2. Inside, add a second console.log:
console.log(‘Hello from index2.js!’);
  1. Then, back in index.js (the first one), we’ll require the other file, as follows:
require('./index2.js');
console.log('Hello from index.js!');

This basically means that index.js now tells Webpack, "Hey, I need this other index!"

  1. Okay, let’s rerun the same Webpack command as earlier:
node_modules/.bin/webpack src/index.js public/bundle.js

Again, we’ll only specify src/index.js, but if you look at the console output, you’ll see that Webpack now grabs the other file too:

  1. Open up public/bundle.js, scroll to the bottom, and you’ll see both console logs.

There’s the beauty of Webpack. We can now expand our app to contain any number of JavaScript files, and use Webpack to merge them all into one.

  1. Okay, let’s ensure that those console logs are working. In our public/index.html, add another script tag below the other three:
<script src="bundle.js"></script>
  1. Reload the page, open up your console, and you'll have this:
主站蜘蛛池模板: 枞阳县| 扶绥县| 新郑市| 达日县| 顺平县| 赤城县| 比如县| 德庆县| 赣榆县| 长治市| 华蓥市| 同江市| 宿州市| 襄汾县| 顺平县| 沙坪坝区| 岚皋县| 新蔡县| 都兰县| 昔阳县| 菏泽市| 镇宁| 鄄城县| 丹棱县| 安平县| 双辽市| 安远县| 来凤县| 拉孜县| 达拉特旗| 连州市| 盈江县| 华亭县| 黑山县| 泰安市| 陆川县| 喜德县| 大渡口区| 申扎县| 潮州市| 顺昌县|