Skip to content

Commit

Permalink
[Script] Use __non_webpack_require__ to fetch partytown server-side (
Browse files Browse the repository at this point in the history
…#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 
- #35630 (comment)
  • Loading branch information
housseindjirdeh committed Mar 31, 2022
1 parent 4464327 commit aa93109
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
1 change: 0 additions & 1 deletion packages/next/build/webpack-config.ts
Expand Up @@ -1573,7 +1573,6 @@ export default async function getBaseWebpackConfig(
webpack5Config.module!.parser = {
javascript: {
url: 'relative',
commonjsMagicComments: true,
},
}
webpack5Config.module!.generator = {
Expand Down
11 changes: 7 additions & 4 deletions packages/next/pages/_document.tsx
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
Expand Down

0 comments on commit aa93109

Please sign in to comment.