mirror of
https://github.com/angular/angular-cli.git
synced 2025-05-21 22:34:21 +08:00
refactor(@angular/cli): remove compression plugin (#4702)
Followup from #4618
This commit is contained in:
parent
4543be9e7b
commit
dd5dda64f8
@ -43,7 +43,6 @@
|
|||||||
"@angular/compiler-cli": "^2.3.1",
|
"@angular/compiler-cli": "^2.3.1",
|
||||||
"@angular/core": "^2.3.1",
|
"@angular/core": "^2.3.1",
|
||||||
"@angular/tsc-wrapped": "^0.5.0",
|
"@angular/tsc-wrapped": "^0.5.0",
|
||||||
"async": "^2.1.4",
|
|
||||||
"autoprefixer": "^6.5.3",
|
"autoprefixer": "^6.5.3",
|
||||||
"chalk": "^1.1.3",
|
"chalk": "^1.1.3",
|
||||||
"common-tags": "^1.3.1",
|
"common-tags": "^1.3.1",
|
||||||
@ -103,7 +102,6 @@
|
|||||||
"webpack": "~2.2.0",
|
"webpack": "~2.2.0",
|
||||||
"webpack-dev-server": "~2.2.0",
|
"webpack-dev-server": "~2.2.0",
|
||||||
"webpack-merge": "^2.4.0",
|
"webpack-merge": "^2.4.0",
|
||||||
"webpack-sources": "^0.1.3",
|
|
||||||
"zone.js": "^0.7.2"
|
"zone.js": "^0.7.2"
|
||||||
},
|
},
|
||||||
"ember-addon": {
|
"ember-addon": {
|
||||||
|
@ -1,112 +0,0 @@
|
|||||||
/** 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 = 0;
|
|
||||||
private minRatio = 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');
|
|
||||||
|
|
||||||
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);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
@ -4,7 +4,6 @@ import * as fs from 'fs';
|
|||||||
import { stripIndent } from 'common-tags';
|
import { stripIndent } from 'common-tags';
|
||||||
import { StaticAssetPlugin } from '../../plugins/static-asset';
|
import { StaticAssetPlugin } from '../../plugins/static-asset';
|
||||||
import { GlobCopyWebpackPlugin } from '../../plugins/glob-copy-webpack-plugin';
|
import { GlobCopyWebpackPlugin } from '../../plugins/glob-copy-webpack-plugin';
|
||||||
import { CompressionPlugin } from '../../lib/webpack/compression-plugin';
|
|
||||||
import { WebpackConfigOptions } from '../webpack-config';
|
import { WebpackConfigOptions } from '../webpack-config';
|
||||||
|
|
||||||
|
|
||||||
@ -75,12 +74,6 @@ export const getProdConfig = function (wco: WebpackConfigOptions) {
|
|||||||
mangle: { screw_ie8: true },
|
mangle: { screw_ie8: true },
|
||||||
compress: { screw_ie8: true, warnings: buildOptions.verbose },
|
compress: { screw_ie8: true, warnings: buildOptions.verbose },
|
||||||
sourceMap: buildOptions.sourcemap
|
sourceMap: buildOptions.sourcemap
|
||||||
}),
|
|
||||||
new CompressionPlugin({
|
|
||||||
asset: '[path].gz[query]',
|
|
||||||
algorithm: 'gzip',
|
|
||||||
test: /\.js$|\.html$|\.css$/,
|
|
||||||
threshold: 10240
|
|
||||||
})
|
})
|
||||||
].concat(extraPlugins)
|
].concat(extraPlugins)
|
||||||
};
|
};
|
||||||
|
@ -28,7 +28,6 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@ngtools/json-schema": "1.0.3",
|
"@ngtools/json-schema": "1.0.3",
|
||||||
"@ngtools/webpack": "1.2.9",
|
"@ngtools/webpack": "1.2.9",
|
||||||
"async": "^2.1.4",
|
|
||||||
"autoprefixer": "^6.5.3",
|
"autoprefixer": "^6.5.3",
|
||||||
"chalk": "^1.1.3",
|
"chalk": "^1.1.3",
|
||||||
"common-tags": "^1.3.1",
|
"common-tags": "^1.3.1",
|
||||||
@ -84,7 +83,6 @@
|
|||||||
"webpack": "~2.2.0",
|
"webpack": "~2.2.0",
|
||||||
"webpack-dev-server": "~2.2.0",
|
"webpack-dev-server": "~2.2.0",
|
||||||
"webpack-merge": "^2.4.0",
|
"webpack-merge": "^2.4.0",
|
||||||
"webpack-sources": "^0.1.3",
|
|
||||||
"zone.js": "^0.7.2"
|
"zone.js": "^0.7.2"
|
||||||
},
|
},
|
||||||
"ember-addon": {
|
"ember-addon": {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user