diff --git a/clean-css/clean-css-tests.ts b/clean-css/clean-css-tests.ts new file mode 100644 index 0000000000..ffbb3d1278 --- /dev/null +++ b/clean-css/clean-css-tests.ts @@ -0,0 +1,55 @@ +/// + +import * as CleanCSS from 'clean-css'; + +var source = 'a{font-weight:bold;}'; +var minified = new CleanCSS().minify(source).styles; + +var source = '@import url(http://path/to/remote/styles);'; +new CleanCSS().minify(source, function (error, minified) { + console.log(minified.styles); +}); + +const pathToOutputDirectory = 'path'; + +new CleanCSS({ sourceMap: true, target: pathToOutputDirectory }) + .minify(source, function (error, minified) { + // access minified.sourceMap for SourceMapGenerator object + // see https://github.com/mozilla/source-map/#sourcemapgenerator for more details + // see https://github.com/jakubpawlowicz/clean-css/blob/master/bin/cleancss#L114 on how it's used in clean-css' CLI + console.log(minified.sourceMap); +}); + +const inputSourceMapAsString = 'input'; +new CleanCSS({ sourceMap: inputSourceMapAsString, target: pathToOutputDirectory }) + .minify(source, function (error, minified) { + // access minified.sourceMap to access SourceMapGenerator object + // see https://github.com/mozilla/source-map/#sourcemapgenerator for more details + // see https://github.com/jakubpawlowicz/clean-css/blob/master/bin/cleancss#L114 on how it's used in clean-css' CLI + console.log(minified.sourceMap); +}); + +new CleanCSS({ sourceMap: true, target: pathToOutputDirectory }).minify({ + 'path/to/source/1': { + styles: '...styles...', + sourceMap: '...source-map...' + }, + 'path/to/source/2': { + styles: '...styles...', + sourceMap: '...source-map...' + } +}, function (error, minified) { + // access minified.sourceMap as above + console.log(minified.sourceMap); +}); + +new CleanCSS().minify(['path/to/file/one', 'path/to/file/two']); + +new CleanCSS().minify({ + 'path/to/file/one': { + styles: 'contents of file one' + }, + 'path/to/file/two': { + styles: 'contents of file two' + } +}); diff --git a/clean-css/clean-css.d.ts b/clean-css/clean-css.d.ts new file mode 100644 index 0000000000..25bb2471a5 --- /dev/null +++ b/clean-css/clean-css.d.ts @@ -0,0 +1,109 @@ +// Type definitions for clean-css v3.4.9 +// Project: https://github.com/jakubpawlowicz/clean-css +// Definitions by: Tanguy Krotoff +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module 'clean-css' { + namespace CleanCSS { + interface Options { + // Set to false to disable advanced optimizations - selector & property merging, reduction, etc. + advanced?: boolean; + + // Set to false to disable aggressive merging of properties. + aggressiveMerging?: boolean; + + // Turns on benchmarking mode measuring time spent on cleaning up (run npm run bench to see example) + benchmark?: boolean; + + // Enables compatibility mode + compatibility?: Object; + + // Set to true to get minification statistics under stats property (see test/custom-test.js for examples) + debug?: boolean; + + // A hash of options for @import inliner, see test/protocol-imports-test.js for examples, or this comment for a proxy use case. + inliner?: Object; + + // Whether to keep line breaks (default is false) + keepBreaks?: boolean; + + // * for keeping all (default), 1 for keeping first one only, 0 for removing all + keepSpecialComments?: string | number; + + // Whether to merge @media at-rules (default is true) + mediaMerging?: boolean; + + // Whether to process @import rules + processImport?: boolean; + + // A list of @import rules, can be ['all'] (default), ['local'], ['remote'], or a blacklisted path e.g. ['!fonts.googleapis.com'] + processImportFrom?: Array; + + // Set to false to skip URL rebasing + rebase?: boolean; + + // Path to resolve relative @import rules and URLs + relativeTo?: string; + + // Set to false to disable restructuring in advanced optimizations + restructuring?: boolean; + + // Path to resolve absolute @import rules and rebase relative URLs + root?: string; + + // Rounding precision; defaults to 2; -1 disables rounding + roundingPrecision?: number; + + // Set to true to enable semantic merging mode which assumes BEM-like content (default is false as it's highly likely this will break your stylesheets - use with caution!) + semanticMerging?: boolean; + + // Set to false to skip shorthand compacting (default is true unless sourceMap is set when it's false) + shorthandCompacting?: boolean; + + // Exposes source map under sourceMap property, e.g. new CleanCSS().minify(source).sourceMap (default is false) If input styles are a product of CSS preprocessor (Less, Sass) an input source map can be passed as a string. + sourceMap?: boolean | string; + + // Set to true to inline sources inside a source map's sourcesContent field (defaults to false) It is also required to process inlined sources from input source maps. + sourceMapInlineSources?: boolean; + + // Path to a folder or an output file to which rebase all URLs + target?: string; + } + + interface Output { + // Optimized output CSS as a string + styles: string; + + // Output source map (if requested with sourceMap option) + sourceMap: string; + + // A list of errors raised + errors: Array; + + // A list of warnings raised + warnings: Array; + + // A hash of statistic information (if requested with debug option) + stats: { + // Original content size (after import inlining) + originalSize: number; + + // Optimized content size + minifiedSize: number; + + // Time spent on optimizations + timeSpent: number; + + // A ratio of output size to input size (e.g. 25% if content was reduced from 100 bytes to 75 bytes) + efficiency: number; + }; + } + } + + class CleanCSS { + constructor(options?: CleanCSS.Options); + minify(sources: string | Array | Object, callback?: (error: any, minified: CleanCSS.Output) => void): CleanCSS.Output; + } + + export = CleanCSS; +} diff --git a/gulp-htmlmin/gulp-htmlmin-tests.ts b/gulp-htmlmin/gulp-htmlmin-tests.ts new file mode 100644 index 0000000000..78149e787e --- /dev/null +++ b/gulp-htmlmin/gulp-htmlmin-tests.ts @@ -0,0 +1,11 @@ +/// +/// + +import * as gulp from 'gulp'; +import * as htmlmin from 'gulp-htmlmin'; + +gulp.task('minify', function() { + return gulp.src('src/*.html') + .pipe(htmlmin({collapseWhitespace: true})) + .pipe(gulp.dest('dist')) +}); diff --git a/gulp-htmlmin/gulp-htmlmin.d.ts b/gulp-htmlmin/gulp-htmlmin.d.ts new file mode 100644 index 0000000000..2cf947d23d --- /dev/null +++ b/gulp-htmlmin/gulp-htmlmin.d.ts @@ -0,0 +1,18 @@ +// Type definitions for gulp-htmlmin v1.3.0 +// Project: https://github.com/jonschlinkert/gulp-htmlmin +// Definitions by: Tanguy Krotoff +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// +/// + +declare module 'gulp-htmlmin' { + import * as HTMLMinifier from 'html-minifier'; + + namespace htmlmin { + } + + function htmlmin(options?: HTMLMinifier.Options): NodeJS.ReadWriteStream; + + export = htmlmin; +} diff --git a/gulp-minify-css/gulp-minify-css-tests.ts b/gulp-minify-css/gulp-minify-css-tests.ts index d8ac4d9dfc..9bfe9697a0 100644 --- a/gulp-minify-css/gulp-minify-css-tests.ts +++ b/gulp-minify-css/gulp-minify-css-tests.ts @@ -1,4 +1,4 @@ -/// +/// /// import * as gulp from "gulp"; diff --git a/gulp-minify-css/gulp-minify-css.d.ts b/gulp-minify-css/gulp-minify-css.d.ts index bc990a6e0e..cb0eea502b 100644 --- a/gulp-minify-css/gulp-minify-css.d.ts +++ b/gulp-minify-css/gulp-minify-css.d.ts @@ -4,28 +4,12 @@ // Definitions: https://github.com/borisyankov/DefinitelyTyped /// +/// declare module "gulp-minify-css" { + import * as CleanCSS from 'clean-css'; - interface IOptions { - cache?: boolean; - advanced?: boolean; - aggressiveMerging?: boolean; - benchmark?: boolean; - compatibility?: string; - debug?: boolean; - inliner?: Object; - keepBreaks?: boolean; - keepSpecialComments?: string | number; - processImport?: boolean; - rebase?: boolean; - relativeTo?: string; - root?: string; - roundingPrecision?: number; - shorthandCompacting?: boolean; - } - - function minifyCSS(options?: IOptions): NodeJS.ReadWriteStream; + function minifyCSS(options?: CleanCSS.Options): NodeJS.ReadWriteStream; namespace minifyCSS {} diff --git a/gulp-minify-html/gulp-minify-html-tests.ts b/gulp-minify-html/gulp-minify-html-tests.ts index 2ec41e556a..1b3c7639da 100644 --- a/gulp-minify-html/gulp-minify-html-tests.ts +++ b/gulp-minify-html/gulp-minify-html-tests.ts @@ -4,11 +4,13 @@ import * as gulp from 'gulp'; import * as minifyHtml from 'gulp-minify-html'; +// This package has been deprecated in favor of gulp-htmlmin, which should be faster and more comprehensive. + minifyHtml(); minifyHtml({conditionals: true, loose: true}); gulp.task('minify-html', () => { - var opts = { + var opts: minifyHtml.Options = { conditionals: true, spare: true }; diff --git a/gulp-minify-html/gulp-minify-html.d.ts b/gulp-minify-html/gulp-minify-html.d.ts index 11ce298a49..770789bbb0 100644 --- a/gulp-minify-html/gulp-minify-html.d.ts +++ b/gulp-minify-html/gulp-minify-html.d.ts @@ -5,33 +5,36 @@ /// +// This package has been deprecated in favor of gulp-htmlmin, which should be faster and more comprehensive. + declare module 'gulp-minify-html' { - interface IOptions { - // Do not remove empty attributes - empty?: boolean; + namespace minifyHtml { + // Options from https://github.com/Swaagie/minimize#options + interface Options { + // Do not remove empty attributes + empty?: boolean; - // Do not strip CDATA from scripts - cdata?: boolean; + // Do not strip CDATA from scripts + cdata?: boolean; - // Do not remove comments - comments?: boolean; + // Do not remove comments + comments?: boolean; - // Do not remove conditional internet explorer comments - conditionals?: boolean; + // Do not remove conditional internet explorer comments + conditionals?: boolean; - // Do not remove redundant attributes - spare?: boolean; + // Do not remove redundant attributes + spare?: boolean; - // Do not remove arbitrary quotes - quotes?: boolean; + // Do not remove arbitrary quotes + quotes?: boolean; - // Preserve one whitespace - loose?: boolean; + // Preserve one whitespace + loose?: boolean; + } } - function minifyHtml(options?: IOptions): NodeJS.ReadWriteStream; - - namespace minifyHtml {} + function minifyHtml(options?: minifyHtml.Options): NodeJS.ReadWriteStream; export = minifyHtml; } diff --git a/gulp-uglify/gulp-uglify-tests.ts b/gulp-uglify/gulp-uglify-tests.ts index e4f1f0d8a0..01cfb06f9e 100644 --- a/gulp-uglify/gulp-uglify-tests.ts +++ b/gulp-uglify/gulp-uglify-tests.ts @@ -1,8 +1,8 @@ -/// +/// /// -import gulp = require("gulp"); -import uglify = require("gulp-uglify"); +import * as gulp from 'gulp'; +import * as uglify from 'gulp-uglify'; gulp.task('compress', function() { var tsResult = gulp.src('lib/*.ts') @@ -21,4 +21,4 @@ gulp.task('compress2', function() { } })) .pipe(gulp.dest('dist')); -}); \ No newline at end of file +}); diff --git a/gulp-uglify/gulp-uglify.d.ts b/gulp-uglify/gulp-uglify.d.ts index 05eb937ed3..b070f3f356 100644 --- a/gulp-uglify/gulp-uglify.d.ts +++ b/gulp-uglify/gulp-uglify.d.ts @@ -4,172 +4,39 @@ // Definitions: https://github.com/borisyankov/DefinitelyTyped /// +/// declare module "gulp-uglify" { - function GulpUglify(options?: IGulpUglifyOptions): NodeJS.ReadWriteStream; + import * as UglifyJS from 'uglify-js'; - interface IGulpUglifyOptions { - /** - * Pass false to skip mangling names. - */ - mangle?: boolean; + namespace GulpUglify { + interface Options { + /** + * Pass false to skip mangling names. + */ + mangle?: boolean; - /** - * Pass if you wish to specify additional output options. The defaults are optimized for best compression. - */ - output?: IOutputOptions; + /** + * Pass if you wish to specify additional output options. The defaults are optimized for best compression. + */ + output?: UglifyJS.BeautifierOptions; - /** - * Pass an object to specify custom compressor options. Pass false to skip compression completely. - */ - compress?: boolean; + /** + * Pass an object to specify custom compressor options. Pass false to skip compression completely. + */ + compress?: UglifyJS.CompressorOptions | boolean; - /** - * A convenience option for options.output.comments. Defaults to preserving no comments. - * all - Preserve all comments in code blocks - * some - Preserve comments that start with a bang (!) or include a Closure Compiler directive (@preserve, @license, @cc_on) - * function - Specify your own comment preservation function. You will be passed the current node and the current comment and are expected to return either true or false. - */ - preserverComments?: string|((node: any, comment: ITokenizer) => boolean); + /** + * A convenience option for options.output.comments. Defaults to preserving no comments. + * all - Preserve all comments in code blocks + * some - Preserve comments that start with a bang (!) or include a Closure Compiler directive (@preserve, @license, @cc_on) + * function - Specify your own comment preservation function. You will be passed the current node and the current comment and are expected to return either true or false. + */ + preserverComments?: string|((node: any, comment: UglifyJS.Tokenizer) => boolean); + } } - interface IOutputOptions { - /** - * Start indentation on every line (only when `beautify`) - */ - indent_start?: number; + function GulpUglify(options?: GulpUglify.Options): NodeJS.ReadWriteStream; - /** - * Indentation level (only when `beautify`) - */ - indent_level?: number; - - /** - * Quote all keys in object literals? - */ - quote_keys?: boolean; - - /** - * Add a space after colon signs? - */ - space_colon?: boolean; - - /** - * Output ASCII-safe? (encodes Unicode characters as ASCII) - */ - ascii_only?: boolean; - - /** - * Escape " + +import * as HTMLMinifier from 'html-minifier'; +const minify = HTMLMinifier.minify; + +var result = minify('

