Skip to content

Commit 375e213

Browse files
authored
feat: add oxfmt support (#103)
1 parent 2724ba4 commit 375e213

File tree

6 files changed

+127
-7
lines changed

6 files changed

+127
-7
lines changed

index.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ export default function createConfig(options: {
2020
devDependencies: Record<string, string>
2121
}
2222
files: {
23-
'eslint.config.js': string
23+
'eslint.config.js'?: string
24+
'eslint.config.ts'?: string
2425
'.editorconfig': string
26+
'.gitattributes': string
27+
'.oxfmtrc.jsonc'?: string
2528
'.prettierrc.json'?: string
2629
}
2730
}

index.js

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,26 @@ import renderEjsFile from './renderEjsFile.js'
44
import thisPackage from './package.json' with { type: 'json' }
55
const versionMap = thisPackage.devDependencies
66

7-
// This is also used in `create-vue`
7+
/**
8+
* Creates an ESLint configuration for Vue.js projects.
9+
* This is also used in `create-vue`.
10+
*
11+
* @param {object} options - Configuration options
12+
* @param {string} [options.styleGuide='default'] - The style guide to use (only 'default' is supported for now)
13+
* @param {boolean} [options.hasTypeScript=false] - Whether the project uses TypeScript
14+
* @param {boolean} [options.needsPrettier=false] - Whether to include Prettier integration
15+
* @param {boolean} [options.needsOxlint=false] - Whether to include Oxlint (faster linter to complement ESLint)
16+
* @param {boolean} [options.needsOxfmt=false] - Whether to include Oxfmt (faster formatter to complement Prettier, experimental)
17+
* @param {Array<{devDependencies?: object, beforeVuePlugin?: Array<{importer: string, content: string}>, afterVuePlugin?: Array<{importer: string, content: string}>}>} [options.additionalConfigs=[]] - Additional configurations to merge
18+
* @returns {{pkg: {devDependencies: object, scripts: object}, files: object}} An object containing package.json modifications and generated config files
19+
*/
820
export default function createConfig({
921
styleGuide = 'default', // only the default is supported for now
1022

1123
hasTypeScript = false,
1224
needsPrettier = false,
1325
needsOxlint = false,
26+
needsOxfmt = false,
1427

1528
additionalConfigs = [],
1629
}) {
@@ -20,6 +33,7 @@ export default function createConfig({
2033

2134
const pkg = {
2235
devDependencies: pickDependencies(['eslint', 'eslint-plugin-vue']),
36+
/** @type Record<string, string> */
2337
scripts: {},
2438
}
2539

@@ -79,9 +93,22 @@ export default function createConfig({
7993
// Users can still append any paths they'd like to format to the command,
8094
// e.g. `npm run format cypress/`.
8195
pkg.scripts.format = 'prettier --write src/'
96+
97+
// For now, oxfmt's feature isn't complete enough to fully replace prettier
98+
if (needsOxfmt) {
99+
additionalConfigs.push({
100+
devDependencies: pickDependencies(['oxfmt']),
101+
})
102+
pkg.scripts['format:oxfmt'] = 'oxfmt src/'
103+
pkg.scripts['format:prettier'] = `prettier --write src/ '!**/*.{js,jsx,ts,tsx}'`
104+
pkg.scripts.format = 'run-p format:oxfmt format:prettier'
105+
}
82106
}
83107

84-
if (needsOxlint && needsPrettier) {
108+
// TBH they don't share dependencies so the plugin can be added with or without oxlint/oxfmt.
109+
// But I don't feel like pushing this as the default option right now.
110+
// If the user shows interest in oxlint/oxfmt, we can safely assume they wants this plugin too.
111+
if (needsPrettier && (needsOxlint || needsOxfmt)) {
85112
additionalConfigs.push({
86113
devDependencies: pickDependencies(['@prettier/plugin-oxc']),
87114
})
@@ -104,6 +131,7 @@ export default function createConfig({
104131
configsAfterVuePlugin,
105132
}
106133

134+
/** @type Record<string, string> */
107135
const files = {
108136
'.editorconfig': renderEjsFile(
109137
'./templates/_editorconfig.ejs',
@@ -164,9 +192,9 @@ const isObject = val => val && typeof val === 'object'
164192
const mergeArrayWithDedupe = (a, b) => Array.from(new Set([...a, ...b]))
165193

166194
/**
167-
* Recursively merge the content of the new object to the existing one
168-
* @param {object} target the existing object
169-
* @param {object} obj the new object
195+
* Recursively merge the content of the new object to the existing one.
196+
* @param {object} target - The existing object
197+
* @param {object} obj - The new object
170198
*/
171199
export function deepMerge(target, obj) {
172200
for (const key of Object.keys(obj)) {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"globals": "^16.5.0",
5151
"jiti": "^2.6.1",
5252
"npm-run-all2": "^8.0.4",
53+
"oxfmt": "^0.17.0",
5354
"oxlint": "~1.32.0",
5455
"prettier": "3.7.4",
5556
"typescript": "~5.9.0"

pnpm-lock.yaml

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

templates/_oxfmtrc.jsonc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"$schema": "./node_modules/oxfmt/configuration_schema.json",
3+
"semi": false,
4+
"singleQuote": true,
5+
}

templates/_prettierrc.json.ejs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"$schema": "https://json.schemastore.org/prettierrc",
77
"semi": false,
88
"singleQuote": true,
9-
"printWidth": 100<%_ if (needsOxlint) { _%>,
9+
"printWidth": 100<%_ if (needsOxlint || needsOxfmt) { _%>,
1010
"plugins": [
1111
"@prettier/plugin-oxc"
1212
]

0 commit comments

Comments
 (0)