Skip to content

Commit e222b78

Browse files
committed
Add support for esbuild code splitting
1 parent f2f0a9c commit e222b78

11 files changed

Lines changed: 300 additions & 186 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
node_modules/
2-
dist/
2+
dist*/
33
coverage/
44

55
.DS_store

example/second.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import rimraf from 'rimraf';
22

33
export const addTest = (a: number, b: number) => a + b;
44

5-
export const test = () => {
5+
const test = () => {
66
console.log('rimraf', rimraf);
77
};
8+
9+
export default test;

example/test.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
import { addTest } from './second';
1+
const test = {
2+
second: () => import('./second'),
3+
third: () => import('./third'),
4+
};
5+
6+
type testKeysType = keyof typeof test;
27

3-
export const handler = () => {
4-
console.log(addTest(1, 3));
8+
export const handler = async (payload: unknown = 'second') => {
9+
if (typeof payload === 'string' && payload in test) {
10+
test[payload as testKeysType]();
11+
}
512
};

example/third.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const justForTest = () => console.log('Example');
2+
3+
export default justForTest;

package-lock.json

Lines changed: 214 additions & 168 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"dependencies": {
5050
"archiver": "^5.3.1",
5151
"commander": "^9.3.0",
52-
"esbuild": "^0.14.43",
52+
"esbuild": "^0.15.13",
5353
"zod": "^3.17.3"
5454
}
5555
}

src/bulidFunction.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ const buildFunction = async (
1717
const esbuildConfig: BuildOptions =
1818
packageObject.funpack.settings.esbuildConfigOverride;
1919
const outputDir = packageObject.funpack.settings.outputDir;
20-
const outputFilePath = join(outputDir, name, OUTPUT_FILE_NAME);
20+
const functionOutputDir = join(outputDir, name);
2121

2222
// Use esbuild to compile the function
2323
const buildResult = await useEsbuild(
2424
entrypoint,
25-
outputFilePath,
25+
functionOutputDir,
26+
OUTPUT_FILE_NAME.replace('.js', ''),
2627
esbuildConfig
2728
);
2829

@@ -32,6 +33,7 @@ const buildFunction = async (
3233
error('Missing metafile in esbuild result!');
3334
return false;
3435
}
36+
const outputFilePath = join(outputDir, name, OUTPUT_FILE_NAME);
3537
generatePackageJson(
3638
metafile,
3739
packageObject,
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
import { join } from 'path';
22

3-
import funpack from './index';
3+
import funpack from '../index';
44

55
process.env.GIT_REPO_URL = 'https://example.com/test/repo.git';
66

7-
jest.mock('./utils/getPackageJsonObject', () =>
7+
jest.mock('../utils/getPackageJsonObject', () =>
88
jest.fn().mockReturnValue({
99
name: 'my-lambdas',
1010
version: '0.0.0-dev',
11-
type: 'module',
1211
funpack: {
1312
settings: {
1413
outputDir: 'example/dist',
1514
packageFieldsToCopy: ['version', 'type'],
1615
cleanupOutputDir: true,
1716
zip: true,
18-
removeDirAfterZip: true,
17+
//removeDirAfterZip: true,
1918
customPackageFields: {
2019
repository: {
2120
type: 'git',
@@ -39,13 +38,13 @@ describe('funpack', () => {
3938
expect(console.log).toBeCalledWith(
4039
join('example', 'dist', 'testfunc.zip'),
4140
'-',
42-
20771,
41+
21113,
4342
'total bytes'
4443
);
4544
expect(console.log).toBeCalledWith(
4645
join('example', 'dist', 'second.zip'),
4746
'-',
48-
20768,
47+
20815,
4948
'total bytes'
5049
);
5150
});

src/packTests/splitting.test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { join } from 'path';
2+
3+
import funpack from '../index';
4+
5+
process.env.GIT_REPO_URL = 'https://example.com/test/repo.git';
6+
7+
// TODO: Change to using full examples instead of mocking like that
8+
jest.mock('../utils/getPackageJsonObject', () =>
9+
jest.fn().mockReturnValue({
10+
name: 'my-lambdas',
11+
version: '0.0.0-dev',
12+
type: 'module',
13+
funpack: {
14+
settings: {
15+
outputDir: 'example/dist-splitting',
16+
packageFieldsToCopy: ['version', 'type'],
17+
cleanupOutputDir: true,
18+
zip: true,
19+
esbuildConfigOverride: {
20+
format: 'esm',
21+
target: 'node16',
22+
splitting: true,
23+
sourcemap: true,
24+
},
25+
},
26+
functions: {
27+
firstSplit: './example/test.ts',
28+
secondSplit: './example/second.ts',
29+
},
30+
},
31+
})
32+
);
33+
34+
describe('funpack', () => {
35+
console.log = jest.fn();
36+
it('builds correctly', async () => {
37+
await funpack();
38+
// TODO: Improve test case to check for files?
39+
expect(console.log).toHaveBeenCalledTimes(2);
40+
const logMock = console.log as jest.Mock;
41+
42+
expect(logMock.mock.calls[0][0]).toBe(
43+
join('example', 'dist-splitting', 'secondSplit.zip')
44+
);
45+
expect(logMock.mock.calls[0][2]).toBeCloseTo(67352, -2);
46+
47+
expect(logMock.mock.calls[1][0]).toBe(
48+
join('example', 'dist-splitting', 'firstSplit.zip')
49+
);
50+
expect(logMock.mock.calls[1][2]).toBeCloseTo(69275, -2);
51+
});
52+
});

src/parts/esbuild.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@ import type { BuildOptions } from 'esbuild';
33

44
const useEsbuild = async (
55
entrypoint: string,
6-
outfile: string,
6+
outputDir: string,
7+
outputName: string,
78
esbuildConfig: BuildOptions = {}
89
) => {
910
const result = await build({
10-
entryPoints: [entrypoint],
11+
entryPoints: {
12+
[outputName]: entrypoint,
13+
},
14+
outdir: outputDir,
1115
bundle: true,
1216
platform: 'node',
13-
outfile,
1417
metafile: true,
1518
...esbuildConfig,
1619
});

0 commit comments

Comments
 (0)