Skip to content

Commit 8cb3b53

Browse files
committed
style: format code and fix the lint error
1 parent 91674de commit 8cb3b53

10 files changed

Lines changed: 168 additions & 101 deletions

File tree

.changeset/config.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
2-
"$schema": "https://unpkg.com/@changesets/config/schema.json",
3-
"changelog": "@changesets/cli/changelog",
4-
"commit": false,
5-
"fixed": [],
6-
"linked": [],
7-
"access": "public",
8-
"baseBranch": "main",
9-
"updateInternalDependencies": "patch",
10-
"ignore": []
2+
"$schema": "https://unpkg.com/@changesets/config/schema.json",
3+
"changelog": "@changesets/cli/changelog",
4+
"commit": false,
5+
"fixed": [],
6+
"linked": [],
7+
"access": "public",
8+
"baseBranch": "main",
9+
"updateInternalDependencies": "patch",
10+
"ignore": []
1111
}

biome.json

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
{
2-
"$schema": "https://biomejs.dev/schemas/2.1.4/schema.json",
3-
"formatter": {
4-
"enabled": true,
5-
"indentStyle": "space",
6-
"indentWidth": 2,
7-
"lineWidth": 80,
8-
"lineEnding": "lf"
9-
},
10-
"linter": {
11-
"enabled": true,
12-
"rules": {
13-
"recommended": true
14-
}
15-
}
16-
2+
"$schema": "https://biomejs.dev/schemas/2.1.4/schema.json",
3+
"formatter": {
4+
"enabled": true,
5+
"indentStyle": "space",
6+
"indentWidth": 2,
7+
"lineWidth": 80,
8+
"lineEnding": "lf"
9+
},
10+
"linter": {
11+
"enabled": true,
12+
"rules": {
13+
"recommended": true
14+
}
15+
}
1716
}

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@
3636
"scripts": {
3737
"build": "node -r rts.js/register scripts/build.ts",
3838
"typecheck": "tsc --noEmit -p tsconfig.json",
39-
"lint": "biome check .",
40-
"format": "biome format --write .",
39+
"lint": "biome check ./src",
40+
"lint:fix": "biome check ./src --write",
41+
"format": "biome format --write ./src",
4142
"version": "pnpm changeset version",
4243
"publish": "pnpm changeset publish"
4344
},

scripts/build.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ assemble({
1717
esm: {
1818
output: "./esm",
1919
},
20-
});
20+
});

src/bin/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env node
22

3-
import { assemble } from "../index";
43
import { program } from "commander";
4+
import { assemble } from "../index";
55

66
program
77
.option("-c, --cjs <output>", "output directory for CommonJS modules")
@@ -12,4 +12,4 @@ program
1212
.parse(process.argv);
1313

1414
const options = program.opts();
15-
assemble(options);
15+
assemble(options);

src/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ export interface Options {
2020
cjs?: OutputOptions | null;
2121
esm?: OutputOptions | null;
2222
target?: string;
23-
}
23+
}

src/index.ts

Lines changed: 78 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
import path from "path";
1+
import fs from "node:fs";
2+
import path from "node:path";
3+
24
import type { Options } from "./config";
3-
import fs from "fs";
45
import { ModuleTransformer } from "./transformer";
56

6-
export { ModuleTransformer, type TransformerHook, type TransformOptions, type TransformProgram } from "./transformer";
7+
export {
8+
ModuleTransformer,
9+
type TransformerHook,
10+
type TransformOptions,
11+
type TransformProgram,
12+
} from "./transformer";
713

