Webpack 捆绑文件 - 它有什么作用?

VSO*_*VSO 1 html javascript webpack

我真的不明白 WebPack 的意义,我在这里阅读了介绍和一堆其他教程,但似乎我必须提出个人问题......我正在通过创建一个基本站点的 2 个文件来遵循一个示例:

应用程序.js:

document.write('welcome to my app');
console.log('app loaded');
Run Code Online (Sandbox Code Playgroud)

索引.html:

<html>
  <body>
    <script src="bundle.js"></script>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

然后我webpack ./app.js bundle.js从 CLI运行以创建包文件,这发生了。

那么......现在如何使用捆绑文件?它是什么?我认为它基本上将“所有内容”编译成一个文件,然后将其丑化,但似乎并非如此,一些输出看起来像这样(编辑为包括完整输出):

/******/ (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');

/***/ }
/******/ ]);
Run Code Online (Sandbox Code Playgroud)

那么,有什么意义呢?应用程序可以从此捆绑文件运行吗?捆绑包是否以某种方式被引用?构建捆绑包后,我还需要原始 index.htmlapp.js文件吗?

And*_*Ray 5

我是您链接的第一篇文章的作者。“捆绑”文件只是意味着您的应用程序/网站需要运行的所有 Javascript,编译成浏览器可以理解的一个文件。例如,在您的源代码中,您可能会使用require()orimport语句。浏览器不知道如何执行这些,因此 Webpack 会将您所有的 Javascript 代码编译成一个“捆绑”文件,浏览器可以理解和执行而不会出错。

默认情况下,Webpack 不会压缩代码,您必须使用 uglify 插件。

您不需要原始源代码,只需输出由 Webpack 构建的 Javascript 文件。您需要设置 HTML 以读取捆绑文件。通常你有两个 Webpack 配置,一个用于本地开发,一个用于生产部署。