From 6320796615054e8a045143227513999b2430d3af Mon Sep 17 00:00:00 2001 From: Pionxzh Date: Sun, 14 Jan 2024 03:37:32 +0800 Subject: [PATCH] feat: biome --- packages/cli/tsup.config.ts | 3 ++ packages/unminify/package.json | 2 + .../unminify/src/transformations/biome.ts | 45 +++++++++++++++++++ .../unminify/src/transformations/index.ts | 6 +-- pnpm-lock.yaml | 27 +++++++++++ 5 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 packages/unminify/src/transformations/biome.ts diff --git a/packages/cli/tsup.config.ts b/packages/cli/tsup.config.ts index 3a7eb8a5..e2c275b7 100644 --- a/packages/cli/tsup.config.ts +++ b/packages/cli/tsup.config.ts @@ -14,6 +14,9 @@ export default defineConfig({ 'process.env.NODE_DEBUG': 'undefined', }, minify: true, + external: [ + '@biomejs/js-api', + ], noExternal: [ 'jscodeshift', 'ast-types', diff --git a/packages/unminify/package.json b/packages/unminify/package.json index e7e8a4e5..80d2855f 100644 --- a/packages/unminify/package.json +++ b/packages/unminify/package.json @@ -35,6 +35,8 @@ "zod": "^3.22.4" }, "devDependencies": { + "@biomejs/js-api": "^0.3.0-nightly.efc084c", + "@biomejs/wasm-nodejs": "^1.5.1", "@types/jscodeshift": "^0.11.11", "@types/prettier": "^2.7.3", "@types/yargs": "^17.0.32", diff --git a/packages/unminify/src/transformations/biome.ts b/packages/unminify/src/transformations/biome.ts new file mode 100644 index 00000000..aa6269d8 --- /dev/null +++ b/packages/unminify/src/transformations/biome.ts @@ -0,0 +1,45 @@ +import { Biome, Distribution } from '@biomejs/js-api' +import { createStringTransformationRule } from '@wakaru/shared/rule' + +type FormatConfiguration = Parameters[0] + +let biome: Biome | null = null + +const formatConfig: FormatConfiguration = { + formatter: { + enabled: true, + indentStyle: 'space', + indentWidth: 2, + lineWidth: 80, + }, + javascript: { + formatter: { + quoteStyle: 'double', + jsxQuoteStyle: 'double', + // trailing_comma: 'all', + trailingComma: 'es5', + semicolons: 'always', + arrowParentheses: 'always', + bracketSameLine: false, + bracketSpacing: true, + enabled: true, + }, + }, +} + +/** + * @url https://github.com/biomejs/biome + */ +export default createStringTransformationRule({ + name: 'prettier', + transform: async (code) => { + if (!biome) { + biome = await Biome.create({ + distribution: Distribution.NODE, + }) + biome.applyConfiguration(formatConfig) + } + + return biome.formatContent(code, { filePath: 'example.js' }).content + }, +}) diff --git a/packages/unminify/src/transformations/index.ts b/packages/unminify/src/transformations/index.ts index afafc131..d21dd651 100644 --- a/packages/unminify/src/transformations/index.ts +++ b/packages/unminify/src/transformations/index.ts @@ -1,6 +1,6 @@ +import biome from './biome' import lebab from './lebab' import moduleMapping from './module-mapping' -import prettier from './prettier' import smartInline from './smart-inline' import smartRename from './smart-rename' import unArgumentSpread from './un-argument-spread' @@ -40,7 +40,7 @@ import type { TransformationRule } from '@wakaru/shared/rule' export const transformationRules: TransformationRule[] = [ // first stage - basically prettify the code - prettier.withId('prettier'), + biome, moduleMapping, unCurlyBraces, // add curly braces so that other transformations can works easier, but generally this is not required unSequenceExpression, // curly braces can bring out return sequence expression, so it runs before this @@ -88,5 +88,5 @@ export const transformationRules: TransformationRule[] = [ unAsyncAwait, // last stage - prettify the code again after we finish all the transformations - prettier.withId('prettier-1'), + biome, ] diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 932b02a5..30856ff8 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -437,6 +437,12 @@ importers: specifier: ^3.22.4 version: 3.22.4 devDependencies: + '@biomejs/js-api': + specifier: ^0.3.0-nightly.efc084c + version: 0.3.0-nightly.efc084c(@biomejs/wasm-nodejs@1.5.1) + '@biomejs/wasm-nodejs': + specifier: ^1.5.1 + version: 1.5.1 '@types/jscodeshift': specifier: ^0.11.11 version: 0.11.11 @@ -1734,6 +1740,27 @@ packages: resolution: {integrity: sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw==} dev: true + /@biomejs/js-api@0.3.0-nightly.efc084c(@biomejs/wasm-nodejs@1.5.1): + resolution: {integrity: sha512-7ja2221rghMjcr2Z6cxrGms52ymeUMdsM6iaOFi6MtKC9YS91rL4z4LB3CJzY7ilju6Wvmn2cCDWBmcf6+qsVA==} + peerDependencies: + '@biomejs/wasm-bundler': ^10.0.0 + '@biomejs/wasm-nodejs': ^10.0.0 + '@biomejs/wasm-web': ^10.0.0 + peerDependenciesMeta: + '@biomejs/wasm-bundler': + optional: true + '@biomejs/wasm-nodejs': + optional: true + '@biomejs/wasm-web': + optional: true + dependencies: + '@biomejs/wasm-nodejs': 1.5.1 + dev: true + + /@biomejs/wasm-nodejs@1.5.1: + resolution: {integrity: sha512-TtmOzCwGZqw2zuWZjAm0NqfSuZRgk10rNKkDshCitgk7paxD4+jKBsGEmJxBnnFg9hVZ8+G5KvjHm2EHzF913A==} + dev: true + /@clack/core@0.3.3(patch_hash=azvwdefg3jpdu6aakjan7setyi): resolution: {integrity: sha512-5ZGyb75BUBjlll6eOa1m/IZBxwk91dooBWhPSL67sWcLS0zt9SnswRL0l26TVdBhb0wnWORRxUn//uH6n4z7+A==} dependencies: