使用pkg打包Egg.js项目 Egg.js线上部署

使用pkg打包egg.js项目

 

pkg原理

pkg打包工具主要会按平台(支持window、mac、linux)分别打包。
pkg中会包含node的可执行文件,还会包含你要打包进去的代码。代码通过一个虚拟的文件系统把所有的代码和资源文件都挂载到 /snapshot/${被打包项目的文件夹名} 下面(pkg hack了 fs 的很多方法,拦截文件操作,如果发现读的文件路径是在挂载目录下就特殊处理,返回打包进去的文件信息,如果不在挂载目录下,则按node默认逻辑进行)
pkg 会根据被打包项目的package.json 中的licence声明判断是否要对源码进行编译,npm上安装的依赖包基本都是开源的,会以源码的形式打包;而用户的源码如果没有声明开源的协议,则会把js文件编译为v8字节码进行保存(在项目中也就没法通过fs读取到源码,会给出抛错,即使是破解安装包也只能拿到v8字节码)

安装pkg

 npm install pkg -g
  • 1

配置egg.js临时文件目录

pkg的虚拟文件系统只是用来进行读文件操作,所有写相关都需要移动到包所在根目录。
在config.default.js文件中配置如下:

  const process = require('process'),
      path = require('path');
  // 通过process.cwd()获取当前执行文件执行的路径
  config.rundir = process.cwd() + '/run';// 配置执行时临时文件的路径
  config.logger = {
    dir: path.join(process.cwd(), 'logs'),//配置普通日志文件地址
  };
  config.customLogger = {
    scheduleLogger: {
      file: path.join(process.cwd(), 'logs', 'egg-schedule.log'),//配置定时任务日志的地址
    },
  };
  config.static = { 
    dir: process.cwd() + '/public',
  };
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

修改package.json文件

由于Egg.js使用nanoid库所有egg.js项目打包时必须打入 ./node_modules/nanoid/**/*.js。egg-mysql、egg-redis等根据实际情况配置。目前发现的问题是不配置egg-redis可以正常挂载到ctx,但egg-mysql不会,所有建议用到的插件(plugin.js)全部配置,防止生产环境运行出错。
scripts中配置pkg命令,打包时可直接运行npm run pkg

{
	"scripts": {
	    "start": "egg-scripts start --daemon --title=egg-server-node-sso",
	    "stop": "egg-scripts stop --title=egg-server-node-sso",
	    "dev": "egg-bin dev",
	    "debug": "egg-bin debug",
	    "test": "npm run lint -- --fix && npm run test-local",
	    "test-local": "egg-bin test",
	    "cov": "egg-bin cov",
	    "lint": "eslint .",
	    "ci": "npm run lint && npm run cov",
	    "autod": "autod",
	    "pkgwin": "pkg . -t win --out-path ./dist --debug",
	    "pkglinux": "pkg . -t linux --out-path /usr/dist --debug"
	 },
	 "bin": "pkg-build.js",
	  "pkg": {
	    "scripts": [
	      "./app/**/*.js",
	      "./config/**/*.js",
	      "./app.js",
	      "./agent.js"
	    ],
	    "assets": [
	      "./app/view/**/*",
	      "./public/**/*",
	      "./node_modules/nanoid/**/*.js",
	      "./node_modules/egg-mysql/**/*",   // 建议将用到的插件全部配置,防止生产环境运行出错
	      "./node_modules/egg-view-nunjucks/**/*",
	      "./node_modules/egg-redis/**/*"
	    ]
	  }
  }
  • 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

配置打包入口文件pkg-build.js

pkg-build.js即为上述配置的bin路径文件。

 'use strict';
 console.log(__dirname);
 require(__dirname + '/node_modules/egg-scripts/bin/egg-scripts.js')
  • 1
  • 2
  • 3

打包发布

 // windows
 npm run pkgwin
 //linux
 npm run pkglinux
  • 1
  • 2
  • 3
  • 4

可能遇到问题:
第一次打包的时候,会遇到下包很慢很可能超时的问题。
请到https://github.com/zeit/pkg-fetch/releases下载对应的包,然后复制到~/.pkh-cache/2.5/目录下。
如果访问困难,可访问:
windows版本 https://download.csdn.net/download/hazhijaio/12241215
linux版本 https://download.csdn.net/download/hazhijaio/12241215
mac版本 https://download.csdn.net/download/hazhijaio/12241215

部署启动

如果public目录非空,请将public复制到打包后的目录,运行以下命令即可。

 // windows
 appName.exe start {__dirname} 
 //linux
./appName start {__dirname} 

 

转载:https://blog.csdn.net/hazhijaio/article/details/104854610 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值