From 30ecdd88e901b1683e989bc87365c319b905ad02 Mon Sep 17 00:00:00 2001 From: Dan Bucholtz Date: Wed, 12 Apr 2017 15:10:28 -0500 Subject: [PATCH] fix(uglifyjs): only minify files processed by webpack or rollup only minify files processed by webpack or rollup --- src/rollup.ts | 3 +++ src/transpile.ts | 2 +- src/uglifyjs.spec.ts | 42 +++++++++++++++++++++++------------------- src/uglifyjs.ts | 28 +++++++++++----------------- src/util/interfaces.ts | 1 + src/webpack.ts | 1 + src/worker-client.ts | 1 + 7 files changed, 41 insertions(+), 37 deletions(-) diff --git a/src/rollup.ts b/src/rollup.ts index a9772775..af503b40 100644 --- a/src/rollup.ts +++ b/src/rollup.ts @@ -94,11 +94,14 @@ export function rollupWorker(context: BuildContext, configFile: string): Promise const promises: Promise[] = []; promises.push(writeFileAsync(rollupConfig.dest, bundleOutput.code)); context.fileCache.set(rollupConfig.dest, { path: rollupConfig.dest, content: bundleOutput.code}); + const filePaths = [rollupConfig.dest]; if (bundleOutput.map) { const sourceMapContent = bundleOutput.map.toString(); promises.push(writeFileAsync(rollupConfig.dest + '.map', sourceMapContent)); context.fileCache.set(rollupConfig.dest + '.map', { path: rollupConfig.dest + '.map', content: sourceMapContent}); + filePaths.push(rollupConfig.dest + '.map'); } + context.bundledFilePaths = filePaths; return Promise.all(promises); }) .then(() => { diff --git a/src/transpile.ts b/src/transpile.ts index 496b5a27..576143a9 100644 --- a/src/transpile.ts +++ b/src/transpile.ts @@ -8,7 +8,7 @@ import { EventEmitter } from 'events'; import { fork, ChildProcess } from 'child_process'; import { inlineTemplate } from './template'; import { Logger } from './logger/logger'; -import { readFileSync, writeFileSync, readdirSync } from 'fs'; +import { readFileSync } from 'fs'; import { runTypeScriptDiagnostics } from './logger/logger-typescript'; import { printDiagnostics, clearDiagnostics, DiagnosticsType } from './logger/logger-diagnostics'; import * as path from 'path'; diff --git a/src/uglifyjs.spec.ts b/src/uglifyjs.spec.ts index 73374f6c..1ea1675a 100644 --- a/src/uglifyjs.spec.ts +++ b/src/uglifyjs.spec.ts @@ -1,4 +1,3 @@ -import * as fs from 'fs'; import { join } from 'path'; import * as uglifyLib from 'uglify-js'; @@ -11,10 +10,16 @@ describe('uglifyjs', () => { describe('uglifyjsWorkerImpl', () => { it('should call uglify for the appropriate files', () => { const buildDir = join('some', 'fake', 'dir', 'myApp', 'www', 'build'); + const pathOne = join(buildDir, '0.main.js'); + const pathOneMap = pathOne + '.map'; + const pathTwo = join(buildDir, '1.main.js'); + const pathTwoMap = pathTwo + '.map'; + const pathThree = join(buildDir, 'main.js'); + const pathThreeMap = pathThree + '.map'; const context = { - buildDir: buildDir + buildDir: buildDir, + bundledFilePaths: [pathOne, pathOneMap, pathTwo, pathTwoMap, pathThree, pathThreeMap] }; - const fileNames = ['polyfills.js', 'sw-toolbox.js', '0.main.js', '0.main.js.map', '1.main.js', '1.main.js.map', 'main.js', 'main.js.map']; const mockMinfiedResponse = { code: 'code', map: 'map' @@ -24,7 +29,6 @@ describe('uglifyjs', () => { compress: true }; - spyOn(fs, 'readdirSync').and.returnValue(fileNames); const uglifySpy = spyOn(uglifyLib, 'minify').and.returnValue(mockMinfiedResponse); const writeFileSpy = spyOn(helpers, helpers.writeFileAsync.name).and.returnValue(Promise.resolve()); @@ -32,38 +36,38 @@ describe('uglifyjs', () => { return promise.then(() => { expect(uglifyLib.minify).toHaveBeenCalledTimes(3); - expect(uglifySpy.calls.all()[0].args[0]).toEqual(join(buildDir, '0.main.js')); + expect(uglifySpy.calls.all()[0].args[0]).toEqual(pathOne); expect(uglifySpy.calls.all()[0].args[1].compress).toEqual(true); expect(uglifySpy.calls.all()[0].args[1].mangle).toEqual(true); - expect(uglifySpy.calls.all()[0].args[1].inSourceMap).toEqual(join(buildDir, '0.main.js.map')); - expect(uglifySpy.calls.all()[0].args[1].outSourceMap).toEqual(join(buildDir, '0.main.js.map')); + expect(uglifySpy.calls.all()[0].args[1].inSourceMap).toEqual(pathOneMap); + expect(uglifySpy.calls.all()[0].args[1].outSourceMap).toEqual(pathOneMap); - expect(uglifySpy.calls.all()[1].args[0]).toEqual(join(buildDir, '1.main.js')); + expect(uglifySpy.calls.all()[1].args[0]).toEqual(pathTwo); expect(uglifySpy.calls.all()[1].args[1].compress).toEqual(true); expect(uglifySpy.calls.all()[1].args[1].mangle).toEqual(true); - expect(uglifySpy.calls.all()[1].args[1].inSourceMap).toEqual(join(buildDir, '1.main.js.map')); - expect(uglifySpy.calls.all()[1].args[1].outSourceMap).toEqual(join(buildDir, '1.main.js.map')); + expect(uglifySpy.calls.all()[1].args[1].inSourceMap).toEqual(pathTwoMap); + expect(uglifySpy.calls.all()[1].args[1].outSourceMap).toEqual(pathTwoMap); - expect(uglifySpy.calls.all()[2].args[0]).toEqual(join(buildDir, 'main.js')); + expect(uglifySpy.calls.all()[2].args[0]).toEqual(pathThree); expect(uglifySpy.calls.all()[2].args[1].compress).toEqual(true); expect(uglifySpy.calls.all()[2].args[1].mangle).toEqual(true); - expect(uglifySpy.calls.all()[2].args[1].inSourceMap).toEqual(join(buildDir, 'main.js.map')); - expect(uglifySpy.calls.all()[2].args[1].outSourceMap).toEqual(join(buildDir, 'main.js.map')); + expect(uglifySpy.calls.all()[2].args[1].inSourceMap).toEqual(pathThreeMap); + expect(uglifySpy.calls.all()[2].args[1].outSourceMap).toEqual(pathThreeMap); expect(writeFileSpy).toHaveBeenCalledTimes(6); - expect(writeFileSpy.calls.all()[0].args[0]).toEqual(join(buildDir, '0.main.js')); + expect(writeFileSpy.calls.all()[0].args[0]).toEqual(pathOne); expect(writeFileSpy.calls.all()[0].args[1]).toEqual(mockMinfiedResponse.code); - expect(writeFileSpy.calls.all()[1].args[0]).toEqual(join(buildDir, '0.main.js.map')); + expect(writeFileSpy.calls.all()[1].args[0]).toEqual(pathOneMap); expect(writeFileSpy.calls.all()[1].args[1]).toEqual(mockMinfiedResponse.map); - expect(writeFileSpy.calls.all()[2].args[0]).toEqual(join(buildDir, '1.main.js')); + expect(writeFileSpy.calls.all()[2].args[0]).toEqual(pathTwo); expect(writeFileSpy.calls.all()[2].args[1]).toEqual(mockMinfiedResponse.code); - expect(writeFileSpy.calls.all()[3].args[0]).toEqual(join(buildDir, '1.main.js.map')); + expect(writeFileSpy.calls.all()[3].args[0]).toEqual(pathTwoMap); expect(writeFileSpy.calls.all()[3].args[1]).toEqual(mockMinfiedResponse.map); - expect(writeFileSpy.calls.all()[4].args[0]).toEqual(join(buildDir, 'main.js')); + expect(writeFileSpy.calls.all()[4].args[0]).toEqual(pathThree); expect(writeFileSpy.calls.all()[4].args[1]).toEqual(mockMinfiedResponse.code); - expect(writeFileSpy.calls.all()[5].args[0]).toEqual(join(buildDir, 'main.js.map')); + expect(writeFileSpy.calls.all()[5].args[0]).toEqual(pathThreeMap); expect(writeFileSpy.calls.all()[5].args[1]).toEqual(mockMinfiedResponse.map); }); }); diff --git a/src/uglifyjs.ts b/src/uglifyjs.ts index cdf192ed..e33f1f88 100644 --- a/src/uglifyjs.ts +++ b/src/uglifyjs.ts @@ -1,6 +1,3 @@ -import { readdirSync } from 'fs'; -import { extname, join } from 'path'; - import * as uglify from 'uglify-js'; import { Logger } from './logger/logger'; @@ -36,23 +33,20 @@ export function uglifyjsWorker(context: BuildContext, configFile: string): Promi export function uglifyjsWorkerImpl(context: BuildContext, uglifyJsConfig: UglifyJsConfig) { return Promise.resolve().then(() => { - // provide a full path for the config options - const files = readdirSync(context.buildDir); + const jsFilePaths = context.bundledFilePaths.filter(bundledFilePath => bundledFilePath.endsWith('.js')); const promises: Promise[] = []; - for (const file of files) { - if (extname(file) === '.js' && file.indexOf('polyfills') === -1 && file.indexOf('sw-toolbox') === -1 && file.indexOf('.map') === -1) { - uglifyJsConfig.sourceFile = join(context.buildDir, file); - uglifyJsConfig.inSourceMap = join(context.buildDir, file + '.map'); - uglifyJsConfig.destFileName = join(context.buildDir, file); - uglifyJsConfig.outSourceMap = join(context.buildDir, file + '.map'); - - const minifyOutput: uglify.MinifyOutput = runUglifyInternal(uglifyJsConfig); - - promises.push(writeFileAsync(uglifyJsConfig.destFileName, minifyOutput.code.toString())); + jsFilePaths.forEach(bundleFilePath => { + uglifyJsConfig.sourceFile = bundleFilePath; + uglifyJsConfig.inSourceMap = bundleFilePath + '.map'; + uglifyJsConfig.destFileName = bundleFilePath; + uglifyJsConfig.outSourceMap = bundleFilePath + '.map'; + + const minifyOutput: uglify.MinifyOutput = runUglifyInternal(uglifyJsConfig); + promises.push(writeFileAsync(uglifyJsConfig.destFileName, minifyOutput.code.toString())); + if (minifyOutput.map) { promises.push(writeFileAsync(uglifyJsConfig.outSourceMap, minifyOutput.map.toString())); } - } - + }); return Promise.all(promises); }).catch((err: any) => { // uglify has it's own strange error format diff --git a/src/util/interfaces.ts b/src/util/interfaces.ts index 4a016ebc..b97f2b06 100644 --- a/src/util/interfaces.ts +++ b/src/util/interfaces.ts @@ -21,6 +21,7 @@ export interface BuildContext { outputCssFileName?: string; nodeModulesDir?: string; ionicAngularDir?: string; + bundledFilePaths?: string[]; moduleFiles?: string[]; appNgModulePath?: string; isProd?: boolean; diff --git a/src/webpack.ts b/src/webpack.ts index 7b947f71..9261c5cf 100644 --- a/src/webpack.ts +++ b/src/webpack.ts @@ -115,6 +115,7 @@ export function writeBundleFilesToDisk(context: BuildContext) { const bundledFilesToWrite = context.fileCache.getAll().filter(file => { return dirname(file.path) === context.buildDir && (file.path.endsWith('.js') || file.path.endsWith('.js.map')); }); + context.bundledFilePaths = bundledFilesToWrite.map(bundledFile => bundledFile.path); const promises = bundledFilesToWrite.map(bundledFileToWrite => writeFileAsync(bundledFileToWrite.path, bundledFileToWrite.content)); return Promise.all(promises); } diff --git a/src/worker-client.ts b/src/worker-client.ts index ea6b3463..d5809540 100644 --- a/src/worker-client.ts +++ b/src/worker-client.ts @@ -21,6 +21,7 @@ export function runWorker(taskModule: string, taskWorker: string, context: Build wwwDir: context.wwwDir, wwwIndex: context.wwwIndex, buildDir: context.buildDir, + bundledFilePaths: context.bundledFilePaths, isProd: context.isProd, isWatch: context.isWatch, runAot: context.runAot,