From e5821d6abef0242448290c690a2b6801f801bd88 Mon Sep 17 00:00:00 2001 From: Pionxzh Date: Sun, 14 Jan 2024 03:37:32 +0800 Subject: [PATCH] feat: biome --- .vscode/settings.json | 1 + 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 +++++++++++ 6 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 packages/unminify/src/transformations/biome.ts diff --git a/.vscode/settings.json b/.vscode/settings.json index f8c0cf8a..dbcb2ada 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -22,6 +22,7 @@ "cSpell.words": [ "arraify", "atou", + "biomejs", "browserfs", "credentialless", "debundle", 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..3bff8e5a 100644 --- a/packages/unminify/package.json +++ b/packages/unminify/package.json @@ -35,6 +35,8 @@ "zod": "^3.22.4" }, "devDependencies": { + "@biomejs/js-api": "^0.5.0", + "@biomejs/wasm-nodejs": "^1.6.3", "@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..2d88894c 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.5.0 + version: 0.5.0(@biomejs/wasm-nodejs@1.6.3) + '@biomejs/wasm-nodejs': + specifier: ^1.6.3 + version: 1.6.3 '@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.5.0(@biomejs/wasm-nodejs@1.6.3): + resolution: {integrity: sha512-H1G29hR7Y1fzVwqIC+7O8kyr/CWa2WNSHEKcAyVX8ams8UCJZyWxIqX50ytzY1BeKnXL5CedULmrXXptP8BZrQ==} + peerDependencies: + '@biomejs/wasm-bundler': ^1.6.1 + '@biomejs/wasm-nodejs': ^1.6.1 + '@biomejs/wasm-web': ^1.6.1 + peerDependenciesMeta: + '@biomejs/wasm-bundler': + optional: true + '@biomejs/wasm-nodejs': + optional: true + '@biomejs/wasm-web': + optional: true + dependencies: + '@biomejs/wasm-nodejs': 1.6.3 + dev: true + + /@biomejs/wasm-nodejs@1.6.3: + resolution: {integrity: sha512-QJLAPspAF30Z/tOH/dRW0oKcUHYxIz0eqNM8VxXMKcdyMXPHyEw9PrmPd/aOo32w1wKzCbNutTzfj/Ie07M5yw==} + dev: true + /@clack/core@0.3.3(patch_hash=azvwdefg3jpdu6aakjan7setyi): resolution: {integrity: sha512-5ZGyb75BUBjlll6eOa1m/IZBxwk91dooBWhPSL67sWcLS0zt9SnswRL0l26TVdBhb0wnWORRxUn//uH6n4z7+A==} dependencies: