From a6e58cfb2d54e8fff487c1b8222bd0dfbdb2eb8d Mon Sep 17 00:00:00 2001 From: Moroine Bentefrit Date: Thu, 24 Mar 2022 14:07:11 +0100 Subject: [PATCH] fix: merge conflicts --- __mocks__/bestzip.js | 18 ---- lib/packExternalModules.js | 187 ++++++++++++++++++----------------- tests/e2e/e2e.test.js | 1 - tests/index.test.js | 8 +- tests/packageModules.test.js | 2 - 5 files changed, 100 insertions(+), 116 deletions(-) delete mode 100644 __mocks__/bestzip.js diff --git a/__mocks__/bestzip.js b/__mocks__/bestzip.js deleted file mode 100644 index 47c1d2c24..000000000 --- a/__mocks__/bestzip.js +++ /dev/null @@ -1,18 +0,0 @@ -'use strict'; - -/** - * Mock object for bestzip - */ - -const bestZipMock = jest.fn(); - -const hasNativeZip = jest.fn(); -hasNativeZip.mockReturnValueOnce(false); -hasNativeZip.mockReturnValueOnce(true); -hasNativeZip.mockReturnValue(false); - -module.exports = { - nativeZip: jest.fn().mockResolvedValue(bestZipMock), - nodeZip: jest.fn().mockResolvedValue(bestZipMock), - hasNativeZip -}; diff --git a/lib/packExternalModules.js b/lib/packExternalModules.js index ab040ceb6..9090d4a3b 100644 --- a/lib/packExternalModules.js +++ b/lib/packExternalModules.js @@ -300,26 +300,26 @@ module.exports = { }); } - // (1) Generate dependency composition - const compositeModules = _.uniq( - _.flatMap(stats.stats, compileStats => { - const externalModules = _.concat( - compileStats.externalModules, - _.map(packageForceIncludes, whitelistedPackage => ({ - external: whitelistedPackage - })) - ); - return getProdModules.call( - this, - externalModules, - packagePath, - nodeModulesRelativeDir, - dependencyGraph, - packageForceExcludes - ); - }) - ); - removeExcludedModules.call(this, compositeModules, packageForceExcludes, true); + // (1) Generate dependency composition + const compositeModules = _.uniq( + _.flatMap(stats.stats, compileStats => { + const externalModules = _.concat( + compileStats.externalModules, + _.map(packageForceIncludes, whitelistedPackage => ({ + external: whitelistedPackage + })) + ); + return getProdModules.call( + this, + externalModules, + packagePath, + nodeModulesRelativeDir, + dependencyGraph, + packageForceExcludes + ); + }) + ); + removeExcludedModules.call(this, compositeModules, packageForceExcludes, true); if (_.isEmpty(compositeModules)) { // The compiled code does not reference any external modules at all @@ -331,24 +331,24 @@ module.exports = { return BbPromise.resolve(); } - // (1.a) Install all needed modules - const compositeModulePath = path.join(this.webpackOutputPath, 'dependencies'); - const compositePackageJson = path.join(compositeModulePath, 'package.json'); - - // (1.a.1) Create a package.json - const compositePackage = _.defaults( - { - name: this.serverless.service.service, - version: '1.0.0', - description: `Packaged externals for ${this.serverless.service.service}`, - private: true, - scripts: packageScripts - }, - packageSections - ); - const relPath = path.relative(compositeModulePath, path.dirname(packageJsonPath)); - addModulesToPackageJson(compositeModules, compositePackage, relPath); - this.serverless.utils.writeFileSync(compositePackageJson, JSON.stringify(compositePackage, null, 2)); + // (1.a) Install all needed modules + const compositeModulePath = path.join(this.webpackOutputPath, 'dependencies'); + const compositePackageJson = path.join(compositeModulePath, 'package.json'); + + // (1.a.1) Create a package.json + const compositePackage = _.defaults( + { + name: this.serverless.service.service, + version: '1.0.0', + description: `Packaged externals for ${this.serverless.service.service}`, + private: true, + scripts: packageScripts + }, + packageSections + ); + const relPath = path.relative(compositeModulePath, path.dirname(packageJsonPath)); + addModulesToPackageJson(compositeModules, compositePackage, relPath); + this.serverless.utils.writeFileSync(compositePackageJson, JSON.stringify(compositePackage, null, 2)); // (1.a.2) Copy package-lock.json if it exists, to prevent unwanted upgrades const packageLockPath = path.join(path.dirname(packageJsonPath), packager.lockfileName); @@ -379,14 +379,6 @@ module.exports = { } else { this.serverless.cli.log(`Warning: Could not read lock file: ${err.message}`); } - - this.serverless.utils.writeFileSync( - path.join(compositeModulePath, packager.lockfileName), - packageLockFile - ); - hasPackageLock = true; - } catch (err) { - this.serverless.cli.log(`Warning: Could not read lock file: ${err.message}`); } } return BbPromise.resolve(); @@ -413,52 +405,65 @@ module.exports = { .mapSeries(compileStats => { const modulePath = compileStats.outputPath; - // Create package.json - const modulePackageJson = path.join(modulePath, 'package.json'); - const modulePackage = _.defaults( - { - name: this.serverless.service.service, - version: '1.0.0', - description: `Packaged externals for ${this.serverless.service.service}`, - private: true, - scripts: packageScripts, - dependencies: {} - }, - packageSections - ); - const prodModules = getProdModules.call( - this, - _.concat( - compileStats.externalModules, - _.map(packageForceIncludes, whitelistedPackage => ({ - external: whitelistedPackage - })) - ), - packagePath, - nodeModulesRelativeDir, - dependencyGraph, - packageForceExcludes - ); - removeExcludedModules.call(this, prodModules, packageForceExcludes); - const relPath = path.relative(modulePath, path.dirname(packageJsonPath)); - addModulesToPackageJson(prodModules, modulePackage, relPath); - this.serverless.utils.writeFileSync(modulePackageJson, JSON.stringify(modulePackage, null, 2)); - - // GOOGLE: Copy modules only if not google-cloud-functions - // GCF Auto installs the package json - if (_.get(this.serverless, 'service.provider.name') === 'google') { - return BbPromise.resolve(); - } + // Create package.json + const modulePackageJson = path.join(modulePath, 'package.json'); + const modulePackage = _.defaults( + { + name: this.serverless.service.service, + version: '1.0.0', + description: `Packaged externals for ${this.serverless.service.service}`, + private: true, + scripts: packageScripts, + dependencies: {} + }, + packageSections + ); + const prodModules = getProdModules.call( + this, + _.concat( + compileStats.externalModules, + _.map(packageForceIncludes, whitelistedPackage => ({ + external: whitelistedPackage + })) + ), + packagePath, + nodeModulesRelativeDir, + dependencyGraph, + packageForceExcludes + ); + removeExcludedModules.call(this, prodModules, packageForceExcludes); + const relPath = path.relative(modulePath, path.dirname(packageJsonPath)); + addModulesToPackageJson(prodModules, modulePackage, relPath); + this.serverless.utils.writeFileSync(modulePackageJson, JSON.stringify(modulePackage, null, 2)); + + // GOOGLE: Copy modules only if not google-cloud-functions + // GCF Auto installs the package json + if (_.get(this.serverless, 'service.provider.name') === 'google') { + return BbPromise.resolve(); + } - const startCopy = _.now(); - return BbPromise.try(() => { - // Only copy dependency modules if demanded by packager - if (packager.mustCopyModules) { - return BbPromise.fromCallback(callback => - fse.copy( - path.join(compositeModulePath, 'node_modules'), - path.join(modulePath, 'node_modules'), - callback + const startCopy = _.now(); + return BbPromise.try(() => { + // Only copy dependency modules if demanded by packager + if (packager.mustCopyModules) { + return BbPromise.fromCallback(callback => + fse.copy( + path.join(compositeModulePath, 'node_modules'), + path.join(modulePath, 'node_modules'), + callback + ) + ); + } + return BbPromise.resolve(); + }) + .then(() => + hasPackageLock + ? BbPromise.fromCallback(callback => + fse.copy( + path.join(compositeModulePath, packager.lockfileName), + path.join(modulePath, packager.lockfileName), + callback + ) ) : BbPromise.resolve() ) diff --git a/tests/e2e/e2e.test.js b/tests/e2e/e2e.test.js index 8781af5f0..ab3c640b2 100644 --- a/tests/e2e/e2e.test.js +++ b/tests/e2e/e2e.test.js @@ -8,7 +8,6 @@ const pkg = require('../../package.json'); const _ = require('lodash'); // unmock global modules -jest.unmock('bestzip'); jest.unmock('fs-extra'); jest.unmock('fs'); jest.unmock('glob'); diff --git a/tests/index.test.js b/tests/index.test.js index e6e198a10..b8d261c38 100644 --- a/tests/index.test.js +++ b/tests/index.test.js @@ -9,6 +9,7 @@ const Serverless = require('serverless'); const ServerlessWebpack = require('../index'); jest.mock('webpack'); +// jest.mock('ts-node/register'); describe('ServerlessWebpack', () => { let serverless; @@ -30,10 +31,6 @@ describe('ServerlessWebpack', () => { }); describe('with a TS webpack configuration', () => { - afterEach(() => { - jest.unmock('ts-node/register'); - }); - it('should support old config and register ts-node', () => { jest.setMock('ts-node/register', null, { virtual: true }); _.set(serverless, 'service.custom.webpack', 'webpack.config.ts'); @@ -55,6 +52,9 @@ describe('ServerlessWebpack', () => { it('should throw an error if config use TS but ts-node was not added as dependency', () => { _.set(serverless, 'service.custom.webpack.webpackConfig', 'webpack.config.ts'); + jest.mock('ts-node/register', () => { + throw new Error('ts-node not found'); + }); const badDeps = function () { new ServerlessWebpack(serverless, {}); diff --git a/tests/packageModules.test.js b/tests/packageModules.test.js index c74712613..ef10c1c4f 100644 --- a/tests/packageModules.test.js +++ b/tests/packageModules.test.js @@ -7,12 +7,10 @@ const Serverless = require('serverless'); const Configuration = require('../lib/Configuration'); const fsMock = require('fs'); const globMock = require('glob'); -const bestzipMock = require('bestzip'); const baseModule = require('../lib/packageModules'); jest.mock('fs'); jest.mock('glob'); -jest.mock('bestzip'); describe('packageModules', () => { let serverless;