Skip to content

Commit

Permalink
feat: biome
Browse files Browse the repository at this point in the history
  • Loading branch information
pionxzh committed Jan 14, 2024
1 parent bd02e79 commit 17315a5
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 3 deletions.
3 changes: 3 additions & 0 deletions packages/cli/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export default defineConfig({
'process.env.NODE_DEBUG': 'undefined',
},
// minify: true,
external: [
'@biomejs/js-api',
],
noExternal: [
'jscodeshift',
'ast-types',
Expand Down
2 changes: 2 additions & 0 deletions packages/unminify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
45 changes: 45 additions & 0 deletions packages/unminify/src/transformations/biome.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Biome, Distribution } from '@biomejs/js-api'
import { createStringTransformationRule } from '@wakaru/shared/rule'

type FormatConfiguration = Parameters<Biome['applyConfiguration']>[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
},
})

Check warning on line 45 in packages/unminify/src/transformations/biome.ts

View check run for this annotation

Codecov / codecov/patch

packages/unminify/src/transformations/biome.ts#L1-L45

Added lines #L1 - L45 were not covered by tests
6 changes: 3 additions & 3 deletions packages/unminify/src/transformations/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import biome from './biome'

Check warning on line 1 in packages/unminify/src/transformations/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/unminify/src/transformations/index.ts#L1

Added line #L1 was not covered by tests
import lebab from './lebab'
import moduleMapping from './module-mapping'
import prettier from './prettier'
import smartInline from './smart-inline'
import smartRename from './smart-rename'
import unAssignmentMerging from './un-assignment-merging'
Expand Down Expand Up @@ -38,7 +38,7 @@ import type { TransformationRule } from '@wakaru/shared/rule'

export const transformationRules: TransformationRule[] = [
// first stage - basically prettify the code
prettier.withId('prettier'),
biome,

Check warning on line 41 in packages/unminify/src/transformations/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/unminify/src/transformations/index.ts#L41

Added line #L41 was not covered by tests
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
Expand Down Expand Up @@ -84,5 +84,5 @@ export const transformationRules: TransformationRule[] = [
unAsyncAwait,

// last stage - prettify the code again after we finish all the transformations
prettier.withId('prettier-1'),
biome,

Check warning on line 87 in packages/unminify/src/transformations/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/unminify/src/transformations/index.ts#L87

Added line #L87 was not covered by tests
]
27 changes: 27 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 17315a5

Please sign in to comment.