1
- import slash from ' slash' ;
1
+ import slash from " slash" ;
2
2
import { promises } from "fs" ;
3
3
import { dirname , relative } from "path" ;
4
4
import { transformFile , transformFileSync } from "@swc/core" ;
5
5
import type { Options , Output } from "@swc/core" ;
6
6
7
- const {
8
- mkdir,
9
- stat,
10
- writeFile
11
- } = promises ;
7
+ const { mkdir, stat, writeFile } = promises ;
12
8
13
- function withSourceMap ( output : Output , options : Options , destFile : string , destDir : string ) {
9
+ function withSourceMap (
10
+ output : Output ,
11
+ options : Options ,
12
+ destFile : string ,
13
+ destDir : string
14
+ ) {
14
15
if ( ! output . map || options . sourceMaps === "inline" ) {
15
16
return {
16
17
sourceCode : output . code ,
17
- }
18
+ } ;
18
19
}
19
20
// TODO: remove once fixed in core https://github.com/swc-project/swc/issues/1388
20
21
const sourceMap = JSON . parse ( output . map ) ;
21
22
if ( options . sourceFileName ) {
22
- sourceMap [ ' sources' ] [ 0 ] = options . sourceFileName ;
23
+ sourceMap [ " sources" ] [ 0 ] = options . sourceFileName ;
23
24
}
24
25
if ( options . sourceRoot ) {
25
- sourceMap [ ' sourceRoot' ] = options . sourceRoot ;
26
+ sourceMap [ " sourceRoot" ] = options . sourceRoot ;
26
27
}
27
28
output . map = JSON . stringify ( sourceMap ) ;
28
29
29
30
const sourceMapPath = destFile + ".map" ;
30
- output . code += `\n//# sourceMappingURL=${ slash ( relative ( destDir , sourceMapPath ) ) } ` ;
31
+ output . code += `\n//# sourceMappingURL=${ slash (
32
+ relative ( destDir , sourceMapPath )
33
+ ) } `;
31
34
32
35
return {
33
36
sourceMap : output . map ,
34
37
sourceMapPath,
35
- sourceCode : output . code
36
- }
38
+ sourceCode : output . code ,
39
+ } ;
37
40
}
38
41
39
42
export async function outputResult (
@@ -44,11 +47,12 @@ export async function outputResult(
44
47
) {
45
48
const destDir = dirname ( destFile ) ;
46
49
47
- const {
48
- sourceMap,
49
- sourceMapPath,
50
- sourceCode
51
- } = withSourceMap ( output , options , destFile , destDir ) ;
50
+ const { sourceMap, sourceMapPath, sourceCode } = withSourceMap (
51
+ output ,
52
+ options ,
53
+ destFile ,
54
+ destDir
55
+ ) ;
52
56
53
57
await mkdir ( destDir , { recursive : true } ) ;
54
58
const { mode } = await stat ( sourceFile ) ;
@@ -58,7 +62,7 @@ export async function outputResult(
58
62
} else {
59
63
await Promise . all ( [
60
64
writeFile ( destFile , sourceCode , { mode } ) ,
61
- writeFile ( sourceMapPath , sourceMap , { mode } )
65
+ writeFile ( sourceMapPath , sourceMap , { mode } ) ,
62
66
] ) ;
63
67
}
64
68
}
@@ -80,7 +84,7 @@ export async function compile(
80
84
: await transformFile ( filename , options ) ;
81
85
82
86
return result ;
83
- } catch ( err ) {
87
+ } catch ( err : any ) {
84
88
if ( ! err . message . includes ( "ignored by .swcrc" ) ) {
85
89
throw err ;
86
90
}
0 commit comments