uglify-js-minify-css-allfiles 2.8.1 → 2.8.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
* Babel plugin to transform Element.append() calls to be compatible with older browsers.
|
|
3
3
|
* This plugin converts:
|
|
4
4
|
* - element.append("text") to element.appendChild(document.createTextNode("text"))
|
|
5
|
-
* - element.append(
|
|
5
|
+
* - element.append(3) to element.appendChild(document.createTextNode(3))
|
|
6
|
+
* - element.append(node) to element.appendChild(node instanceof Node ? node : document.createTextNode(String(node)))
|
|
6
7
|
* - element.append(node1, "text", node2) to a sequence of appendChild calls
|
|
7
8
|
*
|
|
8
9
|
* @param {Object} param0 - Babel plugin parameters
|
|
@@ -10,6 +11,34 @@
|
|
|
10
11
|
* @returns {Object} Babel plugin object
|
|
11
12
|
*/
|
|
12
13
|
export default function ({ types: t }) {
|
|
14
|
+
function createTextNodeExpression(arg) {
|
|
15
|
+
return t.callExpression(t.memberExpression(t.identifier('document'), t.identifier('createTextNode')), [arg]);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function isLiteral(node) {
|
|
19
|
+
return (
|
|
20
|
+
t.isStringLiteral(node) ||
|
|
21
|
+
t.isNumericLiteral(node) ||
|
|
22
|
+
t.isBooleanLiteral(node) ||
|
|
23
|
+
t.isNullLiteral(node) ||
|
|
24
|
+
t.isTemplateLiteral(node)
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function toAppendChildArg(arg) {
|
|
29
|
+
// Known primitives: always wrap with createTextNode
|
|
30
|
+
if (isLiteral(arg)) {
|
|
31
|
+
return createTextNodeExpression(arg);
|
|
32
|
+
}
|
|
33
|
+
// Unknown values (variables, expressions): runtime check
|
|
34
|
+
// If it's a Node, use directly; otherwise wrap with createTextNode
|
|
35
|
+
return t.conditionalExpression(
|
|
36
|
+
t.binaryExpression('instanceof', t.cloneNode(arg), t.identifier('Node')),
|
|
37
|
+
t.cloneNode(arg),
|
|
38
|
+
createTextNodeExpression(t.cloneNode(arg)),
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
13
42
|
return {
|
|
14
43
|
name: 'append-polyfill-transform',
|
|
15
44
|
visitor: {
|
|
@@ -28,39 +57,22 @@ export default function ({ types: t }) {
|
|
|
28
57
|
|
|
29
58
|
// Handle single argument case
|
|
30
59
|
if (args.length === 1) {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
t.callExpression(t.memberExpression(t.cloneNode(objectNode), t.identifier('appendChild')), [
|
|
37
|
-
t.callExpression(t.memberExpression(t.identifier('document'), t.identifier('createTextNode')), [arg]),
|
|
38
|
-
]),
|
|
39
|
-
);
|
|
40
|
-
} else {
|
|
41
|
-
// Node case: element.append(node)
|
|
42
|
-
callee.get('property').replaceWith(t.identifier('appendChild'));
|
|
43
|
-
}
|
|
60
|
+
path.replaceWith(
|
|
61
|
+
t.callExpression(t.memberExpression(t.cloneNode(objectNode), t.identifier('appendChild')), [
|
|
62
|
+
toAppendChildArg(args[0]),
|
|
63
|
+
]),
|
|
64
|
+
);
|
|
44
65
|
return;
|
|
45
66
|
}
|
|
46
67
|
|
|
47
68
|
// Handle multiple arguments: element.append(node1, "text", node2)
|
|
48
|
-
const statements = args.map((arg) =>
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
]);
|
|
54
|
-
} else {
|
|
55
|
-
// Node case
|
|
56
|
-
return t.callExpression(t.memberExpression(t.cloneNode(objectNode), t.identifier('appendChild')), [arg]);
|
|
57
|
-
}
|
|
58
|
-
});
|
|
69
|
+
const statements = args.map((arg) =>
|
|
70
|
+
t.callExpression(t.memberExpression(t.cloneNode(objectNode), t.identifier('appendChild')), [
|
|
71
|
+
toAppendChildArg(arg),
|
|
72
|
+
]),
|
|
73
|
+
);
|
|
59
74
|
|
|
60
|
-
|
|
61
|
-
// Combine multiple statements into a sequence expression
|
|
62
|
-
path.replaceWith(t.sequenceExpression(statements));
|
|
63
|
-
}
|
|
75
|
+
path.replaceWith(t.sequenceExpression(statements));
|
|
64
76
|
},
|
|
65
77
|
},
|
|
66
78
|
};
|