背景
前端工程化是构建一个完整开源项目的必备过程,此处通过浅读cac.js的项目工程来分析下常见的node工程中的各个常用文件作用。
分析目录
cac
├─ .editorconfig
├─ .gitattributes
├─ .github
│ ├─ FUNDING.yml
│ └─ ISSUE_TEMPLATE.md
├─ .gitignore
├─ .prettierrc
├─ LICENSE
├─ README.md
├─ circle.yml
├─ examples
├─ index-compat.js
├─ jest.config.js
├─ mod.js
├─ mod.ts
├─ mod_test.ts
├─ package.json
├─ rollup.config.js
├─ scripts
│ └─ build-deno.ts
├─ src
│ ├─ CAC.ts
│ ├─ Command.ts
│ ├─ Option.ts
│ ├─ __test__
│ ├─ deno.ts
│ ├─ index.ts
│ ├─ node.ts
│ └─ utils.ts
├─ tsconfig.json
└─ yarn.lock
- editorconfig
- gitattributes
- .github: github 配置文件
- FUNDING.yml: github 赞助配置文件
- ISSUE_TEMPLATE.yaml: github yaml 模板配置文件
- .gitignore
- .prettierrc
- LICENSE
- README:项目介绍文件
- circle.yaml: CircleCI 的配置文件
- examples: 示例
- index.compat.js: 主入口,主要是为了兼容 cjs
- jest.config.js: jest 配置文件
- mod.*: 兼容 deno
- package.json
- rollup.config.js: rollup 配置文件(主要是打包用的)
- tsconfig.json: TS 配置文件
- scripts: 项目中用到的脚本
- src: 项目主目录
- yarn.lock: yarn 的依赖锁文件
.editorconfig是干嘛的
用于统一不同 IDE 编码风格配置的一种配置文件。
.gitattributes 是干嘛的
用于处理不同操作系统下的差异,比如window下换行是CRLF,linux下是LF。
* text=auto
checkin时会自动转换为LF。
持续集成是如何实现的
version: 2 版本号
jobs: 步骤单元
build: job名称
docker: 指定运行环境,此处是node环境
- image: circleci/node:12 指定镜像
branches:
ignore: 需要忽略的分支
- gh-pages # list of branches to ignore
- /release\/.*/ # or ignore regexes
steps:
- checkout 把项目源码检出至 job 的工作区 working-directory
- restore_cache: 恢复缓存
key: dependency-cache-{{ checksum "yarn.lock" }}
- run: 指定要执行的命令 此处相当于执行yarn 来安装依赖
name: install dependences
command: yarn
- save_cache: 设置缓存
key: dependency-cache-{{ checksum "yarn.lock" }}
paths:
- ./node_modules
- run: 指定要执行的命令 此处相当于执行yarn test 来执行单元测试
name: test
command: yarn test:cov
- run: 不太明白,似乎是一个上传命令
name: upload coverage
command: bash <(curl -s https://codecov.io/bash)
- run: 指定要执行的命令 用于发布
name: Release
command: yarn semantic-release
分析一下单元测试环境是如何搭建的
- ts-jest
- jest的transformer用于可以使用jest测试ts项目
- jest.config.js
module.exports = { testEnvironment: 'node', // 基于node环境 transform: { '^.+\\.tsx?$': 'ts-jest' // 配置规则转换,如 jest 原本支持 cjs,但是目前前端项目,一般都是使用 esm 规范进行模块化开发,所以可以借助其对命中规则的文件交由如 ts-jest 处理 }, testRegex: '(/__test__/.*|(\\.|/)(test|spec))\\.tsx?$', // 匹配需要执行的测试文件 testPathIgnorePatterns: ['/node_modules/', '/dist/', '/types/'], // 忽略哪些文件路径 moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'] // 测试覆盖的文件的扩展名 }