From 227d24571a8abd303f8dc88e77e3cb0659727717 Mon Sep 17 00:00:00 2001 From: Nikolay Dolzhenkov Date: Fri, 9 Apr 2021 00:01:01 +0300 Subject: [PATCH 1/3] zip only filtered files --- lib/packageModules.js | 14 +++++++++----- lib/wpwatch.js | 2 +- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/packageModules.js b/lib/packageModules.js index 4b06d61f2..858a34e08 100644 --- a/lib/packageModules.js +++ b/lib/packageModules.js @@ -27,15 +27,19 @@ function setArtifactPath(funcName, func, artifactPath) { function zip(directory, name) { // Check that files exist to be zipped - let files = glob.sync('**', { + const globFiles = glob.sync('**', { cwd: directory, dot: true, silent: true, - follow: true + follow: true, + nodir: true }); - if (this.configuration.excludeRegex) { - files = _.filter(files, f => f.match(this.configuration.excludeRegex) === null); + const files = this.configuration.excludeRegex + ? _.filter(globFiles, f => f.match(this.configuration.excludeRegex) === null) + : globFiles; + if (files.length < globFiles.length && this.options.verbose) { + this.serverless.cli.log(`Excluded ${globFiles.length - files.length} file(s) based on excludeRegex`); } if (_.isEmpty(files)) { @@ -49,7 +53,7 @@ function zip(directory, name) { this.serverless.utils.writeFileDir(artifactFilePath); const cwd = directory; - const source = '*'; + const source = files; const destination = path.relative(cwd, artifactFilePath); const zipArgs = { source, diff --git a/lib/wpwatch.js b/lib/wpwatch.js index 2a9b5b19c..36c34caff 100644 --- a/lib/wpwatch.js +++ b/lib/wpwatch.js @@ -83,7 +83,7 @@ module.exports = { } process.env.SLS_DEBUG && - this.serverless.cli.log(`Webpack watch invoke: HASH NEW=${stats.hash} CUR=${lastHash}`); + this.serverless.cli.log(`Webpack watch invoke: HASH NEW=${stats && stats.hash} CUR=${lastHash}`); // If the file hash did not change there were no effective code changes detected // (comment changes do not change the compile hash and do not account for a rebuild!) From ecb23f2dadadbaf32d9b371a46fce5a4617d989d Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Fri, 9 Apr 2021 11:10:06 +0200 Subject: [PATCH 2/3] Update doc about `excludeRegex` Also: - dump a message to log how files were excluded (might be good for debugging) - update unit test to actually match the type of `excludeRegex` from the configuration (which is not a regex but a string) --- README.md | 2 +- lib/packageModules.js | 23 ++++++++++++----------- tests/packageModules.test.js | 2 +- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index d323b0cfb..662ac1899 100644 --- a/README.md +++ b/README.md @@ -463,7 +463,7 @@ regex you want to exclude). # serverless.yml custom: webpack: - excludeRegex: /\.ts|test|\.map/ + excludeRegex: \.ts|test|\.map ``` #### Keep output directory after packaging diff --git a/lib/packageModules.js b/lib/packageModules.js index 858a34e08..31c3238a5 100644 --- a/lib/packageModules.js +++ b/lib/packageModules.js @@ -35,15 +35,18 @@ function zip(directory, name) { nodir: true }); - const files = this.configuration.excludeRegex - ? _.filter(globFiles, f => f.match(this.configuration.excludeRegex) === null) - : globFiles; - if (files.length < globFiles.length && this.options.verbose) { - this.serverless.cli.log(`Excluded ${globFiles.length - files.length} file(s) based on excludeRegex`); + let files = globFiles; + if (this.configuration.excludeRegex) { + files = _.filter(globFiles, f => f.match(this.configuration.excludeRegex) === null); + + if (this.options.verbose) { + this.serverless.cli.log(`Excluded ${globFiles.length - files.length} file(s) based on excludeRegex`); + } } if (_.isEmpty(files)) { const error = new this.serverless.classes.Error('Packaging: No files found'); + return BbPromise.reject(error); } @@ -52,14 +55,12 @@ function zip(directory, name) { const artifactFilePath = path.join(this.webpackOutputPath, name); this.serverless.utils.writeFileDir(artifactFilePath); - const cwd = directory; - const source = files; - const destination = path.relative(cwd, artifactFilePath); const zipArgs = { - source, - cwd, - destination + source: files, + cwd: directory, + destination: path.relative(directory, artifactFilePath) }; + return new BbPromise((resolve, reject) => { bestzip(zipArgs) .then(() => { diff --git a/tests/packageModules.test.js b/tests/packageModules.test.js index 529c28de4..c9cf971c9 100644 --- a/tests/packageModules.test.js +++ b/tests/packageModules.test.js @@ -312,7 +312,7 @@ describe('packageModules', () => { it('should reject if no files are found because all files are excluded using regex', () => { module.configuration = new Configuration({ webpack: { - excludeRegex: /.*/ + excludeRegex: '.*' } }); From 50b9510331161e7fac499b54e7969a2219840bad Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Fri, 9 Apr 2021 11:35:47 +0200 Subject: [PATCH 3/3] Fix coverage --- tests/packageModules.test.js | 48 ++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/tests/packageModules.test.js b/tests/packageModules.test.js index c9cf971c9..c64a69718 100644 --- a/tests/packageModules.test.js +++ b/tests/packageModules.test.js @@ -355,6 +355,54 @@ describe('packageModules', () => { module.compileStats = stats; return expect(module.packageModules()).to.be.rejectedWith('Packaging: No files found'); }); + + it('should reject only .md files without verbose log', () => { + module.options.verbose = false; + module.configuration = new Configuration({ + webpack: { + excludeRegex: '.md$' + } + }); + + // Test data + const stats = { + stats: [ + { + compilation: { + compiler: { + outputPath: '/my/Service/Path/.webpack/service' + } + } + } + ] + }; + const files = [ 'README.md', 'src/handler1.js', 'src/handler1.js.map', 'src/handler2.js', 'src/handler2.js.map' ]; + const allFunctions = [ 'func1', 'func2' ]; + const func1 = { + handler: 'src/handler1', + events: [] + }; + const func2 = { + handler: 'src/handler2', + events: [] + }; + // Serverless behavior + getVersionStub.returns('1.18.0'); + getServiceObjectStub.returns({ + name: 'test-service' + }); + getAllFunctionsStub.returns(allFunctions); + getFunctionStub.withArgs('func1').returns(func1); + getFunctionStub.withArgs('func2').returns(func2); + // Mock behavior + globMock.sync.returns(files); + fsMock._streamMock.on.withArgs('open').yields(); + fsMock._streamMock.on.withArgs('close').yields(); + fsMock._statMock.isDirectory.returns(false); + + module.compileStats = stats; + return expect(module.packageModules()).to.be.fulfilled; + }); }); describe('with individual packaging', () => {