814
/**
915
* Central transformer registry for the build pipeline.
@@ -14,30 +20,57 @@ const moduleTransformer = new ModuleTransformer();
1420
/**
1521
* Recursively transform files from `src` into `output` using the configured transformers.
1622
*/
17-
const transform = async ({ src, output, module, target }: { src: string, output: string, module: 'cjs' | 'esm', target: string }) => {
23+
const transform = async ({
24+
src,
25+
output,
26+
module,
27+
target,
28+
}: {
29+
src: string;
30+
output: string;
31+
module: "cjs" | "esm";
32+
target: string;
33+
}) => {
1834
const root = path.resolve(process.cwd(), src);
1935
const dist = path.resolve(process.cwd(), output);
2036

2137
// Read directory entries and process them in parallel
2238
const files = await fs.promises.readdir(root);
23-
await Promise.all(files.map(async (file) => {
24-
const filePath = path.resolve(root, file);
25-
const stat = await fs.promises.stat(filePath);
26-
if (stat.isDirectory()) {
27-
await transform({ src: filePath, output: path.join(dist, file), module, target });
28-
} else {
29-
const basename = path.basename(filePath);
30-
// Transform file content and determine final filename
31-
const { code, map, filename } = await moduleTransformer.transform(await fs.promises.readFile(filePath), basename, { target, module });
32-
if (!fs.existsSync(dist)) {
33-
await fs.promises.mkdir(dist, { recursive: true });
34-
}
35-
await fs.promises.writeFile(path.join(dist, filename ?? basename), code);
36-
if (map) {
37-
await fs.promises.writeFile(path.join(dist, (filename ?? basename) + '.map'), map);
39+
await Promise.all(
40+
files.map(async (file) => {
41+
const filePath = path.resolve(root, file);
42+
const stat = await fs.promises.stat(filePath);
43+
if (stat.isDirectory()) {
44+
await transform({
45+
src: filePath,
46+
output: path.join(dist, file),
47+
module,
48+
target,
49+
});
50+
} else {
51+
const basename = path.basename(filePath);
52+
// Transform file content and determine final filename
53+
const { code, map, filename } = await moduleTransformer.transform(
54+
await fs.promises.readFile(filePath),
55+
basename,
56+
{ target, module },
57+
);
58+
if (!fs.existsSync(dist)) {
59+
await fs.promises.mkdir(dist, { recursive: true });
60+
}
61+
await fs.promises.writeFile(
62+
path.join(dist, filename ?? basename),
63+
code,
64+
);
65+
if (map) {
66+
await fs.promises.writeFile(
67+
path.join(dist, `${filename ?? basename}.map`),
68+
map,
69+
);
70+
}
3871
}
39-
}
40-
}));
72+
}),
73+
);
4174
};
4275
/**
4376
* Assemble the project to the desired module formats.
@@ -47,20 +80,40 @@ const transform = async ({ src, output, module, target }: { src: string, output:
4780
*/
4881
export const assemble = async (options: Options = {}) => {
4982
const timer = new Date();
50-
const { src = "./src", output = "./dist", cjs = { output: "./cjs" }, esm = { output: "./esm" }, target = "es2020" } = options;
83+
const {
84+
src = "./src",
85+
output = "./dist",
86+
cjs = { output: "./cjs" },
87+
esm = { output: "./esm" },
88+
target = "es2020",
89+
} = options;
5190
const root = path.resolve(process.cwd(), src);
5291
const dist = path.resolve(process.cwd(), output);
5392

5493
const tasks: Promise<void>[] = [];
5594
if (cjs) {
5695
fs.rmSync(path.join(dist, cjs.output), { recursive: true, force: true });
57-
tasks.push(transform({ src: root, output: path.join(dist, cjs.output), module: 'cjs', target }));
96+
tasks.push(
97+
transform({
98+
src: root,
99+
output: path.join(dist, cjs.output),
100+
module: "cjs",
101+
target,
102+
}),
103+
);
58104
}
59105
if (esm) {
60106
fs.rmSync(path.join(dist, esm.output), { recursive: true, force: true });
61-
tasks.push(transform({ src: root, output: path.join(dist, esm.output), module: 'esm', target }));
107+
tasks.push(
108+
transform({
109+
src: root,
110+
output: path.join(dist, esm.output),
111+
module: "esm",
112+
target,
113+
}),
114+
);
62115
}
63116

64117
await Promise.all(tasks);
65118
console.log(`assemble success in ${Date.now() - timer.getTime()}ms`);
66-
};
119+
};

src/transformer/index.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import path from "node:path";
22
import { tsTransformer } from "./ts-transformer";
33

4-
5-
64
/**
75
* A transformer declaration that describes which extensions it handles
86
* and how to transform code synchronously or asynchronously.
@@ -19,7 +17,9 @@ export interface TransformerHook {
1917
/**
2018
* Optional async transform; if provided, it will be preferred.
2119
*/
22-
transform?: (...args: Parameters<TransformProgram>) => Promise<ReturnType<TransformProgram>>;
20+
transform?: (
21+
...args: Parameters<TransformProgram>
22+
) => Promise<ReturnType<TransformProgram>>;
2323
}
2424

2525
/**
@@ -33,20 +33,22 @@ export interface TransformOptions {
3333
/**
3434
* The transform program signature.
3535
*/
36-
export type TransformProgram = (code: Buffer, options: TransformOptions) => { code: Buffer | string, map?: string, };
37-
38-
39-
36+
export type TransformProgram = (
37+
code: Buffer,
38+
options: TransformOptions,
39+
) => { code: Buffer | string; map?: string };
4040

