Skip to content
This repository was archived by the owner on Sep 2, 2025. It is now read-only.

Commit 3bbd14c

Browse files
author
Doug Moscrop
authored
Merge pull request dougmoscrop#50 from rschick/speedup
Use caching to reduce redundant work
2 parents c1e0d41 + 719d3e7 commit 3bbd14c

File tree

14 files changed

+101
-9
lines changed

14 files changed

+101
-9
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ package:
6363
```
6464
But be warned: Smaller individual functions can still mean a larger overall deployment. (10 functions that are 3 MB each is more net data tranfer and storage than 1 function that is 6 MB)
6565
66+
## Dependency caching (Experimental)
67+
68+
When building a shared bundle for several functions, execution time can be reduced by enabling dependency caching. Caching is disabled by default and can be enabled using the `enableCaching` option:
69+
70+
```yaml
71+
custom:
72+
includeDependencies:
73+
enableCaching: true
74+
```
75+
76+
6677
## New In 2.0 - Exclusion Support
6778

6879
Rather than including module folders (e.g. `node_modules/foo/**`, it now includes a list of actual files (e.g. `node_modules/foo/package.json`, `node_modules/foo/index.js`) and *uses the serverless package exclude* to filter these files. Excludes *must* start with `node_modules` to be considered by this plugin.

__tests__/fixtures/bar/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
require('../babel');
12
module.exports = require('./baz');

__tests__/fixtures/foo/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
require('../babel');
12
module.exports = require('./baz');
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require('local/named');
2+
require('./other/other-thing');
3+
require('./symlinked/symlinked');
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require('local/named');
2+
require('./thing');
3+
require('test-dep');

__tests__/fixtures/symlinked/node_modules/test-dep

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "sample",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "ISC",
11+
"dependencies": {
12+
"test-dep": "*"
13+
}
14+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('test-dep');

__tests__/get-dependency-list.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,20 @@ test('understands local named dependencies', (t) => {
119119
t.true(list.some(item => item.endsWith('dep-local-named.js')));
120120
t.true(list.some(item => item.endsWith('local/named/index.js')));
121121
});
122+
123+
test('caches lookups', (t) => {
124+
const fileName = path.join(__dirname, 'fixtures', 'redundancies-1.js');
125+
const fileName2 = path.join(__dirname, 'fixtures', 'redundancies-2.js');
126+
127+
const cache = new Set();
128+
const list1 = getDependencyList(fileName, serverless, cache);
129+
const list2 = getDependencyList(fileName2, serverless, cache);
130+
131+
t.true(list1.some(item => item.endsWith('local/named/index.js')));
132+
t.true(list1.some(item => item.endsWith('symlinked.js')));
133+
t.true(list1.some(item => item.endsWith('node_modules/test-dep/test-dep.js')));
134+
135+
t.false(list2.some(item => item.endsWith('local/named/index.js')));
136+
t.false(list2.some(item => item.endsWith('symlinked.js')));
137+
t.false(list2.some(item => item.endsWith('test-dep.js')));
138+
});

__tests__/include-dependencies.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,3 +415,21 @@ test('processFunction should handle different runtimes', t => {
415415

416416
t.true(processNode.calledTwice);
417417
});
418+
419+
test('disables caching by default', t => {
420+
const instance = createTestInstance();
421+
const file = path.join(__dirname, 'fixtures', 'thing.js');
422+
const list1 = instance.getDependencies(file, []);
423+
const list2 = instance.getDependencies(file, []);
424+
t.deepEqual(list1, list2);
425+
});
426+
427+
test('enables caching', t => {
428+
const instance = createTestInstance({
429+
service: { custom: { includeDependencies: { enableCaching: true } } }
430+
});
431+
const file = path.join(__dirname, 'fixtures', 'thing.js');
432+
const list1 = instance.getDependencies(file, []);
433+
const list2 = instance.getDependencies(file, []);
434+
t.true(list2.length < list1.length);
435+
});

0 commit comments

Comments
 (0)