Skip to content
This repository was archived by the owner on May 1, 2020. It is now read-only.

Commit

Permalink
fix(uglifyjs): only minify files processed by webpack or rollup
Browse files Browse the repository at this point in the history
only minify files processed by webpack or rollup
  • Loading branch information
danbucholtz committed Apr 12, 2017
1 parent 759bb4f commit 30ecdd8
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 37 deletions.
3 changes: 3 additions & 0 deletions src/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,14 @@ export function rollupWorker(context: BuildContext, configFile: string): Promise
const promises: Promise<any>[] = [];
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(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/transpile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
42 changes: 23 additions & 19 deletions src/uglifyjs.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as fs from 'fs';
import { join } from 'path';

import * as uglifyLib from 'uglify-js';
Expand All @@ -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'
Expand All @@ -24,46 +29,45 @@ 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());

const promise = uglifyTask.uglifyjsWorkerImpl(context, mockUglifyConfig);

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);
});
});
Expand Down
28 changes: 11 additions & 17 deletions src/uglifyjs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { readdirSync } from 'fs';
import { extname, join } from 'path';

import * as uglify from 'uglify-js';

import { Logger } from './logger/logger';
Expand Down Expand Up @@ -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<any>[] = [];
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
Expand Down
1 change: 1 addition & 0 deletions src/util/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface BuildContext {
outputCssFileName?: string;
nodeModulesDir?: string;
ionicAngularDir?: string;
bundledFilePaths?: string[];
moduleFiles?: string[];
appNgModulePath?: string;
isProd?: boolean;
Expand Down
1 change: 1 addition & 0 deletions src/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions src/worker-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 30ecdd8

Please sign in to comment.