Skip to content

Commit

Permalink
fix(package): better google function handle
Browse files Browse the repository at this point in the history
  • Loading branch information
olup committed Mar 3, 2021
1 parent 26bcff5 commit da11e33
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
12 changes: 6 additions & 6 deletions src/pack-externals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,12 @@ export async function packExternalModules(this: EsbuildPlugin) {
}
}

// GOOGLE: Copy modules only if not google-cloud-functions
// GCF Auto installs the package json
if (get(['service', 'provider', 'name'], this.serverless) === 'google') {
return;
}

const start = Date.now();
this.serverless.cli.log('Packing external modules: ' + compositeModules.join(', '));
await packager.install(compositeModulePath);
Expand All @@ -248,10 +254,4 @@ export async function packExternalModules(this: EsbuildPlugin) {
await packager.prune(compositeModulePath);
this.options.verbose &&
this.serverless.cli.log(`Prune: ${compositeModulePath} [${Date.now() - startPrune} ms]`);

// GOOGLE: Copy modules only if not google-cloud-functions
// GCF Auto installs the package json
if (get(['service', 'provider', 'name'], this.serverless) === 'google') {
await fse.remove(path.join(compositeModulePath, 'node_modules'));
}
}
11 changes: 8 additions & 3 deletions src/pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ function setFunctionArtifactPath(this: EsbuildPlugin, func, artifactPath) {
const excludedFilesDefault = ['package-lock.json', 'yarn.lock', 'package.json'];

export async function pack(this: EsbuildPlugin) {
// GOOGLE Provider requires a package.json and NO node_modules
const isGoogleProvider = this.serverless?.service?.provider?.name === 'google';
const excludedFiles = isGoogleProvider ? [] : excludedFilesDefault;

// get a list of all path in build
const files: { localPath: string; rootPath: string }[] = glob
.sync('**', {
Expand All @@ -35,7 +39,7 @@ export async function pack(this: EsbuildPlugin) {
silent: true,
follow: true,
})
.filter(p => !excludedFilesDefault.includes(p))
.filter(p => !excludedFiles.includes(p))
.map(localPath => ({ localPath, rootPath: path.join(this.buildDirPath, localPath) }));

if (isEmpty(files)) {
Expand Down Expand Up @@ -68,7 +72,7 @@ export async function pack(this: EsbuildPlugin) {
const buildResults = this.buildResults;
const bundlePathList = buildResults.map(b => path.dirname(b.bundlePath));

// get a list of external dependencies already listed in package.json
// get a list of externals
const externals = without<string>(this.buildOptions.exclude, this.buildOptions.external);
const hasExternals = !!externals?.length;

Expand Down Expand Up @@ -103,7 +107,8 @@ export async function pack(this: EsbuildPlugin) {

// exclude non whitelisted dependencies
if (localPath.startsWith('node_modules')) {
if (!hasExternals) return false;
// if no externals is set or if the provider is google, we do not need any files from node_modules
if (!hasExternals || isGoogleProvider) return false;
if (
// this is needed for dependencies that maps to a path (like scopped ones)
!depWhiteList.find(dep => doSharePath(localPath, 'node_modules/' + dep))
Expand Down

0 comments on commit da11e33

Please sign in to comment.