@@ -2,6 +2,7 @@ import * as esbuild from 'esbuild'
2
2
import Path from 'node:path' ;
3
3
import { createRequire } from 'node:module' ;
4
4
import { existsSync , readFileSync } from 'node:fs' ;
5
+ import { fileURLToPath } from 'node:url' ;
5
6
6
7
import semver from 'semver' ;
7
8
@@ -14,47 +15,59 @@ import swcPlugin from './buildLib/esbuild-plugin-swc.mjs';
14
15
15
16
function getScriptPath ( ) {
16
17
try {
17
- return import . meta. url ;
18
+ return fileURLToPath ( import . meta. url ) ;
18
19
} catch ( e ) {
19
20
return __filename || null ;
20
21
}
21
22
}
22
23
23
24
function getScriptDirectory ( ) {
24
25
try {
25
- return Path . dirname ( import . meta. url ) ;
26
+ return Path . dirname ( fileURLToPath ( import . meta. url ) ) ;
26
27
} catch ( e ) {
27
28
return __dirname || null ;
28
29
}
29
30
}
30
31
31
32
const resolveFileFromPackage = ( function ( ) {
32
- return ( ( new Function ( "try{return import.meta.resolve;}catch(e){return null}" ) ) ( ) || createRequire ( getScriptPath ( ) ) . resolve ) ;
33
+ const rawResolve = ( ( function ( ) { try { return import . meta. resolve ; } catch ( e ) { return null } } ) ( ) || createRequire ( getScriptPath ( ) ) . resolve ) ;
34
+ return function ( fileToResolve ) {
35
+ const result = rawResolve ( fileToResolve ) ;
36
+ if ( result . startsWith ( "file:" ) ) {
37
+ return fileURLToPath ( result ) ;
38
+ }
39
+ return result ;
40
+ } ;
33
41
} ) ( ) ;
34
42
35
- function readKeyFromPackageJson ( key , pkg ) {
36
- let resolvedPath ;
37
- if ( typeof pkg == "undefined" || pkg == null ) {
38
- let currentDir = getScriptDirectory ;
39
- let relativePath = "package.json" ;
40
- resolvedPath = Path . resolve ( currentDir , relativePath ) ;
43
+ function getOwnPackageJsonPath ( ) {
44
+ let currentDir = getScriptDirectory ( ) ;
45
+ let relativePath = "package.json" ;
46
+ let resolvedPath = Path . resolve ( currentDir , relativePath ) ;
41
47
42
- function countSeparators ( str ) {
43
- let count = 0 ;
44
- for ( let i = 0 ; i < str . length ; i ++ ) {
45
- if ( str [ i ] == Path . sep )
46
- count ++ ;
47
- }
48
- return count ;
48
+ function countSeparators ( str ) {
49
+ let count = 0 ;
50
+ for ( let i = 0 ; i < str . length ; i ++ ) {
51
+ if ( str [ i ] == Path . sep )
52
+ count ++ ;
49
53
}
54
+ return count ;
55
+ }
50
56
51
- while ( ! existsSync ( resolvedPath ) ) {
52
- if ( countSeparators ( resolvedPath ) <= 1 ) {
53
- throw new Error ( "Could not find package.json file" ) ;
54
- }
55
- relativePath = Path . join ( ".." , relativePath ) ;
56
- resolvedPath = Path . resolve ( currentDir , relativePath ) ;
57
+ while ( ! existsSync ( resolvedPath ) ) {
58
+ if ( countSeparators ( resolvedPath ) <= 1 ) {
59
+ throw new Error ( "Could not find package.json file" ) ;
57
60
}
61
+ relativePath = Path . join ( ".." , relativePath ) ;
62
+ resolvedPath = Path . resolve ( currentDir , relativePath ) ;
63
+ }
64
+ return resolvedPath ;
65
+ }
66
+
67
+ function readKeyFromPackageJson ( key , pkg ) {
68
+ let resolvedPath ;
69
+ if ( typeof pkg == "undefined" || pkg == null ) {
70
+ resolvedPath = getOwnPackageJsonPath ( ) ;
58
71
} else {
59
72
resolvedPath = resolveFileFromPackage ( Path . join ( pkg , "package.json" ) ) ;
60
73
if ( ! resolvedPath || ! existsSync ( resolvedPath ) ) {
@@ -233,19 +246,36 @@ async function build(params) {
233
246
if ( ! semver . satisfies ( installedCoreJsVersion , readKeyFromPackageJson ( "devDependencies.core-js" ) ) ) {
234
247
throw new Error ( "Please update your dependencies, the core-js version installed does not match the version specified in the package.json file." ) ;
235
248
}
249
+
250
+ const swcTargets = {
251
+ "es2015" : {
252
+ "chrome" : "49" ,
253
+ "firefox" : "44" ,
254
+ "safari" : "10" ,
255
+ "edge" : "13" ,
256
+ "opera" : "36" ,
257
+ "node" : "13.2.0"
258
+ } ,
259
+ "compat" : compatTargets
260
+ } ;
261
+
236
262
swcConfig . env = {
237
263
"bugfixes" : true ,
238
264
"mode" : "usage" ,
239
265
"coreJs" : installedCoreJsVersion
240
266
} ;
267
+ swcConfig . env . targets = swcTargets [ params . options . target ?? 'es2015' ] || swcTargets [ "es2015" ] ;
241
268
if ( params . options . target == "compat" ) {
242
- swcConfig . env . targets = compatTargets ;
243
269
swcConfig . jsc . minify . compress . ecma = 3 ;
244
270
esbuildTarget = convertToEsbuildTarget ( compatTargets ) ;
245
271
//esbuild does not officialy support es3 but outputs es3 compatable code when you specifiy es5 as the target.
246
272
esbuildTarget . push ( "es5" ) ;
247
273
} else {
248
- swcConfig . jsc . target = params . options . target ?? 'es2015' ;
274
+ const ecmaVersion = parseInt ( ( params . options . target ?? 'es2015' ) . slice ( 2 ) ) ;
275
+ if ( isNaN ( ecmaVersion ) ) {
276
+ ecmaVersion = 2015 ;
277
+ }
278
+ swcConfig . jsc . minify . compress . ecma = ecmaVersion ;
249
279
esbuildTarget = params . options . target ?? 'es2015' ;
250
280
}
251
281
}
0 commit comments