Skip to content

Commit 587e3a5

Browse files
committed
fix: simplify import placeholder replacers
1 parent 3875447 commit 587e3a5

File tree

3 files changed

+14
-30
lines changed

3 files changed

+14
-30
lines changed

packages/nextjs/rollup.npm.config.mjs

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -74,44 +74,28 @@ export default [
7474
],
7575
plugins: [
7676
{
77-
name: 'sentry-fix-rolldown-generated-identifiers',
77+
name: 'sentry-fix-missing-serverComponentModule-import',
7878
renderChunk(code, chunk) {
79-
// Rolldown generates import identifiers like `__SENTRY_WRAPPING_TARGET_FILE___default` for default imports
80-
// from our placeholder modules. When the wrapping loader later replaces `__SENTRY_WRAPPING_TARGET_FILE__`
81-
// with `__SENTRY_WRAPPING_TARGET_FILE__.cjs`, this creates invalid syntax like
82-
// `__SENTRY_WRAPPING_TARGET_FILE__.cjs_default`, where `.cjs` breaks the identifier.
83-
// We fix this by replacing the problematic identifier pattern with a safe one that uses a different
84-
// separator that won't be confused with property access when the placeholder is replaced.
85-
let fixedCode = code
86-
.replace(
87-
/__SENTRY_WRAPPING_TARGET_FILE___default/g,
88-
'__SENTRY_WRAPPING_TARGET_FILE_PLACEHOLDER_DEFAULT__',
89-
)
90-
.replace(/__SENTRY_CONFIG_IMPORT_PATH___default/g, '__SENTRY_CONFIG_IMPORT_PATH_PLACEHOLDER_DEFAULT__')
91-
.replace(
92-
/__SENTRY_NEXTJS_REQUEST_ASYNC_STORAGE_SHIM___default/g,
93-
'__SENTRY_NEXTJS_REQUEST_ASYNC_STORAGE_SHIM_PLACEHOLDER_DEFAULT__',
94-
);
95-
9679
// Rolldown has a bug where it removes namespace imports for external modules even when they're still
9780
// referenced in the code (specifically when there's a `declare const` with the same name in the source).
9881
// We need to add back the missing import for serverComponentModule in the serverComponentWrapperTemplate.
9982
if (
10083
chunk.facadeModuleId?.includes('serverComponentWrapperTemplate') &&
101-
fixedCode.includes('serverComponentModule') &&
102-
!fixedCode.includes('import * as serverComponentModule')
84+
code.includes('serverComponentModule') &&
85+
!code.includes('import * as serverComponentModule')
10386
) {
10487
// Find the position after the last import statement to insert our missing import
105-
const lastImportMatch = fixedCode.match(/^import[^;]*;/gm);
88+
const lastImportMatch = code.match(/^import[^;]*;/gm);
10689
if (lastImportMatch) {
10790
const lastImport = lastImportMatch[lastImportMatch.length - 1];
108-
const lastImportEnd = fixedCode.indexOf(lastImport) + lastImport.length;
109-
fixedCode = `${fixedCode.slice(0, lastImportEnd)}
110-
import * as serverComponentModule from "__SENTRY_WRAPPING_TARGET_FILE__";${fixedCode.slice(lastImportEnd)}`;
91+
const lastImportEnd = code.indexOf(lastImport) + lastImport.length;
92+
return {
93+
code: `${code.slice(0, lastImportEnd)}
94+
import * as serverComponentModule from "__SENTRY_WRAPPING_TARGET_FILE__";${code.slice(lastImportEnd)}`,
95+
};
11196
}
11297
}
113-
114-
return { code: fixedCode };
98+
return null;
11599
},
116100
},
117101
],

packages/nextjs/src/config/templates/routeHandlerWrapperTemplate.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ function wrapHandler<T>(handler: T, method: 'GET' | 'POST' | 'PUT' | 'PATCH' | '
6060
// @ts-expect-error See above
6161
export * from '__SENTRY_WRAPPING_TARGET_FILE__';
6262

63-
// @ts-expect-error This is the file we're wrapping
64-
export { default } from '__SENTRY_WRAPPING_TARGET_FILE__';
63+
export default routeModule.default;
6564

6665
type RouteHandler = (...args: unknown[]) => unknown;
6766

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// @ts-expect-error This will be replaced with the user's sentry config gile
22
import '__SENTRY_CONFIG_IMPORT_PATH__';
3+
// @ts-expect-error This is the file we're wrapping
4+
import * as wrappingTargetModule from '__SENTRY_WRAPPING_TARGET_FILE__';
35

46
// @ts-expect-error This is the file we're wrapping
57
export * from '__SENTRY_WRAPPING_TARGET_FILE__';
68

7-
// @ts-expect-error This is the file we're wrapping
8-
export { default } from '__SENTRY_WRAPPING_TARGET_FILE__';
9+
export default wrappingTargetModule.default;

0 commit comments

Comments
 (0)