Skip to content
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

Adds the ability to specify an excludeFiles glob #471

Merged
merged 4 commits into from
Apr 21, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ custom:
webpackConfig: 'webpack.config.js' # Name of webpack configuration file
includeModules: false # Node modules configuration for packaging
packager: 'npm' # Packager that will be used to package your external modules
excludeFiles: src/**/*.test.js # Provide a glob for files to ignore
```

### Webpack configuration file
Expand Down Expand Up @@ -432,6 +433,25 @@ from a local folder (e.g. `"mymodule": "file:../../myOtherProject/mymodule"`).
With that you can do test deployments from the local machine with different
module versions or modules before they are published officially.

#### Exclude Files with similar names

If you have a project structure that uses something like `index.js` and a
co-located `index.test.js` then you have likely seen an error like:
`WARNING: More than one matching handlers found for index. Using index.js`

This config option allows you to exlcude files that match a glob from function
resolution. Just add: `excludeFiles: **/*.test.js` (with whatever glob you want
to exclude).

```yaml
# serverless.yml
custom:
webpack:
excludeFiles: **/*.test.js
```

This is also useful for projects that use TypeScript.

#### Keep output directory after packaging

You can keep the output directory (defaults to `.webpack`) from being removed
Expand Down
6 changes: 5 additions & 1 deletion lib/Configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

const _ = require('lodash');

/**
/**
* Plugin defaults
*/
const DefaultConfig = {
Expand Down Expand Up @@ -51,6 +51,10 @@ class Configuration {
return this._config.includeModules;
}

get excludeFiles() {
return this._config.excludeFiles;
}

get packager() {
return this._config.packager;
}
Expand Down
7 changes: 4 additions & 3 deletions lib/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ module.exports = {
const getEntryExtension = fileName => {
const files = glob.sync(`${fileName}.*`, {
cwd: this.serverless.config.servicePath,
nodir: true
nodir: true,
ignore: this.configuration.excludeFiles ? this.configuration.excludeFiles : undefined
});

if (_.isEmpty(files)) {
Expand Down Expand Up @@ -117,7 +118,7 @@ module.exports = {
return BbPromise.reject(err);
}
}

// Intermediate function to handle async webpack config
const processConfig = _config => {
this.webpackConfig = _config;
Expand Down Expand Up @@ -220,7 +221,7 @@ module.exports = {

return BbPromise.resolve();
};

// Webpack config can be a Promise, If it's a Promise wait for resolved config object.
if (this.webpackConfig && _.isFunction(this.webpackConfig.then)) {
return BbPromise.resolve(this.webpackConfig.then(config => processConfig(config)));
Expand Down
34 changes: 34 additions & 0 deletions tests/validate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,40 @@ describe('validate', () => {
});
});

it('should call glob with ignore parameter if there is an excludeFiles config', () => {
const testOutPath = 'test';
const testFunction = 'func1';
const testConfig = {
entry: 'test',
context: 'testcontext',
output: {
path: testOutPath,
},
};
_.set(module.serverless.service, 'custom.webpack.config', testConfig);
_.set(module.serverless.service, 'custom.webpack.excludeFiles', '**/*.ts');
module.serverless.service.functions = testFunctionsConfig;
module.options.function = testFunction;
globSyncStub.returns(['module1.js']);
return expect(module.validate()).to.be.fulfilled
.then(() => {
const lib = require('../lib/index');
const expectedLibEntries = {
'module1': './module1.js'
};

expect(lib.entries).to.deep.equal(expectedLibEntries);

expect(globSyncStub).to.have.been.calledOnceWith('module1.*', {
ignore: '**/*.ts',
cwd: null,
nodir: true
});
expect(serverless.cli.log).not.to.have.been.called;
return null;
});
});

it('should throw an exception if no handler is found', () => {
const testOutPath = 'test';
const testFunction = 'func1';
Expand Down