diff --git a/README.md b/README.md index 03b329d..4e368d3 100644 --- a/README.md +++ b/README.md @@ -12,15 +12,14 @@ 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; @@ -28,19 +27,18 @@ TsMinifier exposes a Minifier class and several direct minify functions. 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; @@ -72,4 +70,3 @@ Once Gulp is installed, you can build it with the following commands: npm install gulp build ``` - diff --git a/package.json b/package.json index e71d26d..1874131 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tsminifier", - "version": "1.0.0-rc.3", + "version": "1.0.0-rc.6", "author": "Todd Thomson ", "description": "Typescript minifier providing identifier mangling and whitespace removal.", "license": "MIT", @@ -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": { @@ -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", diff --git a/run.ts b/run.ts index bebc7d9..3bdc7b1 100644 --- a/run.ts +++ b/run.ts @@ -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 ); \ No newline at end of file +console.log( "Minified module text: \n", minModuleResult.emitOutput[0].codeFile.data ); +console.log( "Minified files text: \n ", minFilesResult.emitOutput[1].codeFile.data ); \ No newline at end of file diff --git a/src/Compiler/MinifyingCompiler.ts b/src/Compiler/MinifyingCompiler.ts index daa31d8..f6192fa 100644 --- a/src/Compiler/MinifyingCompiler.ts +++ b/src/Compiler/MinifyingCompiler.ts @@ -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"; @@ -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; @@ -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 }; diff --git a/src/TsMinifier.ts b/src/TsMinifier.ts index aaf70fd..a809859 100644 --- a/src/TsMinifier.ts +++ b/src/TsMinifier.ts @@ -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 ); @@ -48,4 +53,5 @@ export namespace TsMinifier { } } +// TJT: Comment out when testing locally. module.exports = TsMinifier; \ No newline at end of file diff --git a/src/tsconfig.json b/src/tsconfig.json index 72909a1..b5c1ee7 100644 --- a/src/tsconfig.json +++ b/src/tsconfig.json @@ -3,7 +3,7 @@ "target": "es5", "module": "commonjs", "moduleResolution": "node", - "declaration": true, + "declaration": false, "diagnostics": true, "listFiles": false, "pretty": true, @@ -18,7 +18,9 @@ "./Ast/Ast.ts", "./Ast/AstTransform.ts", "./Ast/NodeWalker.ts", + "./Compiler/MinifyingCompiler.ts", + "./Minifier/Minifier.ts", "./Minifier/MinifierOptions.ts", "./Minifier/MinifierStatistics.ts", @@ -26,13 +28,16 @@ "./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": { diff --git a/tsconfig.json b/tsconfig.json index 29bcaae..b87fc12 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,7 +3,7 @@ "target": "es5", "module": "commonjs", "moduleResolution": "node", - "declaration": true, + "declaration": false, "noLib": false, "sourceMap": true, "pretty": true, diff --git a/tsminifier.js b/tsminifier.js index 84dde25..f4d7a0a 100644 --- a/tsminifier.js +++ b/tsminifier.js @@ -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; @@ -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); @@ -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); @@ -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;