Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(transformer): added initial draft for transformer uglify
- Loading branch information
Showing
12 changed files
with
395 additions
and
265 deletions.
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,21 @@ | ||
import { Transform, TransformCallback } from 'stream'; | ||
|
||
export class FileInMemoryTransformer extends Transform { | ||
constructor() { | ||
super(); | ||
|
||
this.memory = Buffer.alloc(0); | ||
} | ||
|
||
private memory: Buffer; | ||
|
||
_transform(chunk: Buffer, _: string, cb: TransformCallback): void { | ||
this.memory = Buffer.concat([this.memory, chunk]); | ||
cb(); | ||
} | ||
|
||
_flush(cb: TransformCallback): void { | ||
this.push(this.memory); | ||
cb(); | ||
} | ||
} |
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,32 @@ | ||
import { Transform } from 'stream'; | ||
|
||
export class MemoryStream extends Transform { | ||
constructor(private readonly desiredChunkSize: number) { | ||
super(); | ||
|
||
this.memory = Buffer.alloc(0); | ||
} | ||
|
||
private memory: Buffer; | ||
|
||
_transform(chunk: Buffer, _: string, cb: () => void): void { | ||
if ( | ||
Buffer.byteLength(this.memory) + Buffer.byteLength(chunk) >= | ||
this.desiredChunkSize | ||
) { | ||
this.push(this.memory); | ||
|
||
this.memory = Buffer.alloc(0); | ||
} | ||
|
||
this.memory = Buffer.concat([this.memory, chunk]); | ||
|
||
cb(); | ||
} | ||
|
||
_flush(cb: () => void): void { | ||
this.push(this.memory); | ||
|
||
cb(); | ||
} | ||
} |
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,40 @@ | ||
import { Transform, TransformCallback } from 'stream'; | ||
import { MinifyOptions, minify } from 'terser'; | ||
|
||
export class UglifyJsTransformer extends Transform { | ||
constructor( | ||
protected readonly filePath: string, | ||
protected readonly uglifyOptions: MinifyOptions = {}, | ||
) { | ||
super(); | ||
} | ||
|
||
protected chunks: number = 0; | ||
|
||
async _transform( | ||
chunk: Buffer, | ||
encoding: string, | ||
callback: TransformCallback, | ||
): Promise<void> { | ||
if (this.chunks > 0) { | ||
return callback( | ||
new Error( | ||
'This transformer should not be called more than once. Check if you use MemoryStream before using UglifyJsTransformer', | ||
), | ||
); | ||
} | ||
|
||
console.log(`${this.filePath}:${chunk.byteLength}`); | ||
|
||
const code = chunk.toString('utf-8'); | ||
const result = await minify(code, this.uglifyOptions).catch(() => null); | ||
const data = | ||
!result || !result.code ? chunk : Buffer.from(result.code, 'utf-8'); | ||
|
||
this.chunks++; | ||
|
||
this.push(data); | ||
|
||
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
Oops, something went wrong.