Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); const node_fs = require('node:fs'); const path = require('node:path'); const node_url = require('node:url'); const kit = require('@nuxt/kit'); const core = require('@sentry/core'); const fs = require('fs'); const utils = require('./utils.js'); const SERVER_CONFIG_FILENAME = "sentry.server.config"; const CONFIG_EXTENSIONS = [".ts", ".js", ".mjs", ".cjs", ".mts", ".cts"]; function isServerConfigFile(sourcePath, resolvedPath) { if (sourcePath === resolvedPath) { return true; } const name = path.basename(sourcePath); return name === SERVER_CONFIG_FILENAME || CONFIG_EXTENSIONS.some((ext) => name === `${SERVER_CONFIG_FILENAME}${ext}`); } function addServerConfigToBuild(moduleOptions, nitro, serverConfigFile) { nitro.hooks.hook("rollup:before", (nitro2, rollupConfig) => { if (rollupConfig?.plugins === null || rollupConfig?.plugins === void 0) { rollupConfig.plugins = []; } else if (!Array.isArray(rollupConfig.plugins)) { rollupConfig.plugins = [rollupConfig.plugins]; } rollupConfig.plugins.push(injectServerConfigPlugin(nitro2, serverConfigFile, moduleOptions.debug)); }); } function addSentryTopImport(moduleOptions, nitro) { nitro.hooks.hook("close", async () => { const fileNameFromCommand = nitro.options.commands.preview && utils.getFilenameFromNodeStartCommand(nitro.options.commands.preview); const presetsWithServerFile = ["netlify"]; const entryFileName = fileNameFromCommand ? fileNameFromCommand : typeof nitro.options.rollupConfig?.output.entryFileNames === "string" ? nitro.options.rollupConfig?.output.entryFileNames : presetsWithServerFile.includes(nitro.options.preset) ? "server.mjs" : "index.mjs"; const serverDirResolver = kit.createResolver(nitro.options.output.serverDir); const entryFilePath = serverDirResolver.resolve(entryFileName); try { fs.readFile(entryFilePath, "utf8", (err, data) => { const updatedContent = `import './${SERVER_CONFIG_FILENAME}.mjs'; ${data}`; fs.writeFile(entryFilePath, updatedContent, "utf8", () => { if (moduleOptions.debug) { console.log( `[Sentry] Successfully added the Sentry import to the server entry file "\`${entryFilePath}\`"` ); } }); }); } catch (err) { if (moduleOptions.debug) { console.warn( `[Sentry] An error occurred when trying to add the Sentry import to the server entry file "\`${entryFilePath}\`":`, err ); } } }); } function addDynamicImportEntryFileWrapper(nitro, serverConfigFile, moduleOptions) { if (!nitro.options.rollupConfig) { nitro.options.rollupConfig = { output: {} }; } if (nitro.options.rollupConfig?.plugins === null || nitro.options.rollupConfig?.plugins === void 0) { nitro.options.rollupConfig.plugins = []; } else if (!Array.isArray(nitro.options.rollupConfig.plugins)) { nitro.options.rollupConfig.plugins = [nitro.options.rollupConfig.plugins]; } nitro.options.rollupConfig.plugins.push( wrapEntryWithDynamicImport({ resolvedSentryConfigPath: kit.createResolver(nitro.options.rootDir).resolve(serverConfigFile), experimental_entrypointWrappedFunctions: moduleOptions.experimental_entrypointWrappedFunctions }) ); } function injectServerConfigPlugin(nitro, serverConfigFile, isDebug) { const filePrefix = "\0virtual:sentry-server-config:"; return { name: "rollup-plugin-inject-sentry-server-config", buildStart() { const configPath = kit.createResolver(nitro.options.rootDir).resolve(serverConfigFile); if (!node_fs.existsSync(configPath)) { if (isDebug) { core.debug.log(`[Sentry] Sentry server config file not found: ${configPath}`); } return; } this.emitFile({ type: "chunk", id: `${filePrefix}${serverConfigFile}`, fileName: `${SERVER_CONFIG_FILENAME}.mjs` }); }, resolveId(source) { if (source.startsWith(filePrefix)) { const originalFilePath = source.replace(filePrefix, ""); const configPath = kit.createResolver(nitro.options.rootDir).resolve(originalFilePath); return { id: configPath }; } return null; } }; } function wrapEntryWithDynamicImport({ resolvedSentryConfigPath, experimental_entrypointWrappedFunctions, debug: debug2 }) { const resolutionIdPrefix = "\0raw"; return { name: "sentry-wrap-entry-with-dynamic-import", async resolveId(source, importer, options) { const resolvable = utils.toResolvablePath(source); if (!resolvable) { return null; } const { path: normalizedSource, wasFileUrl } = resolvable; if (isServerConfigFile(normalizedSource, resolvedSentryConfigPath)) { return { id: normalizedSource, moduleSideEffects: true }; } if (source === "import-in-the-middle/hook.mjs") { return { id: source, moduleSideEffects: true, external: true }; } if (options.isEntry && normalizedSource.includes(".mjs") && !normalizedSource.includes(`.mjs${utils.SENTRY_WRAPPED_ENTRY}`)) { const resolution = await this.resolve(normalizedSource, importer, options); if (!resolution || resolution?.external) return resolution; const moduleInfo = await this.load(resolution); moduleInfo.moduleSideEffects = true; return resolution.id.includes(`.mjs${utils.SENTRY_WRAPPED_ENTRY}`) ? resolution.id : `${resolutionIdPrefix}${resolution.id.concat(utils.SENTRY_WRAPPED_ENTRY).concat( utils.constructWrappedFunctionExportQuery( moduleInfo.exportedBindings, experimental_entrypointWrappedFunctions, debug2 ) ).concat(utils.QUERY_END_INDICATOR)}`; } if (wasFileUrl) { const resolved = await this.resolve(normalizedSource, importer, { ...options, isEntry: false }); if (resolved) return resolved; return { id: normalizedSource }; } return null; }, load(id) { if (id.includes(`.mjs${utils.SENTRY_WRAPPED_ENTRY}`)) { const entryId = utils.removeSentryQueryFromPath(id).slice(resolutionIdPrefix.length); const entryIdUrl = node_url.pathToFileURL(entryId).href; const configUrl = node_url.pathToFileURL(resolvedSentryConfigPath).href; const reExportedFunctions = id.includes(utils.SENTRY_WRAPPED_FUNCTIONS) || id.includes(utils.SENTRY_REEXPORTED_FUNCTIONS) ? utils.constructFunctionReExport(id, entryIdUrl) : ""; return ( // Regular `import` of the Sentry config `import ${JSON.stringify(configUrl)}; import(${JSON.stringify(entryIdUrl)}); import 'import-in-the-middle/hook.mjs'; ${reExportedFunctions} ` ); } return null; } }; } exports.addDynamicImportEntryFileWrapper = addDynamicImportEntryFileWrapper; exports.addSentryTopImport = addSentryTopImport; exports.addServerConfigToBuild = addServerConfigToBuild; exports.wrapEntryWithDynamicImport = wrapEntryWithDynamicImport; //# sourceMappingURL=addServerConfig.js.map