Skip to content

Commit 2420e5f

Browse files
chore: add prettier
1 parent c03da62 commit 2420e5f

File tree

12 files changed

+292
-281
lines changed

12 files changed

+292
-281
lines changed

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@
3939
"test:watch": "vitest watch",
4040
"test:coverage": "vitest run --coverage",
4141
"test:core": "vitest run -c ./vitest.config.ts",
42-
"test:core:watch": "vitest watch -c ./vitest.config.ts"
42+
"test:core:watch": "vitest watch -c ./vitest.config.ts",
43+
"format": "prettier --write \"src/**/*.{js,jsx,ts,tsx}\"",
44+
"format:check": "prettier --check \"src/**/*.{js,jsx,ts,tsx}\""
4345
},
4446
"dependencies": {
4547
"@babel/core": "^7.26.3",
@@ -76,6 +78,7 @@
7678
"@types/react-dom": "^19.0.1",
7779
"jiti": "^2.4.1",
7880
"playwright": "^1.50.1",
81+
"prettier": "3.4.2",
7982
"react": "^19.0.0",
8083
"react-dom": "^19.0.0",
8184
"react-router": "^7.4.0",

pnpm-lock.yaml

Lines changed: 11 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const JS_LOADERS = {
66
'.ts': 'ts',
77
'.tsx': 'tsx',
88
'.js': 'js',
9-
'.jsx': 'jsx'
9+
'.jsx': 'jsx',
1010
} as const;
1111

1212
export const SERVER_ONLY_ROUTE_EXPORTS = [

src/dev-server.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ export type DevServerMiddleware = (
88
) => Promise<void>;
99

1010
export const createDevServerMiddleware = (server: any): DevServerMiddleware => {
11-
return async (req: IncomingMessage, res: ServerResponse, next: (err?: any) => void): Promise<void> => {
11+
return async (
12+
req: IncomingMessage,
13+
res: ServerResponse,
14+
next: (err?: any) => void
15+
): Promise<void> => {
1216
try {
13-
const bundle = (await server.environments.node.loadBundle(
14-
'app',
15-
));
17+
const bundle = await server.environments.node.loadBundle('app');
1618

1719
if (!bundle || !bundle.routes) {
1820
throw new Error('Server bundle not found or invalid');

src/index.ts

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ import { generate, parse } from './babel.js';
1111
import { PLUGIN_NAME, SERVER_ONLY_ROUTE_EXPORTS } from './constants.js';
1212
import { createDevServerMiddleware } from './dev-server.js';
1313
import {
14-
generateWithProps,
15-
removeExports,
16-
transformRoute,
17-
findEntryFile
14+
generateWithProps,
15+
removeExports,
16+
transformRoute,
17+
findEntryFile,
1818
} from './plugin-utils.js';
1919
import {
20-
configRoutesToRouteManifest,
21-
getReactRouterManifestForDev
20+
configRoutesToRouteManifest,
21+
getReactRouterManifestForDev,
2222
} from './manifest.js';
2323
import { generateServerBuild } from './server-utils.js';
2424
import { createModifyBrowserManifestPlugin } from './modify-browser-manifest.js';
@@ -39,7 +39,6 @@ export type PluginOptions = {
3939
serverOutput?: 'module' | 'commonjs';
4040
};
4141

42-
4342
export type Route = {
4443
id: string;
4544
parentId?: string;
@@ -62,7 +61,7 @@ export type RouteManifestItem = Omit<Route, 'file' | 'children'> & {
6261
};
6362

6463
export const pluginReactRouter = (
65-
options: PluginOptions = {},
64+
options: PluginOptions = {}
6665
): RsbuildPlugin => ({
6766
name: PLUGIN_NAME,
6867

@@ -90,27 +89,27 @@ export const pluginReactRouter = (
9089
const source = new RawSource(
9190
JSON.stringify({
9291
type: 'commonjs',
93-
}),
92+
})
9493
);
9594

9695
if (compilation.getAsset(packageJsonPath)) {
9796
compilation.updateAsset(packageJsonPath, source);
9897
} else {
9998
compilation.emitAsset(packageJsonPath, source);
10099
}
101-
},
100+
}
102101
);
103102
}
104103

105104
// Run typegen on build/dev
106105
api.onBeforeStartDevServer(async () => {
107106
const { $ } = await import('execa');
108-
$`npx --yes react-router typegen --watch`;
107+
$`npx --yes react-router typegen --watch`;
109108
});
110109

111110
api.onBeforeBuild(async () => {
112111
const { $ } = await import('execa');
113-
$`npx --yes react-router typegen`;
112+
$`npx --yes react-router typegen`;
114113
});
115114

116115
const jiti = createJiti(process.cwd());
@@ -127,7 +126,7 @@ export const pluginReactRouter = (
127126
})
128127
.catch(() => {
129128
console.error(
130-
'No react-router.config.ts found, using default configuration.',
129+
'No react-router.config.ts found, using default configuration.'
131130
);
132131
return {} as Config;
133132
});
@@ -139,25 +138,25 @@ export const pluginReactRouter = (
139138
.import<RouteConfigEntry[]>(routesPath, {
140139
default: true,
141140
})
142-
.catch((error) => {
141+
.catch(error => {
143142
console.error('Failed to load routes.ts:', error);
144143
console.error('No routes.ts found in app directory.');
145144
return [] as RouteConfigEntry[];
146145
});
147146

148147
// Remove local JS_EXTENSIONS definition - use the imported one instead
149-
148+
150149
// Remove duplicate findEntryFile implementation
151150
const entryClientPath = findEntryFile(
152-
resolve(appDirectory, 'entry.client'),
151+
resolve(appDirectory, 'entry.client')
153152
);
154153
const entryServerPath = findEntryFile(
155-
resolve(appDirectory, 'entry.server'),
154+
resolve(appDirectory, 'entry.server')
156155
);
157156

158157
// Check for server app file
159158
const serverAppPath = findEntryFile(
160-
resolve(appDirectory, '../server/index'),
159+
resolve(appDirectory, '../server/index')
161160
);
162161
const hasServerApp = existsSync(serverAppPath);
163162

@@ -176,7 +175,7 @@ export const pluginReactRouter = (
176175

177176
const rootRouteFile = relative(
178177
appDirectory,
179-
resolve(appDirectory, 'root.tsx'),
178+
resolve(appDirectory, 'root.tsx')
180179
);
181180

182181
const routes = {
@@ -241,13 +240,10 @@ export const pluginReactRouter = (
241240
...Object.values(routes).reduce(
242241
(acc: Record<string, string>, route) => {
243242
acc[route.file.slice(0, route.file.lastIndexOf('.'))] =
244-
`${resolve(
245-
appDirectory,
246-
route.file,
247-
)}?react-router-route`;
243+
`${resolve(appDirectory, route.file)}?react-router-route`;
248244
return acc;
249245
},
250-
{} as Record<string, string>,
246+
{} as Record<string, string>
251247
),
252248
},
253249
},
@@ -305,8 +301,14 @@ export const pluginReactRouter = (
305301
externalsType: pluginOptions.serverOutput,
306302
output: {
307303
chunkFormat: pluginOptions.serverOutput,
308-
chunkLoading: pluginOptions.serverOutput === 'module' ? 'import' : 'require',
309-
workerChunkLoading: pluginOptions.serverOutput === 'module' ? 'import' : 'require',
304+
chunkLoading:
305+
pluginOptions.serverOutput === 'module'
306+
? 'import'
307+
: 'require',
308+
workerChunkLoading:
309+
pluginOptions.serverOutput === 'module'
310+
? 'import'
311+
: 'require',
310312
wasmLoading: 'fetch',
311313
library: { type: pluginOptions.serverOutput },
312314
module: pluginOptions.serverOutput === 'module',
@@ -324,10 +326,14 @@ export const pluginReactRouter = (
324326
if (name === 'web') {
325327
return mergeEnvironmentConfig(config, {
326328
tools: {
327-
rspack: (rspackConfig) => {
329+
rspack: rspackConfig => {
328330
if (rspackConfig.plugins) {
329331
rspackConfig.plugins.push(
330-
createModifyBrowserManifestPlugin(routes, pluginOptions, appDirectory)
332+
createModifyBrowserManifestPlugin(
333+
routes,
334+
pluginOptions,
335+
appDirectory
336+
)
331337
);
332338
}
333339
return rspackConfig;
@@ -336,29 +342,31 @@ export const pluginReactRouter = (
336342
});
337343
}
338344
return config;
339-
},
345+
}
340346
);
341347

342348
api.processAssets(
343349
{ stage: 'additional', targets: ['node'] },
344350
({ sources, compilation }) => {
345351
const packageJsonPath = 'package.json';
346-
const source = new sources.RawSource(`{"type": "${pluginOptions.serverOutput}"}`);
352+
const source = new sources.RawSource(
353+
`{"type": "${pluginOptions.serverOutput}"}`
354+
);
347355

348356
if (compilation.getAsset(packageJsonPath)) {
349357
compilation.updateAsset(packageJsonPath, source);
350358
} else {
351359
compilation.emitAsset(packageJsonPath, source);
352360
}
353-
},
361+
}
354362
);
355363

356364
// Add manifest transformations
357365
api.transform(
358366
{
359367
test: /virtual\/react-router\/(browser|server)-manifest/,
360368
},
361-
async (args) => {
369+
async args => {
362370
// For browser manifest, return a placeholder that will be modified by the plugin
363371
if (args.environment.name === 'web') {
364372
return {
@@ -376,15 +384,15 @@ export const pluginReactRouter = (
376384
return {
377385
code: `export default ${jsesc(manifest, { es6: true })};`,
378386
};
379-
},
387+
}
380388
);
381389

382390
// Add route transformation
383391
api.transform(
384392
{
385393
resourceQuery: /\?react-router-route/,
386394
},
387-
async (args) => {
395+
async args => {
388396
let code = (
389397
await esbuild.transform(args.code, {
390398
jsx: 'automatic',
@@ -395,7 +403,7 @@ export const pluginReactRouter = (
395403
).code;
396404

397405
const defaultExportMatch = code.match(
398-
/\n\s{0,}([\w\d_]+)\sas default,?/,
406+
/\n\s{0,}([\w\d_]+)\sas default,?/
399407
);
400408
if (
401409
defaultExportMatch &&
@@ -419,7 +427,7 @@ export const pluginReactRouter = (
419427
filename: args.resource,
420428
sourceFileName: args.resourcePath,
421429
});
422-
},
430+
}
423431
);
424432
},
425433
});

0 commit comments

Comments
 (0)