本文转载自微信公众号「DYBOY」,作者DYBOY。转载本文请联系DYBOY公众号。

我们提供的服务有:成都做网站、成都网站制作、微信公众号开发、网站优化、网站认证、德兴ssl等。为成百上千企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的德兴网站制作公司
随着互联网行业的发展,前端应用也逐渐变得复杂,所以自然而然地前端工程化开发是必然道路,百家争名的时代,涌现了许多优秀的构建工具,今天想从学习的角度,和大家分享一下笔者的学习方法和思路。
笔者个人观点看来,想要完全掌握前端工程化,那么就得对行业现有的构建工具、流程管理、服务管理有一个全面以及深入的认识,无论是前端还是后端开发者,我们通过任意编程语言编写软件应用,这是基本能力,但作为工程师,我想那就是得具有工程化的能力,我们应该在开发工程中能够具有把控全局的能力,有业务上的视野,也得有技术上的沉淀,应该时刻思考,从程序设计、流程设计、方案设计上尽可能得去逼近符合业务场景的最佳实践。
一、什么是Rollup
Rollup 是一个 JavaScript 模块打包器,可以将小块代码编译成大块复杂的代码,例如 library 或应用程序 —— Rollup文档
可以理解为,Rollup是一款集成式的代码打包、应用构建工具。
二、为什么要使用构建工具
如前文所述,前端工程化是作为工程师的必要能力,工程化并非一个人就能搞定的,我们需要站在前人/巨人的肩膀看世界。
随着行业的发展,涌现了一些获得行业认可(可以认为是最佳实践)的构建工具,这些构建工具将一些项目管理和编程开发的行业最佳实践集成到了一个“脚手架”工具中。如此,便有利于其他团队快速构建出高效、高质量的工程化项目。
构建工具能做的事:
- 处理兼容性
- 混淆、压缩代码
- Tree Shaking
- 转译
- 单元测试
- 打包应用
三、上手Rollup
Rollup是一个我认为还比较简单的构建工具,此处先不去讨论其深层次的构建实现细节,先看看如何上手使用。
其实关于Rollup的使用还是比较推荐大家阅读官方文档:https://rollupjs.org/guide/
「3.1 安装」
全局安装roolup
- yarn add rollup -g
「3.2 简单使用」
使用rollup打包构建有两种方式,直接在命令行下需要手打构建必须的参数,当然我们是做工程,那么就得考虑如何尽可能减少团队成员的上手成本。
为此,Rollup提供了通过文件化配置方式,预设所需的参数,同时通过更改配置文件的参数,可以自由地实现我们自定义的需求。
这里大家可能会想到写一个CLI,工欲善其事必先利其器,咱先认识工具才能造工具,就先不扯远了。
3.2.1 创建配置文件
在项目的根目录或其他你满意的位置创建一个 rollup.config.js 文件,其内容主要如下:
- export default {
- input: "./src/index.js",
- output: {
- file: "./dist/bundle.js",
- format: "esm",
- name: "RollupLearn",
- },
- }
3.2.2 添加快捷命令
为了简单,另外再在package.json中配置快捷命令:
- "scripts": {
- "building": "rollup -c -w",
- },
这样就可以直接在命令行下通过执行:yarn building,就创建了一个实时监听文件变化就自动重新构建打包的开发环境。
3.2.3 rollup.config.js配置常用参数解读
官方罗列了rollup的配置参数如下:
- export default {
- // 核心选项
- input, // 必须,打包的入口文件,常见index.js
- external,
- plugins, // 常用:数组中配置一些打包的插件,例如babel等
-
- // 额外选项
- onwarn,
-
- // danger zone
- acorn,
- context,
- moduleContext,
- legacy
-
- output: { // 必须 (如果要输出多个,可以是一个数组)
- // 核心选项
- file, // 必须,生成的打包文件名&路径
- format, // 必须,打包构建的目标模块标准,有cjs、amd、umd、esm、iife等
- name,
- globals,
-
- // 额外选项
- paths,
- banner,
- footer,
- intro,
- outro,
- sourcemap, // 常用,生成sourcemap文件
- sourcemapFile,
- interop,
-
- // 高危选项
- exports,
- amd,
- indent
- strict
- },
- };
简单讲一下几个常用的功能设置
(1)输入(input -i / --input)
String 这个包的入口点 (例如:你的 main.js 或者 app.js 或者 index.js)
(2)文件(file -o / --output.file)
String 要写入的文件。也可用于生成 sourcemaps,如果适用
(3)格式(format -f / --output.format)
String 生成包的格式。下列之一:
- amd – 异步模块定义,用于像RequireJS这样的模块加载器
- cjs – CommonJS,适用于 Node 和 Browserify/Webpack
- esm – 将软件包保存为 ES 模块文件,在现代浏览器中可以通过
- amd – 异步模块定义,用于像RequireJS这样的模块加载器
- cjs – CommonJS,适用于 Node 和 Browserify/Webpack
- esm – 将软件包保存为 ES 模块文件,在现代浏览器中可以通过
基本
文件
流程
错误
SQL
调试
- 请求信息 : 2026-02-22 19:21:09 HTTP/1.1 GET : /article/dpjshds.html
- 运行时间 : 0.0766s ( Load:0.0045s Init:0.0007s Exec:0.0669s Template:0.0044s )
- 吞吐率 : 13.05req/s
- 内存开销 : 2,259.99 kb
- 查询信息 : 12 queries 5 writes
- 文件加载 : 36
- 缓存信息 : 0 gets 2 writes
- 配置加载 : 130
- 会话信息 : SESSION_ID=rlr3bma78o0kmgdi0ftoek7ft5
- /home/wwwroot/jxjierui.cn/index.php ( 1.12 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/ThinkPHP.php ( 4.61 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Think.class.php ( 12.26 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Storage.class.php ( 1.37 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Storage/Driver/File.class.php ( 3.52 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Mode/common.php ( 2.82 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Common/functions.php ( 53.56 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Hook.class.php ( 4.01 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/App.class.php ( 13.49 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Dispatcher.class.php ( 14.79 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Route.class.php ( 13.36 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Controller.class.php ( 11.23 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/View.class.php ( 7.59 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/BuildLiteBehavior.class.php ( 3.68 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/ParseTemplateBehavior.class.php ( 3.88 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/ContentReplaceBehavior.class.php ( 1.91 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Conf/convention.php ( 11.15 KB )
- /home/wwwroot/jxjierui.cn/App/Common/Conf/config.php ( 2.12 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Lang/zh-cn.php ( 2.55 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Conf/debug.php ( 1.48 KB )
- /home/wwwroot/jxjierui.cn/App/Home/Conf/config.php ( 0.32 KB )
- /home/wwwroot/jxjierui.cn/App/Home/Common/function.php ( 3.33 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/ReadHtmlCacheBehavior.class.php ( 5.62 KB )
- /home/wwwroot/jxjierui.cn/App/Home/Controller/ArticleController.class.php ( 6.11 KB )
- /home/wwwroot/jxjierui.cn/App/Home/Controller/CommController.class.php ( 1.60 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Model.class.php ( 60.11 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Db.class.php ( 32.43 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Db/Driver/Pdo.class.php ( 16.74 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Cache.class.php ( 3.83 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Cache/Driver/File.class.php ( 5.87 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Template.class.php ( 28.16 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Template/TagLib/Cx.class.php ( 22.40 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Template/TagLib.class.php ( 9.16 KB )
- /home/wwwroot/jxjierui.cn/App/Runtime/Cache/Home/7540f392f42b28b481b30614275e4e55.php ( 13.96 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/WriteHtmlCacheBehavior.class.php ( 0.97 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/ShowPageTraceBehavior.class.php ( 5.24 KB )
- [ app_init ] --START--
- Run Behavior\BuildLiteBehavior [ RunTime:0.000006s ]
- [ app_init ] --END-- [ RunTime:0.000032s ]
- [ app_begin ] --START--
- Run Behavior\ReadHtmlCacheBehavior [ RunTime:0.000198s ]
- [ app_begin ] --END-- [ RunTime:0.000223s ]
- [ view_parse ] --START--
- [ template_filter ] --START--
- Run Behavior\ContentReplaceBehavior [ RunTime:0.000052s ]
- [ template_filter ] --END-- [ RunTime:0.000069s ]
- Run Behavior\ParseTemplateBehavior [ RunTime:0.003524s ]
- [ view_parse ] --END-- [ RunTime:0.003539s ]
- [ view_filter ] --START--
- Run Behavior\WriteHtmlCacheBehavior [ RunTime:0.000062s ]
- [ view_filter ] --END-- [ RunTime:0.000070s ]
- [ app_end ] --START--
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') LIMIT 1' at line 1
[ SQL语句 ] : SELECT `id`,`pid`,`navname` FROM `cx_nav` WHERE ( id= ) LIMIT 1
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') LIMIT 1' at line 1
[ SQL语句 ] : SELECT `id`,`navname` FROM `cx_nav` WHERE ( id= ) LIMIT 1
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
[ SQL语句 ] : SELECT `id`,`navname` FROM `cx_nav` WHERE ( pid= )
- [8] Undefined index: pid /home/wwwroot/jxjierui.cn/App/Home/Controller/ArticleController.class.php 第 47 行.
- [2] file_put_contents(./App/Runtime/Temp/1a887d41abc8166e50e1f88cb12039e6.php): failed to open stream: Permission denied /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Cache/Driver/File.class.php 第 132 行.
- [8] Undefined index: db_host /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Db.class.php 第 120 行.
- [8] Undefined index: db_port /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Db.class.php 第 121 行.
- [8] Undefined index: db_name /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Db.class.php 第 122 行.
- [2] file_put_contents(./App/Runtime/Temp/9df8221a6c20961a61842249d76e8744.php): failed to open stream: Permission denied /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Cache/Driver/File.class.php 第 132 行.

0.0766s
