javascript - Webpack Bundle File - What Does it Do? - Stack Overflow

I really don't understand the point of WebPack, I read an intro here and a bunch of other tutorial

I really don't understand the point of WebPack, I read an intro here and a bunch of other tutorials, but it seems like I have to ask individual questions... I am following an example by creating 2 files of a basic site:

app.js:

document.write('wele to my app');
console.log('app loaded');

index.html:

<html>
  <body>
    <script src="bundle.js"></script>
  </body>
</html>

Then I run webpack ./app.js bundle.js from the CLI to create the bundle file, which happens.

So...how does the bundle file get used now? What is it? I thought it essentially piled "everything" into a single file and then uglified it, but that doesn't seem to be the case, some of the ouput looks like this (edited to include full output):

/******/ (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('wele to my app');

    console.log('app loaded');

/***/ }
/******/ ]);

So, what's the point? Can the app run from this bundle file? Does the bundle get referenced somehow? Do I still need the original index.html and app.js files once the bundle is built?

I really don't understand the point of WebPack, I read an intro here and a bunch of other tutorials, but it seems like I have to ask individual questions... I am following an example by creating 2 files of a basic site:

app.js:

document.write('wele to my app');
console.log('app loaded');

index.html:

<html>
  <body>
    <script src="bundle.js"></script>
  </body>
</html>

Then I run webpack ./app.js bundle.js from the CLI to create the bundle file, which happens.

So...how does the bundle file get used now? What is it? I thought it essentially piled "everything" into a single file and then uglified it, but that doesn't seem to be the case, some of the ouput looks like this (edited to include full output):

/******/ (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('wele to my app');

    console.log('app loaded');

/***/ }
/******/ ]);

So, what's the point? Can the app run from this bundle file? Does the bundle get referenced somehow? Do I still need the original index.html and app.js files once the bundle is built?

Share Improve this question edited Jan 3, 2017 at 19:35 VSO asked Jan 3, 2017 at 19:26 VSOVSO 12.7k28 gold badges116 silver badges202 bronze badges 2
  • Show your webpack.config.js – haim770 Commented Jan 3, 2017 at 19:28
  • A module bundler takes an entry file, determines all of its dependencies (i.e. other files/modules the file loads) and bines them into one single file (the "bundle"). – Felix Kling Commented Jan 3, 2017 at 19:32
Add a ment  | 

1 Answer 1

Reset to default 7

I'm the author of the first article you linked. A "bundled" file just means all the Javascript your app/website needs to run, piled into one file the browser can understand. For example, in your source code you might use require() or import statements. The browser has no idea how to execute those, so Webpack will pile all of your Javascript code into a "bundled" file that the browser can understand and execute without error.

Webpack doesn't minify code by default, you have to use the uglify plugin.

You do not need the original source code, just the output built Javascript file by Webpack. You need to set up your HTML to read the bundled file. Generally you have two Webpack configs, one for local development and one for production deployment.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745217887a4617110.html

相关推荐

  • javascript - Webpack Bundle File - What Does it Do? - Stack Overflow

    I really don't understand the point of WebPack, I read an intro here and a bunch of other tutorial

    3小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信