-
Notifications
You must be signed in to change notification settings - Fork 12k
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
|
||
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); | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same with
async
andurl
.There was a problem hiding this comment.
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.