Skip to content

Commit

Permalink
Merge pull request #1959 from serverless-heaven/feat/serverless-v4
Browse files Browse the repository at this point in the history
Add support for Serverless v4
  • Loading branch information
j0k3r authored Oct 22, 2024
2 parents 0189c33 + a3bc239 commit 778d011
Show file tree
Hide file tree
Showing 20 changed files with 2,742 additions and 31 deletions.
4 changes: 4 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ updates:
- dependency-name: glob
versions:
- '>= 9.0.0'
# because tests aren't compatible
- dependency-name: serverless
versions:
- '>= 4.0.0'
- package-ecosystem: github-actions
directory: '/'
schedule:
Expand Down
36 changes: 36 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,39 @@ jobs:

- name: 'Run tests'
run: 'npm run test'

serverless-v4:
runs-on: ${{ matrix.os }}
name: Node.js ${{ matrix.node }} on ${{ matrix.os }} with Serverless v4

strategy:
matrix:
os:
- ubuntu-20.04
node:
- '20'

steps:
- name: 'Checkout'
uses: actions/checkout@v4
with:
fetch-depth: 2

- name: 'Install Node.js'
uses: actions/setup-node@v4
with:
node-version: '${{ matrix.node }}'

- name: 'Install dependencies'
run: npm ci

- name: 'Install Serverless v4'
run: npm install serverless@4

- name: 'Run tests'
run: npm run test tests/e2e/e2e.test.js
env:
AWS_DEFAULT_REGION: eu-west-1
AWS_ACCESS_KEY_ID: ${{secrets.AWS_ACCESS_KEY_ID}}
AWS_SECRET_ACCESS_KEY: ${{secrets.AWS_SECRET_ACCESS_KEY}}
SERVERLESS_ACCESS_KEY: ${{secrets.SERVERLESS_ACCESS_KEY}}
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ plugins:
- serverless-webpack
```
## Serverless v4 requirement
If you are using Serverless v4 you must disable the default builtin ESBuild support in your `serverless.yml` (because it conflicts with `serverless-webpack`):

```yml
build:
esbuild: false
```

## Configure

The configuration of the plugin is done by defining a `custom: webpack` object in your `serverless.yml` with your specific configuration. All settings are optional and will be set to reasonable defaults if missing.
Expand Down Expand Up @@ -184,8 +193,8 @@ module.exports = {

`serverless-webpack` generates Webpack entries from the `handler` value by default.

If your handler is different from the webpack entry, e.g. provided by a layer,
you may override the generated entry at function level via the `entrypoint`
If your handler is different from the webpack entry, e.g. provided by a layer,
you may override the generated entry at function level via the `entrypoint`
option in `serverless.yml`.

```yaml
Expand Down
2 changes: 2 additions & 0 deletions examples/include-external-npm-packages-lock-file/_setup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

// That file is only used by the e2e tests

const path = require('path');

module.exports = async (originalFixturePath, fixturePath, utils) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

// That file is only used by the e2e tests

const path = require('path');

module.exports = async (originalFixturePath, fixturePath, utils) => {
Expand Down
3 changes: 3 additions & 0 deletions examples/include-external-npm-packages/_setup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

// That file is only used by the e2e tests

const path = require('path');

module.exports = async (originalFixturePath, fixturePath, utils) => {
Expand All @@ -18,5 +20,6 @@ module.exports = async (originalFixturePath, fixturePath, utils) => {
]);

const command = /^win/.test(process.platform) ? 'yarn.cmd' : 'yarn';

return utils.spawnProcess(command, ['install'], { cwd: __dirname });
};
12 changes: 12 additions & 0 deletions examples/serverless-v4/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "20"
}
}
]
]
}
3 changes: 3 additions & 0 deletions examples/serverless-v4/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
.webpack
.webpackCache
5 changes: 5 additions & 0 deletions examples/serverless-v4/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
You can try to invoke the function locally (be sure to `yarn install` before):

```
yarn serverless invoke local --function=hello --path=./event.json
```
25 changes: 25 additions & 0 deletions examples/serverless-v4/_setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

// That file is only used by the e2e tests

const path = require('path');

module.exports = async (originalFixturePath, fixturePath, utils) => {
const pluginPath = path.resolve(originalFixturePath, '..', '..');

const SLS_CONFIG_PATH = path.join(fixturePath, 'serverless.yml');
const WEBPACK_CONFIG_PATH = path.join(fixturePath, 'webpack.config.js');
const PACKAGE_JSON_PATH = path.join(fixturePath, 'package.json');
const LOCK_PATH = path.join(fixturePath, 'yarn.lock');

await Promise.all([
utils.replaceInFile(SLS_CONFIG_PATH, '- serverless-webpack', `- ${pluginPath}`),
utils.replaceInFile(WEBPACK_CONFIG_PATH, "'serverless-webpack'", `'${pluginPath}'`),
utils.replaceInFile(PACKAGE_JSON_PATH, 'file:../..', `file:${pluginPath}`),
utils.replaceInFile(LOCK_PATH, 'file:../..', `file:${pluginPath}`)
]);

const command = /^win/.test(process.platform) ? 'yarn.cmd' : 'yarn';

return utils.spawnProcess(command, ['install'], { cwd: __dirname });
};
5 changes: 5 additions & 0 deletions examples/serverless-v4/event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"key3": "value3",
"key2": "value2",
"key1": "value1"
}
8 changes: 8 additions & 0 deletions examples/serverless-v4/handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import cookie from 'cookie'

export const hello = (event, context, cb) => cb(null,
{
message: 'Go Serverless Webpack (Serverless v4) v1.0! Your function executed successfully!',
event,
}
);
23 changes: 23 additions & 0 deletions examples/serverless-v4/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "serverless-webpack-serverless-v4",
"version": "1.0.0",
"description": "Serverless webpack example using Serverless v4",
"main": "handler.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"license": "MIT",
"devDependencies": {
"@babel/core": "^7.25.8",
"@babel/preset-env": "^7.25.8",
"@babel/preset-typescript": "^7.25.7",
"babel-loader": "^9.2.1",
"serverless": "^4.4.6",
"serverless-webpack": "file:../..",
"webpack": "^5.95.0",
"webpack-node-externals": "^3.0.0"
},
"dependencies": {
"cookie": "^0.7.0"
}
}
32 changes: 32 additions & 0 deletions examples/serverless-v4/serverless.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
frameworkVersion: "^4.4.0"

service: serverless-webpack-serverless-v4

package:
individually: true

# Add the serverless-webpack plugin
plugins:
- serverless-webpack

provider:
name: aws
runtime: nodejs20.x

build:
esbuild: false

functions:
hello:
handler: handler.hello
events:
- http:
method: get
path: hello
integration: lambda

custom:
webpack:
webpackConfig: 'webpack.config.js'
packager: 'yarn'
includeModules: true
38 changes: 38 additions & 0 deletions examples/serverless-v4/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const path = require('path');
const slsw = require('serverless-webpack');
const nodeExternals = require('webpack-node-externals');

module.exports = {
entry: slsw.lib.entries,
target: 'node',
mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
optimization: {
// We no not want to minimize our code.
minimize: false
},
performance: {
// Turn off size warnings for entry points
hints: false
},
devtool: 'nosources-source-map',
externals: [nodeExternals()],
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: [
{
loader: 'babel-loader'
}
]
}
]
},
output: {
libraryTarget: 'commonjs2',
path: path.join(__dirname, '.webpack'),
filename: '[name].js',
sourceMapFilename: '[file].map'
}
};
Loading

0 comments on commit 778d011

Please sign in to comment.