gpt4 book ai didi

javascript - Webpack:即使源文件为空,编译后的文件中也有一些 JavaScript 代码

转载 作者:行者123 更新时间:2023-12-03 04:39:16 25 4
gpt4 key购买 nike

我正在使用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)
}]);

最佳答案

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) {



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

它基本上模仿了 Node.js 中的 require,同时还处理 ES 模块。所有这一切都是为了使其能够在浏览器(所有浏览器)中工作。另请参阅Node.js - Modules .

关于javascript - Webpack:即使源文件为空,编译后的文件中也有一些 JavaScript 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43151938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com