- Progressive Web Apps with React
- Scott Domes
- 206字
- 2021-07-08 09:36:17
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:
- In our src folder, create another file. Let’s call it index2.js, for lack of creativity.
- Inside, add a second console.log:
console.log(‘Hello from index2.js!’);
- 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!"
- 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:

- 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.
- 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>
- Reload the page, open up your console, and you'll have this:

推薦閱讀
- Delphi程序設計基礎:教程、實驗、習題
- Web交互界面設計與制作(微課版)
- JAVA程序設計實驗教程
- 單片機C語言程序設計實訓100例
- Java EE核心技術與應用
- 批調度與網絡問題的組合算法
- Tableau 10 Bootcamp
- 現代C++編程實戰:132個核心技巧示例(原書第2版)
- uni-app跨平臺開發與應用從入門到實踐
- 計算機應用基礎項目化教程
- App Inventor少兒趣味編程動手做
- Java Web開發實例大全(基礎卷) (軟件工程師開發大系)
- Clojure High Performance Programming(Second Edition)
- Functional Python Programming
- 啊哈C語言!:邏輯的挑戰(修訂版)