Skip to content

Commit

Permalink
fix dev mode
Browse files Browse the repository at this point in the history
  • Loading branch information
s1gr1d committed Oct 14, 2024
1 parent 5b12bc4 commit e12c03c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
13 changes: 8 additions & 5 deletions packages/nuxt/src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,19 @@ export type SentryNuxtModuleOptions = {
debug?: boolean;

/**
* Wraps the server entry file with a dynamic `import()`. This will make it possible to preload Sentry and register
* necessary hooks before other code runs. (Node docs: https://nodejs.org/api/module.html#enabling)
*
* If this option is `false`, the Sentry SDK won't wrap the server entry file with `import()`. Not wrapping the
* server entry file will disable Sentry on the server-side. When you set this option to `true`, make sure
* to add the sentry server config with the node `--import` CLI flag to enable Sentry on the server-side.
* server entry file will disable Sentry on the server-side. When you set this option to `false`, make sure
* to add the Sentry server config with the node `--import` CLI flag to enable Sentry on the server-side.
*
* **DO NOT** add the node CLI flag `--import` in your node start script, when `disableDynamicImportWrapping` is set to `false`.
* **DO NOT** add the node CLI flag `--import` in your node start script, when `dynamicImportWrapping` is set to `true` (default).
* This would initialize Sentry twice on the server-side and this leads to unexpected issues.
*
* @default false
* @default true
*/
disableDynamicImportWrapping?: boolean;
dynamicImportWrapping?: boolean;

/**
* Options to be passed directly to the Sentry Rollup Plugin (`@sentry/rollup-plugin`) and Sentry Vite Plugin (`@sentry/vite-plugin`) that ship with the Sentry Nuxt SDK.
Expand Down
11 changes: 8 additions & 3 deletions packages/nuxt/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ export default defineNuxtModule<ModuleOptions>({
},
},
defaults: {},
setup(moduleOptions, nuxt) {
setup(moduleOptionsParam, nuxt) {
const moduleOptions = {
...moduleOptionsParam,
dynamicImportWrapping: moduleOptionsParam.dynamicImportWrapping !== false, // default: true
};

const moduleDirResolver = createResolver(import.meta.url);
const buildDirResolver = createResolver(nuxt.options.buildDir);

Expand Down Expand Up @@ -48,7 +53,7 @@ export default defineNuxtModule<ModuleOptions>({
const serverConfigFile = findDefaultSdkInitFile('server');

if (serverConfigFile) {
if (moduleOptions.disableDynamicImportWrapping) {
if (moduleOptions.dynamicImportWrapping === false) {
// Inject the server-side Sentry config file with a side effect import
addPluginTemplate({
mode: 'server',
Expand All @@ -69,7 +74,7 @@ export default defineNuxtModule<ModuleOptions>({

nuxt.hooks.hook('nitro:init', nitro => {
if (serverConfigFile && serverConfigFile.includes('.server.config')) {
if (moduleOptions.disableDynamicImportWrapping) {
if (moduleOptions.dynamicImportWrapping === false) {
addServerConfigToBuild(moduleOptions, nuxt, nitro, serverConfigFile);

if (moduleOptions.debug) {
Expand Down
9 changes: 3 additions & 6 deletions packages/nuxt/src/vite/addServerConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,8 @@ function wrapEntryWithDynamicImport(resolvedSentryConfigPath: string): InputPlug
: 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(
exportedFunctions?.length
? SENTRY_FUNCTIONS_REEXPORT.concat(exportedFunctions.join(',')).concat(QUERY_END_INDICATOR)
: '',
);
.concat(exportedFunctions?.length ? SENTRY_FUNCTIONS_REEXPORT.concat(exportedFunctions.join(',')) : '')
.concat(QUERY_END_INDICATOR);
}
return null;
},
Expand All @@ -163,7 +160,7 @@ function wrapEntryWithDynamicImport(resolvedSentryConfigPath: string): InputPlug
// `import()` can be used for any code that should be run after the hooks are registered (https://nodejs.org/api/module.html#enabling)
`import(${JSON.stringify(entryId)});\n` +
// By importing "import-in-the-middle/hook.mjs", we can make sure this file wil be included, as not all node builders are including files imported with `module.register()`.
"import 'import-in-the-middle/hook.mjs'\n" +
"import 'import-in-the-middle/hook.mjs';\n" +
`${reExportedFunctions}\n`
);
}
Expand Down
3 changes: 3 additions & 0 deletions packages/nuxt/test/vite/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,11 @@ describe('findDefaultSdkInitFile', () => {
describe('removeSentryQueryFromPath', () => {
it('strips the Sentry query part from the path', () => {
const url = `/example/path${SENTRY_WRAPPED_ENTRY}${SENTRY_FUNCTIONS_REEXPORT}foo,${QUERY_END_INDICATOR}`;
const url2 = `/example/path${SENTRY_WRAPPED_ENTRY}${QUERY_END_INDICATOR}`;
const result = removeSentryQueryFromPath(url);
const result2 = removeSentryQueryFromPath(url2);
expect(result).toBe('/example/path');
expect(result2).toBe('/example/path');
});

it('returns the same path if the specific query part is not present', () => {
Expand Down

0 comments on commit e12c03c

Please sign in to comment.