@@ -2,6 +2,7 @@ import * as esbuild from 'esbuild'
22import Path from 'node:path' ;
33import { createRequire } from 'node:module' ;
44import { existsSync , readFileSync } from 'node:fs' ;
5+ import { fileURLToPath } from 'node:url' ;
56
67import semver from 'semver' ;
78
@@ -14,47 +15,59 @@ import swcPlugin from './buildLib/esbuild-plugin-swc.mjs';
1415
1516function getScriptPath ( ) {
1617 try {
17- return import . meta. url ;
18+ return fileURLToPath ( import . meta. url ) ;
1819 } catch ( e ) {
1920 return __filename || null ;
2021 }
2122}
2223
2324function getScriptDirectory ( ) {
2425 try {
25- return Path . dirname ( import . meta. url ) ;
26+ return Path . dirname ( fileURLToPath ( import . meta. url ) ) ;
2627 } catch ( e ) {
2728 return __dirname || null ;
2829 }
2930}
3031
3132const 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+ } ;
3341} ) ( ) ;
3442
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 ) ;
4147
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 ++ ;
4953 }
54+ return count ;
55+ }
5056
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" ) ;
5760 }
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 ( ) ;
5871 } else {
5972 resolvedPath = resolveFileFromPackage ( Path . join ( pkg , "package.json" ) ) ;
6073 if ( ! resolvedPath || ! existsSync ( resolvedPath ) ) {
@@ -233,19 +246,37 @@ async function build(params) {
233246 if ( ! semver . satisfies ( installedCoreJsVersion , readKeyFromPackageJson ( "devDependencies.core-js" ) ) ) {
234247 throw new Error ( "Please update your dependencies, the core-js version installed does not match the version specified in the package.json file." ) ;
235248 }
249+
250+ const swcTargets = {
251+ "es2015" : {
252+ "chrome" : "49" ,
253+ "firefox" : "44" ,
254+ "safari" : "10" ,
255+ "edge" : "13" ,
256+ "opera" : "36" ,
257+ "android" : "125" ,
258+ "ios" : "10"
259+ } ,
260+ "compat" : compatTargets
261+ } ;
262+
236263 swcConfig . env = {
237264 "bugfixes" : true ,
238265 "mode" : "usage" ,
239266 "coreJs" : installedCoreJsVersion
240267 } ;
268+ swcConfig . env . targets = swcTargets [ params . options . target ?? 'es2015' ] || swcTargets [ "es2015" ] ;
241269 if ( params . options . target == "compat" ) {
242- swcConfig . env . targets = compatTargets ;
243270 swcConfig . jsc . minify . compress . ecma = 3 ;
244271 esbuildTarget = convertToEsbuildTarget ( compatTargets ) ;
245272 //esbuild does not officialy support es3 but outputs es3 compatable code when you specifiy es5 as the target.
246273 esbuildTarget . push ( "es5" ) ;
247274 } else {
248- swcConfig . jsc . target = params . options . target ?? 'es2015' ;
275+ const ecmaVersion = parseInt ( ( params . options . target ?? 'es2015' ) . slice ( 2 ) ) ;
276+ if ( isNaN ( ecmaVersion ) ) {
277+ ecmaVersion = 2015 ;
278+ }
279+ swcConfig . jsc . minify . compress . ecma = ecmaVersion ;
249280 esbuildTarget = params . options . target ?? 'es2015' ;
250281 }
251282 }
0 commit comments