Javascript 为什么要使用网页包?
我花了几天的时间来安装和运行Web包,刚刚进行了一次测试。然而,我发现webpack中的bundle.js文件做了很多对我来说毫无意义的不必要的事情 index.jsJavascript 为什么要使用网页包?,javascript,webpack,frontend,Javascript,Webpack,Frontend,我花了几天的时间来安装和运行Web包,刚刚进行了一次测试。然而,我发现webpack中的bundle.js文件做了很多对我来说毫无意义的不必要的事情 index.js import greet from './greet'; console.log("I'm the entry point"); greet(); greet.js function greet() { console.log('Have a great day!'); }; export default greet
import greet from './greet';
console.log("I'm the entry point");
greet();
greet.js
function greet() {
console.log('Have a great day!');
};
export default greet;
非常简单。但是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();
}
]);
看起来WebPack做了很多对我来说毫无意义的不必要的代码。bundle.js的文件大小也是index.js和greet.js的3倍。应用程序的开发版本看起来也很混乱,因为它很简单
那么,我为什么要继续投入时间在我的项目中使用WebPack呢?
它输出的所有额外代码是什么?为什么会有?
有没有更好的替代方案可以帮助我从模块化开发环境中发布代码
我非常感谢您帮助我理解为什么我应该或不应该使用WebPack
谢谢
bundle.js的文件大小也是index.js和greet.js的3倍
Webpack必须为浏览器无法完成的事情添加一些多边形填充,例如模块加载。如果您有两行代码,这些多边形填充看起来非常沉重,但是如果您编写了数千行代码,您不会注意到任何显著的开销,因为这些多边形填充只添加了一次
那么,我为什么要继续投入时间在我的项目中使用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 */
]);
有没有更好的替代方案可以帮助我从模块化开发环境中发布代码
还有其他选择,不确定它们是否“更好”。我同意Webpack添加了大量您甚至不需要的原油。它还有一个近乎疯狂的疯狂配置文件 为了简单起见,您可以只加载带有脚本标记的模块文件,然后将Webpack扔出窗口!(仅在现代浏览器中())
您还可以使用简单的网页包替代方案,如ParcelJS或Rollup。根据您的需要,这些编译器都可以做很多事情:
- 将模块捆绑到一个大的
bundle.js文件中
- 使代码与旧的旧浏览器兼容
- 将SCS转换为css,将typescript转换为javascript等
- 通过自动重新加载启动开发服务器
- 构建整个网站文件夹,仅包括项目中实际使用的文件
它被设计成一个替换静态文件服务器的交换。它支持ES2015,TypeScript,汇编脚本,生锈,C,C++,并可能很快支持JSX,TSX和VUE。您不必更改文件结构或直觉开发的方式。只需编写源代码,并将其与脚本元素或模块导入一起使用,它就可以正常工作。在say React中编写一个大型应用程序,无需任何捆绑,即可查看进展情况:)“您只需使用脚本标记加载模块文件,然后将Webpack扔出窗口”—或者更现实地说,您可以使用生成捆绑包的捆绑程序,这些捆绑包可以被现代浏览器和旧浏览器使用。“现实”取决于上下文。现在不是每个项目都必须在IE8上运行。但是是的,我不会说你不需要捆绑机。只是如果您正在学习javascript基础知识和模块,就不需要它。
<script type="module" src="greet.js">
<script type="module" src="app.js">