Skip to content

Commit

Permalink
fix: merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
moroine committed Mar 24, 2022
1 parent 8dec6d1 commit a6e58cf
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 116 deletions.
18 changes: 0 additions & 18 deletions __mocks__/bestzip.js

This file was deleted.

187 changes: 96 additions & 91 deletions lib/packExternalModules.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand 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);
Expand Down Expand Up @@ -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();
Expand All @@ -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()
)
Expand Down
1 change: 0 additions & 1 deletion tests/e2e/e2e.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
8 changes: 4 additions & 4 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const Serverless = require('serverless');
const ServerlessWebpack = require('../index');

jest.mock('webpack');
// jest.mock('ts-node/register');

describe('ServerlessWebpack', () => {
let serverless;
Expand All @@ -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');
Expand All @@ -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, {});
Expand Down
2 changes: 0 additions & 2 deletions tests/packageModules.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit a6e58cf

Please sign in to comment.