minify-js 0.4.3

Extremely fast JavaScript minifier
Documentation
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
use parse_js::{
    ast::{
        ClassOrObjectMemberKey, ClassOrObjectMemberValue, ExportName, ExportNames, NodeId, NodeMap,
        ObjectMemberType, Syntax,
    },
    char::{ID_CONTINUE_CHARSTR, ID_START_CHARSTR},
    lex::KEYWORD_STRS,
    source::SourceRange,
    symbol::{ScopeId, ScopeMap, SymbolMap},
    update::NodeUpdates,
    visit::{JourneyControls, Visitor},
};

const JSX_COMPONENT_NAME_PREFIX: char = '\u{01BC}';
const ALT_MINIFIED_NAMES: &'static [char] = &[
    '\u{01BB}', '\u{02B0}', '\u{02B1}', '\u{02B2}', '\u{02B3}', '\u{02B4}', '\u{02B5}', '\u{02B6}',
    '\u{02B7}', '\u{02B8}', '\u{02B9}', '\u{02BA}', '\u{02BB}', '\u{02BC}', '\u{02BD}', '\u{02BE}',
    '\u{02BF}', '\u{02C0}', '\u{02C1}', '\u{02C6}', '\u{02C7}', '\u{02C8}', '\u{02C9}', '\u{02CA}',
    '\u{02CB}', '\u{02CC}', '\u{02CD}', '\u{02CE}', '\u{02CF}', '\u{02D0}', '\u{02D1}', '\u{02E0}',
    '\u{02E1}', '\u{02E2}', '\u{02E3}', '\u{02E4}', '\u{02EC}', '\u{02EE}', '\u{0374}', '\u{037A}',
    '\u{0559}', '\u{0640}', '\u{06E5}', '\u{06E6}', '\u{07F4}', '\u{07F5}', '\u{07FA}',
];

struct ExportBinding {
    target: SourceRange,
    alias: SourceRange,
}

#[derive(Default)]
struct MinifySymbol {
    // Set to 0 initially, before minification pass. WARNING: 0 is still a valid value, so do not use before setting.
    minified_name_id: usize,
    is_used_as_jsx_component: bool,
}

impl MinifySymbol {
    fn set_minified_name_id(&mut self, id: usize) {
        self.minified_name_id = id;
    }

    fn mark_as_used_as_jsx_component(&mut self) {
        self.is_used_as_jsx_component = true;
    }

    fn generate_minified_name(&self) -> SourceRange {
        let mut id = self.minified_name_id;
        let mut name = Vec::<u8>::new();
        // See main function at bottom for why we do this.
        if self.is_used_as_jsx_component {
            name.extend(b"  ");
            JSX_COMPONENT_NAME_PREFIX.encode_utf8(&mut name[0..2]);
        }
        name.push(ID_START_CHARSTR[id % ID_START_CHARSTR.len()]);
        if id >= ID_START_CHARSTR.len() {
            id /= ID_START_CHARSTR.len();
            while id >= ID_CONTINUE_CHARSTR.len() {
                name.push(ID_CONTINUE_CHARSTR[id % ID_CONTINUE_CHARSTR.len()]);
                id /= ID_CONTINUE_CHARSTR.len();
            }
            name.push(ID_CONTINUE_CHARSTR[id % ID_CONTINUE_CHARSTR.len()]);
        };
        if let Some(alt_id) = KEYWORD_STRS.get(name.as_slice()) {
            // This name is a keyword, so we replace it with a Unicode character instead.
            // This Unicode character is 2 bytes when encoded in UTF-8, so it's more than minimal enough. UTF-8 encodes U+0080 to U+07FF in 2 bytes.
            // There should be exactly one ALT_MINIFIED_NAMES element for each KEYWORD_STRS entry.
            // Using a Unicode name will ensure no chance of clashing with keywords, well-knowns, and almost all variables.
            // Clashes can appear quickly e.g. `in`, `of`, `if`.
            // TODO This could still clash with an untracked or unminified variable; however, that's technically true of any minified name.
            let s = ALT_MINIFIED_NAMES[*alt_id].encode_utf8(&mut name).len();
            name.truncate(s);
        };
        SourceRange::anonymous(name)
    }
}

