1
- import * as uglify from 'uglify-js' ;
1
+ import * as Uglify from 'uglify-js' ;
2
2
3
3
import { Logger } from './logger/logger' ;
4
4
import { fillConfigDefaults , generateContext , getUserConfigFile } from './util/config' ;
5
5
import { BuildError } from './util/errors' ;
6
- import { writeFileAsync } from './util/helpers' ;
6
+ import { readFileAsync , writeFileAsync } from './util/helpers' ;
7
7
import { BuildContext , TaskInfo } from './util/interfaces' ;
8
8
import { runWorker } from './worker-client' ;
9
9
@@ -31,37 +31,34 @@ export function uglifyjsWorker(context: BuildContext, configFile: string): Promi
31
31
return uglifyjsWorkerImpl ( context , uglifyJsConfig ) ;
32
32
}
33
33
34
- export function uglifyjsWorkerImpl ( context : BuildContext , uglifyJsConfig : UglifyJsConfig ) {
35
- return Promise . resolve ( ) . then ( ( ) => {
34
+ export async function uglifyjsWorkerImpl ( context : BuildContext , uglifyJsConfig : UglifyJsConfig ) {
35
+ try {
36
36
const jsFilePaths = context . bundledFilePaths . filter ( bundledFilePath => bundledFilePath . endsWith ( '.js' ) ) ;
37
- const promises : Promise < any > [ ] = [ ] ;
38
- jsFilePaths . forEach ( bundleFilePath => {
39
- uglifyJsConfig . sourceFile = bundleFilePath ;
40
- uglifyJsConfig . inSourceMap = bundleFilePath + '.map' ;
41
- uglifyJsConfig . destFileName = bundleFilePath ;
42
- uglifyJsConfig . outSourceMap = bundleFilePath + '.map' ;
43
-
44
- const minifyOutput : uglify . MinifyOutput = runUglifyInternal ( uglifyJsConfig ) ;
45
- promises . push ( writeFileAsync ( uglifyJsConfig . destFileName , minifyOutput . code . toString ( ) ) ) ;
46
- if ( minifyOutput . map ) {
47
- promises . push ( writeFileAsync ( uglifyJsConfig . outSourceMap , minifyOutput . map . toString ( ) ) ) ;
48
- }
37
+ const promises = jsFilePaths . map ( filePath => {
38
+ const sourceMapPath = filePath + '.map' ;
39
+ return runUglifyInternal ( filePath , filePath , sourceMapPath , sourceMapPath , uglifyJsConfig ) ;
49
40
} ) ;
50
- return Promise . all ( promises ) ;
51
- } ) . catch ( ( err : any ) => {
41
+ return await Promise . all ( promises ) ;
42
+ } catch ( ex ) {
52
43
// uglify has it's own strange error format
53
- const errorString = `${ err . message } in ${ err . filename } at line ${ err . line } , col ${ err . col } , pos ${ err . pos } ` ;
44
+ const errorString = `${ ex . message } in ${ ex . filename } at line ${ ex . line } , col ${ ex . col } , pos ${ ex . pos } ` ;
54
45
throw new BuildError ( new Error ( errorString ) ) ;
55
- } ) ;
46
+ }
56
47
}
57
48
58
- function runUglifyInternal ( uglifyJsConfig : UglifyJsConfig ) : uglify . MinifyOutput {
59
- return uglify . minify ( uglifyJsConfig . sourceFile , {
60
- compress : uglifyJsConfig . compress ,
61
- mangle : uglifyJsConfig . mangle ,
62
- inSourceMap : uglifyJsConfig . inSourceMap ,
63
- outSourceMap : uglifyJsConfig . outSourceMap
49
+ async function runUglifyInternal ( sourceFilePath : string , destFilePath : string , sourceMapPath : string , destMapPath : string , configObject : any ) : Promise < any > {
50
+ const sourceFileContentPromise = readFileAsync ( sourceFilePath ) ;
51
+ const [ sourceFileContent , sourceMapContent ] = await Promise . all ( [ readFileAsync ( sourceFilePath ) , readFileAsync ( sourceMapPath ) ] ) ;
52
+ const uglifyConfig = Object . assign ( { } , configObject , {
53
+ sourceMap : {
54
+ content : sourceMapContent
55
+ }
64
56
} ) ;
57
+ const result = Uglify . minify ( sourceFileContent , uglifyConfig ) ;
58
+ if ( result . error ) {
59
+ throw new BuildError ( `Uglify failed: ${ result . error . message } ` ) ;
60
+ }
61
+ return Promise . all ( [ writeFileAsync ( destFilePath , result . code ) , writeFileAsync ( destMapPath , result . map ) ] ) ;
65
62
}
66
63
67
64
export const taskInfo : TaskInfo = {
@@ -83,3 +80,8 @@ export interface UglifyJsConfig {
83
80
compress ?: boolean ;
84
81
comments ?: boolean ;
85
82
}
83
+
84
+ export interface UglifyResponse {
85
+ code ?: string ;
86
+ map ?: any ;
87
+ }
0 commit comments