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

Commit 30ecdd8

Browse files
committed
fix(uglifyjs): only minify files processed by webpack or rollup
only minify files processed by webpack or rollup
1 parent 759bb4f commit 30ecdd8

File tree

7 files changed

+41
-37
lines changed

7 files changed

+41
-37
lines changed

src/rollup.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,14 @@ export function rollupWorker(context: BuildContext, configFile: string): Promise
9494
const promises: Promise<any>[] = [];
9595
promises.push(writeFileAsync(rollupConfig.dest, bundleOutput.code));
9696
context.fileCache.set(rollupConfig.dest, { path: rollupConfig.dest, content: bundleOutput.code});
97+
const filePaths = [rollupConfig.dest];
9798
if (bundleOutput.map) {
9899
const sourceMapContent = bundleOutput.map.toString();
99100
promises.push(writeFileAsync(rollupConfig.dest + '.map', sourceMapContent));
100101
context.fileCache.set(rollupConfig.dest + '.map', { path: rollupConfig.dest + '.map', content: sourceMapContent});
102+
filePaths.push(rollupConfig.dest + '.map');
101103
}
104+
context.bundledFilePaths = filePaths;
102105
return Promise.all(promises);
103106
})
104107
.then(() => {

src/transpile.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { EventEmitter } from 'events';
88
import { fork, ChildProcess } from 'child_process';
99
import { inlineTemplate } from './template';
1010
import { Logger } from './logger/logger';
11-
import { readFileSync, writeFileSync, readdirSync } from 'fs';
11+
import { readFileSync } from 'fs';
1212
import { runTypeScriptDiagnostics } from './logger/logger-typescript';
1313
import { printDiagnostics, clearDiagnostics, DiagnosticsType } from './logger/logger-diagnostics';
1414
import * as path from 'path';

src/uglifyjs.spec.ts

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as fs from 'fs';
21
import { join } from 'path';
32

43
import * as uglifyLib from 'uglify-js';
@@ -11,10 +10,16 @@ describe('uglifyjs', () => {
1110
describe('uglifyjsWorkerImpl', () => {
1211
it('should call uglify for the appropriate files', () => {
1312
const buildDir = join('some', 'fake', 'dir', 'myApp', 'www', 'build');
13+
const pathOne = join(buildDir, '0.main.js');
14+
const pathOneMap = pathOne + '.map';
15+
const pathTwo = join(buildDir, '1.main.js');
16+
const pathTwoMap = pathTwo + '.map';
17+
const pathThree = join(buildDir, 'main.js');
18+
const pathThreeMap = pathThree + '.map';
1419
const context = {
15-
buildDir: buildDir
20+
buildDir: buildDir,
21+
bundledFilePaths: [pathOne, pathOneMap, pathTwo, pathTwoMap, pathThree, pathThreeMap]
1622
};
17-
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'];
1823
const mockMinfiedResponse = {
1924
code: 'code',
2025
map: 'map'
@@ -24,46 +29,45 @@ describe('uglifyjs', () => {
2429
compress: true
2530
};
2631

27-
spyOn(fs, 'readdirSync').and.returnValue(fileNames);
2832
const uglifySpy = spyOn(uglifyLib, 'minify').and.returnValue(mockMinfiedResponse);
2933
const writeFileSpy = spyOn(helpers, helpers.writeFileAsync.name).and.returnValue(Promise.resolve());
3034

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

3337
return promise.then(() => {
3438
expect(uglifyLib.minify).toHaveBeenCalledTimes(3);
35-
expect(uglifySpy.calls.all()[0].args[0]).toEqual(join(buildDir, '0.main.js'));
39+
expect(uglifySpy.calls.all()[0].args[0]).toEqual(pathOne);
3640
expect(uglifySpy.calls.all()[0].args[1].compress).toEqual(true);
3741
expect(uglifySpy.calls.all()[0].args[1].mangle).toEqual(true);
38-
expect(uglifySpy.calls.all()[0].args[1].inSourceMap).toEqual(join(buildDir, '0.main.js.map'));
39-
expect(uglifySpy.calls.all()[0].args[1].outSourceMap).toEqual(join(buildDir, '0.main.js.map'));
42+
expect(uglifySpy.calls.all()[0].args[1].inSourceMap).toEqual(pathOneMap);
43+
expect(uglifySpy.calls.all()[0].args[1].outSourceMap).toEqual(pathOneMap);
4044

41-
expect(uglifySpy.calls.all()[1].args[0]).toEqual(join(buildDir, '1.main.js'));
45+
expect(uglifySpy.calls.all()[1].args[0]).toEqual(pathTwo);
4246
expect(uglifySpy.calls.all()[1].args[1].compress).toEqual(true);
4347
expect(uglifySpy.calls.all()[1].args[1].mangle).toEqual(true);
44-
expect(uglifySpy.calls.all()[1].args[1].inSourceMap).toEqual(join(buildDir, '1.main.js.map'));
45-
expect(uglifySpy.calls.all()[1].args[1].outSourceMap).toEqual(join(buildDir, '1.main.js.map'));
48+
expect(uglifySpy.calls.all()[1].args[1].inSourceMap).toEqual(pathTwoMap);
49+
expect(uglifySpy.calls.all()[1].args[1].outSourceMap).toEqual(pathTwoMap);
4650

47-
expect(uglifySpy.calls.all()[2].args[0]).toEqual(join(buildDir, 'main.js'));
51+
expect(uglifySpy.calls.all()[2].args[0]).toEqual(pathThree);
4852
expect(uglifySpy.calls.all()[2].args[1].compress).toEqual(true);
4953
expect(uglifySpy.calls.all()[2].args[1].mangle).toEqual(true);
50-
expect(uglifySpy.calls.all()[2].args[1].inSourceMap).toEqual(join(buildDir, 'main.js.map'));
51-
expect(uglifySpy.calls.all()[2].args[1].outSourceMap).toEqual(join(buildDir, 'main.js.map'));
54+
expect(uglifySpy.calls.all()[2].args[1].inSourceMap).toEqual(pathThreeMap);
55+
expect(uglifySpy.calls.all()[2].args[1].outSourceMap).toEqual(pathThreeMap);
5256

5357
expect(writeFileSpy).toHaveBeenCalledTimes(6);
54-
expect(writeFileSpy.calls.all()[0].args[0]).toEqual(join(buildDir, '0.main.js'));
58+
expect(writeFileSpy.calls.all()[0].args[0]).toEqual(pathOne);
5559
expect(writeFileSpy.calls.all()[0].args[1]).toEqual(mockMinfiedResponse.code);
56-
expect(writeFileSpy.calls.all()[1].args[0]).toEqual(join(buildDir, '0.main.js.map'));
60+
expect(writeFileSpy.calls.all()[1].args[0]).toEqual(pathOneMap);
5761
expect(writeFileSpy.calls.all()[1].args[1]).toEqual(mockMinfiedResponse.map);
5862

59-
expect(writeFileSpy.calls.all()[2].args[0]).toEqual(join(buildDir, '1.main.js'));
63+
expect(writeFileSpy.calls.all()[2].args[0]).toEqual(pathTwo);
6064
expect(writeFileSpy.calls.all()[2].args[1]).toEqual(mockMinfiedResponse.code);
61-
expect(writeFileSpy.calls.all()[3].args[0]).toEqual(join(buildDir, '1.main.js.map'));
65+
expect(writeFileSpy.calls.all()[3].args[0]).toEqual(pathTwoMap);
6266
expect(writeFileSpy.calls.all()[3].args[1]).toEqual(mockMinfiedResponse.map);
6367

64-
expect(writeFileSpy.calls.all()[4].args[0]).toEqual(join(buildDir, 'main.js'));
68+
expect(writeFileSpy.calls.all()[4].args[0]).toEqual(pathThree);
6569
expect(writeFileSpy.calls.all()[4].args[1]).toEqual(mockMinfiedResponse.code);
66-
expect(writeFileSpy.calls.all()[5].args[0]).toEqual(join(buildDir, 'main.js.map'));
70+
expect(writeFileSpy.calls.all()[5].args[0]).toEqual(pathThreeMap);
6771
expect(writeFileSpy.calls.all()[5].args[1]).toEqual(mockMinfiedResponse.map);
6872
});
6973
});

src/uglifyjs.ts

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import { readdirSync } from 'fs';
2-
import { extname, join } from 'path';
3-
41
import * as uglify from 'uglify-js';
52

63
import { Logger } from './logger/logger';
@@ -36,23 +33,20 @@ export function uglifyjsWorker(context: BuildContext, configFile: string): Promi
3633

3734
export function uglifyjsWorkerImpl(context: BuildContext, uglifyJsConfig: UglifyJsConfig) {
3835
return Promise.resolve().then(() => {
39-
// provide a full path for the config options
40-
const files = readdirSync(context.buildDir);
36+
const jsFilePaths = context.bundledFilePaths.filter(bundledFilePath => bundledFilePath.endsWith('.js'));
4137
const promises: Promise<any>[] = [];
42-
for (const file of files) {
43-
if (extname(file) === '.js' && file.indexOf('polyfills') === -1 && file.indexOf('sw-toolbox') === -1 && file.indexOf('.map') === -1) {
44-
uglifyJsConfig.sourceFile = join(context.buildDir, file);
45-
uglifyJsConfig.inSourceMap = join(context.buildDir, file + '.map');
46-
uglifyJsConfig.destFileName = join(context.buildDir, file);
47-
uglifyJsConfig.outSourceMap = join(context.buildDir, file + '.map');
48-
49-
const minifyOutput: uglify.MinifyOutput = runUglifyInternal(uglifyJsConfig);
50-
51-
promises.push(writeFileAsync(uglifyJsConfig.destFileName, minifyOutput.code.toString()));
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) {
5247
promises.push(writeFileAsync(uglifyJsConfig.outSourceMap, minifyOutput.map.toString()));
5348
}
54-
}
55-
49+
});
5650
return Promise.all(promises);
5751
}).catch((err: any) => {
5852
// uglify has it's own strange error format

src/util/interfaces.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export interface BuildContext {
2121
outputCssFileName?: string;
2222
nodeModulesDir?: string;
2323
ionicAngularDir?: string;
24+
bundledFilePaths?: string[];
2425
moduleFiles?: string[];
2526
appNgModulePath?: string;
2627
isProd?: boolean;

src/webpack.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ export function writeBundleFilesToDisk(context: BuildContext) {
115115
const bundledFilesToWrite = context.fileCache.getAll().filter(file => {
116116
return dirname(file.path) === context.buildDir && (file.path.endsWith('.js') || file.path.endsWith('.js.map'));
117117
});
118+
context.bundledFilePaths = bundledFilesToWrite.map(bundledFile => bundledFile.path);
118119
const promises = bundledFilesToWrite.map(bundledFileToWrite => writeFileAsync(bundledFileToWrite.path, bundledFileToWrite.content));
119120
return Promise.all(promises);
120121
}

src/worker-client.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export function runWorker(taskModule: string, taskWorker: string, context: Build
2121
wwwDir: context.wwwDir,
2222
wwwIndex: context.wwwIndex,
2323
buildDir: context.buildDir,
24+
bundledFilePaths: context.bundledFilePaths,
2425
isProd: context.isProd,
2526
isWatch: context.isWatch,
2627
runAot: context.runAot,

0 commit comments

Comments
 (0)