【发布时间】:2017-01-03 19:26:08
【问题描述】:
我真的不明白 WebPack 的意义,我读了一个介绍 here 和一堆其他的 tutorials,但似乎我必须提出个别问题......我正在通过创建一个示例一个基本站点的 2 个文件:
app.js:
document.write('welcome to my app');
console.log('app loaded');
index.html:
<html>
<body>
<script src="bundle.js"></script>
</body>
</html>
然后我从 CLI 运行 webpack ./app.js bundle.js 以创建捆绑文件,这会发生。
那么...现在如何使用捆绑文件?它是什么?我认为它基本上将“所有内容”编译成一个文件,然后将其丑化,但似乎并非如此,一些输出看起来像这样(编辑以包含完整输出):
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports) {
document.write('welcome to my app');
console.log('app loaded');
/***/ }
/******/ ]);
那么,有什么意义呢?应用程序可以从此捆绑文件运行吗?捆绑包是否以某种方式被引用?构建捆绑包后,我还需要原始 index.html 和 app.js 文件吗?
【问题讨论】:
-
显示你的
webpack.config.js -
模块捆绑器获取一个入口文件,确定其所有依赖项(即文件加载的其他文件/模块)并将它们组合成一个文件(“捆绑包”)。
标签: javascript html webpack