Skip to content

Commit f50afe7

Browse files
RubenSandwichheatlikeheatwavezchsh
authored
Upload source maps during prod build (#2523)
* Upload source maps during prod build * Remove commented out code * conditional logic for service string Co-authored-by: Zach Shilton <[email protected]> * Make path prefex generic * Add additional logic checks to prevent upload script from running in dev or when VERCEL_ENV is undefined --------- Co-authored-by: Heat Hamilton <[email protected]> Co-authored-by: Zach Shilton <[email protected]>
1 parent 2e1a961 commit f50afe7

File tree

5 files changed

+68
-2
lines changed

5 files changed

+68
-2
lines changed

next.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ module.exports = withHashicorp({
5454
],
5555
webpack(config) {
5656
config.plugins.push(HashiConfigPlugin())
57+
58+
if (process.env.VERCEL_ENV && process.env.VERCEL_ENV !== 'development') {
59+
config.devtool = 'source-map'
60+
}
61+
5762
return config
5863
},
5964
async headers() {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"prestart": "hc-tools ./scripts/generate-tutorial-variant-map.ts && hc-tools ./scripts/extract-hvd-content.ts",
1515
"prettier:check": "prettier --check .",
1616
"prettier:write": "prettier --write .",
17-
"postbuild": "hc-tools ./scripts/capture-build-metrics.ts dev-portal && next-sitemap",
17+
"postbuild": "hc-tools ./scripts/capture-build-metrics.ts dev-portal && next-sitemap && hc-tools ./scripts/upload-source-maps.ts",
1818
"rewrite-docs-content-links": "hc-tools ./scripts/docs-content-link-rewrites/rewrite-links.ts",
1919
"sitemap": "next-sitemap",
2020
"start:local-preview": "./scripts/content-repo-preview/start.sh",

scripts/upload-source-maps.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Copyright (c) HashiCorp, Inc.
3+
* SPDX-License-Identifier: MPL-2.0
4+
*/
5+
import { execSync } from 'child_process'
6+
7+
/**
8+
* Uploads source maps to Datadog and then deletes source maps to prevent bundle size increase and leaking of source code.
9+
*/
10+
const main = () => {
11+
if (
12+
typeof process.env.VERCEL_ENV === 'undefined' ||
13+
process.env.VERCEL_ENV === 'development'
14+
) {
15+
return
16+
}
17+
18+
const LATEST_SHA = process.env.VERCEL_GIT_COMMIT_SHA
19+
const PATH_PREFIX =
20+
process.env.VERCEL_ENV === 'production'
21+
? 'https://developer.hashicorp.com/_next/static/'
22+
: `https://${process.env.VERCEL_BRANCH_URL}/_next/static/`
23+
const SERVICE =
24+
process.env.VERCEL_ENV === 'production'
25+
? 'developer.hashicorp.com'
26+
: 'non-prod.developer.hashicorp.com'
27+
28+
const DATADOG_API_KEY = process.env.DD_API_KEY
29+
30+
try {
31+
execSync(
32+
`DATADOG_API_KEY=${DATADOG_API_KEY} npx @datadog/datadog-ci sourcemaps upload .next/static/ --service=${SERVICE} --release-version=${LATEST_SHA} --minified-path-prefix=${PATH_PREFIX} --disable-git`,
33+
{ stdio: 'inherit' }
34+
)
35+
36+
console.log('Source maps uploaded successfully')
37+
} catch (error) {
38+
console.error(error)
39+
40+
console.log('Failed to upload sourcemaps')
41+
}
42+
43+
// delete sourcemaps
44+
try {
45+
execSync(`rm -f .next/static/**/*.js.map`)
46+
} catch (error) {
47+
console.error(error)
48+
49+
console.log('Failed to delete sourcemaps')
50+
}
51+
}
52+
53+
main()

src/views/homepage/index.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
// Third-party imports
7-
import { ReactElement } from 'react'
7+
import { ReactElement, useEffect } from 'react'
88

99
// Global imports
1010
import BaseLayout from 'layouts/base-layout'
@@ -20,6 +20,13 @@ import {
2020
import s from './homepage.module.css'
2121

2222
function HomePageView(): ReactElement {
23+
useEffect(() => {
24+
const timer = setTimeout(() => {
25+
throw new Error('This is a new test error thrown after 3 seconds')
26+
}, 3000)
27+
28+
return () => clearTimeout(timer) // Cleanup the timer on component unmount
29+
}, [])
2330
return (
2431
<BaseLayout mobileMenuSlot={<MobileMenuLevelsGeneric />}>
2532
<div className={s.root}>

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"module": "esnext",
77
"lib": ["dom", "dom.iterable", "esnext"],
88
"jsx": "preserve",
9+
"sourceMap": true,
910
"allowJs": true,
1011
"esModuleInterop": true,
1112
"forceConsistentCasingInFileNames": true,

0 commit comments

Comments
 (0)