Skip to content

Commit dbceb1b

Browse files
authored
Merge pull request #264 from debanjanbasu/master
Exclude node_modules completely if deploying to google cloud functions, as GCF installs package.json.
2 parents 0c650a0 + ac438d4 commit dbceb1b

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

lib/packExternalModules.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,12 @@ module.exports = {
291291
addModulesToPackageJson(prodModules, modulePackage);
292292
this.serverless.utils.writeFileSync(modulePackageJson, JSON.stringify(modulePackage, null, 2));
293293

294-
// Copy modules
294+
// GOOGLE: Copy modules only if not google-cloud-functions
295+
// GCF Auto installs the package json
296+
if (_.get(this.serverless, 'service.provider.name') === 'google') {
297+
return BbPromise.resolve();
298+
}
299+
295300
const startCopy = _.now();
296301
return BbPromise.fromCallback(callback => fse.copy(path.join(compositeModulePath, 'node_modules'), path.join(modulePath, 'node_modules'), callback))
297302
.tap(() => this.options.verbose && this.serverless.cli.log(`Copy modules: ${modulePath} [${_.now() - startCopy} ms]`))

tests/packExternalModules.test.js

+47
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,53 @@ describe('packExternalModules', () => {
212212
]));
213213
});
214214

215+
it('should skip module copy for Google provider', () => {
216+
const expectedCompositePackageJSON = {
217+
name: 'test-service',
218+
version: '1.0.0',
219+
description: 'Packaged externals for test-service',
220+
private: true,
221+
dependencies: {
222+
'@scoped/vendor': '1.0.0',
223+
uuid: '^5.4.1',
224+
bluebird: '^3.4.0'
225+
}
226+
};
227+
const expectedPackageJSON = {
228+
dependencies: {
229+
'@scoped/vendor': '1.0.0',
230+
uuid: '^5.4.1',
231+
bluebird: '^3.4.0'
232+
}
233+
};
234+
235+
_.set(serverless, 'service.provider.name', 'google');
236+
module.webpackOutputPath = 'outputPath';
237+
fsExtraMock.pathExists.yields(null, false);
238+
fsExtraMock.copy.yields();
239+
childProcessMock.exec.onFirstCall().yields(null, '{}', '');
240+
childProcessMock.exec.onSecondCall().yields(null, '', '');
241+
childProcessMock.exec.onThirdCall().yields();
242+
module.compileStats = stats;
243+
return expect(module.packExternalModules()).to.be.fulfilled
244+
.then(() => BbPromise.all([
245+
// The module package JSON and the composite one should have been stored
246+
expect(writeFileSyncStub).to.have.been.calledTwice,
247+
expect(writeFileSyncStub.firstCall.args[1]).to.equal(JSON.stringify(expectedCompositePackageJSON, null, 2)),
248+
expect(writeFileSyncStub.secondCall.args[1]).to.equal(JSON.stringify(expectedPackageJSON, null, 2)),
249+
// The modules should have been copied
250+
expect(fsExtraMock.copy).to.have.not.been.called,
251+
// npm ls and npm prune should have been called
252+
expect(childProcessMock.exec).to.have.been.calledTwice,
253+
expect(childProcessMock.exec.firstCall).to.have.been.calledWith(
254+
'npm ls -prod -json -depth=1'
255+
),
256+
expect(childProcessMock.exec.secondCall).to.have.been.calledWith(
257+
'npm install'
258+
)
259+
]));
260+
});
261+
215262
it('should reject if npm install fails', () => {
216263
module.webpackOutputPath = 'outputPath';
217264
fsExtraMock.pathExists.yields(null, false);

0 commit comments

Comments
 (0)