From da11e33f5cd869e6adbf179d913dabb1d980a29c Mon Sep 17 00:00:00 2001 From: Olup Date: Wed, 3 Mar 2021 09:36:47 +0100 Subject: [PATCH] fix(package): better google function handle --- src/pack-externals.ts | 12 ++++++------ src/pack.ts | 11 ++++++++--- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/pack-externals.ts b/src/pack-externals.ts index 0be546d0..ac99f2ba 100644 --- a/src/pack-externals.ts +++ b/src/pack-externals.ts @@ -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); @@ -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')); - } } diff --git a/src/pack.ts b/src/pack.ts index e5385ef3..0541da46 100644 --- a/src/pack.ts +++ b/src/pack.ts @@ -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('**', { @@ -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)) { @@ -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(this.buildOptions.exclude, this.buildOptions.external); const hasExternals = !!externals?.length; @@ -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))