From 9590a409fb8125499cda8be81456db4877fa031c Mon Sep 17 00:00:00 2001 From: Heat Hamilton Date: Fri, 26 Jul 2024 17:07:02 -0400 Subject: [PATCH] Add additional logic checks to prevent upload script from running in dev or when VERCEL_ENV is undefined --- next.config.js | 4 +-- scripts/upload-source-maps.ts | 46 ++++++++++++----------------------- 2 files changed, 17 insertions(+), 33 deletions(-) diff --git a/next.config.js b/next.config.js index 33f57da3af..83a7362487 100644 --- a/next.config.js +++ b/next.config.js @@ -55,8 +55,8 @@ module.exports = withHashicorp({ webpack(config) { config.plugins.push(HashiConfigPlugin()) - if (process.env.VERCEL_ENV !== 'development') { - config.devtool = 'hidden-source-map' + if (process.env.VERCEL_ENV && process.env.VERCEL_ENV !== 'development') { + config.devtool = 'source-map' } return config diff --git a/scripts/upload-source-maps.ts b/scripts/upload-source-maps.ts index 9fb5156e23..89e211a193 100644 --- a/scripts/upload-source-maps.ts +++ b/scripts/upload-source-maps.ts @@ -3,16 +3,23 @@ * SPDX-License-Identifier: MPL-2.0 */ import { execSync } from 'child_process' -import { readdirSync, unlinkSync } from 'fs' -import { join } from 'path' +/** + * Uploads source maps to Datadog and then deletes source maps to prevent bundle size increase and leaking of source code. + */ const main = () => { - if (process.env.VERCEL_ENV === 'development') { + if ( + typeof process.env.VERCEL_ENV === 'undefined' || + process.env.VERCEL_ENV === 'development' + ) { return } const LATEST_SHA = process.env.VERCEL_GIT_COMMIT_SHA - const PATH_PREFIX = `https://${process.env.VERCEL_BRANCH_URL}/_next/static/` + const PATH_PREFIX = + process.env.VERCEL_ENV === 'production' + ? 'https://developer.hashicorp.com/_next/static/' + : `https://${process.env.VERCEL_BRANCH_URL}/_next/static/` const SERVICE = process.env.VERCEL_ENV === 'production' ? 'developer.hashicorp.com' @@ -30,39 +37,16 @@ const main = () => { } catch (error) { console.error(error) - console.log('Failed to upload source maps') - } - - // delete sourcemaps from chunks dir - try { - // Read the directory and delete all *.js.map files - const dirPath = `.next/static/chunks` - const files = readdirSync(dirPath) - files.forEach((file) => { - if (file.endsWith('.js.map')) { - unlinkSync(join(dirPath, file)) - } - }) - } catch (error) { - console.error(error) - - console.log('Failed to delete source maps from chunks dir') + console.log('Failed to upload sourcemaps') } - // delete sourcemaps from pages dir + // delete sourcemaps try { - // Read the directory and delete all *.js.map files - const dirPath = `.next/static/chunks/pages` - const files = readdirSync(dirPath) - files.forEach((file) => { - if (file.endsWith('.js.map')) { - unlinkSync(join(dirPath, file)) - } - }) + execSync(`rm -f .next/static/**/*.js.map`) } catch (error) { console.error(error) - console.log('Failed to delete source maps from pages dir') + console.log('Failed to delete sourcemaps') } }