4141
/**
4242
* Registry and dispatcher for file transformers.
4343
*/
4444
export class ModuleTransformer {
45-
46-
private transformers: Map<string, {
47-
ext: [string, string];
48-
hooks: TransformerHook;
49-
}> = new Map();
45+
private transformers: Map<
46+
string,
47+
{
48+
ext: [string, string];
49+
hooks: TransformerHook;
50+
}
51+
> = new Map();
5052

5153
constructor(transformers: TransformerHook[] = []) {
5254
this.addTransformer(tsTransformer as TransformerHook);
@@ -55,8 +57,6 @@ export class ModuleTransformer {
5557
}
5658
}
5759

58-
59-
6060
/**
6161
* Register a transformer and its handled extensions.
6262
*/
@@ -85,11 +85,14 @@ export class ModuleTransformer {
8585
}
8686
}
8787

88-
8988
/**
9089
* Transform a single file buffer based on its extension.
9190
*/
92-
async transform(code: Buffer, filename: string, options: TransformOptions): Promise<{ code: Buffer, map?: string, filename: string }> {
91+
async transform(
92+
code: Buffer,
93+
filename: string,
94+
options: TransformOptions,
95+
): Promise<{ code: Buffer; map?: string; filename: string }> {
9396
const transformers = this.transformers.get(path.extname(filename));
9497

9598
if (transformers) {
@@ -108,7 +111,11 @@ export class ModuleTransformer {
108111
/**
109112
* Synchronous variant of transform. Keeps the same API shape.
110113
*/
111-
async transformSync(code: Buffer, filename: string, options: TransformOptions) {
114+
async transformSync(
115+
code: Buffer,
116+
filename: string,
117+
options: TransformOptions,
118+
) {
112119
const transformers = this.transformers.get(path.extname(filename));
113120
if (transformers) {
114121
filename = filename.replace(transformers.ext[0], transformers.ext[1]);
@@ -123,6 +130,4 @@ export class ModuleTransformer {
123130
}
124131
return { code, filename };
125132
}
126-
127-
128133
}

src/transformer/ts-transformer.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
import {
2+
type JscTarget,
3+
type Options,
4+
transform,
5+
transformSync,
6+
} from "@swc/core";
17
import type { TransformerHook, TransformOptions } from ".";
2-
import { type JscTarget, transform, transformSync, type Options, } from "@swc/core";
3-
48

59
/**
610
* Base SWC configuration for TS/TSX/JS/JSX files.
@@ -39,31 +43,36 @@ const SwcConfig: Options = {
3943
* SWC-powered transformer handling TS/TSX/JS/JSX to JS with source maps.
4044
*/
4145
export const tsTransformer: TransformerHook = {
42-
exts: [[".ts", ".js"], [".tsx", '.js'], ['.js', '.js'], [".jsx", '.js']],
46+
exts: [
47+
[".ts", ".js"],
48+
[".tsx", ".js"],
49+
[".js", ".js"],
50+
[".jsx", ".js"],
51+
],
4352
transformSync: (code: Buffer, options: TransformOptions) => {
44-
const result = transformSync(code.toString('utf-8'), {
53+
const result = transformSync(code.toString("utf-8"), {
4554
...SwcConfig,
4655
jsc: {
4756
...SwcConfig.jsc,
4857
target: options.target as JscTarget,
4958
},
5059
module: {
51-
type: options.module === 'esm' ? 'es6' : 'commonjs',
60+
type: options.module === "esm" ? "es6" : "commonjs",
5261
},
5362
});
54-
return { code: Buffer.from(result.code), map: result.map, }
63+
return { code: Buffer.from(result.code), map: result.map };
5564
},
5665
transform: async (code: Buffer, options: TransformOptions) => {
57-
const result = await transform(code.toString('utf-8'), {
66+
const result = await transform(code.toString("utf-8"), {
5867
...SwcConfig,
5968
jsc: {
6069
...SwcConfig.jsc,
6170
target: options.target as JscTarget,
6271
},
6372
module: {
64-
type: options.module === 'esm' ? 'es6' : 'commonjs',
73+
type: options.module === "esm" ? "es6" : "commonjs",
6574
},
6675
});
67-
return { code: Buffer.from(result.code), map: result.map, }
76+
return { code: Buffer.from(result.code), map: result.map };
6877
},
69-
};
78+
};

0 commit comments

Comments
 (0)