Skip to content

Commit

Permalink
Add additional logic checks to prevent upload script from running in …
Browse files Browse the repository at this point in the history
…dev or when VERCEL_ENV is undefined
  • Loading branch information
heatlikeheatwave committed Jul 26, 2024
1 parent 65a110b commit 9590a40
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 33 deletions.
4 changes: 2 additions & 2 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 15 additions & 31 deletions scripts/upload-source-maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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')
}
}

Expand Down

0 comments on commit 9590a40

Please sign in to comment.