Rollup + eslint + prettier 搭建类库开发环境
上周,我对自己写的一个 uni-app 类库项目,进行了一些改造。引入了 Rollup 打包流程以及 eslint + prettier 代码规范的工具链。下面就来记录一下环境搭建的流程为日后的环境搭建做参考。
写在前面
![image]()
先对 Rollup 做一个简单的介绍吧。Rollup是一款 JavaScript 模块打包器,可以将小块代码编译成大块复杂的代码。从作用来看,Rollup与Webpack很相似,但Rollup更为小巧,仅仅是一款 Javascript 打包器;比如Rollup中不不支持类似的HMR这种高级特性
Rollup是为了提供一个充分利用ESM各项特性的高效(结构比较扁平,性能比较出众的类库)打包器,同时 Rollup 支持各种 plugin 来提升它的拓展性。我们每天都在用的Vue、React 这样的前端框架也是用 Rollup 来打包的。
相较于 Webpack 的大而全,适合打包构建一些完整的前端项目。Rollup 则更加适合打包一些纯 js 的类库项目。
安装依赖
- rollup # 打包工具
- rollup-plugin-node-resolve # 依赖引入插件
- rollup-plugin-commonjs # commonjs 转换 plugin
- rollup-plugin-eslint # eslint 校验 plugin
- rollup-plugin-babel
- @rollup/plugin-json
- babel-eslint
- babel-preset-env
- babel-preset-stage-0
- eslint
- eslint-config-prettier
- eslint-plugin-prettier
- prettier
- prettier-eslint
注:npm install 以上依赖是 记得加上 --save-dev 参数
Rollup 配置
在根目录新建 rollup.config.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
import path from "path"; import resolve from "rollup-plugin-node-resolve"; import commonJs from "rollup-plugin-commonjs"; import json from '@rollup/plugin-json' import { eslint } from "rollup-plugin-eslint"; import pkg from "./package.json";
const getPath = (_path) => path.resolve(__dirname, _path);
const extensions = ["js"];
const eslintPlugin = eslint({ throwOnError: true, include: ["src/**/*.js"], exclude: ["node_modules/**", "dist/**"], });
const commonConf = { input: getPath("./src/index.js"), plugins: [resolve(extensions), commonJs(), json(), eslintPlugin], };
const outputMap = [ { file: pkg.umd, format: "umd", }, { file: pkg.esm, format: "esm", }, ];
const buildConf = (options) => Object.assign({}, commonConf, options);
export default outputMap.map((output) => buildConf({ output: { name: pkg.name, ...output } }) );
|
eslint 配置
在配置完 Rollup 之后,让我们来配置一下书写规范相关的工具链。
首先 在根目录创建 eslintrc.cjs 文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| module.exports = { env: { browser: true, es6: true, }, parser:"babel-eslint", extends: ["eslint:recommended", "prettier"], plugins: ["prettier"], globals:{ process:'writable', uni:'writable', my:'readonly' }, rules: { "strict": 0, "no-unused-vars": ["error", { args: "none" }], "max-lines": [ "error", { max: 600, skipBlankLines: true, skipComments: true }, ], }, };
|
prettier 配置
在配置完了 eslint 后,我们当然还要配置 prettier 这个格式化工具来使得我们的代码规划化跟家如虎添翼。
还是相似的步骤,在根目录创建 prettierrc.cjs 文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| module.exports = { printWidth: 100, useTabs: false, tabWidth: 2, tabSize: 2, semi: true, singleQuote: true, quoteProps: 'as-needed', jsxSingleQuote: false, trailingComma: 'es5', bracketSpacing: true, jsxBracketSameLine: false, arrowParens: 'avoid', rangeStart: 0, rangeEnd: Infinity, requirePragma: false, insertPragma: false, proseWrap: 'preserve', htmlWhitespaceSensitivity: 'css', endOfLine: 'lf', };
|
package.json
最后 我们在 package.json 文件中添加 打包的 npm script,以及 babel 的简单配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| { ... "main":"dist/index.esm.js", "umd":"dist/index.umd.js", "esm":"dist/index.esm.js", "type":"module", "scripts":{ "build":"rm -rf dist && eslint -c .eslintrc.cjs && rollup -c rollup.config.js" }, "babel":{ "presets":{ "env", "stage-0" } } ... }
|
在配置好以上的所有之后,一个简单好用类库开发环境就可以开始使用了。