1- import path from "path" ;
1+ import fs from "node:fs" ;
2+ import path from "node:path" ;
3+
24import type { Options } from "./config" ;
3- import fs from "fs" ;
45import { 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 */
4881export 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+ } ;
0 commit comments