Skip to content

Commit

Permalink
feat(sls-pack): support serverless-offline
Browse files Browse the repository at this point in the history
Closes #13
  • Loading branch information
codingnuclei committed Nov 6, 2024
1 parent 17ac68d commit 2041d86
Show file tree
Hide file tree
Showing 21 changed files with 5,536 additions and 2,057 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,6 @@ Thumbs.db

.nx/cache
examples/complete/package-lock.json

# logs
*-logs.txt
16 changes: 16 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Debug Examples",
"type": "node",
"request": "attach",
"port": 9229,
"sourceMaps": true,
"outFiles": ["${workspaceFolder}/examples/**/*.js"]
}
]
}
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ For Developers - [DEVELOPER.MD](./docs/DEVELOPER.md)
## Features

- From zero to hero: configuration possibilities range from zero-config to fully customizable
- Supports `sls package`, `sls deploy`
- Build and runtime performance at its core
- Supports `sls package`, `sls deploy`, `sls deploy function`
- Integrates with [`Serverless Invoke Local`](https://www.serverless.com/framework/docs/providers/aws/cli-reference/invoke-local) & [`serverless-offline`](https://github.com/dherault/serverless-offline)

## Table of Contents

Expand All @@ -35,6 +36,8 @@ For Developers - [DEVELOPER.MD](./docs/DEVELOPER.md)
- [Function Scripts](#function-scripts)
- [Global Scripts](#global-scripts)
- [Doctor](#doctor)
- [Integrations](#integrations)
- [Serverless Offline](#serverless-offline)
- [Known Issues](#known-issues)


Expand Down Expand Up @@ -85,6 +88,7 @@ See [example folder](../../examples) for example plugin option configuration.
| `esm` | Output format will be ESM (experimental). | `false` |
| `mode` | Used to set the build mode of Rspack to enable the default optimization strategy (https://www.rspack.dev/config/mode). | `production` |
| `tsConfig` | Relative path to your tsconfig. | `undefined` |
| `sourcemap` | Configure rspack [sourcemaps](https://rspack.dev/config/devtool). | `false` |
| [`externals`](#external-dependencies) | Provides a way of excluding dependencies from the output bundles. | `undefined` |
| [`scripts`](#scripts) | Array of scripts to execute after your code has been bundled by rspack. | `undefined` |
| [`doctor`](#doctor) | Enable the `Rsdoctor` plugin. | `undefined` |
Expand Down Expand Up @@ -272,6 +276,30 @@ custom:

⚠️ **Note: Rsdoctor is configured to run in [`brief`](https://rsdoctor.dev/guide/start/cicd#enabling-brief-mode) mode. If you want to use another mode, you can register `RsdoctorRspackPlugin` manually using the [rspack config option](#config-file).**

## Integrations

### Serverless Offline

The plugin has first class support for [serverless-offline](https://github.com/dherault/serverless-offline).

Add the plugins to your `serverless.yml` file and make sure that `serverless-rspack`
precedes `serverless-offline` as the order is important:

```yaml
plugins: ...
- serverless-rspack
...
- serverless-offline
...
```

Run `serverless offline start` to start the Lambda/API simulation.

⚠️ **Note: The plugin will automatically set sourcemap to `source-map` when running in offline mode and change the `devtoolModuleFilenameTemplate` to `[absolute-resource-path]`.**

⚠️ **Note: If you are using a custom rspack config, then the serverless plugin passed to your config function will have `offlineMode` set to true.**


## Known Issues

- Invoke Local does not work with ESM enabled when using serverless V3: [ISSUE-11308](https://github.com/serverless/serverless/issues/11308#issuecomment-1719297694)
Expand Down
5 changes: 3 additions & 2 deletions examples/complete/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"scripts": {
"package": "sls package --verbose",
"deploy": "sls deploy --verbose",
"deploy:func:app3": "sls deploy function -f app3 --verbose"
"deploy:func:app3": "sls deploy function -f app3 --verbose",
"debug": "export SLS_DEBUG=* && node --inspect node_modules/serverless/bin/serverless offline start --noTimeout --verbose"
},
"dependencies": {
"isin-validator": "^1.1.1",
Expand All @@ -15,7 +16,7 @@
"@rspack/core": "1.0.14",
"@types/node": "^18.7.21",
"serverless": "^3.22.0",
"serverless-offline": "^10.2.1",
"serverless-offline": "13.6.0",
"ts-node": "^10.9.1",
"typescript": "^4.8.3"
}
Expand Down
15 changes: 15 additions & 0 deletions examples/complete/serverless-offline/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Serverless-offline commands

## By AWS Cli invoke:

aws lambda invoke ./app3-logs.txt \
--endpoint-url http://localhost:3002 \
--function-name complete-example-dev-app3

## By aws sdk:

node offline-invoke.mjs

## By mocked Rest API

curl http://localhost:3000/dev/helloApp3
38 changes: 38 additions & 0 deletions examples/complete/serverless-offline/offline-invoke.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Buffer } from 'node:buffer';
import aws from 'aws-sdk';

const { stringify } = JSON;

const lambda = new aws.Lambda({
apiVersion: '2015-03-31',
endpoint: 'http://localhost:3002',
region: 'eu-west-1',
});

export async function handler() {
const clientContextData = stringify({
foo: 'foo',
});

const payload = stringify({
isin: 'XX000A1G0AE8',
});

const params = {
ClientContext: Buffer.from(clientContextData).toString('base64'),
// FunctionName is composed of: service name - stage - function name, e.g.
FunctionName: 'complete-example-dev-app3',
InvocationType: 'RequestResponse',
Payload: payload,
};

const response = await lambda.invoke(params).promise();
console.log(response);

return {
body: stringify(response),
statusCode: 200,
};
}

handler();
6 changes: 6 additions & 0 deletions examples/complete/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ plugins:
custom:
rspack:
keepOutputDirectory: true
sourcemap: 'source-map'
stats: true
esm: true
doctor:
enable: false
outputDirectory: ./
Expand Down Expand Up @@ -38,6 +40,10 @@ functions:
handler: app2.lambda_handler
runtime: python3.9
app3:
events:
- http:
method: get
path: helloApp3
handler: src/App3.handler
runtime: nodejs20.x
rspack:
Expand Down
8 changes: 6 additions & 2 deletions examples/complete/src/App3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import { readFileSync } from 'node:fs';
import path from 'node:path';
import sharp from 'sharp';

export async function handler(event: string) {
const isInvalid = validateIsin(event);
type MyEventPayload = {
isin: string;
};

export async function handler(event: MyEventPayload) {
const isInvalid = validateIsin(event.isin);
const imagePath = path.join(__dirname, '../my-image.jpeg');
const imageBuffer = readFileSync(imagePath);

Expand Down
1 change: 1 addition & 0 deletions examples/complete/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"sourceMap": true,
"paths": {
"@kitchenshelf/path-import-libs/*": ["./libs/*"]
}
Expand Down
Loading

0 comments on commit 2041d86

Please sign in to comment.