// See main function at bottom for why we need this.
struct JsxVisitor<'a> {
    nodes: &'a NodeMap,
    scopes: &'a ScopeMap,
    symbols: &'a mut SymbolMap<MinifySymbol>,
}

impl<'a> Visitor for JsxVisitor<'a> {
    fn on_syntax(&mut self, _parent_node: NodeId, node: NodeId, ctl: &mut JourneyControls) -> () {
        let n = &self.nodes[node];
        match n.stx() {
            Syntax::JsxName {
                namespace: None,
                name,
            } if !name.as_slice()[0].is_ascii_lowercase() => {
                match self.scopes[n.scope()].find_symbol(self.scopes, name) {
                    Some(sym) => self.symbols[sym].mark_as_used_as_jsx_component(),
                    None => {
                        // TODO Warn if symbol not found.
                    }
                };
            }
            _ => {}
        }
    }
}

struct MinifyVisitor<'a> {
    // Exports with the same exported name (including multiple default exports) are illegal, so we don't have to worry about/handle that case.
    export_bindings: &'a mut Vec<ExportBinding>,
    nodes: &'a NodeMap,
    scopes: &'a ScopeMap,
    symbols: &'a mut SymbolMap<MinifySymbol>,
    updates: &'a mut NodeUpdates,
}

impl<'a> MinifyVisitor<'a> {
    fn visit_exported_pattern(&mut self, n: NodeId) -> () {
        match self.nodes[n].stx() {
            Syntax::ArrayPattern { elements, rest } => {
                for e in elements {
                    if let Some(e) = e {
                        self.visit_exported_pattern(e.target);
                    }
                }
                if let Some(rest) = rest {
                    self.visit_exported_pattern(*rest);
                }
            }
            Syntax::ObjectPattern { properties, rest } => {
                for p in properties {
                    self.visit_exported_pattern(*p);
                }
                if let Some(rest) = rest {
                    self.visit_exported_pattern(*rest);
                }
            }
            Syntax::ObjectPatternProperty { key, target, .. } => {
                match target {
                    Some(target) => self.visit_exported_pattern(*target),
                    // Shorthand.
                    None => match key {
                        ClassOrObjectMemberKey::Direct(key) => {
                            self.export_bindings.push(ExportBinding {
                                target: key.clone(),
                                alias: key.clone(),
                            })
                        }
                        _ => unreachable!(),
                    },
                }
            }
            Syntax::IdentifierPattern { name } => self.export_bindings.push(ExportBinding {
                target: name.clone(),
                alias: name.clone(),
            }),
            _ => unreachable!(),
        }
    }
}