foo

', { + removeAttributeQuotes: true +}); +result; // '

foo

' diff --git a/html-minifier/html-minifier.d.ts b/html-minifier/html-minifier.d.ts new file mode 100644 index 0000000000..9557de8902 --- /dev/null +++ b/html-minifier/html-minifier.d.ts @@ -0,0 +1,115 @@ +// Type definitions for HTMLMinifier v1.1.1 +// Project: https://github.com/kangax/html-minifier +// Definitions by: Tanguy Krotoff +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// +/// +/// + +declare module 'html-minifier' { + import * as UglifyJS from 'uglify-js'; + import * as CleanCSS from 'clean-css'; + import * as RelateUrl from 'relateurl'; + + namespace HTMLMinifier { + function minify(text: string, options?: Options): string; + + interface Options { + // Strip HTML comments + removeComments?: boolean; + + // Strip HTML comments from scripts and styles + removeCommentsFromCDATA?: boolean; + + // Remove CDATA sections from script and style elements + removeCDATASectionsFromCDATA?: boolean; + + // Collapse white space that contributes to text nodes in a document tree + collapseWhitespace?: boolean; + + // Always collapse to 1 space (never remove it entirely). Must be used in conjunction with collapseWhitespace=true + conservativeCollapse?: boolean; + + // Don't leave any spaces between display:inline; elements when collapsing. Must be used in conjunction with collapseWhitespace=true + collapseInlineTagWhitespace?: boolean; + + // Always collapse to 1 line break (never remove it entirely) when whitespace between tags include a line break. Must be used in conjunction with collapseWhitespace=true + preserveLineBreaks?: boolean; + + // Omit attribute values from boolean attributes + collapseBooleanAttributes?: boolean; + + // Remove quotes around attributes when possible + removeAttributeQuotes?: boolean; + + // Remove attributes when value matches default + removeRedundantAttributes?: boolean; + + // Prevents the escaping of the values of attributes. + preventAttributesEscaping?: boolean; + + // Replaces the doctype with the short (HTML5) doctype + useShortDoctype?: boolean; + + // Remove all attributes with whitespace-only values + removeEmptyAttributes?: boolean; + + // Remove type="text/javascript" from script tags. Other type attribute values are left intact. + removeScriptTypeAttributes?: boolean; + + // Remove type="text/css" from style and link tags. Other type attribute values are left intact. + removeStyleLinkTypeAttributes?: boolean; + + // Remove unrequired tags + removeOptionalTags?: boolean; + + // Remove all elements with empty contents + removeEmptyElements?: boolean; + + // Toggle linting + lint?: boolean; + + // Keep the trailing slash on singleton elements + keepClosingSlash?: boolean; + + // Treat attributes in case sensitive manner (useful for custom HTML tags.) + caseSensitive?: boolean; + + // Minify Javascript in script elements and on* attributes (uses UglifyJS) + minifyJS?: boolean | UglifyJS.MinifyOptions; + + // Minify CSS in style elements and style attributes (uses clean-css) + minifyCSS?: boolean | CleanCSS.Options; + + // Minify URLs in various attributes (uses relateurl) + minifyURLs?: boolean | RelateUrl.Options; + + // Array of regex'es that allow to ignore certain comments, when matched + ignoreCustomComments?: Array; + + // Array of regex'es that allow to ignore certain fragments, when matched (e.g. , {{ ... }}, etc.) + ignoreCustomFragments?: Array; + + // Array of strings corresponding to types of script elements to process through minifier (e.g. text/ng-template, text/x-handlebars-template, etc.) + processScripts?: Array; + + // Specify a maximum line length. Compressed output will be split by newlines at valid HTML split-points + maxLineLength?: number; + + // Arrays of regex'es that allow to support custom attribute assign expressions (e.g. '
') + customAttrAssign?: Array; + + // Arrays of regex'es that allow to support custom attribute surround expressions (e.g. ) + customAttrSurround?: Array; + + // Regex that specifies custom attribute to strip newlines from (e.g. /ng\-class/) + customAttrCollapse?: RegExp; + + // Type of quote to use for attribute values (' or ") + quoteCharacter?: string; + } + } + + export = HTMLMinifier; +} diff --git a/relateurl/relateurl-tests.ts b/relateurl/relateurl-tests.ts new file mode 100644 index 0000000000..89a647d06e --- /dev/null +++ b/relateurl/relateurl-tests.ts @@ -0,0 +1,20 @@ +/// + +import * as RelateUrl from 'relateurl'; + +var from = "http://www.domain.com/asdf/"; +var to = "http://www.domain.com/asdf/asdf"; +var to1 = "http://www.domain.com/asdf/asdf1"; +var to2 = "http://www.domain.com/asdf/asdf1"; +var to3 = "http://www.domain.com/asdf/asdf1"; +var options = {site: "http://www.domain.com/asdf2/"}; +var customOptions = {output: RelateUrl.ABSOLUTE}; + +// Single Instance +var result = RelateUrl.relate(from, to, options); + +// Reusable Instances +var instance = new RelateUrl(from, options); +var result1 = instance.relate(to1); +var result2 = instance.relate(to2, customOptions); +var result3 = instance.relate(to3); diff --git a/relateurl/relateurl.d.ts b/relateurl/relateurl.d.ts new file mode 100644 index 0000000000..9f24e59b9e --- /dev/null +++ b/relateurl/relateurl.d.ts @@ -0,0 +1,125 @@ +// Type definitions for relateurl v0.2.6 +// Project: https://github.com/stevenvachon/relateurl +// Definitions by: Tanguy Krotoff +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module 'relateurl' { + namespace RelateUrl { + interface Options { + /** + * Type: Object + * Default value: {ftp:21, http:80, https:443} + * + * Extend the list with any ports you need. Any URLs containing these default ports will have them removed. Example: http://example.com:80/ will become http://example.com/. + */ + defaultPorts?: Object; + + /** + * Type: Array + * Default value: ["index.html"] + * + * Extend the list with any resources you need. Works with options.removeDirectoryIndexes. + */ + directoryIndexes?: Array; + + /** + * Type: Boolean + * Default value: false + * + * This will, for example, consider any domains containing http://www.example.com/ to be related to any that contain http://example.com/. + */ + ignore_www?: boolean; + + /** + * Type: constant or String + * Choices: RelateUrl.ABSOLUTE,RelateUrl.PATH_RELATIVE,RelateUrl.ROOT_RELATIVE,RelateUrl.SHORTEST + * Choices: "absolute","pathRelative","rootRelative","shortest" + * Default value: RelateUrl.SHORTEST + * + * RelateUrl.ABSOLUTE will produce an absolute URL. Overrides options.schemeRelative with a value of false. + * RelateUrl.PATH_RELATIVE will produce something like ../child-of-parent/etc/. + * RelateUrl.ROOT_RELATIVE will produce something like /child-of-root/etc/. + * RelateUrl.SHORTEST will choose whichever is shortest between root- and path-relative. + */ + output?: string; + + /** + * Type: Array + * Default value: ["data","javascript","mailto"] + * + * Extend the list with any additional schemes. Example: javascript:something will not be modified. + */ + rejectedSchemes?: Array; + + /** + * Type: Boolean + * Default value: false + * + * Remove user authentication information from the output URL. + */ + removeAuth?: boolean; + + /** + * Type: Boolean + * Default value: true + * + * Remove any resources that match any found in options.directoryIndexes. + */ + removeDirectoryIndexes?: boolean; + + /** + * Type: Boolean + * Default value: false + * + * Remove empty query variables. Example: http://domain.com/?var1&var2=&var3=asdf will become http://domain.com/?var3=adsf. This does not apply to unrelated URLs (with other protocols, auths, hosts and/or ports). + */ + removeEmptyQueries?: boolean; + + /** + * Type: Boolean + * Default value: true + * + * Remove trailing slashes from root paths. Example: http://domain.com/?var will become http://domain.com?var while http://domain.com/dir/?var will not be modified. + */ + removeRootTrailingSlash?: boolean; + + /** + * Type: Boolean + * Default value: true + * + * Output URLs relative to the scheme. Example: http://example.com/ will become //example.com/. + */ + schemeRelative?: boolean; + + /** + * Type: String + * Default value: undefined + * + * An options-based version of the from argument. If both are specified, from takes priority. + */ + site?: string; + + /** + * Type: Boolean + * Default value: true + * + * Passed to Node's url.parse. + */ + slashesDenoteHost?: boolean; + } + } + + class RelateUrl { + static ABSOLUTE: string; + static PATH_RELATIVE: string; + static ROOT_RELATIVE: string; + static SHORTEST: string; + + static relate(from: string, to: string, options?: RelateUrl.Options): string; + + constructor(from: string, options?: RelateUrl.Options); + relate(to: string, options?: RelateUrl.Options): string; + } + + export = RelateUrl; +} diff --git a/uglify-js/uglify-js-tests.ts b/uglify-js/uglify-js-tests.ts new file mode 100644 index 0000000000..23b935dec2 --- /dev/null +++ b/uglify-js/uglify-js-tests.ts @@ -0,0 +1,73 @@ +/// +/// + +import * as UglifyJS from 'uglify-js'; +import * as fs from 'fs'; + +var result = UglifyJS.minify("/path/to/file.js"); +console.log(result.code); // minified output +// if you need to pass code instead of file name +var result = UglifyJS.minify("var b = function () {};", {fromString: true}); + +var result = UglifyJS.minify([ "file1.js", "file2.js", "file3.js" ]); +console.log(result.code); + +var result = UglifyJS.minify([ "file1.js", "file2.js", "file3.js" ], { + outSourceMap: "out.js.map" +}); +console.log(result.code); // minified output +console.log(result.map); + +var result = UglifyJS.minify([ "file1.js", "file2.js", "file3.js" ], { + outSourceMap: "out.js.map", + sourceRoot: "http://example.com/src" +}); + +var result = UglifyJS.minify("compiled.js", { + inSourceMap: "compiled.js.map", + outSourceMap: "minified.js.map" +}); +// same as before, it returns `code` and `map` + +const my_source_map_string = 'sourceMap'; +var result = UglifyJS.minify("compiled.js", { + inSourceMap: JSON.parse(my_source_map_string), + outSourceMap: "minified.js.map" +}); + +var toplevel_ast = UglifyJS.parse(code, {}); + +var toplevel: UglifyJS.AST_Toplevel = null; +const files = ['file1', 'file2']; +files.forEach(function(file){ + var code = fs.readFileSync(file, "utf8"); + toplevel = UglifyJS.parse(code, { + filename: file, + toplevel: toplevel + }); +}); + +toplevel.figure_out_scope() + +var compressor = UglifyJS.Compressor({}); +var compressed_ast = toplevel.transform(compressor); + +compressed_ast.figure_out_scope(); +compressed_ast.compute_char_frequency(); +compressed_ast.mangle_names(); + +var stream = UglifyJS.OutputStream({}); +compressed_ast.print(stream); +var code = stream.toString(); // this is your minified code + +var code = compressed_ast.print_to_string({}); + +var source_map = UglifyJS.SourceMap({}); +var stream = UglifyJS.OutputStream({ + //... + source_map: source_map +}); +compressed_ast.print(stream); + +var code = stream.toString(); +var map = source_map.toString(); // json output for your source map diff --git a/uglify-js/uglify-js.d.ts b/uglify-js/uglify-js.d.ts new file mode 100644 index 0000000000..b9c22bc6ed --- /dev/null +++ b/uglify-js/uglify-js.d.ts @@ -0,0 +1,430 @@ +// Type definitions for UglifyJS 2 v2.6.1 +// Project: https://github.com/mishoo/UglifyJS2 +// Definitions by: Tanguy Krotoff +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +declare module 'uglify-js' { + import * as MOZ_SourceMap from 'source-map'; + + namespace UglifyJS { + interface Tokenizer { + /** + * The type of this token. + * Can be "num", "string", "regexp", "operator", "punc", "atom", "name", "keyword", "comment1" or "comment2". + * "comment1" and "comment2" are for single-line, respectively multi-line comments. + */ + type: string; + + /** + * The name of the file where this token originated from. Useful when compressing multiple files at once to generate the proper source map. + */ + file: string; + + /** + * The "value" of the token. + * That's additional information and depends on the token type: "num", "string" and "regexp" tokens you get their literal value. + * - For "operator" you get the operator. + * - For "punc" it's the punctuation sign (parens, comma, semicolon etc). + * - For "atom", "name" and "keyword" it's the name of the identifier + * - For comments it's the body of the comment (excluding the initial "//" and "/*". + */ + value: string; + + /** + * The line number of this token in the original code. + * 1-based index. + */ + line: number; + + /** + * The column number of this token in the original code. + * 0-based index. + */ + col: number; + + /** + * Short for "newline before", it's a boolean that tells us whether there was a newline before this node in the original source. It helps for automatic semicolon insertion. + * For multi-line comments in particular this will be set to true if there either was a newline before this comment, or * * if this comment contains a newline. + */ + nlb: boolean; + + /** + * This doesn't apply for comment tokens, but for all other token types it will be an array of comment tokens that were found before. + */ + comments_before: string[]; + } + + interface AST_Node { + // The first token of this node + start: AST_Node; + + // The last token of this node + end: AST_Node; + + transform(tt: TreeTransformer): AST_Toplevel; + } + + interface AST_Toplevel extends AST_Node { + // UglifyJS contains a scope analyzer which figures out variable/function definitions, references etc. + // You need to call it manually before compression or mangling. + // The figure_out_scope method is defined only on the AST_Toplevel node. + figure_out_scope(): void; + + // Get names that are optimized for GZip compression (names will be generated using the most frequent characters first) + compute_char_frequency(): void; + + mangle_names(): void; + + print(stream: OutputStream): void; + + print_to_string(options?: BeautifierOptions): string; + } + + interface MinifyOptions { + spidermonkey?: boolean; + outSourceMap?: string; + sourceRoot?: string; + inSourceMap?: string; + fromString?: boolean; + warnings?: boolean; + mangle?: Object; + output?: MinifyOutput, + compress?: Object; + } + + interface MinifyOutput { + code: string; + map: string; + } + + function minify(files: string | Array, options?: MinifyOptions): MinifyOutput; + + + interface ParseOptions { + // Default is false + strict?: boolean; + + // Input file name, default is null + filename?: string; + + // Default is null + toplevel?: AST_Toplevel; + } + + /** + * The parser creates a custom abstract syntax tree given a piece of JavaScript code. + * Perhaps you should read about the AST first. + */ + function parse(code: string, options?: ParseOptions): AST_Toplevel; + + + interface BeautifierOptions { + /** + * Start indentation on every line (only when `beautify`) + */ + indent_start?: number; + + /** + * Indentation level (only when `beautify`) + */ + indent_level?: number; + + /** + * Quote all keys in object literals? + */ + quote_keys?: boolean; + + /** + * Add a space after colon signs? + */ + space_colon?: boolean; + + /** + * Output ASCII-safe? (encodes Unicode characters as ASCII) + */ + ascii_only?: boolean; + + /** + * Escape " boolean; + + /** + * UglifyJS provides a TreeWalker object and every node has a walk method that given a walker will apply your visitor to each node in the tree. + * Your visitor can return a non-falsy value in order to prevent descending the current node. + */ + function TreeWalker(visitor: visitor): TreeWalker; + + + // TODO + interface TreeTransformer extends TreeWalker { + } + + /** + * The tree transformer is a special case of a tree walker. + * In fact it even inherits from TreeWalker and you can use the same methods, but initialization and visitor protocol are a bit different. + */ + function TreeTransformer(before: visitor, after: visitor): TreeTransformer; + } + + export = UglifyJS; +} diff --git a/webpack/webpack.d.ts b/webpack/webpack.d.ts index 5bf5050b82..be4c22328e 100644 --- a/webpack/webpack.d.ts +++ b/webpack/webpack.d.ts @@ -3,7 +3,11 @@ // Definitions by: Qubo // Definitions: https://github.com/borisyankov/DefinitelyTyped +/// + declare module "webpack" { + import * as UglifyJS from 'uglify-js'; + namespace webpack { interface Configuration { context?: string; @@ -426,7 +430,7 @@ declare module "webpack" { new(preferEntry: boolean): Plugin; } interface UglifyJsPluginStatic { - new(options?: any): Plugin; + new(options?: UglifyJS.MinifyOptions): Plugin; } interface CommonsChunkPluginStatic { new(chunkName: string, filenames?: string|string[]): Plugin;