From aa931091561316674c3367aad34fa8a1ad10ab53 Mon Sep 17 00:00:00 2001 From: Houssein Djirdeh Date: Thu, 31 Mar 2022 15:44:15 -0400 Subject: [PATCH 1/2] [Script] Use `__non_webpack_require__` to fetch partytown server-side (#35793) A few errors about Next.js not correctly handling the missing partytown dependency in `_document.js` have been flagged. Instead of `/* webpackIgnore: true */`, this PR switches to use [`__non_webpack_require__`](https://webpack.js.org/api/module-variables/#__non_webpack_require__-webpack-specific) to ensure webpack doesn't process fetching the dep server-side. ----- Related issues: - #35645 - https://github.com/vercel/next.js/issues/35630#issuecomment-1084365365 --- packages/next/build/webpack-config.ts | 1 - packages/next/pages/_document.tsx | 11 +++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/next/build/webpack-config.ts b/packages/next/build/webpack-config.ts index 33b3bdf0a34e..2e058db57250 100644 --- a/packages/next/build/webpack-config.ts +++ b/packages/next/build/webpack-config.ts @@ -1573,7 +1573,6 @@ export default async function getBaseWebpackConfig( webpack5Config.module!.parser = { javascript: { url: 'relative', - commonjsMagicComments: true, }, } webpack5Config.module!.generator = { diff --git a/packages/next/pages/_document.tsx b/packages/next/pages/_document.tsx index f7513c1ab252..b989817a6a7e 100644 --- a/packages/next/pages/_document.tsx +++ b/packages/next/pages/_document.tsx @@ -85,7 +85,8 @@ function getPreNextWorkerScripts(context: HtmlProps, props: OriginProps) { try { let { partytownSnippet, - } = require(/* webpackIgnore: true */ '@builder.io/partytown/integration'!) + // @ts-ignore: Prevent webpack from processing this require + } = __non_webpack_require__('@builder.io/partytown/integration'!) const children = Array.isArray(props.children) ? props.children @@ -135,9 +136,11 @@ function getPreNextWorkerScripts(context: HtmlProps, props: OriginProps) { ) } catch (err) { - console.warn( - `Warning: Partytown could not be instantiated in your application due to an error. ${err}` - ) + if (isError(err) && err.code !== 'MODULE_NOT_FOUND') { + console.warn( + `Warning: Partytown could not be instantiated in your application due to an error. ${err.message}` + ) + } return null } } From 69a166b263d6ebc34055c47953891b0000b0a9ef Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Thu, 31 Mar 2022 13:23:23 -0700 Subject: [PATCH 2/2] ci(trace): adjust agent env variables (#35790) * ci(trace): adjust agent env variables * refactor(trace): exit early when there's no key * ci(trace): try to skip agent if api key is not available --- .github/workflows/build_test_deploy.yml | 19 +++++++++++++++---- scripts/trace-dd.mjs | 9 ++++++++- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/.github/workflows/build_test_deploy.yml b/.github/workflows/build_test_deploy.yml index 1a889988566c..834c46f59d17 100644 --- a/.github/workflows/build_test_deploy.yml +++ b/.github/workflows/build_test_deploy.yml @@ -1199,10 +1199,21 @@ jobs: name: wasm-dev-binary path: packages/next-swc/crates/wasm/pkg-nodejs + check-trace-secrests: + runs-on: ubuntu-latest + outputs: + trace-api-key: ${{ steps.trace-api-key.outputs.defined }} + steps: + - id: trace-api-key + env: + TRACE_API_KEY: ${{ secrets.DATA_DOG_API_KEY }} + if: "${{ env.TRACE_API_KEY != '' }}" + run: echo "::set-output name=defined::true" build-performance-metrics: name: Performance Metrics for Release Build runs-on: ubuntu-latest - needs: [build, build-native-test] + needs: [build, build-native-test, check-trace-secrests] + if: needs.check-trace-secrests.trace-api-key == 'true' steps: - name: Setup node uses: actions/setup-node@v2 @@ -1236,16 +1247,16 @@ jobs: run: | yarn --cwd bench/nested-deps install node bench/nested-deps/bench.mjs build - - uses: datadog/agent-github-action@v1 if: ${{needs.build.outputs.docsChange != 'docs only change'}} with: api_key: ${{ secrets.DATA_DOG_API_KEY }} - - name: Sending metrics data to Datadog if: ${{needs.build.outputs.docsChange != 'docs only change'}} run: | node scripts/trace-dd.mjs bench/nested-deps/.next/trace build ${GIT_SHORT_SHA} ./bench/nested-deps/next.config.js env: + DD_TRACE_PARTIAL_FLUSH_MIN_SPANS: 10 DD_ENV: canary - DD_SERVICE: http://localhost:8126 + DD_SERVICE: nextjs-dev-build + DD_TRACE_DEBUG: true diff --git a/scripts/trace-dd.mjs b/scripts/trace-dd.mjs index 9f09ce6018b6..3b8336b70a36 100644 --- a/scripts/trace-dd.mjs +++ b/scripts/trace-dd.mjs @@ -132,7 +132,14 @@ const collectTraces = async (filePath, metadata) => { * Naively validate, collect necessary args. */ const validateArgs = async () => { - const { DD_ENV, DD_SERVICE } = process.env + const { DD_ENV, DD_SERVICE, DATA_DOG_API_KEY } = process.env + + if (!DATA_DOG_API_KEY) { + console.log( + "Skipping trace collection, api key is not available. Ensure 'DATA_DOG_API_KEY' env variable is set." + ) + return + } if (!DD_ENV || !DD_SERVICE) { throw new Error(