-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(nuxt): Add server config to root folder #13583
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ea95e37
feat(nuxt): Add server config to root folder
s1gr1d 7671bd9
Merge branch 'refs/heads/develop' into sig/server-config-nuxt
s1gr1d ae6d51c
use public file method in e2e test
s1gr1d d21dac2
Merge branch 'develop' into sig/server-config-nuxt
s1gr1d 16ef5ac
review comments: refine console logs
s1gr1d 5a72b0b
change filename to sentry.server.config.mjs
s1gr1d 2be97af
create it.each test
s1gr1d e6a78fb
Merge branch 'develop' into sig/server-config-nuxt
s1gr1d File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import { createResolver } from '@nuxt/kit'; | ||
import type { Nuxt } from '@nuxt/schema'; | ||
import type { SentryNuxtModuleOptions } from '../common/types'; | ||
|
||
/** | ||
* Adds the `server.config.ts` file as `instrument-sentry.mjs` to the `.output` directory to be able to reference this file in the node --import option. | ||
* | ||
* 1. Adding the file as a rollup import, so it is included in the build (automatically transpiles the file). | ||
* 2. Copying the file to the `.output` directory after the build process is finished. | ||
*/ | ||
export function addServerConfigToBuild( | ||
moduleOptions: SentryNuxtModuleOptions, | ||
nuxt: Nuxt, | ||
serverConfigFile: string, | ||
): void { | ||
nuxt.hook('vite:extendConfig', async (viteInlineConfig, _env) => { | ||
if ( | ||
typeof viteInlineConfig?.build?.rollupOptions?.input === 'object' && | ||
'server' in viteInlineConfig.build.rollupOptions.input | ||
) { | ||
// Create a rollup entry for the server config to add it as `instrument-sentry.mjs` to the build | ||
(viteInlineConfig.build.rollupOptions.input as { [entryName: string]: string })['instrument-sentry'] = | ||
createResolver(nuxt.options.srcDir).resolve(`/${serverConfigFile}`); | ||
} | ||
|
||
/** | ||
* When the build process is finished, copy the `sentry.server.config` file to the `.output` directory. | ||
* This is necessary because we need to reference this file path in the node --import option. | ||
*/ | ||
nuxt.hook('close', async () => { | ||
const source = path.resolve('.nuxt/dist/server/instrument-sentry.mjs'); | ||
const destination = path.resolve('.output/server/instrument-sentry.mjs'); | ||
|
||
try { | ||
await fs.promises.access(source, fs.constants.F_OK); | ||
await fs.promises.copyFile(source, destination); | ||
|
||
if (moduleOptions.debug) { | ||
// eslint-disable-next-line no-console | ||
console.log(`[Sentry] Successfully added the content of the ${serverConfigFile} file to \`${destination}\``); | ||
} | ||
} catch (error) { | ||
if (moduleOptions.debug) { | ||
// eslint-disable-next-line no-console | ||
console.warn( | ||
`[Sentry] An error occurred when trying to add the ${serverConfigFile} file to the \`.output\` directory`, | ||
error, | ||
); | ||
} | ||
} | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
|
||
/** | ||
* Find the default SDK init file for the given type (client or server). | ||
* The sentry.server.config file is prioritized over the instrument.server file. | ||
*/ | ||
export function findDefaultSdkInitFile(type: 'server' | 'client'): string | undefined { | ||
const possibleFileExtensions = ['ts', 'js', 'mjs', 'cjs', 'mts', 'cts']; | ||
const cwd = process.cwd(); | ||
|
||
const filePaths: string[] = []; | ||
if (type === 'server') { | ||
for (const ext of possibleFileExtensions) { | ||
// order is important here - we want to prioritize the server.config file | ||
filePaths.push(path.join(cwd, `sentry.${type}.config.${ext}`)); | ||
filePaths.push(path.join(cwd, 'public', `instrument.${type}.${ext}`)); | ||
} | ||
} else { | ||
for (const ext of possibleFileExtensions) { | ||
filePaths.push(path.join(cwd, `sentry.${type}.config.${ext}`)); | ||
} | ||
} | ||
|
||
const filePath = filePaths.find(filename => fs.existsSync(filename)); | ||
|
||
return filePath ? path.basename(filePath) : undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import * as fs from 'fs'; | ||
import { afterEach, describe, expect, it, vi } from 'vitest'; | ||
import { findDefaultSdkInitFile } from '../../src/vite/utils'; | ||
|
||
vi.mock('fs'); | ||
|
||
describe('findDefaultSdkInitFile', () => { | ||
afterEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it('should return the server file if it exists', () => { | ||
vi.spyOn(fs, 'existsSync').mockImplementation(filePath => { | ||
return !(filePath instanceof URL) && filePath.includes('sentry.server.config.js'); | ||
}); | ||
|
||
const result = findDefaultSdkInitFile('server'); | ||
expect(result).toBe('sentry.server.config.js'); | ||
}); | ||
|
||
it('should return the client file if it exists', () => { | ||
vi.spyOn(fs, 'existsSync').mockImplementation(filePath => { | ||
return !(filePath instanceof URL) && filePath.includes('sentry.client.config.js'); | ||
}); | ||
|
||
const result = findDefaultSdkInitFile('client'); | ||
expect(result).toBe('sentry.client.config.js'); | ||
}); | ||
|
||
it('should return undefined if no file exists', () => { | ||
vi.spyOn(fs, 'existsSync').mockReturnValue(false); | ||
|
||
const result = findDefaultSdkInitFile('server'); | ||
expect(result).toBeUndefined(); | ||
}); | ||
|
||
it('should return the server config file if server.config and instrument exist', () => { | ||
vi.spyOn(fs, 'existsSync').mockImplementation(filePath => { | ||
return ( | ||
!(filePath instanceof URL) && | ||
(filePath.includes('sentry.server.config.js') || filePath.includes('instrument.server.js')) | ||
); | ||
}); | ||
|
||
const result = findDefaultSdkInitFile('server'); | ||
expect(result).toBe('sentry.server.config.js'); | ||
}); | ||
|
||
it('should return the server file with .ts extension if it exists', () => { | ||
vi.spyOn(fs, 'existsSync').mockImplementation(filePath => { | ||
return !(filePath instanceof URL) && filePath.includes('sentry.server.config.ts'); | ||
}); | ||
|
||
const result = findDefaultSdkInitFile('server'); | ||
expect(result).toBe('sentry.server.config.ts'); | ||
}); | ||
|
||
it('should return the client file with .mjs extension if it exists', () => { | ||
vi.spyOn(fs, 'existsSync').mockImplementation(filePath => { | ||
return !(filePath instanceof URL) && filePath.includes('sentry.client.config.mjs'); | ||
}); | ||
|
||
const result = findDefaultSdkInitFile('client'); | ||
expect(result).toBe('sentry.client.config.mjs'); | ||
}); | ||
|
||
it('should return undefined if no file with specified extensions exists', () => { | ||
vi.spyOn(fs, 'existsSync').mockReturnValue(false); | ||
|
||
const result = findDefaultSdkInitFile('server'); | ||
expect(result).toBeUndefined(); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
l: For some test extra credit (feel free to disregard) - we could use an
it.each
to test against all file types we search for.