-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sls-rspack): support sls cli deploy function
Closes #11
- Loading branch information
1 parent
e19a6ad
commit 20fe405
Showing
11 changed files
with
303 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
libs/serverless-rspack/src/lib/hooks/deploy-function/after-package-function.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import type { RspackServerlessPlugin } from '../../serverless-rspack.js'; | ||
|
||
export async function AfterDeployFunctionPackageFunction( | ||
this: RspackServerlessPlugin | ||
) { | ||
this.log.verbose('[sls-rspack] after:deploy:function:packageFunction'); | ||
await this.cleanup(); | ||
} |
34 changes: 34 additions & 0 deletions
34
libs/serverless-rspack/src/lib/hooks/deploy-function/before-package-function.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import type { RspackServerlessPlugin } from '../../serverless-rspack.js'; | ||
import { isDeployFunctionOptions } from '../../types.js'; | ||
|
||
export async function BeforeDeployFunctionPackageFunction( | ||
this: RspackServerlessPlugin | ||
) { | ||
this.log.verbose('[sls-rspack] before:deploy:function:packageFunction'); | ||
|
||
if (!isDeployFunctionOptions(this.options)) { | ||
throw new this.serverless.classes.Error( | ||
'This hook only supports deploy function options' | ||
); | ||
} | ||
|
||
const deployFunc = this.options.function; | ||
|
||
if (!(deployFunc in this.functionEntries)) { | ||
throw new this.serverless.classes.Error( | ||
`Function ${deployFunc} not found in function entries` | ||
); | ||
} | ||
|
||
const entry = this.functionEntries[deployFunc]; | ||
|
||
await this.bundle({ | ||
[deployFunc]: entry, | ||
}); | ||
|
||
this.functionEntries = {}; | ||
this.functionEntries[deployFunc] = entry; | ||
|
||
await this.scripts(); | ||
await this.pack(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
libs/serverless-rspack/src/test/hooks/deploy-function/after-package-function.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { rm } from 'node:fs/promises'; | ||
import { RspackServerlessPlugin } from '../../../lib/serverless-rspack.js'; | ||
import { PluginOptions } from '../../../lib/types.js'; | ||
import { logger, mockOptions, mockServerlessConfig } from '../../test-utils.js'; | ||
|
||
jest.mock('../../../lib/bundle', () => ({ | ||
bundle: jest.fn(), | ||
})); | ||
|
||
jest.mock('node:fs', () => ({ | ||
readdirSync: () => ['hello1.ts', 'hello2.ts'], | ||
})); | ||
|
||
jest.mock('node:fs/promises', () => ({ | ||
rm: jest.fn(), | ||
})); | ||
|
||
afterEach(() => { | ||
jest.resetModules(); | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
describe('after:deploy:function:packageFunction hook', () => { | ||
it('should be defined', () => { | ||
const serverless = mockServerlessConfig(); | ||
const plugin = new RspackServerlessPlugin(serverless, mockOptions, logger); | ||
|
||
expect(plugin.hooks['after:deploy:function:packageFunction']).toBeDefined(); | ||
}); | ||
|
||
it('should by default remove the build dir', async () => { | ||
const serverless = mockServerlessConfig(); | ||
const plugin = new RspackServerlessPlugin(serverless, mockOptions, logger); | ||
|
||
plugin.pluginOptions = {} as Required<PluginOptions>; | ||
|
||
await plugin.hooks['after:deploy:function:packageFunction'](); | ||
|
||
expect(rm).toHaveBeenCalledWith('/workDir/.rspack', { | ||
recursive: true, | ||
}); | ||
}); | ||
|
||
it('should remove the build dir when keepOutputDirectory is false', async () => { | ||
const serverless = mockServerlessConfig(); | ||
const plugin = new RspackServerlessPlugin(serverless, mockOptions, logger); | ||
|
||
plugin.pluginOptions = { | ||
keepOutputDirectory: false, | ||
} as Required<PluginOptions>; | ||
|
||
await plugin.hooks['after:deploy:function:packageFunction'](); | ||
|
||
expect(rm).toHaveBeenCalledWith('/workDir/.rspack', { | ||
recursive: true, | ||
}); | ||
}); | ||
|
||
it('keep the build dir when keepOutputDirectory is true', async () => { | ||
const serverless = mockServerlessConfig(); | ||
const plugin = new RspackServerlessPlugin(serverless, mockOptions, logger); | ||
|
||
plugin.pluginOptions = { | ||
keepOutputDirectory: true, | ||
} as Required<PluginOptions>; | ||
|
||
await plugin.hooks['after:deploy:function:packageFunction'](); | ||
expect(rm).not.toHaveBeenCalledWith('/workDir/.rspack', { | ||
recursive: true, | ||
}); | ||
}); | ||
}); |
160 changes: 160 additions & 0 deletions
160
libs/serverless-rspack/src/test/hooks/deploy-function/before-package-function.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
import { bundle } from '../../../lib/bundle.js'; | ||
import { pack } from '../../../lib/pack.js'; | ||
import { scripts } from '../../../lib/scripts.js'; | ||
import { RspackServerlessPlugin } from '../../../lib/serverless-rspack.js'; | ||
import { logger, mockOptions, mockServerlessConfig } from '../../test-utils.js'; | ||
|
||
jest.mock('../../../lib/bundle', () => ({ | ||
bundle: jest.fn(), | ||
})); | ||
|
||
jest.mock('../../../lib/pack', () => ({ | ||
pack: jest.fn(), | ||
})); | ||
|
||
jest.mock('../../../lib/scripts', () => ({ | ||
scripts: jest.fn(), | ||
})); | ||
|
||
afterEach(() => { | ||
jest.resetModules(); | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
describe('before:deploy:function:packageFunction', () => { | ||
let plugin: RspackServerlessPlugin; | ||
|
||
it('should be defined', () => { | ||
const serverless = mockServerlessConfig(); | ||
const plugin = new RspackServerlessPlugin(serverless, mockOptions, logger); | ||
|
||
expect( | ||
plugin.hooks['before:deploy:function:packageFunction'] | ||
).toBeDefined(); | ||
}); | ||
|
||
it('should throw error if options are invalid', async () => { | ||
const serverless = mockServerlessConfig(); | ||
plugin = new RspackServerlessPlugin(serverless, {}, logger); | ||
|
||
try { | ||
await plugin.hooks['before:deploy:function:packageFunction'](); | ||
fail('Expected function to throw an error'); | ||
} catch (error) { | ||
expect(serverless.classes.Error).toHaveBeenCalledTimes(1); | ||
expect(serverless.classes.Error).toHaveBeenCalledWith( | ||
'This hook only supports deploy function options' | ||
); | ||
} | ||
}); | ||
|
||
it('should throw error if function not found in entries', async () => { | ||
const serverless = mockServerlessConfig(); | ||
plugin = new RspackServerlessPlugin( | ||
serverless, | ||
{ ...mockOptions, function: 'nonexistentFunc' }, | ||
logger | ||
); | ||
plugin.functionEntries = {}; | ||
try { | ||
await plugin.hooks['before:deploy:function:packageFunction'](); | ||
fail('Expected function to throw an error'); | ||
} catch (error) { | ||
expect(serverless.classes.Error).toHaveBeenCalledTimes(1); | ||
expect(serverless.classes.Error).toHaveBeenCalledWith( | ||
'Function nonexistentFunc not found in function entries' | ||
); | ||
} | ||
}); | ||
|
||
it('should bundle the entries', async () => { | ||
const serverless = mockServerlessConfig(); | ||
const plugin = new RspackServerlessPlugin( | ||
serverless, | ||
{ ...mockOptions, function: 'myFunc' }, | ||
logger | ||
); | ||
|
||
plugin.functionEntries = { | ||
myFunc: { import: 'test/path/entry.ts', filename: 'test/path/entry.ts' }, | ||
}; | ||
|
||
await plugin.hooks['before:deploy:function:packageFunction'](); | ||
|
||
expect(bundle).toHaveBeenCalledTimes(1); | ||
expect(bundle).toHaveBeenCalledWith(plugin.functionEntries); | ||
}); | ||
|
||
it('should run scripts', async () => { | ||
const serverless = mockServerlessConfig(); | ||
const plugin = new RspackServerlessPlugin( | ||
serverless, | ||
{ ...mockOptions, function: 'myFunc' }, | ||
logger | ||
); | ||
|
||
plugin.functionEntries = { | ||
myFunc: { import: 'test/path/entry.ts', filename: 'test/path/entry.ts' }, | ||
}; | ||
|
||
await plugin.hooks['before:deploy:function:packageFunction'](); | ||
|
||
expect(scripts).toHaveBeenCalledTimes(1); | ||
expect(scripts).toHaveBeenCalledWith(); | ||
}); | ||
|
||
it('should pack the entries', async () => { | ||
const serverless = mockServerlessConfig(); | ||
const plugin = new RspackServerlessPlugin( | ||
serverless, | ||
{ ...mockOptions, function: 'myFunc' }, | ||
logger | ||
); | ||
|
||
plugin.functionEntries = { | ||
myFunc: { import: 'test/path/entry.ts', filename: 'test/path/entry.ts' }, | ||
}; | ||
|
||
await plugin.hooks['before:deploy:function:packageFunction'](); | ||
|
||
expect(pack).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should bundle before scripts', async () => { | ||
const serverless = mockServerlessConfig(); | ||
const plugin = new RspackServerlessPlugin( | ||
serverless, | ||
{ ...mockOptions, function: 'myFunc' }, | ||
logger | ||
); | ||
|
||
plugin.functionEntries = { | ||
myFunc: { import: 'test/path/entry.ts', filename: 'test/path/entry.ts' }, | ||
}; | ||
|
||
await plugin.hooks['before:deploy:function:packageFunction'](); | ||
|
||
expect(jest.mocked(bundle).mock.invocationCallOrder[0]).toBeLessThan( | ||
jest.mocked(scripts).mock.invocationCallOrder[0] | ||
); | ||
}); | ||
|
||
it('should pack after scripts', async () => { | ||
const serverless = mockServerlessConfig(); | ||
const plugin = new RspackServerlessPlugin( | ||
serverless, | ||
{ ...mockOptions, function: 'myFunc' }, | ||
logger | ||
); | ||
|
||
plugin.functionEntries = { | ||
myFunc: { import: 'test/path/entry.ts', filename: 'test/path/entry.ts' }, | ||
}; | ||
|
||
await plugin.hooks['before:deploy:function:packageFunction'](); | ||
|
||
expect(jest.mocked(scripts).mock.invocationCallOrder[0]).toBeLessThan( | ||
jest.mocked(pack).mock.invocationCallOrder[0] | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters