@@ -3,6 +3,7 @@ import path from 'node:path';
3
3
import { Log , __dirname } from '@tsbb/typescript' ;
4
4
import { TransformOptions } from '@babel/core' ;
5
5
import babelPluginJsx from '@vue/babel-plugin-jsx' ;
6
+ // import { TransformOptions } from '@babel/core';
6
7
import { transform } from './transform.js' ;
7
8
import { getOutputPath } from './utils.js' ;
8
9
import { getCjsTransformOption , getESMTransformOption } from './config.js' ;
@@ -89,34 +90,7 @@ export default async function compile(fileName: string, options: BabelCompileOpt
89
90
esmBabelOptions . cwd = dt . projectDirectory ;
90
91
91
92
if ( typeof esm === 'string' ) {
92
- esmBabelOptions . sourceFileName = path . relative ( path . dirname ( dt . esm . path ) , fileName ) ;
93
- transform ( fileName , { ...esmBabelOptions } )
94
- . then ( ( result ) => {
95
- fs . ensureFileSync ( dt . esm . path ) ;
96
- fs . writeFile ( dt . esm . path , result ?. code || '' ) ;
97
- log . icon ( '🐶' ) . success ( `┈┈▶ \x1b[32;1m${ dt . folderFilePath } \x1b[0m => \x1b[34;1m${ dt . esm . fileName } \x1b[0m` ) ;
98
- if ( esmBabelOptions . sourceMaps === 'both' || esmBabelOptions . sourceMaps ) {
99
- if ( result ?. map ) {
100
- const sourceMapPath = path . join ( dt . esm . path + '.map' ) ;
101
- fs . writeFileSync ( sourceMapPath , JSON . stringify ( result ?. map , null , 2 ) ) ;
102
- log
103
- . icon ( '🐶' )
104
- . success (
105
- `┈┈▶ \x1b[32;1m${ dt . folderFilePath } \x1b[0m => \x1b[34;1m${ path . relative (
106
- dt . projectDirectory ,
107
- sourceMapPath ,
108
- ) } \x1b[0m`,
109
- ) ;
110
- }
111
- }
112
- } )
113
- . catch ( ( error ) => {
114
- if ( error instanceof Error ) {
115
- log . icon ( '\n🚨' ) . error ( `\x1b[33;1m ${ error . message } \x1b[0m\n` ) ;
116
- } else {
117
- log . icon ( '\n🚨' ) . error ( `\x1b[33;1m ${ JSON . stringify ( error ) } \x1b[0m\n` ) ;
118
- }
119
- } ) ;
93
+ transformFile ( fileName , dt . esm . path , dt . folderFilePath , dt . projectDirectory , dt . esm . fileName , esmBabelOptions ) ;
120
94
}
121
95
122
96
let cjsBabelOptions = getCjsTransformOption ( ) ;
@@ -133,33 +107,46 @@ export default async function compile(fileName: string, options: BabelCompileOpt
133
107
cjsBabelOptions . cwd = dt . projectDirectory ;
134
108
135
109
if ( typeof cjs === 'string' ) {
136
- cjsBabelOptions . sourceFileName = path . relative ( path . dirname ( dt . cjs . path ) , fileName ) ;
137
- transform ( fileName , { ...cjsBabelOptions } )
138
- . then ( ( result ) => {
139
- fs . ensureFileSync ( dt . cjs . path ) ;
140
- fs . writeFile ( dt . cjs . path , result ?. code || '' ) ;
141
- log . icon ( '🐶' ) . success ( `┈┈▶ \x1b[33;1m${ dt . folderFilePath } \x1b[0m => \x1b[33;1m${ dt . cjs . fileName } \x1b[0m` ) ;
142
- if ( cjsBabelOptions . sourceMaps === 'both' || cjsBabelOptions . sourceMaps ) {
143
- if ( result ?. map ) {
144
- const sourceMapPath = path . join ( dt . cjs . path + '.map' ) ;
145
- fs . writeFileSync ( sourceMapPath , JSON . stringify ( result ?. map , null , 2 ) ) ;
146
- log
147
- . icon ( '🐶' )
148
- . success (
149
- `┈┈▶ \x1b[33;1m${ dt . folderFilePath } \x1b[0m => \x1b[33;1m${ path . relative (
150
- dt . projectDirectory ,
151
- sourceMapPath ,
152
- ) } \x1b[0m`,
153
- ) ;
154
- }
155
- }
156
- } )
157
- . catch ( ( error ) => {
158
- if ( error instanceof Error ) {
159
- log . icon ( '\n🚨' ) . error ( `\x1b[33;1m ${ error . message } \x1b[0m\n` ) ;
160
- } else {
161
- log . icon ( '\n🚨' ) . error ( `\x1b[33;1m ${ JSON . stringify ( error ) } \x1b[0m\n` ) ;
162
- }
163
- } ) ;
110
+ transformFile ( fileName , dt . cjs . path , dt . folderFilePath , dt . projectDirectory , dt . cjs . fileName , cjsBabelOptions ) ;
164
111
}
165
112
}
113
+
114
+ function transformFile (
115
+ fileName : string ,
116
+ outputFile : string ,
117
+ folderFilePath : string ,
118
+ projectDirectory : string ,
119
+ outFileName : string ,
120
+ options : TransformOptions ,
121
+ ) {
122
+ const log = new Log ( ) ;
123
+ log . name ( ) ;
124
+ options . sourceFileName = path . relative ( path . dirname ( outputFile ) , fileName ) ;
125
+ transform ( fileName , { ...options } )
126
+ . then ( ( result ) => {
127
+ fs . ensureFileSync ( outputFile ) ;
128
+ fs . writeFile ( outputFile , result ?. code || '' ) ;
129
+ log . icon ( '🐶' ) . success ( `┈┈▶ \x1b[33;1m${ folderFilePath } \x1b[0m => \x1b[33;1m${ outFileName } \x1b[0m` ) ;
130
+ if ( options . sourceMaps === 'both' || options . sourceMaps ) {
131
+ if ( result ?. map ) {
132
+ const sourceMapPath = path . join ( outputFile + '.map' ) ;
133
+ fs . writeFileSync ( sourceMapPath , JSON . stringify ( result ?. map , null , 2 ) ) ;
134
+ log
135
+ . icon ( '🐶' )
136
+ . success (
137
+ `┈┈▶ \x1b[33;1m${ folderFilePath } \x1b[0m => \x1b[33;1m${ path . relative (
138
+ projectDirectory ,
139
+ sourceMapPath ,
140
+ ) } \x1b[0m`,
141
+ ) ;
142
+ }
143
+ }
144
+ } )
145
+ . catch ( ( error ) => {
146
+ if ( error instanceof Error ) {
147
+ log . icon ( '\n🚨' ) . error ( `\x1b[33;1m ${ error . message } \x1b[0m\n` ) ;
148
+ } else {
149
+ log . icon ( '\n🚨' ) . error ( `\x1b[33;1m ${ JSON . stringify ( error ) } \x1b[0m\n` ) ;
150
+ }
151
+ } ) ;
152
+ }
0 commit comments