1- import * as uglify from 'uglify-js' ;
1+ import * as Uglify from 'uglify-js' ;
22
33import { Logger } from './logger/logger' ;
44import { fillConfigDefaults , generateContext , getUserConfigFile } from './util/config' ;
55import { BuildError } from './util/errors' ;
6- import { writeFileAsync } from './util/helpers' ;
6+ import { readFileAsync , writeFileAsync } from './util/helpers' ;
77import { BuildContext , TaskInfo } from './util/interfaces' ;
88import { runWorker } from './worker-client' ;
99
@@ -31,37 +31,34 @@ export function uglifyjsWorker(context: BuildContext, configFile: string): Promi
3131 return uglifyjsWorkerImpl ( context , uglifyJsConfig ) ;
3232}
3333
34- export function uglifyjsWorkerImpl ( context : BuildContext , uglifyJsConfig : UglifyJsConfig ) {
35- return Promise . resolve ( ) . then ( ( ) => {
34+ export async function uglifyjsWorkerImpl ( context : BuildContext , uglifyJsConfig : UglifyJsConfig ) {
35+ try {
3636 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 ) ;
4940 } ) ;
50- return Promise . all ( promises ) ;
51- } ) . catch ( ( err : any ) => {
41+ return await Promise . all ( promises ) ;
42+ } catch ( ex ) {
5243 // 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 } ` ;
5445 throw new BuildError ( new Error ( errorString ) ) ;
55- } ) ;
46+ }
5647}
5748
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+ }
6456 } ) ;
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 ) ] ) ;
6562}
6663
6764export const taskInfo : TaskInfo = {
@@ -83,3 +80,8 @@ export interface UglifyJsConfig {
8380 compress ?: boolean ;
8481 comments ?: boolean ;
8582}
83+
84+ export interface UglifyResponse {
85+ code ?: string ;
86+ map ?: any ;
87+ }
0 commit comments