UNPKG

2.11 kBJavaScriptView Raw
1'use strict';
2
3exports.__esModule = true;
4
5function _classCallCheck(instance, Constructor) {
6 if (!(instance instanceof Constructor)) {
7 throw new TypeError('Cannot call a class as a function');
8 }
9}
10
11function _inherits(subClass, superClass) {
12 if (typeof superClass !== 'function' && superClass !== null) {
13 throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass);
14 }
15 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; }
16
17var _uglifyJs = require('uglify-js');
18
19var compressor = _uglifyJs.Compressor();
20
21var LocationFixer = (function (_TreeWalker) {
22 _inherits(LocationFixer, _TreeWalker);
23
24 function LocationFixer(path) {
25 _classCallCheck(this, LocationFixer);
26
27 _TreeWalker.call(this, function (node) {
28 node.start.file = node.end.file = path.hub.file.opts.filenameRelative;
29 });
30 }
31
32 return LocationFixer;
33})(_uglifyJs.TreeWalker);
34
35exports['default'] = function (_ref) {
36 var Plugin = _ref.Plugin;
37 var t = _ref.types;
38 return new Plugin('uglify', {
39 visitor: {
40 Program: function (ast) {
41 // Convert to UglifyJS AST
42 var uAST = _uglifyJs.AST_Node.from_mozilla_ast(ast);
43 // Fix locations (Babel doesn't insert `loc.source` into nodes for some reason)
44 uAST.walk(new LocationFixer(this));
45
46 // Compress
47 uAST.figure_out_scope();
48 uAST = uAST.transform(compressor);
49
50 // Mangle names
51 uAST.figure_out_scope();
52 uAST.compute_char_frequency();
53 uAST.mangle_names();
54
55 // Convert back to ESTree AST
56 var result = uAST.to_mozilla_ast();
57 return result;
58 }
59 }
60 });
61};
62
63module.exports = exports['default'];