impl<'a> Visitor for MinifyVisitor<'a> {
    fn on_syntax(&mut self, _parent_node: NodeId, node: NodeId, ctl: &mut JourneyControls) -> () {
        let scope_id = self.nodes[node].scope();
        let scope = &self.scopes[scope_id];
        match self.nodes[node].stx() {
            Syntax::ArrowFunctionExpr {
                parenthesised,
                is_async,
                signature,
                body,
            } => {
                if let Syntax::BlockStmt { body } = self.nodes[*body].stx() {
                    if body.len() == 1 {
                        if let Syntax::ReturnStmt { value } = self.nodes[body[0]].stx() {
                            if let Some(return_value) = value {
                                self.updates.replace_node(
                                    node,
                                    scope_id,
                                    self.nodes[node].loc().clone(),
                                    Syntax::ArrowFunctionExpr {
                                        parenthesised: *parenthesised,
                                        is_async: *is_async,
                                        signature: *signature,
                                        body: *return_value,
                                    },
                                );
                            }
                        };
                    };
                };
            }
            stx @ (Syntax::IdentifierPattern { name }
            | Syntax::IdentifierExpr { name }
            | Syntax::ClassOrFunctionName { name }
            | Syntax::JsxMember { base: name, .. }
            | Syntax::JsxName {
                name,
                namespace: None,
            }) => {
                let sym = scope.find_symbol(self.scopes, name);
                // TODO JsxMember and JsxNamespacedName must be capitalised to be interpreted as a component.
                if let Some(sym) = sym {
                    let minified = self.symbols[sym].generate_minified_name();
                    self.updates.replace_node(
                        node,
                        scope_id,
                        minified.clone(),
                        match stx {
                            Syntax::IdentifierPattern { .. } => Syntax::IdentifierPattern {
                                name: minified.clone(),
                            },
                            Syntax::IdentifierExpr { .. } => Syntax::IdentifierExpr {
                                name: minified.clone(),
                            },
                            Syntax::ClassOrFunctionName { .. } => Syntax::ClassOrFunctionName {
                                name: minified.clone(),
                            },
                            Syntax::JsxMember { path, .. } => Syntax::JsxMember {
                                base: minified.clone(),
                                path: path.clone(),
                            },
                            Syntax::JsxName { namespace, .. } => Syntax::JsxName {
                                namespace: namespace.clone(),
                                name: minified.clone(),
                            },
                            _ => unreachable!(),
                        },
                    );
                };
            }
            Syntax::ExportDeclStmt {
                declaration,
                default,
            } => match self.nodes[*declaration].stx() {
                Syntax::ClassDecl { name, .. } | Syntax::FunctionDecl { name, .. } => {
                    match name {
                        Some(name) => match self.nodes[*name].stx() {
                            Syntax::ClassOrFunctionName { name } => {
                                self.export_bindings.push(ExportBinding {
                                    target: name.clone(),
                                    alias: if *default {
                                        SourceRange::anonymous("default")
                                    } else {
                                        name.clone()
                                    },
                                });
                            }
                            _ => unreachable!(),
                        },
                        _ => {}
                    };
                }
                Syntax::VarStmt { declaration } => match self.nodes[*declaration].stx() {
                    Syntax::VarDecl { declarators, .. } => {
                        for decl in declarators {
                            self.visit_exported_pattern(decl.pattern);
                        }
                    }
                    _ => unreachable!(),
                },
                _ => unreachable!(),
            },
            Syntax::ExportListStmt { names, from } => {
                ctl.skip();
                match from {
                    None => match names {
                        ExportNames::Specific(names) => {
                            for e in names {
                                self.export_bindings.push(ExportBinding {
                                    target: e.target.clone(),
                                    alias: match self.nodes[e.alias].stx() {
                                        Syntax::IdentifierPattern { name } => name.clone(),
                                        _ => unreachable!(),
                                    },
                                });
                                self.updates.replace_node(
                                    node,
                                    scope_id,
                                    SourceRange::anonymous(""),
                                    Syntax::EmptyStmt {},
                                );
                            }
                        }
                        ExportNames::All(_) => unreachable!(),
                    },
                    // `export ... from ...` do not touch/alter the module's scope, so we can ignore completely.
                    _ => {}
                }
            }
            Syntax::ObjectPatternProperty {
                key,
                target,
                default_value,
            } => {
                match key {
                    ClassOrObjectMemberKey::Direct(name) => {
                        if target.is_none() {
                            let sym = scope.find_symbol(self.scopes, name);
                            if let Some(sym) = sym {
                                let minified = self.symbols[sym].generate_minified_name();
                                let replacement_target_node = self.updates.create_node(
                                    scope_id,
                                    minified.clone(),
                                    Syntax::IdentifierPattern {
                                        name: minified.clone(),
                                    },
                                );
                                self.updates.replace_node(
                                    node,
                                    scope_id,
                                    minified.clone(),
                                    Syntax::ObjectPatternProperty {
                                        key: key.clone(),
                                        target: Some(replacement_target_node),
                                        default_value: default_value.clone(),
                                    },
                                );
                            };
                        }
                    }
                    _ => {}
                };
            }
            Syntax::ObjectMember { typ } => {
                match typ {
                    ObjectMemberType::Shorthand { name } => {
                        let sym = scope.find_symbol(self.scopes, name);
                        if let Some(sym) = sym {
                            let minified = self.symbols[sym].generate_minified_name();
                            let replacement_initializer_node = self.updates.create_node(
                                scope_id,
                                minified.clone(),
                                Syntax::IdentifierExpr {
                                    name: minified.clone(),
                                },
                            );
                            self.updates.replace_node(
                                node,
                                scope_id,
                                minified.clone(),
                                Syntax::ObjectMember {
                                    typ: ObjectMemberType::Valued {
                                        key: ClassOrObjectMemberKey::Direct(name.clone()),
                                        value: ClassOrObjectMemberValue::Property {
                                            initializer: Some(replacement_initializer_node),
                                        },
                                    },
                                },
                            );
                        };
                    }
                    _ => {}
                };
            }
            _ => {}
        }
    }
}

