-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(nextjs): Use compiler hook for uploading turbopack sourcemaps #17352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
5de3eb7
use runAfterProductionCompile for sourcemaps
chargome 8a47b41
Merge branch 'develop' into cg-use-compiler-hook-for-sourcemaps
chargome 0dca599
dedupe
chargome 9ca2600
bump bundler plugin and add update sourcemap handling
chargome 244a93e
Merge branch 'develop' into cg-use-compiler-hook-for-sourcemaps
chargome 17a409f
Merge branch 'develop' into cg-use-compiler-hook-for-sourcemaps
chargome 4bf8eee
Merge branch 'develop' into cg-use-compiler-hook-for-sourcemaps
chargome 6a1332e
disable hook for webpack and move flag to experimental
chargome 6112eb1
automatic source map generation for turbopack
chargome 8d488eb
add tests for turbopack source map generation
chargome 5d299c0
more tests
chargome ce54409
format
chargome 2522da0
dedupe again
chargome a80564c
bump plugin
chargome 729d336
Update packages/nextjs/src/config/handleRunAfterProductionCompile.ts
chargome 1c73c29
log only after webpack check
chargome 2ee3c57
Update packages/nextjs/src/config/types.ts
chargome e418dd2
bump webpack plugin version
chargome a6d8e5f
update tests
chargome c766201
dedupe
chargome 9753610
update sourcemap deletion logic
chargome 7fe941b
Merge branch 'develop' into cg-use-compiler-hook-for-sourcemaps
chargome File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import type { Options as SentryBuildPluginOptions } from '@sentry/bundler-plugin-core'; | ||
import * as path from 'path'; | ||
import type { SentryBuildOptions } from './types'; | ||
|
||
/** | ||
* Get Sentry Build Plugin options for the runAfterProductionCompile hook. | ||
*/ | ||
export function getBuildPluginOptions({ | ||
sentryBuildOptions, | ||
releaseName, | ||
distDirAbsPath, | ||
}: { | ||
sentryBuildOptions: SentryBuildOptions; | ||
releaseName: string | undefined; | ||
distDirAbsPath: string; | ||
}): SentryBuildPluginOptions { | ||
const sourcemapUploadAssets: string[] = []; | ||
const sourcemapUploadIgnore: string[] = []; | ||
|
||
const filesToDeleteAfterUpload: string[] = []; | ||
|
||
// We need to convert paths to posix because Glob patterns use `\` to escape | ||
// glob characters. This clashes with Windows path separators. | ||
// See: https://www.npmjs.com/package/glob | ||
const normalizedDistDirAbsPath = distDirAbsPath.replace(/\\/g, '/'); | ||
|
||
sourcemapUploadAssets.push( | ||
path.posix.join(normalizedDistDirAbsPath, '**'), // Next.js build output | ||
); | ||
if (sentryBuildOptions.sourcemaps?.deleteSourcemapsAfterUpload) { | ||
filesToDeleteAfterUpload.push( | ||
path.posix.join(normalizedDistDirAbsPath, '**', '*.js.map'), | ||
path.posix.join(normalizedDistDirAbsPath, '**', '*.mjs.map'), | ||
path.posix.join(normalizedDistDirAbsPath, '**', '*.cjs.map'), | ||
); | ||
} | ||
|
||
return { | ||
authToken: sentryBuildOptions.authToken, | ||
headers: sentryBuildOptions.headers, | ||
org: sentryBuildOptions.org, | ||
project: sentryBuildOptions.project, | ||
telemetry: sentryBuildOptions.telemetry, | ||
debug: sentryBuildOptions.debug, | ||
errorHandler: sentryBuildOptions.errorHandler, | ||
reactComponentAnnotation: { | ||
...sentryBuildOptions.reactComponentAnnotation, | ||
...sentryBuildOptions.unstable_sentryWebpackPluginOptions?.reactComponentAnnotation, | ||
}, | ||
silent: sentryBuildOptions.silent, | ||
url: sentryBuildOptions.sentryUrl, | ||
sourcemaps: { | ||
disable: sentryBuildOptions.sourcemaps?.disable, | ||
rewriteSources(source) { | ||
if (source.startsWith('webpack://_N_E/')) { | ||
return source.replace('webpack://_N_E/', ''); | ||
} else if (source.startsWith('webpack://')) { | ||
return source.replace('webpack://', ''); | ||
} else { | ||
return source; | ||
} | ||
}, | ||
assets: sentryBuildOptions.sourcemaps?.assets ?? sourcemapUploadAssets, | ||
ignore: sentryBuildOptions.sourcemaps?.ignore ?? sourcemapUploadIgnore, | ||
filesToDeleteAfterUpload, | ||
...sentryBuildOptions.unstable_sentryWebpackPluginOptions?.sourcemaps, | ||
}, | ||
release: | ||
releaseName !== undefined | ||
? { | ||
inject: false, // The webpack plugin's release injection breaks the `app` directory - we inject the release manually with the value injection loader instead. | ||
name: releaseName, | ||
create: sentryBuildOptions.release?.create, | ||
finalize: sentryBuildOptions.release?.finalize, | ||
dist: sentryBuildOptions.release?.dist, | ||
vcsRemote: sentryBuildOptions.release?.vcsRemote, | ||
setCommits: sentryBuildOptions.release?.setCommits, | ||
deploy: sentryBuildOptions.release?.deploy, | ||
...sentryBuildOptions.unstable_sentryWebpackPluginOptions?.release, | ||
} | ||
: { | ||
inject: false, | ||
create: false, | ||
finalize: false, | ||
}, | ||
bundleSizeOptimizations: { | ||
...sentryBuildOptions.bundleSizeOptimizations, | ||
}, | ||
_metaOptions: { | ||
loggerPrefixOverride: '[@sentry/nextjs]', | ||
telemetry: { | ||
metaFramework: 'nextjs', | ||
}, | ||
}, | ||
...sentryBuildOptions.unstable_sentryWebpackPluginOptions, | ||
}; | ||
} |
59 changes: 59 additions & 0 deletions
59
packages/nextjs/src/config/handleRunAfterProductionCompile.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import type { createSentryBuildPluginManager as createSentryBuildPluginManagerType } from '@sentry/bundler-plugin-core'; | ||
import { loadModule } from '@sentry/core'; | ||
import { getBuildPluginOptions } from './getBuildPluginOptions'; | ||
import type { SentryBuildOptions } from './types'; | ||
|
||
/** | ||
* This function is called by Next.js after the production build is complete. | ||
* It is used to upload sourcemaps to Sentry. | ||
*/ | ||
export async function handleRunAfterProductionCompile( | ||
{ releaseName, distDir, buildTool }: { releaseName?: string; distDir: string; buildTool: 'webpack' | 'turbopack' }, | ||
sentryBuildOptions: SentryBuildOptions, | ||
): Promise<void> { | ||
// We don't want to do anything for webpack at this point because the plugin already handles this | ||
// TODO: Actually implement this for webpack as well | ||
if (buildTool === 'webpack') { | ||
return; | ||
} | ||
|
||
if (sentryBuildOptions.debug) { | ||
// eslint-disable-next-line no-console | ||
console.debug('[@sentry/nextjs] Running runAfterProductionCompile logic.'); | ||
} | ||
|
||
const { createSentryBuildPluginManager } = | ||
loadModule<{ createSentryBuildPluginManager: typeof createSentryBuildPluginManagerType }>( | ||
'@sentry/bundler-plugin-core', | ||
module, | ||
) ?? {}; | ||
|
||
if (!createSentryBuildPluginManager) { | ||
// eslint-disable-next-line no-console | ||
console.warn( | ||
'[@sentry/nextjs] Could not load build manager package. Will not run runAfterProductionCompile logic.', | ||
); | ||
return; | ||
} | ||
|
||
const sentryBuildPluginManager = createSentryBuildPluginManager( | ||
getBuildPluginOptions({ | ||
sentryBuildOptions, | ||
releaseName, | ||
distDirAbsPath: distDir, | ||
}), | ||
{ | ||
buildTool, | ||
loggerPrefix: '[@sentry/nextjs]', | ||
}, | ||
); | ||
|
||
await sentryBuildPluginManager.telemetry.emitBundlerPluginExecutionSignal(); | ||
await sentryBuildPluginManager.createRelease(); | ||
await sentryBuildPluginManager.injectDebugIds([distDir]); | ||
await sentryBuildPluginManager.uploadSourcemaps([distDir], { | ||
// We don't want to prepare the artifacts because we injected debug ids manually before | ||
prepareArtifacts: false, | ||
}); | ||
await sentryBuildPluginManager.deleteArtifacts(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.