Skip to content

Commit 3f3bbbc

Browse files
authored
Merge pull request #366 from scaffoldly/364-aws-sdk-node18
Conditional `forceExclude` of `aws-sdk` based on `runtime` version
2 parents bf18ef3 + b1fc98c commit 3f3bbbc

File tree

8 files changed

+64
-7
lines changed

8 files changed

+64
-7
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
name: CI
55

66
on:
7+
workflow_dispatch: {}
78
push:
89
branches: [ master ]
910
pull_request:

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ The three options (`externals`, `forceExclude`, and `excludeFiles`) look similar
451451

452452
- `forceExclude`
453453

454-
These packages are available in the Lambda runtime. Either by default (in the case of `aws-sdk`) or through a Lambda layer that you might be using. So these are not included in the Lambda package. And they are also marked as `externals`. Meaning that packages that are in `forceExclude` are automatically added to the `externals` list as well. By default, `aws-sdk` is listed in the `forceExclude`.
454+
These packages are available in the Lambda runtime. Either by default (in the case of `aws-sdk`) or through a Lambda layer that you might be using. So these are not included in the Lambda package. And they are also marked as `externals`. Meaning that packages that are in `forceExclude` are automatically added to the `externals` list as well. By default, `aws-sdk` is listed in the `forceExclude` when `runtime` is lower than `nodejs18.x`.
455455

456456
- `excludeFiles`
457457

index.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ function applyWebpackOptions(custom, config) {
4747
function applyUserConfig(config, userConfig, servicePath, runtime) {
4848
config.servicePath = servicePath;
4949

50+
// Default to Node 12 if no runtime found
51+
const runtimeVersion =
52+
Number.parseInt((runtime || "").replace("nodejs", ""), 10) || 12;
53+
54+
// Force exclude aws-sdk for versions below Node 18
55+
if (runtimeVersion < 18) {
56+
config.options.forceExclude.push("aws-sdk");
57+
}
58+
5059
// Concat forceExclude if provided
5160
if (userConfig.forceExclude) {
5261
userConfig.forceExclude = config.options.forceExclude.concat(
@@ -70,9 +79,7 @@ function applyUserConfig(config, userConfig, servicePath, runtime) {
7079

7180
Object.assign(config.options, userConfig);
7281

73-
// Default to Node 12 if no runtime found
74-
config.nodeVersion =
75-
Number.parseInt((runtime || "").replace("nodejs", ""), 10) || 12;
82+
config.nodeVersion = runtimeVersion;
7683
}
7784

7885
class ServerlessPlugin extends ServerlessWebpack {

src/config.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ module.exports = {
2020
packagerOptions: {},
2121
generateStatsFiles: false,
2222
tsConfig: "tsconfig.json",
23-
// Exclude aws-sdk since it's available in the Lambda runtime
24-
forceExclude: ["aws-sdk"],
23+
forceExclude: [],
2524
disableForkTsChecker: false,
2625
// Set non Webpack compatible packages as externals
2726
// Or if we want to exclude all packages in the node_modules:

tests/force-exclude/force-exclude.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,30 @@ test("force-exclude", async () => {
1313

1414
expect(result).not.toMatch(errorRegex);
1515
});
16+
17+
test("force-exclude package", async () => {
18+
const result = await runSlsCommand(__dirname, "package -c serverless.yml");
19+
20+
expect(result).not.toMatch(errorRegex);
21+
22+
/*
23+
Ensure that is-sorted and aws-sdk is excluded
24+
*/
25+
expect(result).toMatch(
26+
/Excluding external modules: is-sorted@\^[\d\\.]+, aws-sdk@\^[\d\\.]+\n/
27+
);
28+
});
29+
30+
test("force-exclude package (node18)", async () => {
31+
const result = await runSlsCommand(
32+
__dirname,
33+
"package -c serverless.node18.yml"
34+
);
35+
36+
expect(result).not.toMatch(errorRegex);
37+
38+
/*
39+
Ensure that is-sorted is excluded
40+
*/
41+
expect(result).toMatch(/Excluding external modules: is-sorted@\^[\d\\.]+\n/);
42+
});

tests/force-exclude/handler.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import sorted from "is-sorted";
2+
import AWS from "aws-sdk";
23

3-
export const hello = async (event, context) => {
4+
export const hello = async (event) => {
5+
// Include a dummy AWS SDK call to ensure webpack attempts to bundle it
6+
AWS.config.update({
7+
region: "us-east-1",
8+
});
49
sorted([1, 2, 3]);
510
return {
611
statusCode: 200,

tests/force-exclude/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"author": "",
1111
"license": "ISC",
1212
"dependencies": {
13+
"aws-sdk": "^2.1502.0",
1314
"is-sorted": "^1.0.5"
1415
}
1516
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
service: my-service
2+
3+
plugins:
4+
- '../../index'
5+
6+
custom:
7+
bundle:
8+
forceExclude:
9+
- "is-sorted"
10+
11+
provider:
12+
name: aws
13+
runtime: nodejs18.x
14+
15+
functions:
16+
hello:
17+
handler: handler.hello

0 commit comments

Comments
 (0)