summaryrefslogtreecommitdiff
path: root/node_modules/babel-plugin-uglify/dist/index.js
blob: 9a0b29c6a9591cc1edb9fe938c285e2399bf5277 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
'use strict';

exports.__esModule = true;

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }

function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var _uglifyJs = require('uglify-js');

var compressor = _uglifyJs.Compressor();

var LocationFixer = (function (_TreeWalker) {
	_inherits(LocationFixer, _TreeWalker);

	function LocationFixer(path) {
		_classCallCheck(this, LocationFixer);

		_TreeWalker.call(this, function (node) {
			node.start.file = node.end.file = path.hub.file.opts.filenameRelative;
		});
	}

	return LocationFixer;
})(_uglifyJs.TreeWalker);

exports['default'] = function (_ref) {
	var Plugin = _ref.Plugin;
	var t = _ref.types;
	return new Plugin('uglify', {
		visitor: {
			Program: function Program(ast) {
				// Convert to UglifyJS AST
				var uAST = _uglifyJs.AST_Node.from_mozilla_ast(ast);

				// Fix locations (Babel doesn't insert `loc.source` into nodes for some reason)
				uAST.walk(new LocationFixer(this));

				// Compress
				uAST.figure_out_scope();
				uAST = uAST.transform(compressor);

				// Mangle names
				uAST.figure_out_scope();
				uAST.compute_char_frequency();
				uAST.mangle_names();

				// Convert back to ESTree AST
				return uAST.to_mozilla_ast();
			}
		}
	});
};

module.exports = exports['default'];