Skip to content

Commit 50ace57

Browse files
committed
fix: support customize entry file
1 parent 9ea67e3 commit 50ace57

File tree

5 files changed

+43
-32
lines changed

5 files changed

+43
-32
lines changed

docs/configure.md

+23-21
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ inputs:
1717
serviceName: mytest # api网关服务名称
1818
runtime: Nodejs10.15 # 运行环境
1919
serviceId: service-np1uloxw # api网关服务ID
20+
entryFile: sls.js # 自定义 server 的入口文件名,默认为 sls.js,如果不想修改文件名为 sls.js 可以自定义
2021
src: ./src # 第一种为string时,会打包src对应目录下的代码上传到默认cos上。
2122
# src: # 第二种,部署src下的文件代码,并打包成zip上传到bucket上
2223
# src: ./src # 本地需要打包的文件目录
@@ -97,29 +98,30 @@ inputs:
9798
9899
主要的参数
99100
100-
| 参数名称 | 是否必选 | 默认值 | 描述 |
101-
| ------------------------------------ | :------: | :-------------: | :------------------------------------------------------------------ |
102-
| runtime | 否 | Nodejs10.15 | 执行环境, 目前支持: Nodejs6.10, Nodejs8.9, Nodejs10.15, Nodejs12.16 |
103-
| region | 否 | ap-guangzhou | 项目部署所在区域,默认广州区 |
104-
| functionName | 否 | | 云函数名称 |
105-
| serviceName | 否 | | API 网关服务名称, 默认创建一个新的服务名称 |
106-
| serviceId | 否 | | API 网关服务 ID,如果存在将使用这个 API 网关服务 |
107-
| src | 否 | `process.cwd()` | 默认为当前目录, 如果是对象, 配置参数参考 [执行目录](#执行目录) |
108-
| layers | 否 | | 云函数绑定的 layer, 配置参数参考 [层配置](#层配置) |
109-
| [functionConf](#函数配置) | 否 | | 函数配置 |
110-
| [apigatewayConf](#API-网关配置) | 否 | | API 网关配置 |
111-
| [cloudDNSConf](#DNS-配置) | 否 | | DNS 配置 |
112-
| [Region special config](#指定区配置) | 否 | | 指定区配置 |
113-
| [staticConf](#静态资源-CDN-配置) | 否 | | 静态资源 CDN 配置 |
101+
| 参数名称 | 必选 | 默认值 | 描述 |
102+
| ------------------------------------ | :--: | :-------------: | :-------------------------------------------------------------- |
103+
| runtime | 否 | `Nodejs10.15` | 执行环境, 支持: Nodejs6.10, Nodejs8.9, Nodejs10.15, Nodejs12.16 |
104+
| region | 否 | `ap-guangzhou` | 项目部署所在区域 |
105+
| functionName | 否 | | 云函数名称 |
106+
| serviceName | 否 | | API 网关服务名称, 默认创建一个新的服务名称 |
107+
| serviceId | 否 | | API 网关服务 ID, 如果存在将使用这个 API 网关服务 |
108+
| entryFile | 否 | `sls.js` | 自定义 server 的入口文件名 |
109+
| src | 否 | `process.cwd()` | 默认为当前目录, 如果是对象, 配置参数参考 [执行目录](#执行目录) |
110+
| layers | 否 | | 云函数绑定的 layer, 配置参数参考 [层配置](#层配置) |
111+
| [functionConf](#函数配置) | 否 | | 函数配置 |
112+
| [apigatewayConf](#API-网关配置) | 否 | | API 网关配置 |
113+
| [cloudDNSConf](#DNS-配置) | 否 | | DNS 配置 |
114+
| [Region special config](#指定区配置) | 否 | | 指定区配置 |
115+
| [staticConf](#静态资源-CDN-配置) | 否 | | 静态资源 CDN 配置 |
114116

115117
## 执行目录
116118

117-
| 参数名称 | 是否必选 | 类型 | 默认值 | 描述 |
118-
| -------- | :------: | :-------------: | :----: | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
119-
| src | 否 | String | | 代码路径。与 object 不能同时存在。 |
120-
| exclude | 否 | Array of String | | 不包含的文件或路径, 遵守 [glob 语法](https://github.com/isaacs/node-glob) |
121-
| bucket | 否 | String | | bucket 名称。如果配置了 src,表示部署 src 的代码并压缩成 zip 后上传到 bucket-appid 对应的存储桶中;如果配置了 object,表示获取 bucket-appid 对应存储桶中 object 对应的代码进行部署。 |
122-
| object | 否 | String | | 部署的代码在存储桶中的路径。 |
119+
| 参数名称 | 是否必选 | 类型 | 默认值 | 描述 |
120+
| -------- | :------: | :------: | :----: | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
121+
| src | 否 | String | | 代码路径。与 object 不能同时存在。 |
122+
| exclude | 否 | String[] | | 不包含的文件或路径, 遵守 [glob 语法](https://github.com/isaacs/node-glob) |
123+
| bucket | 否 | String | | bucket 名称。如果配置了 src,表示部署 src 的代码并压缩成 zip 后上传到 bucket-appid 对应的存储桶中;如果配置了 object,表示获取 bucket-appid 对应存储桶中 object 对应的代码进行部署。 |
124+
| object | 否 | String | | 部署的代码在存储桶中的路径。 |
123125

124126
## 层配置
125127

@@ -134,7 +136,7 @@ inputs:
134136

135137
| 参数名称 | 是否必选 | 类型 | 默认值 | 描述 |
136138
| ---------- | :------: | -------- | :----: | :---------------------------------------------- |
137-
| ttl | 否 | Number | 600 | TTL 值,范围 1 - 604800,不同等级域名最小值不同 |
139+
| ttl | 否 | Number | `600` | TTL 值,范围 1 - 604800,不同等级域名最小值不同 |
138140
| recordLine | 否 | String[] | | 记录的线路名称 |
139141

140142
### 指定区配置

serverless.component.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: nuxtjs
2-
version: 0.1.3
2+
version: 0.1.4
33
author: 'Tencent Cloud, Inc.'
44
org: 'Tencent Cloud, Inc.'
55
description: Deploy a serverless Nuxt.js application onto Tencent SCF and API Gateway.

src/_shims/handler.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ let app
77
let server
88

99
module.exports.handler = async (event, context) => {
10-
const userSls = path.join(__dirname, '..', 'sls.js')
10+
const userSls = path.join(__dirname, '..', process.env.SLS_ENTRY_FILE)
1111
if (fs.existsSync(userSls)) {
1212
// eslint-disable-next-line
13-
console.log('Using user custom sls.js')
13+
console.log(`Using user custom entry file ${process.env.SLS_ENTRY_FILE}`)
1414
app = await require(userSls)(true)
1515
} else {
1616
app = await require('./sls')(false)

src/config.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const CONFIGS = {
33
'https://serverless-templates-1300862921.cos.ap-beijing.myqcloud.com/nuxtjs-demo.zip',
44
compName: 'nuxtjs',
55
compFullname: 'Nuxt.js',
6+
defaultEntryFile: 'sls.js',
67
region: 'ap-guangzhou',
78
handler: 'sl_handler.handler',
89
runtime: 'Nodejs10.15',

src/utils.js

+16-8
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ const prepareInputs = async (instance, credentials, inputs = {}) => {
326326
// chenck state function name
327327
const stateFunctionName =
328328
instance.state[regionList[0]] && instance.state[regionList[0]].functionName
329-
const functionConf = {
329+
const functionConf = Object.assign(tempFunctionConf, {
330330
code: {
331331
src: inputs.src,
332332
bucket: inputs.srcOriginal && inputs.srcOriginal.bucket,
@@ -366,14 +366,12 @@ const prepareInputs = async (instance, credentials, inputs = {}) => {
366366
publish: inputs.publish,
367367
traffic: inputs.traffic,
368368
lastVersion: instance.state.lastVersion,
369-
eip: tempFunctionConf.eip === true,
370-
l5Enable: tempFunctionConf.l5Enable === true,
371369
timeout: tempFunctionConf.timeout ? tempFunctionConf.timeout : CONFIGS.timeout,
372370
memorySize: tempFunctionConf.memorySize ? tempFunctionConf.memorySize : CONFIGS.memorySize,
373371
tags: ensureObject(tempFunctionConf.tags ? tempFunctionConf.tags : inputs.tag, {
374372
default: null
375373
})
376-
}
374+
})
377375

378376
// validate traffic
379377
if (inputs.traffic !== undefined) {
@@ -382,15 +380,25 @@ const prepareInputs = async (instance, credentials, inputs = {}) => {
382380
functionConf.needSetTraffic = inputs.traffic !== undefined && functionConf.lastVersion
383381

384382
if (tempFunctionConf.environment) {
385-
functionConf.environment = inputs.functionConf.environment
383+
functionConf.environment = tempFunctionConf.environment
384+
functionConf.environment.variables = functionConf.environment.variables || {}
385+
functionConf.environment.variables.SERVERLESS = '1'
386+
functionConf.environment.variables.SLS_ENTRY_FILE = inputs.entryFile || CONFIGS.defaultEntryFile
387+
} else {
388+
functionConf.environment = {
389+
variables: {
390+
SERVERLESS: '1',
391+
SLS_ENTRY_FILE: inputs.entryFile || CONFIGS.defaultEntryFile
392+
}
393+
}
386394
}
387395
if (tempFunctionConf.vpcConfig) {
388-
functionConf.vpcConfig = inputs.functionConf.vpcConfig
396+
functionConf.vpcConfig = tempFunctionConf.vpcConfig
389397
}
390398

391399
// 对apigw inputs进行标准化
392400
const tempApigwConf = inputs.apigatewayConf ? inputs.apigatewayConf : {}
393-
const apigatewayConf = {
401+
const apigatewayConf = Object.assign(tempApigwConf, {
394402
serviceId: inputs.serviceId,
395403
region: regionList,
396404
isDisabled: tempApigwConf.isDisabled === true,
@@ -413,7 +421,7 @@ const prepareInputs = async (instance, credentials, inputs = {}) => {
413421
}
414422
],
415423
customDomains: tempApigwConf.customDomains || []
416-
}
424+
})
417425
if (tempApigwConf.usagePlan) {
418426
apigatewayConf.endpoints[0].usagePlan = {
419427
usagePlanId: tempApigwConf.usagePlan.usagePlanId,

0 commit comments

Comments
 (0)