首页 > 其他分享 >rollupjs

rollupjs

时间:2023-01-12 10:46:16浏览次数:64  
标签:__ exports rollupjs require module js webpack

掉落神坛的webpack

webpack诞生之初的根本原因就是处理前端js模块化的工具。
如果浏览器本身慢慢的已经支持了模块化。
那么webpack存在的意义就不大了。

webpack的其它瑕疵

在wp中,哪怕是一行代码,最终也会budle成特别冗长的大文件。
因为编译后的代码包含有关于模块化方案的处理代码夹杂其中,侵入性极高
而且可读性比较差

造成的结果

在这种情况下,就想要寻求一种更好的解决方案,这便是 rollup
现在已经有很多类库都在使用 rollup 进行打包了,比如:react, vue, preact, three.js, moment, d3 等。

webpack VS rollup

两个文件的源代码如下

// index.js
import {add} from './util';
console.log(add);

// util.js
export const add = (x, y)=> {
    return x + y
}
export const reduce= (x, y)=> {
    return x - y
}

webpack编译后
生产

((()=>{"use strict";console.log(((o,s)=>o+s))})();

开发

/*
 * ATTENTION: The "eval" devtool has been used (maybe by default in mode: "development").
 * This devtool is not neither made for production nor for readable output files.
 * It uses "eval()" calls to create a separate source file in the browser devtools.
 * If you are trying to read the output file, select a different devtool (https://webpack.js.org/configuration/devtool/)
 * or disable the default devtool with "devtool: false".
 * If you are looking for production-ready output files, see mode: "production" (https://webpack.js.org/configuration/mode/).
 */
/******/ (() => { // webpackBootstrap
/******/ 	"use strict";
/******/ 	var __webpack_modules__ = ({

/***/ "./src/index.js":
/*!**********************!*
  !*** ./src/index.js ***!
  \**********************/
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {

eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var _util__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./util */ \"./src/util.js\");\n\r\nconsole.log(_util__WEBPACK_IMPORTED_MODULE_0__.add);\r\n\n\n//# sourceURL=webpack://webpack-demo/./src/index.js?");

/***/ }),

/***/ "./src/util.js":
/*!*********************!*
  !*** ./src/util.js ***!
  \*********************/
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {

eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */   \"add\": () => /* binding */ add,\n/* harmony export */   \"reduce\": () => /* binding */ reduce\n/* harmony export */ });\nconst add = (x, y)=> {\r\n    return x + y\r\n}\r\nconst reduce= (x, y)=> {\r\n    return x - y\r\n}\n\n//# sourceURL=webpack://webpack-demo/./src/util.js?");

/***/ })

/******/ 	});
/************************************************************************/
/******/ 	// The module cache
/******/ 	var __webpack_module_cache__ = {};
/******/ 	
/******/ 	// The require function
/******/ 	function __webpack_require__(moduleId) {
/******/ 		// Check if module is in cache
/******/ 		if(__webpack_module_cache__[moduleId]) {
/******/ 			return __webpack_module_cache__[moduleId].exports;
/******/ 		}
/******/ 		// Create a new module (and put it into the cache)
/******/ 		var module = __webpack_module_cache__[moduleId] = {
/******/ 			// no module.id needed
/******/ 			// no module.loaded needed
/******/ 			exports: {}
/******/ 		};
/******/ 	
/******/ 		// Execute the module function
/******/ 		__webpack_modules__[moduleId](module, module.exports, __webpack_require__);
/******/ 	
/******/ 		// Return the exports of the module
/******/ 		return module.exports;
/******/ 	}
/******/ 	
/************************************************************************/
/******/ 	/* webpack/runtime/define property getters */
/******/ 	(() => {
/******/ 		// define getter functions for harmony exports
/******/ 		__webpack_require__.d = (exports, definition) => {
/******/ 			for(var key in definition) {
/******/ 				if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
/******/ 					Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
/******/ 				}
/******/ 			}
/******/ 		};
/******/ 	})();
/******/ 	
/******/ 	/* webpack/runtime/hasOwnProperty shorthand */
/******/ 	(() => {
/******/ 		__webpack_require__.o = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop)
/******/ 	})();
/******/ 	
/******/ 	/* webpack/runtime/make namespace object */
/******/ 	(() => {
/******/ 		// define __esModule on exports
/******/ 		__webpack_require__.r = (exports) => {
/******/ 			if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ 				Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ 			}
/******/ 			Object.defineProperty(exports, '__esModule', { value: true });
/******/ 		};
/******/ 	})();
/******/ 	
/************************************************************************/
/******/ 	// startup
/******/ 	// Load entry module
/******/ 	__webpack_require__("./src/index.js");
/******/ 	// This entry module used 'exports' so it can't be inlined
/******/ })()
;

rollup

(function () {
    'use strict';

    const add = (x, y)=> {
        return x + y
    };

    console.log(add);

}());

可以看到,rollup编译后的可阅读性更强。
不过目前rollup并不支持动态引入,所以它更适用于单纯的js库,引用官方的话:

Rollup 是用来构建库还是应用程序?
Rollup 已被许多主流的 JavaScript 库使用,也可用于构建绝大多数应用程序。但是 Rollup 还不支持一些特定的高级功能,尤其是用在构建一些应用程序的时候,特别是代码拆分和运行时态的动态导入 dynamic imports at runtime. 如果你的项目中更需要这些功能,那使用 Webpack可能更符合你的需求。

浏览器现状

浏览器目前几乎已经全部支持模块化,除了ie

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script type="module">
        import {add} from './utils.js';
        console.log(add);
    </script>
</body>
</html>
export const add = (x, y)=> {
    return x + y
}
export const reduce= (x, y)=> {
    return x - y
}

标签:__,exports,rollupjs,require,module,js,webpack
From: https://www.cnblogs.com/dingshaohua/p/17045764.html

相关文章