Skip to content

Commit

Permalink
Merge pull request #166 from ddnlink/father4
Browse files Browse the repository at this point in the history
升级到 Father4 打包工具
  • Loading branch information
imfly authored Oct 20, 2024
2 parents 6ada849 + 69337a4 commit de1677e
Show file tree
Hide file tree
Showing 163 changed files with 32,276 additions and 131,660 deletions.
7 changes: 7 additions & 0 deletions .fatherrc.base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {defineConfig} from 'father'

export default defineConfig({
cjs: {
output: 'dist'
}
})
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,9 @@ config/mainnet/*
**/**/*.hbl.js
**/**/*.custom.js
examples/peer*
# .ddnrc.js
# .ddnrc.js

.turbo
.pnpm-store
.husky
.github
21 changes: 21 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
registry=https://registry.npmjs.org/

use-node-version=20.13.1
# Prevent jsx namespace conflicts, because Vue global types will auto install.
# In the future, Vue 3 will fix this issue
# See https://github.com/vuejs/core/blob/main/CHANGELOG.md#features-1
auto-install-peers=false

# 在安装本地时自动连接
# 或者 pnpm add web --filter docs --link-workspace-packages=true
link-workspace-packages = true

# 当安装时,使用workspace协议,默认是 true,版本号就会是这样 "@ddn/asset-aob": "workspace:^1.0.0"
# save-workspace-protocol = false
save-prefix = '^'

build-from-source = true

# 使用类似 npm 的扁平化结构
; node-linker=hoisted
shamefully-hoist=true
27 changes: 27 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@

# [3.7.0](https://github.com/ddnlink/ddn/compare/v2.4.3...v3.7.0) (2024-08-05)


### Bug Fixes

* DDN release ([68a53ed](https://github.com/ddnlink/ddn/commit/68a53ed12f798b36150d3549728f625b551a24cc))
* set corepack disable to use pnpm ([ddbd1d0](https://github.com/ddnlink/ddn/commit/ddbd1d03e12f9c485c0a40c3ccde2ea73ea6eb2d))
* update .fatherrc.js ([3f89b46](https://github.com/ddnlink/ddn/commit/3f89b46d47ea44f563781fcc5224efca81f5a7e4))
* update @ddn/peer to write ddn.pid when isDaemonMode is true ([efd372a](https://github.com/ddnlink/ddn/commit/efd372add4bf4e6b9aab0fc1155407fe0caef5a4))
* update ddn-cli ([4c73dfd](https://github.com/ddnlink/ddn/commit/4c73dfd3a297fd31ce06f736bfb35d329225baa3))
* update require ([7c2d5ca](https://github.com/ddnlink/ddn/commit/7c2d5ca7b6d4313b2bec10572ccff233a5adcbe4))


### Features

* add access constans.*.js method to @ddn/core ([8c4c7bd](https://github.com/ddnlink/ddn/commit/8c4c7bd60dfa79aa8525455301ce1ef7f6a8fe36))
* add jest to root, delete @ddn/test and update @ddn/core to add getConstants methods ([1ee95a3](https://github.com/ddnlink/ddn/commit/1ee95a3e3b39eddf6de05e7d2f7ee6781b4a69f1))
* release @ddn/core 2.4.7 ([adf2ede](https://github.com/ddnlink/ddn/commit/adf2ede2a9a2ed965c6fbd61a45ba2b7794d425a))
* release 2.4.4 ([fccdd34](https://github.com/ddnlink/ddn/commit/fccdd34039a437d24b3e104a7dea49af15b28e1c))
* update @ddn/node-sdk for ddnrc.default.js erroo and jest config for pro-packages ([46eb734](https://github.com/ddnlink/ddn/commit/46eb73414712511bc330576c1bb0f2d5c149a04d))
* update asset* from .ddnrc.js to config, and Update to father4 + turbo + pnpm all right ([0ed2838](https://github.com/ddnlink/ddn/commit/0ed283880ab8b3a1f7976534661f84d31313012a))
* update docker, and crypto/crypto-base ([6ada849](https://github.com/ddnlink/ddn/commit/6ada849bede2f9838b66c20360fd6777992a0539))
* update father, jest to lastest and pnpm, packing all packages to dist by esbuild ([f798e26](https://github.com/ddnlink/ddn/commit/f798e266f6fa1e616f914407a49b0c00b3367a20))
* update to pack contract all right ([ec82cdd](https://github.com/ddnlink/ddn/commit/ec82cddc36de71f9e83d0db00b71ce321ed44694))


## [3.6.4](https://github.com/ddnlink/ddn/compare/v2.2.0...v3.6.4) (2022-08-06)


Expand Down
4 changes: 3 additions & 1 deletion README-zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ DDN, 是一个成熟的企业级区块链系统,已经服务于版权存证、
- [x] **多功能**, 支付转账、数据存证、链上资产(AoB)、去中心化应用(DAPP)等强大功能;
- [x] **多行业**, 当前已经覆盖版权保护、电子医疗、食品安全、防伪溯源等众多领域;
- [x] **可视化**, 你可以通过点点鼠标定制自己的区块链并部署到节点服务器上去,欢迎访问:<http://ui.ddn.net>;
- [ ] **Docker化**, 支持docker、云部署等更多主流部署方式;
- [x] **Docker化**, 支持docker、云部署等更多主流部署方式;
- [x] **全新教程**, 基于最新版本,推出区块链开发的全新教程;
- [x] **更多案例**, 请访问官网,<https://ddn.net>
- [ ] 更多功能,持续开发中...

## 架构
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ More infomation please visit our [official website](http://www.ddn.net) , [docs]
- [x] **Multi function**, payment, certificate storage, AOB, DAPP, etc;
- [x] **Multi industry**, currently covering copyright protection, electronic medicine, food safety, anti-counterfeiting traceability and other fields
- [x] **Visual deployment**, you can customize your own blockchain by clicking the mouse and publish it to the peer server, please access <http://ui.ddn.net>;
- [ ] **Docker**, tod support mainstream deployment methods such as docker;
- [x] **Docker**, tod support mainstream deployment methods such as docker;
- [x] **Tutorial**, based on the latest version, a new tutorial for blockchain development is launched;
- [x] **More cases**, please visit the official website,<https://ddn.net>
- [ ] More...

## Architect
Expand Down
5 changes: 2 additions & 3 deletions docs/api/http-api/blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,7 @@ JSON返回示例:
接口地址:/api/blocks/full
请求方式:get
支持格式:无
请求参数说明:无



请求参数说明:

Expand Down Expand Up @@ -336,4 +335,4 @@ JSON返回示例:
"transactions": []
}
}
```
```
51 changes: 51 additions & 0 deletions docs/development/build.zh-CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
项目构建
--------

## 概述

DDN 项目大多是 node.js 的后台应用,也就是 commonjs 项目,所以只要构建 cjs 规范的工具就可以。当然,在这个过程中,有 @ddn/js-sdk 是面向前端的,需要做 ESM 的转换(支持 IE11 以上,直接使用 esbuild 转换即可,与 cjs 难度一样)。更重要的是,有 @ddn/contract 等,是面向企业的,需要做 bundler 处理,将其打包、压缩并混淆,需要使用 UMD 格式,处理的事情会多一些。

综上所属,vite、umi 这类面向前端的打包构建工具是不适用的,只能寻求更加底层的打包工具,目前是 father4 ,它封装了 EsBuild 和 webpack 等工具,并且支持CJS、ESM和UMD的转换。但是,也需要配合 babel 等工具进行更加细致的处理。

在此基础上,再使用 turbo、pnpm 等多包管理工具,才能构建起完整的打包和开发工具链。

## 参考

- [rollup/parcel/esbuild](https://cloud.tencent.com/developer/article/2030300)
- [umijs -> vite](https://juejin.cn/post/7033243995425734663)
- [vite](https://vitejs.cn/vite3-cn/guide/)
- [Corepack - 解决 pnpm 或 yarn 的多版本管理、解决本地版本与 packageManager 中的版本一致性问题](https://ksh7.com/posts/node-corepack/index.html)
- [pnpm 如何使用 workspace 构建 monorepo](https://ksh7.com/posts/pnpm-use-workspace/index.html)

## 结论

- esbuild 专注、快,但扩展不足;
- webpack 老牌、全,但速度太慢,不支持 esm 格式;
- rollup 专注模块、快,但 cjs 支持的不好,cjs2esm 效果不好
- parcel 了解的少

**什么是bundler**
bundler的工作就是将一系列通过模块方式组织的代码将其打包成一个或多个文件,我们常见的bundler包括webpack、rollup、esbuild等。

webpack :强调对web开发的支持,尤其是内置了HMR的支持,插件系统比较强大,对各种模块系统兼容性最佳(amd,cjs,umd,esm等,兼容性好的有点过分了,这实际上有利有弊,导致面向webpack编程),有丰富的生态,缺点是产物不够干净,产物不支持生成esm格式, 插件开发上手较难,不太适合库的开发。
rollup: 强调对库开发的支持,基于ESM模块系统,对tree shaking有着良好的支持,产物非常干净,支持多种输出格式,适合做库的开发,插件api比较友好,缺点是对cjs支持需要依赖插件,且支持效果不佳需要较多的hack,不支持HMR,做应用开发时需要依赖各种插件。
parcel:强调极速零配置Web应用打包工具,它利用多核处理提供了极快的速度,并且不需要任何配置。
esbuild: 强调性能,内置了对css、图片、react、typescript等内置支持,编译速度特别快(是webpack和rollup速度的100倍+),缺点是目前插件系统较为简单,生态不如webpack和rollup成熟。

## 工具

1. 为什么不选择 rollup?

https://cloud.tencent.com/developer/article/2030300

- 对CommonJS的兼容问题

因为rollup原生只支持ESM模块的bundle,因此如果实际业务中需要对commonjs进行bundle,第一步就是需要将CJS转换成ESM,不幸的是,Commonjs和ES Module的interop问题是个非常棘手的问题(搜一搜babel、rollup、typescript等工具下关于interop的issue:https://sokra.github.io/interop-test/

其两者语义上存在着天然的鸿沟,将ESM转换成Commonjs一般问题不太大(小心避开default导出问题),但是将CJS转换为ESM则存在着更多的问题。 实际上rollup也正在重写该核心模块:https://github.com/rollup/plugins/pull/658。

- 一些典型的问题如下

由于commonjs的导出模块并非是live binding的,所以导致一旦出现了commonjs的循环引用,则将其转换成esm就会出问题
同步的动态require几乎无法转换为esm,如果将其转换为top-level的import,根据import的语义,bundler需要将同步require的内容进行hoist,但是这与同步require相违背,因此动态require也很难处理
cjs2esm的复杂性,导致该转换算法十分复杂,导致一旦业务里包含了很多cjs的模块,rollup其编译性能就会急剧下降,这在编译一些库的时候可能不是大问题,但是用于大型业务的开发,其编译速度难以接受。
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

## 开发环境

- 安装 [Node.js](http://nodejs.org/en/) 建议使用最新版
- 安装 [Yarn](https://yarnpkg.com/zh-Hans/) 1.15.2+
- jest 27.0.0 以上 (必须,否则无法运行测试)
- father-build 1.22.3 (必须,没有必要,暂时不升级到 father4

- [Node.js](http://nodejs.org/en/) 建议使用最新版
- [pnpm] (必须,负责多包管理的安装、发布等)
- turbo (必须,负责多包管理的任务执行)
- fathe4 (必须,负责打包、编译
- jest (必须,负责测试)

## Docker环境

Expand All @@ -27,7 +27,7 @@ $ docker pull ubuntu:22.04
这里我们使用的是 ubuntu 22.04 版本,如果你是其他版本的,自行修改镜像名称。另外,一定要使用 -it 选项,否则无法进入交互模式。同时,-v 选项挂载本地 ddn 代码到容器内,方便后续操作。

```bash
$ docker run -d -it --name ddn-mainnet -v /Users/imfly/projects/DDN/ddn:/root/ddn ubuntu:22.04
$ docker run -d -it -p 8001:8001 -p 8000:8000 --name ddn-starter -v /path/to/ddn:/ddn/mainnet -v /path/to/ddn-starter:/ddn/starter ubuntu:22.04
```

这样,一个 ubuntu 运行环境就搭建起来了。
Expand All @@ -40,7 +40,7 @@ $ docker run -d -it --name ddn-mainnet -v /Users/imfly/projects/DDN/ddn:/root/dd
当然,你也可以使用下面的命令,进入容器:

```bash
$ docker exec -it ddn-mainnet /bin/bash
$ docker exec -it ddn-starter /bin/bash
```

这样,在不离开 IDE 编辑器的情况下,就能执行命令了,就行在本地电脑操作 ubuntu 的体验一样。
Expand All @@ -60,6 +60,17 @@ $ yarn install

网上有人提供了解决方法,使用 echo 命令向/etc/apt/sources.list添加更新源:


在更新源之前,先备份一下:

```shell
# 备份
$ cp /etc/apt/sources.list /etc/apt/sources.list.bak

# 执行更新
$ apt update
```

```shell
# 向/etc/apt/sources.list添加清华的源
echo 'deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse' >> /etc/apt/sources.list && \
Expand All @@ -78,21 +89,12 @@ echo 'deb http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted univ
echo 'deb-src http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse' >> /etc/apt/sources.list
```

在更新源之前,先备份一下:

```shell
# 备份
$ cp /etc/apt/sources.list /etc/apt/sources.list.bak

# 执行更新
$ apt update
```

**特别注意**:这里遇到过一个非常奇葩的问题,在更新源的过程中,千万不要开启代理,否则会找不到地址,出现“没有签名”、“无法访问”等错误。我就因为这个问题,折腾了很久。

2. 安装依赖

这里我们需要安装 vim, git, curl, wget 等常用命令,以及 nodejs 和 yarn 等依赖。
这里我们需要安装 vim, git, curl, wget 等常用命令,以及 nodejs 和 pnpm 等依赖。

```shell
$ apt install -y vim git curl wget
Expand All @@ -106,9 +108,21 @@ $ apt install -y vim git curl wget
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

# 安装 node
$ nvm install 16.13.1
$ nvm install 20.13.1

# 安装 yarn
$ npm install -g yarn
# 安装 pnpm
$ npm install -g pnpm
```

## 运行

1. 持续构建


2. 本地运行


3. 打包


4. 发布
13 changes: 13 additions & 0 deletions docs/development/faq.zh-CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FAQ
-----


1. 在 git cz 进行提交的时候,出现 --no-install is not in the npm registry 错误

原因是 husky 版本过低了,但是简单安装是无效的,需要这样:

```shell
$ pnpm dlx husky install
```

命令很奇特吧
2 changes: 1 addition & 1 deletion examples/fun-tests/.ddnrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,6 @@ module.exports = {
* "@ddn/asset-aob"
* ]
*/
assets: ['@ddn/asset-evidence', '@ddn/asset-dapp', '@ddn/asset-aob'],
assets: ['@ddn/asset-evidence', '@ddn/asset-dapp', '@ddn/asset-aob', '@ddn/asset-dao'],
crypto: '@ddn/crypto-nacl' // 绿化钠 @ddn/crypto-nacl ,国密 @ddn/crypto-sm
}
59 changes: 59 additions & 0 deletions examples/fun-tests/.fatherrc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// import builtins from "rollup-plugin-node-builtins";

