UNPKG

@hint/hint-minified-js

Version:

hint that that checks if the JavaScript used by the web page is minified

39 lines (38 loc) 1.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const utils_debug_1 = require("@hint/utils-debug"); const utils_types_1 = require("@hint/utils-types"); const meta_1 = require("./meta"); const i18n_import_1 = require("./i18n.import"); const debug = (0, utils_debug_1.debug)(__filename); class MinifiedJsHint { constructor(context) { let threshold = 75; if (context.hintOptions && context.hintOptions.threshold) { threshold = context.hintOptions.threshold; } const getImprovementIndex = ({ sourceCode, tokens }) => { const contentLength = sourceCode.length; const tokenRatio = tokens.length / contentLength; return Math.round((1 - tokenRatio) * 100); }; const validateContentMinified = (scriptData) => { const { element, resource, sourceCode } = scriptData; const improvementIndex = getImprovementIndex(scriptData); if (sourceCode.length < 1024) { debug(`Ignoring minification for script under 1KB: ${resource}`); return; } debug(`Calculated improvementIndex for ${resource}: ${improvementIndex}`); if (improvementIndex > threshold) { context.report(resource, (0, i18n_import_1.getMessage)('shouldBeMinified', context.language), { element, severity: utils_types_1.Severity.warning }); } }; context.on('parse::end::javascript', validateContentMinified); } } exports.default = MinifiedJsHint; MinifiedJsHint.meta = meta_1.default;