Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs-extra';
import globby from 'globby';
import path from 'path';
import { concat, always, memoizeWith, mergeRight } from 'ramda';
import { concat, mergeRight } from 'ramda';
import Serverless from 'serverless';
import ServerlessPlugin from 'serverless/classes/Plugin';
import chokidar from 'chokidar';
Expand Down Expand Up @@ -54,6 +54,7 @@ class EsbuildServerlessPlugin implements ServerlessPlugin {
serverless: Serverless;
options: Serverless.Options;
hooks: ServerlessPlugin.Hooks;
buildOptions: Configuration;
buildResults: FunctionBuildResult[];
/** Used for storing previous esbuild build results so we can rebuild more efficiently */
buildCache: Record<string, FileBuildResult>;
Expand Down Expand Up @@ -82,13 +83,8 @@ class EsbuildServerlessPlugin implements ServerlessPlugin {
this.preLocal = preLocal.bind(this);
this.bundle = bundle.bind(this);

this.outputWorkFolder = this.buildOptions.outputWorkFolder || WORK_FOLDER;
this.outputBuildFolder = this.buildOptions.outputBuildFolder || BUILD_FOLDER;

this.workDirPath = path.join(this.serviceDirPath, this.outputWorkFolder);
this.buildDirPath = path.join(this.workDirPath, this.outputBuildFolder);

this.hooks = {
initialize: () => this.init(),
'before:run:run': async () => {
await this.bundle();
await this.packExternalModules();
Expand Down Expand Up @@ -135,6 +131,15 @@ class EsbuildServerlessPlugin implements ServerlessPlugin {
};
}

private init() {
this.buildOptions = this.getBuildOptions();

this.outputWorkFolder = this.buildOptions.outputWorkFolder || WORK_FOLDER;
this.outputBuildFolder = this.buildOptions.outputBuildFolder || BUILD_FOLDER;
this.workDirPath = path.join(this.serviceDirPath, this.outputWorkFolder);
this.buildDirPath = path.join(this.workDirPath, this.outputBuildFolder);
}

/**
* Checks if the runtime for the given function is nodejs.
* If the runtime is not set , checks the global runtime.
Expand Down Expand Up @@ -225,7 +230,7 @@ class EsbuildServerlessPlugin implements ServerlessPlugin {
return { patterns, ignored };
}

private getCachedOptions = memoizeWith(always('cache'), () => {
private getBuildOptions() {
const DEFAULT_BUILD_OPTIONS: Partial<Configuration> = {
concurrency: Infinity,
bundle: true,
Expand Down Expand Up @@ -260,10 +265,6 @@ class EsbuildServerlessPlugin implements ServerlessPlugin {
return withResolvedOptions<Configuration>(
config ? config(this.serverless) : this.serverless.service.custom?.esbuild ?? {}
);
});

get buildOptions() {
return this.getCachedOptions();
}

get functionEntries() {
Expand Down
5 changes: 5 additions & 0 deletions src/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ afterEach(() => {
describe('Move Artifacts', () => {
it('should copy files from the esbuild folder to the serverless folder', async () => {
const plugin = new EsbuildServerlessPlugin(mockServerlessConfig(), mockOptions);
plugin.hooks.initialize();

await plugin.moveArtifacts();

Expand All @@ -83,6 +84,7 @@ describe('Move Artifacts', () => {
...mockOptions,
function: 'hello1',
});
plugin.hooks.initialize();

await plugin.moveArtifacts();

Expand All @@ -103,6 +105,7 @@ describe('Move Artifacts', () => {
describe('package individually', () => {
it('should update function package artifacts base path to the serverless folder', async () => {
const plugin = new EsbuildServerlessPlugin(mockServerlessConfig(), mockOptions);
plugin.hooks.initialize();

await plugin.moveArtifacts();

Expand Down Expand Up @@ -136,6 +139,7 @@ describe('Move Artifacts', () => {
}),
mockOptions
);
plugin.hooks.initialize();

await plugin.moveArtifacts();

Expand Down Expand Up @@ -163,6 +167,7 @@ describe('Move Artifacts', () => {
describe('service package', () => {
it('should update the service package artifact base path to the serverless folder', async () => {
const plugin = new EsbuildServerlessPlugin(mockServerlessConfig(packageService), mockOptions);
plugin.hooks.initialize();

await plugin.moveArtifacts();

Expand Down