Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/guide/fmt.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ vp fmt . --write

Put formatting configuration directly in the `fmt` block in `vite.config.ts` so all your configuration stays in one place. We do not recommend using `.oxfmtrc.json` with Vite+.

For editors, point the formatter config path at `./vite.config.ts` so format-on-save uses the same `fmt` block:

```json
{
"oxc.fmt.configPath": "./vite.config.ts"
}
```

For the upstream formatter behavior and configuration reference, see the [Oxfmt docs](https://oxc.rs/docs/guide/usage/formatter.html).

```ts
Expand Down
3 changes: 2 additions & 1 deletion docs/guide/ide-integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ When you create or migrate a project, Vite+ prompts whether you want editor conf
```json
{
"editor.defaultFormatter": "oxc.oxc-vscode",
"oxc.fmt.configPath": "./vite.config.ts",
"editor.formatOnSave": true,
"editor.formatOnSaveMode": "file",
"editor.codeActionsOnSave": {
Expand All @@ -32,4 +33,4 @@ When you create or migrate a project, Vite+ prompts whether you want editor conf
}
```

This gives the project a shared default formatter and enables Oxc-powered fix actions on save. Vite+ uses `formatOnSaveMode: "file"` because Oxfmt does not support partial formatting.
This gives the project a shared default formatter and enables Oxc-powered fix actions on save. Setting `oxc.fmt.configPath` to `./vite.config.ts` keeps editor format-on-save aligned with the `fmt` block in your Vite+ config. Vite+ uses `formatOnSaveMode: "file"` because Oxfmt does not support partial formatting.
71 changes: 71 additions & 0 deletions packages/cli/src/utils/__tests__/editor.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';

import { afterEach, describe, expect, it } from 'vitest';

import { writeEditorConfigs } from '../editor.js';

const tempDirs: string[] = [];

function createTempDir() {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'vp-editor-config-'));
tempDirs.push(dir);
return dir;
}

afterEach(() => {
for (const dir of tempDirs.splice(0, tempDirs.length)) {
fs.rmSync(dir, { recursive: true, force: true });
}
});

describe('writeEditorConfigs', () => {
it('writes vscode settings that align formatter config with vite.config.ts', async () => {
const projectRoot = createTempDir();

await writeEditorConfigs({
projectRoot,
editorId: 'vscode',
interactive: false,
silent: true,
});

const settings = JSON.parse(
fs.readFileSync(path.join(projectRoot, '.vscode', 'settings.json'), 'utf8'),
) as Record<string, unknown>;

expect(settings['editor.defaultFormatter']).toBe('oxc.oxc-vscode');
expect(settings['oxc.fmt.configPath']).toBe('./vite.config.ts');
expect(settings['editor.formatOnSave']).toBe(true);
});

it('writes zed settings that align formatter config with vite.config.ts', async () => {
const projectRoot = createTempDir();

await writeEditorConfigs({
projectRoot,
editorId: 'zed',
interactive: false,
silent: true,
});

const settings = JSON.parse(
fs.readFileSync(path.join(projectRoot, '.zed', 'settings.json'), 'utf8'),
) as {
lsp?: {
oxfmt?: {
initialization_options?: {
settings?: {
configPath?: string;
};
};
};
};
};

expect(settings.lsp?.oxfmt?.initialization_options?.settings?.configPath).toBe(
'./vite.config.ts',
);
});
});
3 changes: 2 additions & 1 deletion packages/cli/src/utils/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { readJsonFile, writeJsonFile } from './json.js';
const VSCODE_SETTINGS = {
// Set as default over per-lang to avoid conflicts with other formatters
'editor.defaultFormatter': 'oxc.oxc-vscode',
'oxc.fmt.configPath': './vite.config.ts',
'editor.formatOnSave': true,
// Oxfmt does not support partial formatting
'editor.formatOnSaveMode': 'file',
Expand Down Expand Up @@ -38,7 +39,7 @@ const ZED_SETTINGS = {
oxfmt: {
initialization_options: {
settings: {
configPath: './.oxfmtrc.jsonc',
configPath: './vite.config.ts',
run: 'onSave',
},
},
Expand Down
Loading