Skip to content

Commit

Permalink
feat(core): new life-cycle: beforeBuild & afterBuild
Browse files Browse the repository at this point in the history
  • Loading branch information
Diablohu committed Jan 26, 2021
1 parent bf0c5bd commit 25f6f24
Show file tree
Hide file tree
Showing 26 changed files with 317 additions and 59 deletions.
13 changes: 9 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- 此次更新包含诸多重大改动,对于已有项目的升级,请参阅[升级指南](https://koot.js.org/#/migration/0.14-to-0.15)
- _Node.js_ 最低版本要求提升到 `10.16.0`
- 更新依赖包 **major** 版本号
- `commander` -> _^7.0.0_
- `execa` -> _^5.0.0_
- `koa-convert` -> _^2.0.0_
- `koa-helmet` -> _^6.0.0_
Expand All @@ -21,6 +22,10 @@
- 现已支持全新的 _JSX_ 转译引擎
- 该功能对从 0.15 之前版本升级而来的项目默认关闭,如需开启请参阅[升级指南](https://koot.js.org/#/migration/0.14-to-0.15)
- 相关信息请查阅 [React 官方文档](https://reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html)
- **新配置项** `beforeBuild` - 生命周期方法: 打包即将开始时
- 详情请参见文档 [生命周期](https://koot.js.org/#/life-cycle?id=打包)
- **新配置项** `afterBuild` - 生命周期方法: 打包刚刚完成时
- 详情请参见文档 [生命周期](https://koot.js.org/#/life-cycle?id=打包)
- 优化
- `serviceWorker``cacheFirst` `networkFirst` `networkOnly` 扩展缓存策略选项,其数组 (`Array`) 内现在可以直接传入正则表达式和用以分析请求的函数,请参见文档 [Service Worker/扩展缓存规则](/pwa?id=扩展缓存规则)
- 添加依赖包
Expand All @@ -44,15 +49,15 @@
- `copy-webpack-plugin` -> _^7.0.0_
- `css-loader` -> _^5.0.1_
- `less` -> _^4.1.0_
- `less-loader` -> _^7.2.1_
- `less-loader` -> _^7.3.0_
- `mini-css-extract-plugin` -> _^1.3.4_
- `postcss` -> _^8.2.4_
- `postcss-loader` -> _^4.1.0_
- `postcss-loader` -> _^4.2.0_
- `sass-loader` -> _^10.1.1_
- `style-loader` -> _^2.0.0_
- `thread-loader` -> _^3.0.0_
- `webpack` -> _^5.16.0_
- `webpack-bundle-analyzer` -> _^4.3.0_
- `webpack` -> _^5.17.0_
- `webpack-bundle-analyzer` -> _^4.4.0_
- `webpack-dev-middleware` -> _^4.0.4_
- `workbox-webpack-plugin` -> _^6.0.2_

Expand Down
54 changes: 54 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,8 @@ module.exports = {

Webpack 打包执行之前执行的方法。

****: 该生命周期针对**每个**打包流程,如客户端和服务器端打包时均会触发。若只需要打包流程前执行一次的生命周期,请参照 `beforeBuild`

```javascript
module.exports = {
// 默认值
Expand Down Expand Up @@ -1017,6 +1019,8 @@ module.exports = {

Webpack 打包执行之后执行的方法。

****: 该生命周期针对**每个**打包流程,如客户端和服务器端打包时均会触发。若只需要打包流程后执行一次的生命周期,请参照 `afterBuild`

```javascript
module.exports = {
// 默认值
Expand All @@ -1033,6 +1037,56 @@ module.exports = {
};
```

### beforeBuild

- 类型: `Function`
- 默认值: __
- **仅针对**: Webpack 打包过程

在打包流程即将开始之前,执行的方法。

```javascript
module.exports = {
// 默认值
beforeBuild: undefined,

/**
* @async
* @param {Object} appConfig 经过兼容处理后的完整的配置对象
* @void
*/
beforeBuild: async (appConfig) => {
// 案例:动态压缩图片资源
},
};
```

### afterBuild

- 类型: `Function`
- 默认值: __
- **仅针对**: Webpack 打包过程

在打包流程结束之后立刻执行的方法。

****: 开发环境热更新时不会触发。

```javascript
module.exports = {
// 默认值
afterBuild: undefined,

/**
* @async
* @param {Object} appConfig 经过兼容处理后的完整的配置对象
* @void
*/
afterBuild: async (appConfig) => {
// 案例:自动发布到线上服务器
},
};
```

---

## 开发环境 & 开发设置
Expand Down
15 changes: 14 additions & 1 deletion docs/life-cycle.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,24 @@ _Koot.js_ 为客户端和服务器端分别提供了生命周期,可编写回

---

### 打包

在打包过程中执行。

配置方式请查阅 [项目配置/打包 & Webpack](/config?id=打包-amp-webpack)

| 方法 | 执行时机 |
| ------------------------ | --------------------------------------------------------------------------- |
| `beforeBuild(appConfig)` | 在打包流程即将开始之前触发 |
| `afterBuild(appConfig)` | 在打包流程结束之后立刻触发<br><b>注</b>: 开发环境热更新时不会触发该生命周期 |

---

### 客户端

在浏览器中运行的代码。

配置方式请查阅 [项目配置/客户端生命周期](/config?id=客户端生命周期)
配置方式请查阅 [项目配置/客户端设置 & 生命周期](/config?id=客户端设置-amp-生命周期)

| 方法 | 执行时机 |
| -------------------------------------- | ----------------------------------------- |
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@
"cheerio": "^1.0.0-rc.5",
"crlf": "^1.1.1",
"cz-customizable": "^6.3.0",
"eslint": "^7.17.0",
"eslint": "^7.18.0",
"eslint-config-koot": "^3.1.1",
"fs-extra": "^9.0.1",
"husky": "^4.3.7",
"fs-extra": "^9.1.0",
"husky": "^4.3.8",
"inquirer": "^7.3.3",
"is-valid-path": "^0.1.1",
"jest": "^26.6.3",
Expand Down
3 changes: 3 additions & 0 deletions packages/koot-boilerplate/koot.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
* 配置文档请查阅: [https://koot.js.org/#/config]
*/

require('koot/typedef');

const fs = require('fs');
const path = require('path');

/** @type {AppConfig} */
module.exports = {
/**************************************************************************
* 项目基本信息
Expand Down
6 changes: 3 additions & 3 deletions packages/koot-boilerplate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@
},
"devDependencies": {
"@types/classnames": "^2.2.11",
"autoprefixer": "^10.2.1",
"autoprefixer": "^10.2.3",
"babel-eslint": "^10.1.0",
"cssnano": "^4.1.10",
"eslint": "^7.17.0",
"eslint": "^7.18.0",
"eslint-config-koot": "^3.1.1",
"husky": "^4.3.7",
"husky": "^4.3.8",
"lint-staged": "^10.5.3",
"normalize.css": "^8.0.1",
"prettier": "^2.2.1",
Expand Down
4 changes: 2 additions & 2 deletions packages/koot-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"axios": "^0.21.1",
"chalk": "^4.1.0",
"download-git-repo": "^3.0.2",
"fs-extra": "^9.0.1",
"fs-extra": "^9.1.0",
"gitignore-globs": "^0.1.1",
"glob": "^7.1.6",
"glob-promise": "^3.4.0",
Expand All @@ -61,7 +61,7 @@
"isbinaryfile": "^4.0.6",
"latest-version": "^5.1.0",
"npm-email": "^3.2.1",
"ora": "^5.2.0",
"ora": "^5.3.0",
"os-locale": "^5.0.0",
"package-json": "^6.5.0",
"sanitize-filename": "^1.6.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/koot-electron/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"node": ">= 8.9.0"
},
"dependencies": {
"electron": "^11.2.0",
"electron": "^11.2.1",
"electron-builder": "^22.9.1"
},
"devDependencies": {
Expand Down
9 changes: 9 additions & 0 deletions packages/koot-webpack/building/client-prod.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint-disable no-console */

require('koot/typedef');

const fs = require('fs-extra');
const chalk = require('chalk');
const webpack = require('webpack');
Expand All @@ -16,6 +18,13 @@ const spinner = require('koot/utils/spinner');

const statsHandling = require('../libs/building-webpack-stats-handling');

/**
* @async
* 打包: 客户端 | 生产环境
* @param {Object} param
* @param {AppConfig} param.appConfig
* @returns {Object} result 结果对象
*/
async function buildClientProd({
appConfig = {},
resultStats = {},
Expand Down
8 changes: 4 additions & 4 deletions packages/koot-webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@
"css-loader": "^5.0.1",
"extract-hoc": "^0.0.5",
"less": "^4.1.0",
"less-loader": "^7.2.1",
"less-loader": "^7.3.0",
"mini-css-extract-plugin": "^1.3.4",
"postcss": "^8.2.4",
"postcss-loader": "^4.1.0",
"postcss-loader": "^4.2.0",
"react-hot-loader": "^4.13.0",
"sass": "^1.32.5",
"sass-loader": "^10.1.1",
Expand All @@ -60,8 +60,8 @@
"style-loader": "^2.0.0",
"thread-loader": "^3.0.1",
"universal-alias-loader": "^1.2.2",
"webpack": "^5.16.0",
"webpack-bundle-analyzer": "^4.3.0",
"webpack": "^5.17.0",
"webpack-bundle-analyzer": "^4.4.0",
"webpack-config": "^7.5.0",
"webpack-dev-middleware": "^4.1.0",
"webpack-dev-server": "^3.11.2",
Expand Down
6 changes: 5 additions & 1 deletion packages/koot/bin/analyze.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const program = require('commander');

const willValidateConfig = require('./lifecycle/will-validate-config');
const willBuild = require('./lifecycle/will-build');
const didBuild = require('./lifecycle/did-build');

// const __ = require('../utils/translate')
const validateConfig = require('../libs/validate-config');
Expand All @@ -33,7 +34,7 @@ const run = async () => {
// 清空 log
process.stdout.write('\x1B[2J\x1B[0f');

const { client, server, stage: _stage, config, type } = program;
const { client, server, stage: _stage, config, type } = program.opts();

initNodeEnv();
// console.log(program)
Expand Down Expand Up @@ -85,6 +86,9 @@ const run = async () => {
// 清理结果目录
await fs.remove(dirAnalyzeBuild);

// 打包流程完成
await didBuild(kootConfig);

console.log(' ');
};

Expand Down
12 changes: 8 additions & 4 deletions packages/koot/bin/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ const chalk = require('chalk');

const willValidateConfig = require('./lifecycle/will-validate-config');
const willBuild = require('./lifecycle/will-build');
const didBuild = require('./lifecycle/did-build');

const {
// keyConfigQuiet,
filenameBuilding,
} = require('../defaults/before-build');
const { KOOT_BUILD_START_TIME } = require('../defaults/envs');

const __ = require('../utils/translate');
const sleep = require('../utils/sleep');
Expand Down Expand Up @@ -62,7 +62,7 @@ const run = async () => {
kootDev = false,
kootTest = false,
kootDevelopment = false,
} = program;
} = program.opts();

initNodeEnv();
// console.log(program)
Expand All @@ -84,8 +84,6 @@ const run = async () => {

process.env.KOOT_TEST_MODE = JSON.stringify(kootTest);
process.env.KOOT_DEVELOPMENT_MODE = JSON.stringify(kootDevelopment);
if (!process.env[KOOT_BUILD_START_TIME])
process.env[KOOT_BUILD_START_TIME] = Date.now() + '';

const stage = (() => {
if (_stage) return _stage;
Expand Down Expand Up @@ -154,6 +152,12 @@ const run = async () => {

await after(kootConfig);
// if (!fromCommandStart)

// 打包流程完成
if (!fromOtherCommand) {
await didBuild(kootConfig);
}

console.log(' ');

// 结束
Expand Down
10 changes: 8 additions & 2 deletions packages/koot/bin/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const opn = require('open');

const willValidateConfig = require('./lifecycle/will-validate-config');
const willBuild = require('./lifecycle/will-build');
const didBuild = require('./lifecycle/did-build');

const contentWaiting = require('../defaults/content-waiting');
const {
Expand Down Expand Up @@ -109,7 +110,7 @@ const run = async () => {
dll = true,
kootTest = false,
kootDevelopment = false,
} = program;
} = program.opts();

initNodeEnv();
setEnvFromCommand({
Expand Down Expand Up @@ -225,7 +226,7 @@ const run = async () => {
);

// 判断是否自动打开浏览器访问
let { open } = program;
let { open } = program.opts();
if (process.env.KOOT_PROJECT_TYPE === 'ReactElectronSPA') {
open = false;
} else if (!stageFromCommand && (typeof open === 'undefined' || open)) {
Expand Down Expand Up @@ -466,6 +467,8 @@ const run = async () => {
});

// console.log(' ')
// 打包流程完成
await didBuild(kootConfig);

await new Promise((resolve) => {
setTimeout(() => {
Expand Down Expand Up @@ -743,6 +746,9 @@ const run = async () => {
})
);

// 打包流程完成
await didBuild(kootConfig);

// 启动服务器
await start('run');

Expand Down
13 changes: 13 additions & 0 deletions packages/koot/bin/lifecycle/did-build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require('../../typedef');

/**
* 针对所有命令:在所有打包流程结束之后,此时会执行
* 1. 执行项目配置的 afterBuild 生命周期方法
* @async
* @param {AppConfig} appConfig
* @void
*/
module.exports = async (appConfig) => {
if (typeof appConfig.afterBuild === 'function')
await appConfig.afterBuild(appConfig);
};
Loading

0 comments on commit 25f6f24

Please sign in to comment.