Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(install time): Remove dependency to zopfli. #3414

Merged
merged 2 commits into from
Dec 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@
"@angular/core": "2.2.3",
"@angular/tsc-wrapped": "0.4.0",
"angular2-template-loader": "^0.5.0",
"async": "^2.1.4",
"autoprefixer": "^6.5.3",
"chalk": "^1.1.3",
"common-tags": "^1.3.1",
"compression-webpack-plugin": "^0.3.2",
"configstore": "^2.0.0",
"core-js": "^2.4.0",
"css-loader": "^0.23.1",
Expand Down
112 changes: 112 additions & 0 deletions packages/angular-cli/lib/webpack/compression-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/** Forked from https://github.com/webpack/compression-webpack-plugin. */
const async = require('async');
const url = require('url');

const RawSource = require('webpack-sources/lib/RawSource');


export interface CompressionPluginOptions {
algorithm?: string;
asset?: string;
level?: number;
flush?: boolean;
chunkSize?: number;
test?: RegExp | RegExp[];
windowBits?: number;
memLevel?: number;
strategy?: number;
dictionary?: any;
threshold?: number;
minRatio?: number;
}


export class CompressionPlugin {
private asset = '[path].gz[query]';
private algorithm: Function;
private compressionOptions: any = {};
private test: RegExp[];
private threshold: number = 0;
private minRatio: number = 0.8;

constructor(options: CompressionPluginOptions = {}) {
if (options.hasOwnProperty('asset')) {
this.asset = options.asset;
}

const algorithm = options.hasOwnProperty('algorithm') ? options.algorithm : 'gzip';

const zlib = require('zlib');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll need to add this to our package.json

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same with async and url.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only async is needed. The others are part of node itself.


this.compressionOptions = {};
this.algorithm = zlib[algorithm];
if (!this.algorithm) {
throw new Error(`Algorithm not found in zlib: "${algorithm}".`);
}

this.compressionOptions = {
level: options.level || 9,
flush: options.flush,
chunkSize: options.chunkSize,
windowBits: options.windowBits,
memLevel: options.memLevel,
strategy: options.strategy,
dictionary: options.dictionary
};

if (options.hasOwnProperty('test')) {
if (Array.isArray(options.test)) {
this.test = options.test as RegExp[];
} else {
this.test = [options.test as RegExp];
}
}
if (options.hasOwnProperty('threshold')) {
this.threshold = options.threshold;
}
if (options.hasOwnProperty('minRatio')) {
this.minRatio = options.minRatio;
}
}

apply(compiler: any) {
compiler.plugin('this-compilation', (compilation: any) => {
compilation.plugin('optimize-assets', (assets: any, callback: Function) => {
async.forEach(Object.keys(assets), (file: string, callback: Function) => {
if (this.test.every((t) => !t.test(file))) {
return callback();
}

const asset = assets[file];
let content = asset.source();
if (!Buffer.isBuffer(content)) {
content = new Buffer(content, 'utf-8');
}

const originalSize = content.length;
if (originalSize < this.threshold) {
return callback();
}

this.algorithm(content, this.compressionOptions, (err: Error, result: string) => {
if (err) {
return callback(err);
}
if (result.length / originalSize > this.minRatio) {
return callback();
}

const parse = url.parse(file);
const newFile = this.asset
.replace(/\[file]/g, file)
.replace(/\[path]/g, parse.pathname)
.replace(/\[query]/g, parse.query || '');

assets[newFile] = new RawSource(result);
callback();
});
}, callback);
});
});
}
}
6 changes: 3 additions & 3 deletions packages/angular-cli/models/webpack-build-production.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as path from 'path';
import {CompressionPlugin} from '../lib/webpack/compression-plugin';

const WebpackMd5Hash = require('webpack-md5-hash');
const CompressionPlugin = require('compression-webpack-plugin');
import * as webpack from 'webpack';
const ExtractTextPlugin = require('extract-text-webpack-plugin');

Expand Down Expand Up @@ -67,8 +68,7 @@ export const getWebpackProdConfigPartial = function(projectRoot: string,
asset: '[path].gz[query]',
algorithm: 'gzip',
test: /\.js$|\.html$|\.css$/,
threshold: 10240,
minRatio: 0.8
threshold: 10240
}),
new webpack.LoaderOptionsPlugin({
options: {
Expand Down
2 changes: 1 addition & 1 deletion packages/angular-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
"@angular/core": "2.2.3",
"@ngtools/webpack": "^1.0.0",
"angular2-template-loader": "^0.5.0",
"async": "^2.1.4",
"chalk": "^1.1.3",
"common-tags": "^1.3.1",
"compression-webpack-plugin": "^0.3.2",
"configstore": "^2.0.0",
"core-js": "^2.4.0",
"css-loader": "^0.23.1",
Expand Down