(moduleOptions: SentryNuxtModuleOptions, nitro: Nitro)
| 46 | * However, only limited tracing instrumentation is supported when doing this. |
| 47 | */ |
| 48 | export function addSentryTopImport(moduleOptions: SentryNuxtModuleOptions, nitro: Nitro): void { |
| 49 | nitro.hooks.hook('close', async () => { |
| 50 | const fileNameFromCommand = |
| 51 | nitro.options.commands.preview && getFilenameFromNodeStartCommand(nitro.options.commands.preview); |
| 52 | |
| 53 | // other presets ('node-server' or 'vercel') have an index.mjs |
| 54 | const presetsWithServerFile = ['netlify']; |
| 55 | |
| 56 | const entryFileName = fileNameFromCommand |
| 57 | ? fileNameFromCommand |
| 58 | : typeof nitro.options.rollupConfig?.output.entryFileNames === 'string' |
| 59 | ? nitro.options.rollupConfig?.output.entryFileNames |
| 60 | : presetsWithServerFile.includes(nitro.options.preset) |
| 61 | ? 'server.mjs' |
| 62 | : 'index.mjs'; |
| 63 | |
| 64 | const serverDirResolver = createResolver(nitro.options.output.serverDir); |
| 65 | const entryFilePath = serverDirResolver.resolve(entryFileName); |
| 66 | |
| 67 | try { |
| 68 | fs.readFile(entryFilePath, 'utf8', (err, data) => { |
| 69 | const updatedContent = `import './${SERVER_CONFIG_FILENAME}.mjs';\n${data}`; |
| 70 | |
| 71 | fs.writeFile(entryFilePath, updatedContent, 'utf8', () => { |
| 72 | if (moduleOptions.debug) { |
| 73 | // eslint-disable-next-line no-console |
| 74 | console.log( |
| 75 | `[Sentry] Successfully added the Sentry import to the server entry file "\`${entryFilePath}\`"`, |
| 76 | ); |
| 77 | } |
| 78 | }); |
| 79 | }); |
| 80 | } catch (err) { |
| 81 | if (moduleOptions.debug) { |
| 82 | // eslint-disable-next-line no-console |
| 83 | console.warn( |
| 84 | `[Sentry] An error occurred when trying to add the Sentry import to the server entry file "\`${entryFilePath}\`":`, |
| 85 | err, |
| 86 | ); |
| 87 | } |
| 88 | } |
| 89 | }); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * This function modifies the Rollup configuration to include a plugin that wraps the entry file with a dynamic import (`import()`) |
no test coverage detected