flow like the river
This commit is contained in:
commit
013fe673f3
42435 changed files with 5764238 additions and 0 deletions
63
BACK_BACK/node_modules/htmlnano/lib/modules/mergeScripts.js
generated
vendored
Executable file
63
BACK_BACK/node_modules/htmlnano/lib/modules/mergeScripts.js
generated
vendored
Executable file
|
|
@ -0,0 +1,63 @@
|
|||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = mergeScripts;
|
||||
|
||||
/* Merge multiple <script> into one */
|
||||
function mergeScripts(tree) {
|
||||
let scriptNodesIndex = {};
|
||||
let scriptSrcIndex = 1;
|
||||
tree.match({
|
||||
tag: 'script'
|
||||
}, node => {
|
||||
const nodeAttrs = node.attrs || {};
|
||||
|
||||
if (nodeAttrs.src) {
|
||||
scriptSrcIndex++;
|
||||
return node;
|
||||
}
|
||||
|
||||
const scriptType = nodeAttrs.type || 'text/javascript';
|
||||
|
||||
if (scriptType !== 'text/javascript' && scriptType !== 'application/javascript') {
|
||||
return node;
|
||||
}
|
||||
|
||||
const scriptKey = JSON.stringify({
|
||||
id: nodeAttrs.id,
|
||||
class: nodeAttrs.class,
|
||||
type: scriptType,
|
||||
defer: nodeAttrs.defer !== undefined,
|
||||
async: nodeAttrs.async !== undefined,
|
||||
index: scriptSrcIndex
|
||||
});
|
||||
|
||||
if (!scriptNodesIndex[scriptKey]) {
|
||||
scriptNodesIndex[scriptKey] = [];
|
||||
}
|
||||
|
||||
scriptNodesIndex[scriptKey].push(node);
|
||||
return node;
|
||||
});
|
||||
|
||||
for (const scriptNodes of Object.values(scriptNodesIndex)) {
|
||||
let lastScriptNode = scriptNodes.pop();
|
||||
scriptNodes.reverse().forEach(scriptNode => {
|
||||
let scriptContent = (scriptNode.content || []).join(' ');
|
||||
scriptContent = scriptContent.trim();
|
||||
|
||||
if (scriptContent.slice(-1) !== ';') {
|
||||
scriptContent += ';';
|
||||
}
|
||||
|
||||
lastScriptNode.content = lastScriptNode.content || [];
|
||||
lastScriptNode.content.unshift(scriptContent);
|
||||
scriptNode.tag = false;
|
||||
scriptNode.content = [];
|
||||
});
|
||||
}
|
||||
|
||||
return tree;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue