Skip to content

Commit

Permalink
Add nullbyte prefix to rollup plugin to ensure correct bundling
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiborza committed Nov 13, 2024
1 parent 43bce89 commit 53552cf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ export function wrapServerEntryWithDynamicImport(config: WrapServerEntryPluginOp
debug,
} = config;

// In order to correctly import the server config file
// and dynamically import the nitro runtime, we need to
// mark the resolutionId with '\0raw' to fall into the
// raw chunk group, c.f. https://github.com/nitrojs/nitro/commit/8b4a408231bdc222569a32ce109796a41eac4aa6#diff-e58102d2230f95ddeef2662957b48d847a6e891e354cfd0ae6e2e03ce848d1a2R142
const resolutionIdPrefix = '\0raw';

return {
name: 'sentry-wrap-server-entry-with-dynamic-import',
async resolveId(source, importer, options) {
Expand Down Expand Up @@ -70,19 +76,19 @@ export function wrapServerEntryWithDynamicImport(config: WrapServerEntryPluginOp
// The enclosing `if` already checks for the suffix in `source`, but a check in `resolution.id` is needed as well to prevent multiple attachment of the suffix
return resolution.id.includes(`.mjs${SENTRY_WRAPPED_ENTRY}`)
? resolution.id
: resolution.id
: `${resolutionIdPrefix}${resolution.id
// Concatenates the query params to mark the file (also attaches names of re-exports - this is needed for serverless functions to re-export the handler)
.concat(SENTRY_WRAPPED_ENTRY)
.concat(
constructWrappedFunctionExportQuery(moduleInfo.exportedBindings, entrypointWrappedFunctions, debug),
)
.concat(QUERY_END_INDICATOR);
.concat(QUERY_END_INDICATOR)}`;
}
return null;
},
load(id: string) {
if (id.includes(`.mjs${SENTRY_WRAPPED_ENTRY}`)) {
const entryId = removeSentryQueryFromPath(id);
const entryId = removeSentryQueryFromPath(id).slice(resolutionIdPrefix.length);

// Mostly useful for serverless `handler` functions
const reExportedFunctions =
Expand Down Expand Up @@ -188,7 +194,7 @@ export function constructWrappedFunctionExportQuery(
consoleSandbox(() =>
// eslint-disable-next-line no-console
console.warn(
"[Sentry] No functions found to wrap. In case the server needs to export async functions other than `handler` or `server`, consider adding the name(s) to Sentry's build options `sentry.entrypointWrappedFunctions` in `nuxt.config.ts`.",
'[Sentry] No functions found to wrap. In case the server needs to export async functions other than `handler` or `server`, consider adding the name(s) to `entrypointWrappedFunctions`.',
),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ describe('constructWrappedFunctionExportQuery', () => {
const result = constructWrappedFunctionExportQuery(exportedBindings, entrypointWrappedFunctions, debug);
expect(result).toBe('?sentry-query-reexported-functions=handler');
expect(consoleWarnSpy).toHaveBeenCalledWith(
"[Sentry] No functions found to wrap. In case the server needs to export async functions other than `handler` or `server`, consider adding the name(s) to Sentry's build options `sentry.entrypointWrappedFunctions` in `nuxt.config.ts`.",
'[Sentry] No functions found to wrap. In case the server needs to export async functions other than `handler` or `server`, consider adding the name(s) to `entrypointWrappedFunctions`.',
);

consoleWarnSpy.mockRestore();
Expand Down

0 comments on commit 53552cf

Please sign in to comment.