1
- import { bundle } from ' @swc/core' ;
2
- import { mkdir , writeFile } from 'fs' ;
3
- import { basename , dirname , extname , join , relative } from ' path'
4
- import { promisify } from ' util' ;
1
+ import { bundle } from " @swc/core" ;
2
+ import { mkdir , writeFile } from "fs" ;
3
+ import { basename , dirname , extname , join , relative } from " path" ;
4
+ import { promisify } from " util" ;
5
5
6
- import parseSpackArgs from ' ./options' ;
6
+ import parseSpackArgs from " ./options" ;
7
7
8
8
const write = promisify ( writeFile ) ;
9
9
const makeDir = promisify ( mkdir ) ;
10
10
11
-
12
11
( async ( ) => {
13
- const { spackOptions } = await parseSpackArgs ( process . argv ) ;
12
+ const { spackOptions } = await parseSpackArgs ( process . argv ) ;
14
13
15
- function isUserDefinedEntry ( name : string ) {
16
- if ( typeof spackOptions . entry === 'string' ) {
17
- return spackOptions . entry === name
18
- }
19
- if ( Array . isArray ( spackOptions . entry ) ) {
20
- for ( const e of spackOptions . entry ) {
21
- if ( e === name ) {
22
- return true ;
23
- }
24
- }
25
- return false ;
14
+ function isUserDefinedEntry ( name : string ) {
15
+ if ( typeof spackOptions . entry === "string" ) {
16
+ return spackOptions . entry === name ;
17
+ }
18
+ if ( Array . isArray ( spackOptions . entry ) ) {
19
+ for ( const e of spackOptions . entry ) {
20
+ if ( e === name ) {
21
+ return true ;
26
22
}
27
-
28
- return name in spackOptions . entry ;
23
+ }
24
+ return false ;
29
25
}
30
26
27
+ return name in spackOptions . entry ;
28
+ }
31
29
32
- async function build ( ) {
33
- const bundleStart = process . hrtime ( ) ;
34
- const output = await bundle ( spackOptions ) ;
35
- const bundleEnd = process . hrtime ( bundleStart ) ;
36
- console . info ( `Bundling done: ${ bundleEnd [ 0 ] } s ${ bundleEnd [ 1 ] / 1000000 } ms` ) ;
37
-
38
- const emitStart = process . hrtime ( ) ;
39
- if ( spackOptions . output ?. path ) {
40
- await Object . keys ( output ) . map ( async ( name ) => {
41
- let fullPath = '' ;
42
- if ( isUserDefinedEntry ( name ) ) {
43
- fullPath = join ( spackOptions . output . path , spackOptions . output . name . replace ( '[name]' , name ) ) ;
44
- } else {
45
- const ext = extname ( name ) ;
46
- const base = basename ( name , ext ) ;
47
- const filename = relative ( process . cwd ( ) , name ) ;
48
- fullPath = join ( spackOptions . output . path , dirname ( filename ) , `${ base } .js` )
49
- }
30
+ async function build ( ) {
31
+ const bundleStart = process . hrtime ( ) ;
32
+ const output = await bundle ( spackOptions ) ;
33
+ const bundleEnd = process . hrtime ( bundleStart ) ;
34
+ console . info ( `Bundling done: ${ bundleEnd [ 0 ] } s ${ bundleEnd [ 1 ] / 1000000 } ms` ) ;
50
35
51
- await makeDir ( dirname ( fullPath ) , { recursive : true } ) ;
52
- await write ( fullPath , output [ name ] . code , 'utf-8' ) ;
53
- if ( output [ name ] . map ) {
54
- await write ( `${ fullPath } .map` , output [ name ] . map , 'utf-8' )
55
- }
56
- } ) ;
36
+ const emitStart = process . hrtime ( ) ;
37
+ if ( spackOptions . output ?. path ) {
38
+ await Object . keys ( output ) . map ( async name => {
39
+ let fullPath = "" ;
40
+ if ( isUserDefinedEntry ( name ) ) {
41
+ fullPath = join (
42
+ spackOptions . output . path ,
43
+ spackOptions . output . name . replace ( "[name]" , name )
44
+ ) ;
57
45
} else {
58
- throw new Error ( 'Cannot print to stdout: not implemented yet' )
46
+ const ext = extname ( name ) ;
47
+ const base = basename ( name , ext ) ;
48
+ const filename = relative ( process . cwd ( ) , name ) ;
49
+ fullPath = join (
50
+ spackOptions . output . path ,
51
+ dirname ( filename ) ,
52
+ `${ base } .js`
53
+ ) ;
59
54
}
60
- const emitEnd = process . hrtime ( emitStart ) ;
61
- console . info ( `Done: ${ emitEnd [ 0 ] } s ${ emitEnd [ 1 ] / 1000000 } ms` ) ;
62
55
56
+ await makeDir ( dirname ( fullPath ) , { recursive : true } ) ;
57
+ await write ( fullPath , output [ name ] . code , "utf-8" ) ;
58
+ if ( output [ name ] . map ) {
59
+ await write ( `${ fullPath } .map` , output [ name ] . map , "utf-8" ) ;
60
+ }
61
+ } ) ;
62
+ } else {
63
+ throw new Error ( "Cannot print to stdout: not implemented yet" ) ;
63
64
}
65
+ const emitEnd = process . hrtime ( emitStart ) ;
66
+ console . info ( `Done: ${ emitEnd [ 0 ] } s ${ emitEnd [ 1 ] / 1000000 } ms` ) ;
67
+ }
64
68
65
- // if (cliOptions.watch) {
66
- // throw new Error('watch is not implemented yet')
67
- // }
69
+ // if (cliOptions.watch) {
70
+ // throw new Error('watch is not implemented yet')
71
+ // }
68
72
69
- await build ( ) ;
70
- } ) ( )
73
+ await build ( ) ;
74
+ } ) ( ) ;
0 commit comments