Hexo个人博客搭建踩坑记
1   开发环境配置
1.1   安装Node.js和启用Yarn
下载链接:
- Node.js:推荐LTS长期版,不推荐用最新版,可能很多npm并未能适配新版。
- Yarn(<=v1.22.19才有编译好的安装包):若要使用新版Yarn,则不需要下载离线包安装,通过Node.js的corepack启用即可。
启用Yarn:
- 管理员终端运行命令启用:
corepack enable
,执行成功之后yarn命令即可系统全局正常调用。 - 终端运行命令:
yarn global bin
,复制路径添加到用户的PATH环境变量中。(这一步一定需要手动添加路径,因为程序没有自动添加PATH变量)
下载安装Yarn离线包即可,程序会自动添加PATH环境变量(Yarn程序路径和Package二进制程序路径)。
1.2   yarn和npm部分对应命令
yarn命令 | npm命令 | 说明 |
---|---|---|
yarn init | npm init | 初始化 |
yarn / yarn install | npm install | 安装包 |
yarn add [package] | npm install [package] --save | 安装包 |
yarn add global [package] | npm install -g [package] --save | 全局安装包 |
yarn remove | npm uninstall | 卸载包 |
yarn upgrade | npm update | 更新包 |
yarn global dir | npm config get prefix | 查看安装目录 |
yarn global bin | 查看安装bin目录 | |
yarn cache dir | 查看缓存目录 | |
yarn cache clear | npm cache clean | 清除缓存目录 |
1.3   版本和包更新
站点配置文件
_config.yml
和主题配置文件_config.next.yml
的更新改动项,通过查看Github仓库代码是否存在更新改动。
Next主题直接可通过package版本号更新即可。
1 | yarn outdated # 检查已过时的包,并修改 package 中 dependencies 中相关版本号 |
1.4   npm源设定
1 | yarn config get registry // 查看当前npm源 |
当前有效镜像链接:npm config set registry https://registry.npmmirror.com
已失效镜像链接:npm config set registry http://registry.npm.taobao.org
1.5   安装hexo和hexo-cli
在想要安装hexo的位置建立文件夹「hexo」,打开命令行终端,以此路径作为终端工作路径。然后执行下面命令(推荐全局安装)。
1 | yarn global add hexo-cli # 全局安装hexo-cli脚手架 |
1 | yarn add hexo-cli |
2   hexo配置
2.1   _config.yml
注:language根据主题设置,注意新版Next主题已经没有zh-Hans语言配置文件,设置中文简体请用zh-CN参数。
注:新版Next主题主仓库已从 iissnan 名下迁移至 theme-next 组织。
注:现阶段新版Next支持Font Awesome 5.13.0,而5.1.4旧版Next版本只支持Font Awesome 4.7.0
2.2   live2d的修正处理
参考链接:"idle" 设置多个动作时每次加载只会执行其中一个动作,不会随机到别的动作
关于适配自动构建的两个解决方法:
- Github Action自动构建需要相应修改命令(比较繁琐,因此放弃)
- fork源码修改重新编译发布npm包(需要注册npmjs,发布全局包也需审核;使用个人仓库链接作为npm使用即可)
详细修改:
- cModel.js line 369(注释掉原来的,再添加以下代码)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16if (this.motions[motionName] == null)
{
this.loadMotion(motionName, this.modelHomeDir + motionName, function(mtn) {
motion = mtn;
thisRef.setFadeInFadeOut(name, no, priority, motion);
});
}
else
{
motion = this.motions[motionName];
thisRef.setFadeInFadeOut(name, no, priority, motion);
} - cManager.js line 89(注释掉原来的,再添加以下代码)
1
2this.models[i].startRandomMotion(cDefine.MOTION_GROUP_FLICK_HEAD,
cDefine.PRIORITY_NORMAL); - 修改两个文件的代码后,在live2d-widget路径下执行构建命令:
npm run inst:dev
和npm run build:prod
1
2yarn global add commitizen && yarn global add conventional-changelog-cli && yarn
./node_modules/.bin/webpack --env prod --progress --colors && ./node_modules/.bin/webpack --env prod --progress --colors --config webpack.config.common.js1
2npm run inst:dev
npm run build:prod
yarn版本详细命令可通过查看
node_modules/live2d-widget/package.json
包得知:
1 | "inst:dev": "npm install -g commitizen && npm install -g conventional-changelog-cli && npm install" |
3   Github Action自动构建
3.1   以往编写actions测试的一些命令
Bash版本(ubuntu runner):
1 | export PATH="$PATH:$(yarn global bin)" |
Powershell版本(windows runner):
1 | (get-content _config.yml) -replace "GITHUB_ACCESS_TOKEN", "$TOKEN_VALUE" | Set-content _config.yml |
4   hexo部署报错排查
4.1   postinstall: npm run build:highlight failed
问题现象:在操作更新hexo-renderer-marked,在使用yarn执行构建时候,出现报错:hexo-util: command failed. Command: npm run build:highlight
。经过尝试全局单独安装hexo-util
排查发现,"npm'is not recognized as an internal or external command.
。
问题原因:安装node.js
,由于未勾选启用npm
模块,导致系统安装的node.js
环境缺失npm
,但由于新依赖包直接在包构建命令里面新增并写死了npm
命令,导致无法正常构建。
解决方法:重新运行node.js
安装包,选择「change」选项,启用npm
模块。然后重新运行yarn
构建。
4.2   Stylus for Node v14 'Accessing non-existent property' errors
注 :Node版本选择v12,避免Stylus for Node v14 'Accessing non-existent property' errors。
5   参考文献
[1] npm 淘宝镜像最新官方指引(2023.08.31)[EB/OL]. https://zhuanlan.zhihu.com/p/653480874.
[2] postinstall: npm run build:highlight failes leaving full hexo install unusable...[EB/OL]. https://github.com/hexojs/hexo‑util/issues/9.
[3] Step-by-step[EB/OL]. https://yarnpkg.com/migration/guide.