Skip to content

Commit

Permalink
NPM Release 1.0.0-rc.6
Browse files Browse the repository at this point in the history
Progress towards issue #2
  • Loading branch information
ToddThomson committed Dec 2, 2016
1 parent d47e4ec commit b88fcca
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 58 deletions.
33 changes: 15 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,33 @@ TsMinifier is a Typescript minifier providing identifier mangling and whitespace
TsMinifier exposes a Minifier class and several direct minify functions.

```
class Minifier {
constructor(program: ts.Program, compilerOptions: ts.CompilerOptions, minifierOptions: MinifierOptions);
// Public Methods
transform(sourceFile: ts.SourceFile): ts.SourceFile;
removeWhitespace(jsContents: string): string;
interface ProjectConfig {
success: boolean;
compilerOptions?: ts.CompilerOptions;
fileNames?: string[];
errors?: ts.Diagnostic[];
}
interface MinifierOptions {
interface MinifierOptions {
mangleIdentifiers?: boolean;
removeWhitespace?: boolean;
externalNamespace?: string;
}
interface MinifierResult {
emitSkipped: boolean;
emitOutput?: CompilerOutput[];
emitOutput?: ts2js.CompilerOutput[];
diagnostics: ts.Diagnostic[];
}
interface CompilerOutput {
fileName: string;
emitSkipped: boolean;
text?: string;
mapText?: string;
dtsText?: string;
diagnostics: ts.Diagnostic[];
class Minifier {
constructor(program: ts.Program, compilerOptions: ts.CompilerOptions, minifierOptions: MinifierOptions);
// Public Methods
transform(sourceFile: ts.SourceFile): ts.SourceFile;
removeWhitespace(jsContents: string): string;
}
function minify( fileNames: string[], compilerOptions: ts.CompilerOptions, minifierOptions: MinifierOptions): MinifierOutput[];
function minifyModule( input: string, compilerOptions: ts.CompilerOptions, minifierOptions: MinifierOptions): MinifierOutput;
Expand Down Expand Up @@ -72,4 +70,3 @@ Once Gulp is installed, you can build it with the following commands:
npm install
gulp build
```

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tsminifier",
"version": "1.0.0-rc.3",
"version": "1.0.0-rc.6",
"author": "Todd Thomson <[email protected]>",
"description": "Typescript minifier providing identifier mangling and whitespace removal.",
"license": "MIT",
Expand All @@ -9,7 +9,7 @@
"dependencies": {
"@types/chalk": "^0.4.31",
"@types/node": "^6.0.46",
"ts2js": "1.0.0-rc.4",
"ts2js": "^1.0.0-rc.7",
"typescript": "2.0.10"
},
"devDependencies": {
Expand All @@ -21,12 +21,12 @@
"vinyl-paths": "^2.1.0"
},
"keywords": [
"typescript",
"minifier",
"minify",
"minification",
"compile",
"compiler",
"typescript",
"typescript minifier"
],
"homepage": "https://github.com/toddthomson/tsminifier",
Expand Down
6 changes: 3 additions & 3 deletions run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

var projectSettings = TsMinifier.ProjectHelper.getProjectConfig( "./src/tsconfig.json" );

var sourceText: string = 'import * as ts from "typescript"; import * as TsMinifier from "./src/tsminifier"; var projectSettings = TsMinifier.ProjectHelper.getProjectConfig( "./src/tsconfig.json" );';
var sourceText: string = 'import * as ts from "typescript"; import { TsMinifier } from "./src/tsminifier"; var projectSettings = TsMinifier.ProjectHelper.getProjectConfig( "./src/tsconfig.json" );';

var minModuleResult = TsMinifier.minifyModule( sourceText, "module.ts", projectSettings.compilerOptions, { mangleIdentifiers: true, removeWhitespace: true } );
var minFilesResult = TsMinifier.minify( ["./run.ts", "./src/TsMinifier.ts"], projectSettings.compilerOptions, { mangleIdentifiers: true, removeWhitespace: true } );

