Skip to content

Commit

Permalink
feat(sls-rspack): only bundle functions that are node based or have n…
Browse files Browse the repository at this point in the history
…ew rspack flag
  • Loading branch information
codingnuclei committed May 11, 2024
1 parent 8fe4c1a commit f070c1a
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 30 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ testem.log
Thumbs.db

.nx/cache
.env.local
5 changes: 5 additions & 0 deletions examples/complete/app2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def lambda_handler(event, context):
message = 'Hello {} {}!'.format(event['first_name'], event['last_name'])
return {
'message' : message
}
13 changes: 0 additions & 13 deletions examples/complete/app2.ts

This file was deleted.

15 changes: 11 additions & 4 deletions examples/complete/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,29 @@ plugins:

custom:
rspack:
config: './rspack.config.js'
keepOutputDirectory: true
stats: true
# esm: true
# stats: true
# config: './rspack.config.js'
esm: true

provider:
name: aws
runtime: nodejs20.x

package:
individually: true

functions:
App1:
handler: app1.handler
runtime: 'provided.al2023'
rspack: true
app2:
handler: app2.handler
handler: app2.lambda_handler
runtime: python3.9
app3:
handler: src/App3.handler
runtime: nodejs20.x
app4:
handler: src/app4.handler
app5:
Expand Down
54 changes: 41 additions & 13 deletions libs/serverless-rspack/src/lib/serverless-rspack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import type ServerlessPlugin from 'serverless/classes/Plugin';
import { bundle } from './bundle.js';
import { SERVERLESS_FOLDER, WORK_FOLDER } from './constants.js';
import { pack } from './pack.js';
import { PluginConfiguration, PluginConfigurationSchema } from './types.js';
import {
PluginConfiguration,
PluginConfigurationSchema,
RsPackFunctionDefinitionHandler,
} from './types.js';

export class RspackServerlessPlugin implements ServerlessPlugin {
serviceDirPath: string;
Expand Down Expand Up @@ -38,6 +42,13 @@ export class RspackServerlessPlugin implements ServerlessPlugin {
logging: ServerlessPlugin.Logging
) {
assert(logging, 'Please use serverless V4');

serverless.configSchemaHandler.defineFunctionProperties('aws', {
properties: {
rspack: { type: 'boolean' },
},
});

this.serverless = serverless;
this.options = options;
this.log = logging.log;
Expand Down Expand Up @@ -149,18 +160,23 @@ export class RspackServerlessPlugin implements ServerlessPlugin {
let entries = {};

functions.forEach((functionName) => {
const functionDefinitionHandler =
this.serverless.service.getFunction(functionName);

// TODO: support container images
const entry = this.getEntryForFunction(
functionName,
functionDefinitionHandler as Serverless.FunctionDefinitionHandler
);
entries = {
...entries,
...entry,
};
const functionDefinitionHandler = this.serverless.service.getFunction(
functionName
) as RsPackFunctionDefinitionHandler;
if (
functionDefinitionHandler.rspack ||
this.isNodeFunction(functionDefinitionHandler)
) {
// TODO: support container images
const entry = this.getEntryForFunction(
functionName,
functionDefinitionHandler as Serverless.FunctionDefinitionHandler
);
entries = {
...entries,
...entry,
};
}
});
return entries;
}
Expand Down Expand Up @@ -227,6 +243,18 @@ export class RspackServerlessPlugin implements ServerlessPlugin {
throw new this.serverless.classes.Error(`malformed handler: ${handler}`);
}

/**
* Checks if the runtime for the given function is nodejs.
* If the runtime is not set , checks the global runtime.
* @param {Serverless.FunctionDefinitionHandler} func the function to be checked
* @returns {boolean} true if the function/global runtime is nodejs; false, otherwise
*/
private isNodeFunction(func: Serverless.FunctionDefinitionHandler): boolean {
const runtime = func.runtime || this.serverless.service.provider.runtime;

return typeof runtime === 'string' && runtime.startsWith('node');
}

private async cleanup(): Promise<void> {
if (!this.pluginConfig.keepOutputDirectory) {
await rm(path.join(this.buildOutputFolderPath), { recursive: true });
Expand Down
5 changes: 5 additions & 0 deletions libs/serverless-rspack/src/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Serverless from 'serverless';
import { z } from 'zod';

export const PluginConfigurationSchema = z.object({
Expand All @@ -12,3 +13,7 @@ export const PluginConfigurationSchema = z.object({
});

export type PluginConfiguration = z.infer<typeof PluginConfigurationSchema>;

export type RsPackFunctionDefinitionHandler = {
rspack?: boolean;
} & Serverless.FunctionDefinitionHandler;

0 comments on commit f070c1a

Please sign in to comment.