diff --git a/README.md b/README.md index e19ccf8ab3..835cfc836d 100644 --- a/README.md +++ b/README.md @@ -439,7 +439,7 @@ 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 +This config option allows you to exclude files that match a glob from function resolution. Just add: `excludeFiles: **/*.test.js` (with whatever glob you want to exclude). @@ -452,6 +452,19 @@ custom: This is also useful for projects that use TypeScript. +#### Exclude Files with Regular Expression + +This config option allows you to filter files that match a regex pattern before +adding to the zip file. Just add: `excludeRegex: \.ts|test|\.map` (with whatever +regex you want to exclude). + +```yaml +# serverless.yml +custom: + webpack: + excludeRegex: \.ts|test|\.map +``` + #### Keep output directory after packaging You can keep the output directory (defaults to `.webpack`) from being removed diff --git a/lib/Configuration.js b/lib/Configuration.js index 2022ca9135..d2fea80c7e 100644 --- a/lib/Configuration.js +++ b/lib/Configuration.js @@ -53,6 +53,10 @@ class Configuration { get excludeFiles() { return this._config.excludeFiles; } + + get excludeRegex() { + return this._config.excludeRegex; + } get packager() { return this._config.packager; diff --git a/lib/packageModules.js b/lib/packageModules.js index 5026f2c29a..ff7bda5683 100644 --- a/lib/packageModules.js +++ b/lib/packageModules.js @@ -34,12 +34,14 @@ function zip(directory, name) { const output = fs.createWriteStream(artifactFilePath); - const files = glob.sync('**', { + let files = glob.sync('**', { cwd: directory, dot: true, silent: true, follow: true }); + + if (this.configuration.excludeRegex) files = files.filter(f => f.match(this.configuration.excludeRegex) === null); if (_.isEmpty(files)) { const error = new this.serverless.classes.Error('Packaging: No files found');