Blu*_*rry 3 javascript frontend webpack
我花了几天的时间来安装和运行Webpack,并进行了一次测试。但是我发现从webpack出来的bundle.js文件正在做很多不必要的事情,这些事情对我来说毫无意义。
index.js
import greet from './greet';
console.log("I'm the entry point");
greet();
Run Code Online (Sandbox Code Playgroud)
greet.js
function greet() {
console.log('Have a great day!');
};
export default greet;
Run Code Online (Sandbox Code Playgroud)
超级简单。但是bundle.js
!(function(e) {
var t = {};
function n(r) {
if (t[r]) return t[r].exports;
var o = (t[r] = { i: r, l: !1, exports: {} });
return e[r].call(o.exports, o, o.exports, n), (o.l = !0), o.exports;
}
(n.m = e),
(n.c = t),
(n.d = function(e, t, r) {
n.o(e, t) || Object.defineProperty(e, t, { enumerable: !0, get: r });
}),
(n.r = function(e) {
"undefined" != typeof Symbol &&
Symbol.toStringTag &&
Object.defineProperty(e, Symbol.toStringTag, { value: "Module" }),
Object.defineProperty(e, "__esModule", { value: !0 });
}),
(n.t = function(e, t) {
if ((1 & t && (e = n(e)), 8 & t)) return e;
if (4 & t && "object" == typeof e && e && e.__esModule) return e;
var r = Object.create(null);
if (
(n.r(r),
Object.defineProperty(r, "default", { enumerable: !0, value: e }),
2 & t && "string" != typeof e)
)
for (var o in e)
n.d(
r,
o,
function(t) {
return e[t];
}.bind(null, o)
);
return r;
}),
(n.n = function(e) {
var t =
e && e.__esModule
? function() {
return e.default;
}
: function() {
return e;
};
return n.d(t, "a", t), t;
}),
(n.o = function(e, t) {
return Object.prototype.hasOwnProperty.call(e, t);
}),
(n.p = ""),
n((n.s = 0));
})([
function(e, t, n) {
"use strict";
n.r(t);
var r = function() {
console.log("Have a great day!");
};
console.log("I'm the entry point"), r();
}
]);
Run Code Online (Sandbox Code Playgroud)
WebPack似乎在做很多不必要的代码,这对我来说毫无意义。bundle.js的文件大小也比index.js和greet.js大3倍。该应用程序的开发版本对于如此简单的操作也显得非常混乱和混乱。
那么,为什么我要继续花时间在我的项目中使用WebPack?它输出的所有额外代码是什么,为什么在其中?有没有其他更好的选择可以帮助我从模块化开发环境中发布代码?
非常感谢您的帮助,让我了解为什么我应该或不应该使用WebPack。
谢谢!
bundle.js的文件大小也比index.js和greet.js大3倍
Webpack必须为浏览器无法实现的功能(例如,模块加载)添加一些polyfill。如果您有2行代码,则这些polyfill看起来很沉重,但是,如果您编写了数千行代码,您将不会注意到任何明显的开销,因为这些poyfill仅添加一次。
那么,为什么我要继续花时间在我的项目中使用WebPack?
因为它会为较大的项目产生较小的包,所以它还允许您编写ESnext和干净的模块化代码。
它输出的所有额外代码是什么,为什么在其中?
它保持全局作用域整洁,添加一些帮助器和模块加载器,然后加载第一个模块:
// IIFE to keep global scope clean, ! to prevent Automatic Semicolon Insertion fun
!(function init(modules) {
var cache = {}; // cache the modules results
// All modules are in an array, their index is a unique identifier
function require/*n*/(index/*r*/) {
if (cache[index]) return cache[index].exports;
var context/*o*/= (cache[index = { index/*i*/: index, loaded/*l*/: false/*!1*/, exports: {} });
modules[index].call(
context.exports,
context,
context.exports,
require
);
context.loaded = true /*!0*/;
return context.exports;
}
require.modules = modules; // I'm not sure why?...
require.cache = cache;
// helper for adding a getter
require.addGetter /*n.d*/ = function(object, key, getter) {
require.has(object, key) || Object.defineProperty(object, key, { enumerable: true, get: getter });
});
require.prepareExport /*n.r*/ = function(export) {
if("undefined" != typeof Symbol && Symbol.toStringTag)
Object.defineProperty(export, Symbol.toStringTag, { value: "Module" });
Object.defineProperty(export, "__esModule", { value: true });
};
// I have no idea what that is doing
require.startModule /*n.s*/ = 0;
require(require.startModule); // start execution
})([
/* Your modules, identified by index */
function mainModule(context, exports, require) {
"use strict"; // better performance
require.prepareExport(export); // as you could override exports in your module, this has to be called afterwards
var otherModule = function() { // inlined!
console.log("Have a great day!");
};
console.log("I'm the entry point"),
otherModule();
} /* ... more modules would follow here if not inlined */
]);
Run Code Online (Sandbox Code Playgroud)
有没有其他更好的选择可以帮助我从模块化开发环境中发布代码?
有其他选择,不确定是否“更好”。
| 归档时间: |
|
| 查看次数: |
152 次 |
| 最近记录: |