source: imaps-frontend/node_modules/terser/lib/compress/global-defs.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 3.3 KB
RevLine 
[79a0317]1import {
2 AST_Array,
3 AST_Chain,
4 AST_Constant,
5 AST_Dot,
6 AST_ImportMeta,
7 AST_Node,
8 AST_Object,
9 AST_ObjectKeyVal,
10 AST_PropAccess,
11 AST_SymbolDeclaration,
12 AST_SymbolRef,
13 AST_Toplevel,
14 TreeTransformer,
15} from "../ast.js";
16import { make_node, noop, HOP } from "../utils/index.js";
17import { make_node_from_constant } from "./common.js";
18import { is_lhs } from "./inference.js";
19
20(function(def_find_defs) {
21 function to_node(value, orig) {
22 if (value instanceof AST_Node) {
23 if (!(value instanceof AST_Constant)) {
24 // Value may be a function, an array including functions and even a complex assign / block expression,
25 // so it should never be shared in different places.
26 // Otherwise wrong information may be used in the compression phase
27 value = value.clone(true);
28 }
29 return make_node(value.CTOR, orig, value);
30 }
31 if (Array.isArray(value)) return make_node(AST_Array, orig, {
32 elements: value.map(function(value) {
33 return to_node(value, orig);
34 })
35 });
36 if (value && typeof value == "object") {
37 var props = [];
38 for (var key in value) if (HOP(value, key)) {
39 props.push(make_node(AST_ObjectKeyVal, orig, {
40 key: key,
41 value: to_node(value[key], orig)
42 }));
43 }
44 return make_node(AST_Object, orig, {
45 properties: props
46 });
47 }
48 return make_node_from_constant(value, orig);
49 }
50
51 AST_Toplevel.DEFMETHOD("resolve_defines", function(compressor) {
52 if (!compressor.option("global_defs")) return this;
53 this.figure_out_scope({ ie8: compressor.option("ie8") });
54 return this.transform(new TreeTransformer(function(node) {
55 var def = node._find_defs(compressor, "");
56 if (!def) return;
57 var level = 0, child = node, parent;
58 while (parent = this.parent(level++)) {
59 if (!(parent instanceof AST_PropAccess)) break;
60 if (parent.expression !== child) break;
61 child = parent;
62 }
63 if (is_lhs(child, parent)) {
64 return;
65 }
66 return def;
67 }));
68 });
69 def_find_defs(AST_Node, noop);
70 def_find_defs(AST_Chain, function(compressor, suffix) {
71 return this.expression._find_defs(compressor, suffix);
72 });
73 def_find_defs(AST_Dot, function(compressor, suffix) {
74 return this.expression._find_defs(compressor, "." + this.property + suffix);
75 });
76 def_find_defs(AST_SymbolDeclaration, function() {
77 if (!this.global()) return;
78 });
79 def_find_defs(AST_SymbolRef, function(compressor, suffix) {
80 if (!this.global()) return;
81 var defines = compressor.option("global_defs");
82 var name = this.name + suffix;
83 if (HOP(defines, name)) return to_node(defines[name], this);
84 });
85 def_find_defs(AST_ImportMeta, function(compressor, suffix) {
86 var defines = compressor.option("global_defs");
87 var name = "import.meta" + suffix;
88 if (HOP(defines, name)) return to_node(defines[name], this);
89 });
90})(function(node, func) {
91 node.DEFMETHOD("_find_defs", func);
92});
Note: See TracBrowser for help on using the repository browser.