├── .npmrc ├── .travis.yml ├── .gitignore ├── CHANGELOG.md ├── LICENSE.md ├── package.json ├── index.js ├── README.md └── test └── index.js /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - 14 4 | - 12 5 | - 10 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # node-waf configuration 20 | .lock-wscript 21 | 22 | # Compiled binary addons (http://nodejs.org/api/addons.html) 23 | build/Release 24 | 25 | # Dependency directory 26 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git 27 | node_modules 28 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # minify-stream change log 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | This project adheres to [Semantic Versioning](http://semver.org/). 6 | 7 | ## 2.1.0 8 | * Support asynchronous uglify implementations. 9 | You can now use minify-stream with terser v5: 10 | ```js 11 | var minifyStream = require('minify-stream') 12 | var minifier = minifyStream({ uglify: require('terser') }) 13 | ``` 14 | 15 | ## 2.0.1 16 | * Unpin default terser version. 17 | 18 | ## 2.0.0 19 | * Upgrade default terser version to v4. 20 | 21 | ## 1.2.1 22 | * Pin terser to a version that does uses ES5 syntax. 23 | * Update streams dependencies. 24 | 25 | ## 1.2.0 26 | * Switch to [terser](https://github.com/fabiosantoscode/terser) as the default uglify module. ([#1](https://github.com/goto-bus-stop/minify-stream/pull/1)) 27 | 28 | ## 1.1.0 29 | * Now outputs sourcemaps by default 30 | 31 | ## 1.0.0 32 | * Initial release 33 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # [MIT License](https://spdx.org/licenses/MIT) 2 | 3 | Copyright (c) 2017 Renée Kooi 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "minify-stream", 3 | "description": "minify javascript in a stream using uglify-js", 4 | "version": "2.1.0", 5 | "author": "Renée Kooi ", 6 | "bugs": { 7 | "url": "https://github.com/goto-bus-stop/minify-stream/issues" 8 | }, 9 | "dependencies": { 10 | "concat-stream": "^2.0.0", 11 | "convert-source-map": "^1.5.0", 12 | "duplexify": "^4.1.1", 13 | "from2-string": "^1.1.0", 14 | "terser": "^5.2.1", 15 | "xtend": "^4.0.1" 16 | }, 17 | "devDependencies": { 18 | "dedent": "^0.7.0", 19 | "pump": "^3.0.0", 20 | "standard": "^15.0.0", 21 | "tap-spec": "^5.0.0", 22 | "tape": "^5.0.1" 23 | }, 24 | "engines": { 25 | "node": ">= 6" 26 | }, 27 | "homepage": "https://github.com/goto-bus-stop/minify-stream", 28 | "keywords": [ 29 | "minify", 30 | "stream", 31 | "uglify-js" 32 | ], 33 | "license": "MIT", 34 | "main": "index.js", 35 | "repository": { 36 | "type": "git", 37 | "url": "https://github.com/goto-bus-stop/minify-stream.git" 38 | }, 39 | "scripts": { 40 | "test": "standard && tape test/*.js | tap-spec" 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var duplexify = require('duplexify') 2 | var concat = require('concat-stream') 3 | var fromString = require('from2-string') 4 | var defaultUglify = require('terser') 5 | var convert = require('convert-source-map') 6 | var xtend = require('xtend') 7 | 8 | module.exports = uglifyStream 9 | 10 | function defaultOpts () { 11 | return { 12 | sourceMap: { content: 'inline' } 13 | } 14 | } 15 | 16 | function uglifyStream (opts) { 17 | opts = xtend(defaultOpts(), opts || {}) 18 | var uglify = opts.uglify || defaultUglify 19 | delete opts.uglify 20 | 21 | var stream = duplexify() 22 | 23 | var writer = concat({ encoding: 'string' }, function (source) { 24 | var result = uglify.minify(source, opts) 25 | if (result.then) { 26 | result.then(onsuccess, onerror) 27 | } else if (result.error) { 28 | return onerror(result.error) 29 | } else onsuccess(result) 30 | 31 | function onsuccess (minified) { 32 | var final = minified.code 33 | if (minified.map) { 34 | final += '\n' + convert.fromJSON(minified.map).toComment() 35 | } 36 | var reader = fromString(final) 37 | stream.setReadable(reader) 38 | } 39 | function onerror (error) { 40 | stream.emit('error', error) 41 | } 42 | }) 43 | 44 | stream.setWritable(writer) 45 | 46 | return stream 47 | } 48 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # minify-stream 2 | 3 | minify javascript in a stream using uglify-js 4 | 5 | [![npm][npm-image]][npm-url] 6 | [![travis][travis-image]][travis-url] 7 | [![standard][standard-image]][standard-url] 8 | 9 | [npm-image]: https://img.shields.io/npm/v/minify-stream.svg?style=flat-square 10 | [npm-url]: https://www.npmjs.com/package/minify-stream 11 | [travis-image]: https://img.shields.io/travis/goto-bus-stop/minify-stream.svg?style=flat-square 12 | [travis-url]: https://travis-ci.org/goto-bus-stop/minify-stream 13 | [standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square 14 | [standard-url]: http://npm.im/standard 15 | 16 | ## Install 17 | 18 | ``` 19 | npm install minify-stream 20 | ``` 21 | 22 | ## Usage 23 | 24 | ```js 25 | var minifyStream = require('minify-stream') 26 | 27 | fs.createReadStream('app.js') 28 | .pipe(minifyStream()) 29 | .pipe(fs.createWriteStream('app.min.js')) 30 | ``` 31 | 32 | ## API 33 | 34 | ### `minifyStream(?options)` 35 | 36 | Create a new minify stream. Write a Javascript file or bundle to it. 37 | Possible `options` are: 38 | 39 | - `uglify` - An uglify module to use, defaults to [`terser`](https://npmjs.com/package/terser). 40 | It must have an uglify-compatible `minify()` function. 41 | - All other options are passed to the `minify()` function as the second parameter. 42 | See the [terser docs](https://github.com/fabiosantoscode/terser#minify-options) for available options. 43 | 44 | `minify-stream` adds inline source maps by default. Use [`exorcist`](https://npmjs.com/package/exorcist) 45 | to extract source maps from the output stream into a separate file. If you don't need source maps, pass 46 | the `sourceMap: false` option to disable them. 47 | 48 | ```js 49 | minifyStream({ sourceMap: false }) 50 | ``` 51 | 52 | ## License 53 | 54 | [MIT](LICENSE.md) 55 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | var test = require('tape') 2 | var minify = require('../') 3 | var pump = require('pump') 4 | var fromString = require('from2-string') 5 | var concat = require('concat-stream') 6 | var dedent = require('dedent') 7 | 8 | test('Example Test', function (t) { 9 | t.plan(2) 10 | fromString(dedent` 11 | global.value = (function a () { 12 | var b = 2 13 | function test () { 14 | return 1 + b 15 | } 16 | return test() 17 | }()) 18 | `).pipe(minify()).pipe(concat({ encoding: 'string' }, function (result) { 19 | t.ok(result) 20 | eval(result) // eslint-disable-line no-eval 21 | t.equal(global.value, 3) 22 | delete global.value 23 | })) 24 | }) 25 | 26 | test('emits errors', function (t) { 27 | t.plan(2) 28 | var src = dedent` 29 | (function() { 30 | syntax error; 31 | })(); 32 | ` 33 | 34 | var stream = minify() 35 | stream.on('data', function () {}) 36 | stream.on('error', function (err) { 37 | t.ok(err) 38 | t.ok(/Unexpected token: name/.test(err.message)) 39 | }) 40 | stream.end(src) 41 | }) 42 | 43 | test('supports es2015 syntax', function (t) { 44 | t.plan(1) 45 | 46 | var src = dedent` 47 | const fn = (...args) => { 48 | return args.map(x => x ** 2) 49 | } 50 | ` 51 | 52 | var stream = minify() 53 | stream.pipe(concat({ encoding: 'string' }, done)) 54 | stream.on('error', t.fail) 55 | stream.end(src) 56 | 57 | function done (result) { 58 | result = result.toString() 59 | t.notEqual(result, src) 60 | } 61 | }) 62 | 63 | test('handles sourcemaps', function (t) { 64 | t.plan(1) 65 | var input = dedent` 66 | "use strict"; 67 | 68 | Object.defineProperty(exports, "__esModule", { 69 | value: true 70 | }); 71 | 72 | exports.default = function (a) { 73 | return a * 2; 74 | }; 75 | 76 | //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImFyci5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7a0JBQWUsVUFBQyxDQUFEO0FBQUEsU0FBTyxJQUFJLENBQVg7QUFBQSxDIiwiZmlsZSI6InN0ZG91dCIsInNvdXJjZXNDb250ZW50IjpbImV4cG9ydCBkZWZhdWx0IChhKSA9PiBhICogMlxuIl19 77 | ` 78 | 79 | pump( 80 | fromString(input), 81 | minify(), 82 | concat({ encoding: 'string' }, done) 83 | ).on('error', t.fail) 84 | 85 | function done (result) { 86 | var expected = dedent` 87 | "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=function(e){return 2*e}; 88 | //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImFyci5qcyJdLCJuYW1lcyI6WyJhIl0sIm1hcHBpbmdzIjoib0ZBQWUsU0FBQ0EsR0FBRCxPQUFXLEVBQUpBIiwic291cmNlc0NvbnRlbnQiOlsiZXhwb3J0IGRlZmF1bHQgKGEpID0+IGEgKiAyXG4iXX0= 89 | ` 90 | 91 | t.equal(result, expected) 92 | } 93 | }) 94 | --------------------------------------------------------------------------------