理解 Webpack:为什么要添加这几行 javascript?

Lor*_*ssi 2 javascript laravel webpack laravel-mix

我正在学习 Laravel 并试图了解如何使用Laravel Mix来捆绑资产(scss 和 javascript)。

正如文档所说:

Laravel Mix 提供了一个流畅的 API,用于使用几种常见的 CSS 和 JavaScript 预处理器为 Laravel 应用程序定义 Webpack 构建步骤。


我认为Webpack会简单地将所有 javascript 文件连接成一个(并可选择缩小它),就像它处理.scss文件一样,这些文件被编译成 CSS 并合并到public/css/app.css

正如这个答案所说:

Webpack 是一个命令行工具,用于创建资产包(代码和文件)。Webpack 不在服务器或浏览器上运行。Webpack 获取您所有的 javascript 文件和任何其他资产,然后将其转换为一个巨大的文件。


如果它实际上只是一个构建工具并且它的任何部分都没有在浏览器上运行,那么我不明白为什么它在我构建时将这个 javascript 添加到编译文件的顶部npm run dev(以及它究竟做了什么):

/******/ (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] = {
/******/            i: moduleId,
/******/            l: false,
/******/            exports: {}
/******/        };
/******/
/******/        // Execute the module function
/******/        modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/        // Flag the module as loaded
/******/        module.l = 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;
/******/
/******/    // define getter function for harmony exports
/******/    __webpack_require__.d = function(exports, name, getter) {
/******/        if(!__webpack_require__.o(exports, name)) {
/******/            Object.defineProperty(exports, name, {
/******/                configurable: false,
/******/                enumerable: true,
/******/                get: getter
/******/            });
/******/        }
/******/    };
/******/
/******/    // getDefaultExport function for compatibility with non-harmony modules
/******/    __webpack_require__.n = function(module) {
/******/        var getter = module && module.__esModule ?
/******/            function getDefault() { return module['default']; } :
/******/            function getModuleExports() { return module; };
/******/        __webpack_require__.d(getter, 'a', getter);
/******/        return getter;
/******/    };
/******/
/******/    // Object.prototype.hasOwnProperty.call
/******/    __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/    // __webpack_public_path__
/******/    __webpack_require__.p = "";
/******/
/******/    // Load entry module and return exports
/******/    return __webpack_require__(__webpack_require__.s = 0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {

__webpack_require__(1);
module.exports = __webpack_require__(2);


/***/ }),
/* 1 */
/***/ (function(module, exports) {



/***/ }),
/* 2 */
/***/ (function(module, exports) {

// removed by extract-text-webpack-plugin

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

如果我运行npm run prod,这是 public/app.js 中的输出:

!function(n){function t(e){if(r[e])return r[e].exports;var o=r[e]={i:e,l:!1,exports:{}};return n[e].call(o.exports,o,o.exports,t),o.l=!0,o.exports}var r={};t.m=n,t.c=r,t.d=function(n,r,e){t.o(n,r)||Object.defineProperty(n,r,{configurable:!1,enumerable:!0,get:e})},t.n=function(n){var r=n&&n.__esModule?function(){return n.default}:function(){return n};return t.d(r,"a",r),r},t.o=function(n,t){return Object.prototype.hasOwnProperty.call(n,t)},t.p="",t(t.s=0)}([function(n,t,r){r(1),n.exports=r(2)},function(n,t){},function(n,t){}]);
Run Code Online (Sandbox Code Playgroud)


这是我的webpack.mix.js文件的内容:

let mix = require('laravel-mix');

/*
 |--------------------------------------------------------------------------
 | Mix Asset Management
 |--------------------------------------------------------------------------
 |
 | Mix provides a clean, fluent API for defining some Webpack build steps
 | for your Laravel application. By default, we are compiling the Sass
 | file for the application as well as bundling up all the JS files.
 |
 */

mix.js('resources/assets/js/app.js', 'public/js')
   .sass('resources/assets/sass/app.scss', 'public/css');
Run Code Online (Sandbox Code Playgroud)


而且,由于resources/assets/js/app.js文件是空的,我希望(编译的)public/js/app.js也是完全空的。

Jef*_*eff 6

如果您有课本、计算器、午餐盒和三个格兰诺拉麦片棒,WebPack 可以将它们全部放入一个背包中,这样您就可以更轻松地携带它。那个代码就是背包。即使你没有东西可以放进去,它仍然是一个背包。

部分原因是拆分可能会相互干扰的代码,如果您将它们放在一个命名空间/范围内。如果范围不正确,将代码放在多个文件中可能会产生意外干扰,而 WebPack 使用其中的一些代码来处理所有这些。

是的,它像 SCSS 一样编译和缩小,但 WebPack 还添加了更多需要一些样板代码的功能。