diff --git a/package.json b/package.json index 4f01286064..b6d7d5c042 100644 --- a/package.json +++ b/package.json @@ -122,7 +122,7 @@ "tslint": "3.15.1", "tslint-ionic-rules": "0.0.7", "typescript": "2.0.9", - "uglify": "0.1.5", + "uglify-js": "2.7.5", "vinyl": "1.2.0", "webpack": "2.1.0-beta.26", "yargs": "5.0.0" diff --git a/scripts/gulp/declarations.d.ts b/scripts/gulp/declarations.d.ts index d73cc38eff..32ab9ceca7 100644 --- a/scripts/gulp/declarations.d.ts +++ b/scripts/gulp/declarations.d.ts @@ -22,6 +22,7 @@ declare module 'rollup-plugin-node-resolve'; declare module 'rollup-plugin-uglify'; declare module 'through2'; declare module 'semver'; +declare module 'uglify-js'; declare module 'vinyl'; declare module 'yargs'; declare module 'strip-function'; \ No newline at end of file diff --git a/scripts/gulp/util.ts b/scripts/gulp/util.ts index 9bd5e6ac05..bd79bfc4d3 100644 --- a/scripts/gulp/util.ts +++ b/scripts/gulp/util.ts @@ -7,8 +7,9 @@ import { Replacer } from 'strip-function'; import * as commonjs from 'rollup-plugin-commonjs'; import * as multiEntry from 'rollup-plugin-multi-entry'; import * as nodeResolve from 'rollup-plugin-node-resolve'; -import * as uglify from 'rollup-plugin-uglify'; import * as through from 'through2'; +import * as uglifyJS from 'uglify-js'; +import * as uglifyPlugin from 'rollup-plugin-uglify'; export function mergeObjects(obj1: any, obj2: any ) { if (! obj1) { @@ -60,7 +61,44 @@ function removeDebugStatements() { file.contents = new Buffer(cleanedJs, 'utf8'); callback(null, file); }); -}; +} + +function minifyInlineStrings() { + // used to manually minify the inline web workers + // which are strings of code and not actual code + + const start = '/** minify-start **/'; + const end = '/** minify-end **/'; + + return through.obj(function (file, encoding, callback) { + let content: string = file.contents.toString(); + + const startIndex = content.indexOf(start); + const endIndex = content.indexOf(end); + + if (startIndex > -1 && endIndex > startIndex) { + let startContent = content.substring(0, startIndex); + let minifyContent = content.substring(startIndex, endIndex + end.length); + let endContent = content.substring(endIndex + end.length); + + minifyContent = uglifyJS.minify(minifyContent, { + fromString: true, + compress: { + dead_code: true, + global_defs: { + DEBUG: false + } + } + }).code; + + content = startContent + minifyContent + endContent; + + file.contents = new Buffer(content, 'utf8'); + } + + callback(null, file); + }); +} export function copySourceToDest(destinationPath: string, excludeSpecs: boolean, excludeE2e: boolean, stripDebug: boolean) { let glob = [`${SRC_ROOT}/**/*.ts`]; @@ -74,9 +112,12 @@ export function copySourceToDest(destinationPath: string, excludeSpecs: boolean, } let stream = src(glob); if (stripDebug) { - console.log('Removing debug statements ', destinationPath); + console.log('Removing debug statements:', destinationPath); stream = stream.pipe(removeDebugStatements()); } + console.log('Minifying inline web-worker strings:', destinationPath); + stream = stream.pipe(minifyInlineStrings()); + return stream.pipe(dest(destinationPath)); } @@ -246,7 +287,7 @@ function bundlePolyfill(pathsToIncludeInPolyfill: string[], outputPath: string) main: true }), commonjs(), - uglify() + uglifyPlugin() ] }).then((bundle) => { return bundle.write({