pub fn minify_js(
    scope_map: &mut ScopeMap,
    node_map: &mut NodeMap,
    top_level_scope_id: ScopeId,
    top_level_node_id: NodeId,
) -> () {
    let mut symbols: SymbolMap<MinifySymbol> = SymbolMap::<MinifySymbol>::new(&scope_map);
    // We need these since we cannot look up in scope_map while mutably borrowed by iter_mut().
    let symbol_counts: Vec<usize> = scope_map.iter().map(|s| s.symbol_count()).collect();
    let mut minified_name_starts: Vec<usize> = vec![0; scope_map.len()];
    // Iterating like this assumes that any parent of an element is always present before it in the list.
    for (id, scope) in scope_map.iter_mut().enumerate() {
        // TODO Opportunity for optimisation: not all variables are used by descendant scopes, so some of their names can be reused by descendants.
        let minified_name_start = match scope.parent() {
            Some(p) => symbol_counts[p.id()] + minified_name_starts[p.id()],
            None => 0,
        };
        for symbol_id in scope.symbols_iter() {
            symbols[symbol_id]
                .set_minified_name_id(minified_name_start + symbol_id.ordinal_in_scope())
        }
        minified_name_starts[id] = minified_name_start;
    }

    // Since React requires that non-namespaced JSX elements that refer to a JS component must never start with a lowercase letter (otherwise it will always be interpreted as an HTML tag, even if a same-named variable is in scope), we must detect all usages and prefix them with a unique non-lowercase character JSX_COMPONENT_NAME_PREFIX (this is the simplest solution given how we currently minify identifiers). Since we can't know which variables are used as such (for the same reason we cannot minify while parsing in the same step i.e. due to forward references across scopes), we must run a separate pass after parsing and before minifying.
    let mut jsx_visitor = JsxVisitor {
        nodes: node_map,
        scopes: scope_map,
        symbols: &mut symbols,
    };
    jsx_visitor.visit_top_level(node_map, top_level_node_id);

    let mut export_bindings = Vec::new();
    let mut updates = NodeUpdates::new(node_map);
    let mut visitor = MinifyVisitor {
        export_bindings: &mut export_bindings,
        nodes: node_map,
        scopes: scope_map,
        symbols: &mut symbols,
        updates: &mut updates,
    };
    visitor.visit_top_level(node_map, top_level_node_id);
    updates.apply_updates(node_map);
    let export_names = export_bindings
        .iter()
        .map(|e| ExportName {
            target: (symbols[scope_map[top_level_scope_id]
                .find_symbol(scope_map, &e.target)
                .expect(format!("failed to find top-level export `{:?}`", e.target).as_str())]
            .generate_minified_name()),
            alias: node_map.create_node(
                top_level_scope_id,
                e.alias.clone(),
                Syntax::IdentifierPattern {
                    name: e.alias.clone(),
                },
            ),
        })
        .collect::<Vec<ExportName>>();

    if !export_names.is_empty() {
        let final_export_stmt = node_map.create_node(
            top_level_scope_id,
            SourceRange::anonymous(""),
            Syntax::ExportListStmt {
                names: ExportNames::Specific(export_names),
                from: None,
            },
        );
        match node_map[top_level_node_id].stx_mut() {
            Syntax::TopLevel { body } => {
                body.push(final_export_stmt);
            }
            _ => unreachable!(),
        }
    }
}