Projekt

Obecné

Profil

Stáhnout (5.48 KB) Statistiky
| Větev: | Revize:
1
"use strict";
2

    
3
Object.defineProperty(exports, "__esModule", {
4
  value: true
5
});
6
exports.default = void 0;
7

    
8
var _terser = require("terser");
9

    
10
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
11

    
12
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
13

    
14
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
15

    
16
const buildTerserOptions = ({
17
  ecma,
18
  warnings,
19
  parse = {},
20
  compress = {},
21
  mangle,
22
  module,
23
  output,
24
  toplevel,
25
  nameCache,
26
  ie8,
27

    
28
  /* eslint-disable camelcase */
29
  keep_classnames,
30
  keep_fnames,
31

    
32
  /* eslint-enable camelcase */
33
  safari10
34
} = {}) => ({
35
  ecma,
36
  warnings,
37
  parse: _objectSpread({}, parse),
38
  compress: typeof compress === 'boolean' ? compress : _objectSpread({}, compress),
39
  // eslint-disable-next-line no-nested-ternary
40
  mangle: mangle == null ? true : typeof mangle === 'boolean' ? mangle : _objectSpread({}, mangle),
41
  output: _objectSpread({
42
    shebang: true,
43
    comments: false,
44
    beautify: false,
45
    semicolons: true
46
  }, output),
47
  module,
48
  // Ignoring sourceMap from options
49
  sourceMap: null,
50
  toplevel,
51
  nameCache,
52
  ie8,
53
  keep_classnames,
54
  keep_fnames,
55
  safari10
56
});
57

    
58
const buildComments = (options, terserOptions, extractedComments) => {
59
  const condition = {};
60
  const commentsOpts = terserOptions.output.comments; // Use /^\**!|@preserve|@license|@cc_on/i RegExp
61

    
62
  if (typeof options.extractComments === 'boolean') {
63
    condition.preserve = commentsOpts;
64
    condition.extract = /^\**!|@preserve|@license|@cc_on/i;
65
  } else if (typeof options.extractComments === 'string' || options.extractComments instanceof RegExp) {
66
    // extractComments specifies the extract condition and commentsOpts specifies the preserve condition
67
    condition.preserve = commentsOpts;
68
    condition.extract = options.extractComments;
69
  } else if (typeof options.extractComments === 'function') {
70
    condition.preserve = commentsOpts;
71
    condition.extract = options.extractComments;
72
  } else if (Object.prototype.hasOwnProperty.call(options.extractComments, 'condition')) {
73
    // Extract condition is given in extractComments.condition
74
    condition.preserve = commentsOpts;
75
    condition.extract = options.extractComments.condition;
76
  } else {
77
    // No extract condition is given. Extract comments that match commentsOpts instead of preserving them
78
    condition.preserve = false;
79
    condition.extract = commentsOpts;
80
  } // Ensure that both conditions are functions
81

    
82

    
83
  ['preserve', 'extract'].forEach(key => {
84
    let regexStr;
85
    let regex;
86

    
87
    switch (typeof condition[key]) {
88
      case 'boolean':
89
        condition[key] = condition[key] ? () => true : () => false;
90
        break;
91

    
92
      case 'function':
93
        break;
94

    
95
      case 'string':
96
        if (condition[key] === 'all') {
97
          condition[key] = () => true;
98

    
99
          break;
100
        }
101

    
102
        if (condition[key] === 'some') {
103
          condition[key] = (astNode, comment) => {
104
            return comment.type === 'comment2' && /^\**!|@preserve|@license|@cc_on/i.test(comment.value);
105
          };
106

    
107
          break;
108
        }
109

    
110
        regexStr = condition[key];
111

    
112
        condition[key] = (astNode, comment) => {
113
          return new RegExp(regexStr).test(comment.value);
114
        };
115

    
116
        break;
117

    
118
      default:
119
        regex = condition[key];
120

    
121
        condition[key] = (astNode, comment) => regex.test(comment.value);
122

    
123
    }
124
  }); // Redefine the comments function to extract and preserve
125
  // comments according to the two conditions
126

    
127
  return (astNode, comment) => {
128
    if (condition.extract(astNode, comment)) {
129
      const commentText = comment.type === 'comment2' ? `/*${comment.value}*/` : `//${comment.value}`; // Don't include duplicate comments
130

    
131
      if (!extractedComments.includes(commentText)) {
132
        extractedComments.push(commentText);
133
      }
134
    }
135

    
136
    return condition.preserve(astNode, comment);
137
  };
138
};
139

    
140
const minify = options => {
141
  const {
142
    file,
143
    input,
144
    inputSourceMap,
145
    extractComments,
146
    minify: minifyFn
147
  } = options;
148

    
149
  if (minifyFn) {
150
    return minifyFn({
151
      [file]: input
152
    }, inputSourceMap);
153
  } // Copy terser options
154

    
155

    
156
  const terserOptions = buildTerserOptions(options.terserOptions); // Let terser generate a SourceMap
157

    
158
  if (inputSourceMap) {
159
    terserOptions.sourceMap = true;
160
  }
161

    
162
  const extractedComments = [];
163

    
164
  if (extractComments) {
165
    terserOptions.output.comments = buildComments(options, terserOptions, extractedComments);
166
  }
167

    
168
  const {
169
    error,
170
    map,
171
    code,
172
    warnings
173
  } = (0, _terser.minify)({
174
    [file]: input
175
  }, terserOptions);
176
  return {
177
    error,
178
    map,
179
    code,
180
    warnings,
181
    extractedComments
182
  };
183
};
184

    
185
var _default = minify;
186
exports.default = _default;
(4-4/6)