0

我正在使用LaravelLaravel Mix。开箱即用,包含的 app.js 文件引导了很多依赖项,如 jQuery、Bootstrap-sass 等。

当我删除app.js文件中的所有内容时,为什么编译后的js文件中还有一些代码?它是 Webpack 的东西吗?即使在运行之后npm run production它仍然存在并且我无法弄清楚它的作用或它的含义并且不知道让我真的很不舒服。

这是留下的代码:

! 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.i = function(n) {
    return n
}, 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 = 3)
}([function(n, t) {}, function(n, t) {}, function(n, t) {}, function(n, t, r) {
r(0), r(1), n.exports = r(2)
}]);
4

1 回答 1

1

Webpack 将一些代码添加到包中以正确处理模块。这段代码被称为webpack bootstrap。如果你想知道代码做了什么,你应该看看非丑化版本。有很多评论,通常很容易理解它的要点,尽管您不需要了解每个细节。

这是非丑化代码(这是带有空模块的整个捆绑包):

/******/ (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;
/******/
/******/    // identity function for calling harmony imports with the correct context
/******/    __webpack_require__.i = function(value) { return value; };
/******/
/******/    // 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) {



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

它基本上是在模仿requireNode.js 的同时也处理 ES 模块。所有这一切都是为了让它在浏览器中工作(所有浏览器)。另请参阅Node.js - 模块

于 2017-04-01T02:49:26.423 回答