Skip to content
This repository has been archived by the owner on Feb 9, 2023. It is now read-only.

Commit

Permalink
Rename to terser-brunch
Browse files Browse the repository at this point in the history
* update deps

* fix capitalization & grammar

* update code

* update tests

* run on latest nodes

* use exact versions

* add link to api

* use uglify-es

* add modern js test

* link to harmony

* add credits

* Delete CHANGELOG.md

* Update .travis.yml

* Clean up dotfiles

* Update README.md

* Update test.js

* Update index.js

* Update package.json

* Update package.json

* Code tweak
  • Loading branch information
shvaikalesh authored and paulmillr committed Feb 21, 2019
1 parent 8c67a01 commit c0e84b2
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 206 deletions.
5 changes: 1 addition & 4 deletions .gitignore
@@ -1,5 +1,2 @@
*~
*.bak
.DS_Store
npm-debug.log
npm-debug.log*
node_modules/
6 changes: 1 addition & 5 deletions .npmignore
@@ -1,6 +1,2 @@
*~
*.bak
.DS_Store
npm-debug.log
.travis.yml
test.js
CHANGELOG.md
5 changes: 2 additions & 3 deletions .travis.yml
@@ -1,5 +1,4 @@
language: node_js
node_js:
- 'node'
- '5'
- '4'
- node
- 8
57 changes: 0 additions & 57 deletions CHANGELOG.md

This file was deleted.

41 changes: 18 additions & 23 deletions README.md
@@ -1,47 +1,42 @@
# uglify-js-brunch
# terser-brunch

Adds [UglifyJS](https://github.com/mishoo/UglifyJS2) support to
[brunch](http://brunch.io).
Adds [Terser](https://github.com/terser-js/terser) support to [Brunch](http://brunch.io).

The plugin will minify your javascript files.
The plugin will minify your JavaScript files. Supports modern language features.

## Usage

Install the plugin via npm with `npm install --save-dev uglify-js-brunch`.
Previously known as `uglify-js-brunch`.

Or, do manual install:
## Usage

* Add `"uglify-js-brunch": "x.y.z"` to `package.json` of your brunch app. Pick a plugin version that corresponds to your minor (y) brunch version.
* If you want to use git version of plugin, add
`"uglify-js-brunch": "git+ssh://git@github.com:brunch/uglify-js-brunch.git"`.
Install the plugin via npm with `npm install --save-dev terser-brunch`.

To specify UglifyJS options, use `config.plugins.uglify` object, for example:
To specify [Terser options](https://github.com/terser-js/terser#minify-options), use `config.plugins.terser` object, for example:

```js
module.exports = {
// ...
plugins: {
uglify: {
terser: {
mangle: false,
compress: {
global_defs: {
DEBUG: false
}
}
}
}
DEBUG: false,
},
},
},
},
};
```

Joined files can be ignored and be passed-through, using 'ignored' option:
Joined files can be ignored and be passed-through, using `ignored` option:

```js
module.exports = {
plugins: {
uglify: {
ignored: /non_minimize\.js/
}
}
terser: {
ignored: /dont-minimize\.js/,
},
},
};
```

Expand Down
78 changes: 32 additions & 46 deletions index.js
@@ -1,65 +1,51 @@
'use strict';
const {minify} = require('terser');
const anymatch = require('anymatch');

const uglify = require('uglify-js');

const formatError = (error) => {
const err = new Error(`L${error.line}:${error.col} ${error.message}`);
err.name = '';
err.stack = error.stack;
const formatError = err => {
err.message = `L${err.line}:${err.col} ${err.message}`;
return err;
};

class UglifyJSOptimizer {
class TerserOptimizer {
constructor(config) {
this.options = Object.assign({}, config.plugins.uglify);
this.options.fromString = true;
this.options.sourceMaps = !!config.sourceMaps;
const {ignored, ...options} = config.plugins.terser || {};

this.isIgnored = anymatch(ignored);
this.options = {
sourceMap: !!config.sourceMaps,
...options,
};
}

optimize(file) {
const data = file.data;
const path = file.path;

try {
if (this.options.ignored && this.options.ignored.test(file.path)) {
// ignored file path: return non minified
const result = {
data,
// brunch passes in a SourceMapGenerator object, but wants a string back.
map: file.map ? file.map.toString() : null,
};
return Promise.resolve(result);
}
} catch (e) {
return Promise.reject(`error checking ignored files to uglify ${e}`);
if (this.isIgnored(file.path)) {
return {
data: file.data,
map: file.map && `${file.map}`,
};
}

const options = {...this.options};
if (file.map) {
this.options.inSourceMap = file.map.toJSON();
options.sourceMap = {
content: JSON.stringify(file.map),
url: `${file.path}.map`,
};
}

this.options.outSourceMap = this.options.sourceMaps ?
`${path}.map` : undefined;
const res = minify(file.data, options);
if (res.error) throw formatError(res.error);
if (!res.map) return {data: res.code};

try {
const optimized = uglify.minify(data, this.options);

const result = optimized && this.options.sourceMaps ? {
data: optimized.code,
map: optimized.map,
} : {
data: optimized.code,
};
result.data = result.data.replace(/\n\/\/# sourceMappingURL=\S+$/, '');

return Promise.resolve(result);
} catch (err) {
return Promise.reject(formatError(err));
}
return {
data: res.code.replace(/\/\/# sourceMappingURL=\S+$/, ''),
map: res.map,
};
}
}

UglifyJSOptimizer.prototype.brunchPlugin = true;
UglifyJSOptimizer.prototype.type = 'javascript';
TerserOptimizer.prototype.brunchPlugin = true;
TerserOptimizer.prototype.type = 'javascript';

module.exports = UglifyJSOptimizer;
module.exports = TerserOptimizer;
38 changes: 21 additions & 17 deletions package.json
@@ -1,27 +1,31 @@
{
"name": "uglify-js-brunch",
"version": "2.10.0",
"description": "Adds Uglify minifying support to brunch.",
"author": "Paul Miller (http://paulmillr.com/)",
"homepage": "https://github.com/brunch/uglify-js-brunch",
"keywords": ["brunchplugin", "uglify", "uglify-js", "optimizer"],
"repository": {
"type": "git",
"url": "git@github.com:brunch/uglify-js-brunch.git"
},
"version": "3.0.0",
"description": "Adds Terser mangler/compressor support to Brunch",
"keywords": [
"brunch-plugin",
"terser",
"uglify",
"minifier",
"optimizer"
],
"homepage": "https://github.com/brunch/terser-brunch",
"bugs": "https://github.com/brunch/terser-brunch/issues",
"license": "MIT",
"author": "Paul Miller (http://paulmillr.com)",
"repository": "brunch/terser-brunch",
"scripts": {
"test": "eslint index.js && mocha"
"test": "mocha -r chai/register-should test"
},
"dependencies": {
"uglify-js": "~2.6.1"
"terser": "^3",
"anymatch": "^2"
},
"devDependencies": {
"chai": "~1.9.0",
"eslint": "^3.12.2",
"eslint-config-brunch": "^1",
"mocha": "~1.17.1"
"chai": "^4",
"mocha": "^6"
},
"eslintConfig": {
"extends": "brunch"
"peerDependencies": {
"brunch": "^2"
}
}

0 comments on commit c0e84b2

Please sign in to comment.