// /**
// * Non-existent export 'default' is imported from ../../node_modules/lodash/lodash.js
// * 请安装使用 lodash-es 模块,因为 lodash 默认是 cjs 的
// * 参考:https://stackoverflow.com/questions/51846889/rollup-import-of-non-existent-export-when-importing-function-from-lodash
// *
// * 其他有用插件,必须时可以使用
// * import globals from 'rollup-plugin-node-globals'
// * import replace from 'rollup-plugin-replace'
// */

// export default {
// // target: "browser",
// // cjs: { type: "rollup", lazy: false },
// esm: { type: "rollup" },
// disableTypeCheck: false,

// // 这些包从外面引入,就不用打包进来了
// // extraExternals: [
// // 'memcpy', // bytebuffer-node 包使用的,但是该包已经无法使用
// // 'shelljs',
// // ],

// // father-build 所未默认安装的插件, 将 fs 等 node 端的包打包给前端使用,其实一般不需要,应该排除
// extraRollupPlugins: [
// // globals(), // 导致错误
// builtins(),
// // replace({
// // // process.versions is undefinded
// // 'process.versions.electron': JSON.stringify(isElectronStr)
// // }),
// ],

// // 下面的问题未解决
// // The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten
// // https://github.com/rollup/rollup/issues/794
// // onwarn: function(warning) {
// // // Skip certain warnings

// // // should intercept ... but doesn't in some rollup versions
// // if (warning.code === "THIS_IS_UNDEFINED") {
// // return;
// // }

// // // console.warn everything else
// // console.error(warning.message);
// // },
// };

// .fatherrc.js
import {defineConfig} from 'father'

export default defineConfig({
cjs: {
platform: 'node',
output: 'build'
}
})
Loading

0 comments on commit de1677e

Please sign in to comment.