Skip to content

Add functionality to suppress missing optional dependency warnings #102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,21 @@ custom:
handlerRoot: ../../
```

## Suppress "missing optional dependency" warning
When building you may have missing optional dependencies that are not installed. In this case you will receive messages like:

```
Serverless: [serverless-plugin-include-dependencies]: WARNING missing optional dependency: aws-crt
```

To suppress these messages, add a list of the optional dependencies to ignore

```yaml
custom:
ignoreOptionalDependenciesList:
- aws-crt
```

## New In 2.0 - Exclusion Support

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 patterns* to filter these files. Patterns *must* start with `!node_modules` to be considered by this plugin.
Expand Down
16 changes: 14 additions & 2 deletions __tests__/get-dependency-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ test('should handle requires to a missing optionalDependency listed in dependenc
t.true(log.called);
});

test('should suppress missing optionalDependency warning', (t) => {
const fileName = path.join(__dirname, 'fixtures', 'optional-dep-missing.js');
const log = sinon.stub();
const ignoreOptionalDependenciesList = ['optional-dep-wont-be-found'];

const list = getDependencyList(fileName, Object.assign({ cli: { log } }, serverless), ignoreOptionalDependenciesList);

t.true(list.some(item => item.indexOf(`optional-dep-missing.js`) !== -1));
t.true(list.some(item => item.indexOf(`node_modules/optional-dep-parent/index.js`) !== -1));
t.true(log.notCalled);
});

test('should handle requires to a missing peerDependency listed in peerDependenciesMeta as optional', (t) => {
const fileName = path.join(__dirname, 'fixtures', 'optional-dep-meta-missing.js');
const log = sinon.stub();
Expand Down Expand Up @@ -129,8 +141,8 @@ test('caches lookups', (t) => {
const fileName2 = path.join(__dirname, 'fixtures', 'redundancies-2.js');

const cache = new Set();
const list1 = getDependencyList(fileName, serverless, cache);
const list2 = getDependencyList(fileName2, serverless, cache);
const list1 = getDependencyList(fileName, serverless, [], cache);
const list2 = getDependencyList(fileName2, serverless, [], cache);

t.true(list1.some(item => item.endsWith('local/named/index.js')));
t.true(list1.some(item => item.endsWith('symlinked.js')));
Expand Down
6 changes: 5 additions & 1 deletion get-dependency-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function ignoreMissing(dependency, optional, peerDependenciesMeta) {
|| peerDependenciesMeta && dependency in peerDependenciesMeta && peerDependenciesMeta[dependency].optional;
}

module.exports = function(filename, serverless, cache) {
module.exports = function(filename, serverless, ignoreOptionalDependenciesList = [], cache) {
const servicePath = serverless.config.servicePath;
const modulePaths = new Set();
const filePaths = new Set();
Expand Down Expand Up @@ -44,6 +44,10 @@ module.exports = function(filename, serverless, cache) {
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
if (ignoreMissing(moduleName, optionalDependencies, peerDependenciesMeta)) {
if (ignoreOptionalDependenciesList.includes(moduleName)) {
return null;
}

serverless.cli.log(`[serverless-plugin-include-dependencies]: WARNING missing optional dependency: ${moduleName}`);
return null;
}
Expand Down
3 changes: 2 additions & 1 deletion include-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ module.exports = class IncludeDependencies {

getDependencies(fileName, patterns, useCache = false) {
const servicePath = this.serverless.config.servicePath;
const dependencies = getDependencyList(fileName, this.serverless, useCache && this.cache) || [];
const ignoreOptionalDependenciesList = this.getPluginOptions().ignoreOptionalDependenciesList || [];
const dependencies = getDependencyList(fileName, this.serverless, ignoreOptionalDependenciesList, useCache && this.cache) || [];
const relativeDependencies = dependencies.map(p => path.relative(servicePath, p));

const exclusions = patterns.filter(p => p.startsWith('!') && p.includes('node_modules'));
Expand Down