console.log( "Minified module text: \n", minModuleResult.emitOutput[0].text );
console.log( "Minified files text: \n ", minFilesResult.emitOutput[1].text );
console.log( "Minified module text: \n", minModuleResult.emitOutput[0].codeFile.data );
console.log( "Minified files text: \n ", minFilesResult.emitOutput[1].codeFile.data );
27 changes: 15 additions & 12 deletions src/Compiler/MinifyingCompiler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CompilerOutput, CompilerResult } from "ts2js";
import { CompilerOutput, CompilerResult, CompilerFile } from "ts2js";
import { Minifier } from "../Minifier/Minifier";
import { MinifierOptions } from "../Minifier/MinifierOptions";
import { TsCore } from "../Utils/TsCore";
Expand All @@ -19,9 +19,10 @@ export class MinifyingCompiler extends tscompiler.Compiler {

protected emit(): CompilerResult {
var output: CompilerOutput[] = [];
var outputText: string;
var mapText: string;
var dtsText: string;

var codeFile: CompilerFile;
var mapFile: CompilerFile;
var dtsFile: CompilerFile;

// Modify compiler options for the minifiers purposes
const options = this.options;
Expand Down Expand Up @@ -64,27 +65,29 @@ export class MinifyingCompiler extends tscompiler.Compiler {
sourceFile = minifier.transform( sourceFile );
}

const emitResult = this.program.emit( sourceFile, (fileName: string, content: string) => {
const emitResult = this.program.emit( sourceFile, (fileName: string, data: string, writeByteOrderMark: boolean ) => {
var file: CompilerFile = { fileName: fileName, data: data, writeByteOrderMark: writeByteOrderMark };

if ( TsCore.fileExtensionIs( fileName, ".js" ) || TsCore.fileExtensionIs( fileName, ".jsx" ) ) {
outputText = content;
codeFile = file;
} else if ( TsCore.fileExtensionIs( fileName, "d.ts" ) ) {
dtsText = content;
dtsFile = file;
} else if ( TsCore.fileExtensionIs( fileName, ".map" ) ) {
mapText = content;
mapFile = file;
}
});

if ( !emitResult.emitSkipped && this.minifierOptions.removeWhitespace ) {
// Whitespace removal cannot be performed in the AST minification transform, so we do it here for now
outputText = minifier.removeWhitespace( outputText );
codeFile.data = minifier.removeWhitespace( codeFile.data );
}

const minifyOutput: CompilerOutput = {
fileName: fileNames[ fileNameIndex ],
emitSkipped: emitResult.emitSkipped,
text: outputText,
mapText: mapText,
dtsText: dtsText,
codeFile: codeFile,
mapFile: mapFile,
dtsFile: dtsFile,
diagnostics: emitResult.diagnostics
};

Expand Down
22 changes: 14 additions & 8 deletions src/TsMinifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,22 @@ import { format } from "./Utils/formatter";
import * as ts from "typescript";
import * as tsc from "ts2js";

//export { Minifier }
export { MinifierOptions }
export { ProjectConfig }

// Exported types
export { ProjectConfig };
export { MinifierOptions };

export interface MinifierResult {
emitSkipped: boolean;
diagnostics: ts.Diagnostic[];
emitOutput?: tsc.CompilerOutput[];
};

export namespace TsMinifier {

export interface MinifierResult {
emitSkipped: boolean;
diagnostics: ts.Diagnostic[];
emitOutput?: tsc.CompilerOutput[];
}
//export var Minifier: Minifier = Minifier;

exports.TsMinifier.Minifier = Minifier;

export function minify( fileNames: string[], compilerOptions: ts.CompilerOptions, minifierOptions: MinifierOptions ): MinifierResult {
const compiler = new MinifyingCompiler( compilerOptions, minifierOptions );
Expand Down Expand Up @@ -48,4 +53,5 @@ export namespace TsMinifier {
}
}

// TJT: Comment out when testing locally.
module.exports = TsMinifier;
7 changes: 6 additions & 1 deletion src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"declaration": true,
"declaration": false,
"diagnostics": true,
"listFiles": false,
"pretty": true,
Expand All @@ -18,21 +18,26 @@
"./Ast/Ast.ts",
"./Ast/AstTransform.ts",
"./Ast/NodeWalker.ts",

"./Compiler/MinifyingCompiler.ts",

"./Minifier/Minifier.ts",
"./Minifier/MinifierOptions.ts",
"./Minifier/MinifierStatistics.ts",
"./Minifier/ContainerContext.ts",
"./Minifier/IdentifierSymbolInfo.ts",
"./Minifier/NameGenerator.ts",
"./Project/ProjectConfig.ts",

"./Reporting/DiagnosticsReporter.ts",
"./Reporting/Logger.ts",
"./Reporting/StatisticsReporter.ts",

"./Utils/Debug.ts",
"./Utils/Formatter.ts",
"./Utils/TsCore.ts",
"./Utils/Utilities.ts",

"./TsMinifier.ts"
],
"bundles": {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"declaration": true,
"declaration": false,
"noLib": false,
"sourceMap": true,
"pretty": true,
Expand Down
29 changes: 17 additions & 12 deletions tsminifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -1606,9 +1606,9 @@ var MinifyingCompiler = (function (_super) {
}
MinifyingCompiler.prototype.emit = function () {
var output = [];
var outputText;
var mapText;
var dtsText;
var codeFile;
var mapFile;
var dtsFile;
// Modify compiler options for the minifiers purposes
var options = this.options;
options.noEmit = undefined;
Expand Down Expand Up @@ -1639,27 +1639,28 @@ var MinifyingCompiler = (function (_super) {
if (this.minifierOptions.mangleIdentifiers) {
sourceFile = minifier.transform(sourceFile);
}
var emitResult = this.program.emit(sourceFile, function (fileName, content) {
var emitResult = this.program.emit(sourceFile, function (fileName, data, writeByteOrderMark) {
var file = { fileName: fileName, data: data, writeByteOrderMark: writeByteOrderMark };
if (TsCore.fileExtensionIs(fileName, ".js") || TsCore.fileExtensionIs(fileName, ".jsx")) {
outputText = content;
codeFile = file;
}
else if (TsCore.fileExtensionIs(fileName, "d.ts")) {
dtsText = content;
dtsFile = file;
}
else if (TsCore.fileExtensionIs(fileName, ".map")) {
mapText = content;
mapFile = file;
}
});
if (!emitResult.emitSkipped && this.minifierOptions.removeWhitespace) {
// Whitespace removal cannot be performed in the AST minification transform, so we do it here for now
outputText = minifier.removeWhitespace(outputText);
codeFile.data = minifier.removeWhitespace(codeFile.data);
}
var minifyOutput = {
fileName: fileNames[fileNameIndex],
emitSkipped: emitResult.emitSkipped,
text: outputText,
mapText: mapText,
dtsText: dtsText,
codeFile: codeFile,
mapFile: mapFile,
dtsFile: dtsFile,
diagnostics: emitResult.diagnostics
};
output.push(minifyOutput);
Expand Down Expand Up @@ -1713,9 +1714,12 @@ function format(input) {
};
}
}
//export { Minifier }
// Exported types
;
var TsMinifier;
(function (TsMinifier) {
//export var Minifier: Minifier = Minifier;
exports.TsMinifier.Minifier = Minifier;
function minify(fileNames, compilerOptions, minifierOptions) {
var compiler = new MinifyingCompiler(compilerOptions, minifierOptions);
return compiler.compile(fileNames);
Expand Down Expand Up @@ -1743,4 +1747,5 @@ var TsMinifier;
ProjectHelper.getProjectConfig = getProjectConfig;
})(ProjectHelper = TsMinifier.ProjectHelper || (TsMinifier.ProjectHelper = {}));
})(TsMinifier = exports.TsMinifier || (exports.TsMinifier = {}));
// TJT: Comment out when testing locally.
module.exports = TsMinifier;

0 comments on commit b88fcca

Please sign in to comment.