Skip to content

Commit 5546d11

Browse files
committed
Fix typing, unify vite plugin options
1 parent 3aa3944 commit 5546d11

File tree

9 files changed

+46
-20
lines changed

9 files changed

+46
-20
lines changed

packages/solidstart/src/config/addInstrumentation.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ import type { Nitro } from './types';
1111
* added to `app.config.ts` to enable building the instrumentation file.
1212
*/
1313
export async function addInstrumentationFileToBuild(nitro: Nitro): Promise<void> {
14-
const { buildDir, serverDir } = nitro.options.output;
15-
const source = path.join(buildDir, 'build', 'ssr', 'instrument.server.js');
16-
const destination = path.join(serverDir, 'instrument.server.mjs');
14+
const buildDir = nitro.options.buildDir;
15+
const serverDir = nitro.options.output.serverDir;
16+
const source = path.resolve(buildDir, 'build', 'ssr', 'instrument.server.js');
17+
const destination = path.resolve(serverDir, 'instrument.server.mjs');
1718

1819
try {
1920
await fs.promises.access(source, fs.constants.F_OK);
@@ -44,9 +45,9 @@ export async function experimental_addInstrumentationFileTopLevelImportToServerE
4445
): Promise<void> {
4546
// other presets ('node-server' or 'vercel') have an index.mjs
4647
const presetsWithServerFile = ['netlify'];
47-
const instrumentationFile = path.join(serverDir, 'instrument.server.mjs');
48+
const instrumentationFile = path.resolve(serverDir, 'instrument.server.mjs');
4849
const serverEntryFileName = presetsWithServerFile.includes(preset) ? 'server.mjs' : 'index.mjs';
49-
const serverEntryFile = path.join(serverDir, serverEntryFileName);
50+
const serverEntryFile = path.resolve(serverDir, serverEntryFileName);
5051

5152
try {
5253
await fs.promises.access(instrumentationFile, fs.constants.F_OK);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './withSentry';

packages/solidstart/src/config/types.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { defineConfig } from '@solidjs/start/config';
12
// Types to avoid pulling in extra dependencies
23
// These are non-exhaustive
34
export type Nitro = {
@@ -11,12 +12,12 @@ export type Nitro = {
1112
};
1213
};
1314

14-
export type SolidStartInlineConfig = {
15-
server?: {
16-
hooks?: {
17-
close?: () => unknown;
18-
'rollup:before'?: (nitro: Nitro) => unknown;
19-
};
15+
export type SolidStartInlineConfig = Parameters<typeof defineConfig>[0];
16+
17+
export type SolidStartInlineConfigNitroHooks = {
18+
hooks?: {
19+
close?: () => unknown;
20+
'rollup:before'?: (nitro: Nitro) => unknown;
2021
};
2122
};
2223

packages/solidstart/src/config/withSentry.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@ import {
22
addInstrumentationFileToBuild,
33
experimental_addInstrumentationFileTopLevelImportToServerEntry,
44
} from './addInstrumentation';
5-
import type { SentrySolidStartConfigOptions, SolidStartInlineConfig } from './types';
5+
import type {
6+
Nitro,
7+
SentrySolidStartConfigOptions,
8+
SolidStartInlineConfig,
9+
SolidStartInlineConfigNitroHooks,
10+
} from './types';
611

712
export const withSentry = (
813
solidStartConfig: SolidStartInlineConfig = {},
914
sentrySolidStartConfigOptions: SentrySolidStartConfigOptions = {},
1015
): SolidStartInlineConfig => {
11-
const server = solidStartConfig.server || {};
16+
const server = (solidStartConfig.server || {}) as SolidStartInlineConfigNitroHooks;
1217
const hooks = server.hooks || {};
1318

1419
let serverDir: string;
@@ -30,7 +35,7 @@ export const withSentry = (
3035
hooks.close();
3136
}
3237
},
33-
async 'rollup:before'(nitro) {
38+
async 'rollup:before'(nitro: Nitro) {
3439
serverDir = nitro.options.output.serverDir;
3540
buildPreset = nitro.options.preset;
3641

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './server';
22
export * from './vite';
3+
export * from './config';

packages/solidstart/src/index.types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
export * from './client';
55
export * from './server';
66
export * from './vite';
7+
export * from './config';
78

89
import type { Integration, Options, StackParser } from '@sentry/types';
910

packages/solidstart/src/vite/buildInstrumentationFile.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ import * as fs from 'fs';
22
import * as path from 'path';
33
import { consoleSandbox } from '@sentry/utils';
44
import type { Plugin, UserConfig } from 'vite';
5+
import type { SentrySolidStartPluginOptions } from './types';
56

67
/**
78
* A Sentry plugin for SolidStart to build the server
89
* `instrument.server.ts` file.
910
*/
10-
export function makeBuildInstrumentationFilePlugin(
11-
instrumentationFilePath: string = './src/instrument.server.ts',
12-
): Plugin {
11+
export function makeBuildInstrumentationFilePlugin(options: SentrySolidStartPluginOptions = {}): Plugin {
1312
return {
1413
name: 'sentry-solidstart-build-instrumentation-file',
1514
apply: 'build',
1615
enforce: 'post',
1716
async config(config: UserConfig, { command }) {
17+
const instrumentationFilePath = options.instrumentation || './src/instrument.server.ts';
1818
const router = (config as UserConfig & { router: { target: string; name: string; root: string } }).router;
1919
const build = config.build || {};
2020
const rollupOptions = build.rollupOptions || {};
@@ -38,7 +38,7 @@ export function makeBuildInstrumentationFilePlugin(
3838
return config;
3939
}
4040

41-
input.push(path.join(router.root, instrumentationFilePath));
41+
input.push(path.resolve(router.root, instrumentationFilePath));
4242

4343
return {
4444
...config,

packages/solidstart/src/vite/sentrySolidStartVite.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type { SentrySolidStartPluginOptions } from './types';
99
export const sentrySolidStartVite = (options: SentrySolidStartPluginOptions = {}): Plugin[] => {
1010
const sentryPlugins: Plugin[] = [];
1111

12-
sentryPlugins.push(makeBuildInstrumentationFilePlugin(options.instrumentation));
12+
sentryPlugins.push(makeBuildInstrumentationFilePlugin(options));
1313

1414
if (process.env.NODE_ENV !== 'development') {
1515
if (options.sourceMapsUploadOptions?.enabled ?? true) {

packages/solidstart/test/vite/buildInstrumentation.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,26 @@ describe('makeBuildInstrumentationFilePlugin()', () => {
4747

4848
it('adds the instrumentation file for server builds', async () => {
4949
const buildInstrumentationFilePlugin = makeBuildInstrumentationFilePlugin();
50+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
51+
// @ts-ignore - this is always defined and always a function
5052
const config = await buildInstrumentationFilePlugin.config(viteConfig, { command: 'build' });
5153
expect(config.build.rollupOptions.input).toContain('/some/project/path/src/instrument.server.ts');
5254
});
5355

5456
it('adds the correct instrumentation file', async () => {
55-
const buildInstrumentationFilePlugin = makeBuildInstrumentationFilePlugin('./src/myapp/instrument.server.ts');
57+
const buildInstrumentationFilePlugin = makeBuildInstrumentationFilePlugin({
58+
instrumentation: './src/myapp/instrument.server.ts',
59+
});
60+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
61+
// @ts-ignore - this is always defined and always a function
5662
const config = await buildInstrumentationFilePlugin.config(viteConfig, { command: 'build' });
5763
expect(config.build.rollupOptions.input).toContain('/some/project/path/src/myapp/instrument.server.ts');
5864
});
5965

6066
it("doesn't add the instrumentation file for server function builds", async () => {
6167
const buildInstrumentationFilePlugin = makeBuildInstrumentationFilePlugin();
68+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
69+
// @ts-ignore - this is always defined and always a function
6270
const config = await buildInstrumentationFilePlugin.config(
6371
{
6472
...viteConfig,
@@ -74,6 +82,8 @@ describe('makeBuildInstrumentationFilePlugin()', () => {
7482

7583
it("doesn't add the instrumentation file for client builds", async () => {
7684
const buildInstrumentationFilePlugin = makeBuildInstrumentationFilePlugin();
85+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
86+
// @ts-ignore - this is always defined and always a function
7787
const config = await buildInstrumentationFilePlugin.config(
7888
{
7989
...viteConfig,
@@ -89,13 +99,17 @@ describe('makeBuildInstrumentationFilePlugin()', () => {
8999

90100
it("doesn't add the instrumentation file when serving", async () => {
91101
const buildInstrumentationFilePlugin = makeBuildInstrumentationFilePlugin();
102+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
103+
// @ts-ignore - this is always defined and always a function
92104
const config = await buildInstrumentationFilePlugin.config(viteConfig, { command: 'serve' });
93105
expect(config.build.rollupOptions.input).not.toContain('/some/project/path/src/instrument.server.ts');
94106
});
95107

96108
it("doesn't modify the config if the instrumentation file doesn't exist", async () => {
97109
fsAccessMock.mockRejectedValueOnce(undefined);
98110
const buildInstrumentationFilePlugin = makeBuildInstrumentationFilePlugin();
111+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
112+
// @ts-ignore - this is always defined and always a function
99113
const config = await buildInstrumentationFilePlugin.config(viteConfig, { command: 'build' });
100114
expect(config).toEqual(viteConfig);
101115
});
@@ -104,6 +118,8 @@ describe('makeBuildInstrumentationFilePlugin()', () => {
104118
const error = new Error("File doesn't exist.");
105119
fsAccessMock.mockRejectedValueOnce(error);
106120
const buildInstrumentationFilePlugin = makeBuildInstrumentationFilePlugin();
121+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
122+
// @ts-ignore - this is always defined and always a function
107123
const config = await buildInstrumentationFilePlugin.config(viteConfig, { command: 'build' });
108124
expect(config).toEqual(viteConfig);
109125
expect(consoleWarnSpy).toHaveBeenCalledWith(

0 commit comments

Comments
 (0)