Skip to content

Commit fd547cd

Browse files
committed
move out of WIP
1 parent 321df67 commit fd547cd

File tree

5 files changed

+117
-17
lines changed

5 files changed

+117
-17
lines changed

lighthouse-cli/test/smokehouse/byte-config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module.exports = {
1616
'uses-webp-images',
1717
'uses-optimized-images',
1818
'uses-responsive-images',
19+
'unminified-javascript',
1920
'unused-css-rules',
2021
'unused-javascript',
2122
],

lighthouse-cli/test/smokehouse/byte-efficiency/expectations.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ module.exports = [
1313
initialUrl: 'http://localhost:10200/byte-efficiency/tester.html',
1414
url: 'http://localhost:10200/byte-efficiency/tester.html',
1515
audits: {
16+
'unminified-javascript': {
17+
score: '<100',
18+
extendedInfo: {
19+
value: {
20+
wastedKb: 14,
21+
results: {
22+
length: 1,
23+
},
24+
},
25+
},
26+
},
1627
'unused-css-rules': {
1728
score: '<100',
1829
extendedInfo: {

lighthouse-core/audits/byte-efficiency/unminified-javascript.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ class UnminifiedJavaScript extends ByteEfficiencyAudit {
1818
static get meta() {
1919
return {
2020
name: 'unminified-javascript',
21-
description: 'Unminified JavaScript',
21+
description: 'Minify JavaScript',
2222
informative: true,
23-
helpText: 'Minify JavaScript to save network bytes.',
23+
helpText: 'Minifying JavaScript files can reduce payload sizes and script parse time.' +
24+
'[Learn more](https://developers.google.com/speed/docs/insights/MinifyResources).',
2425
requiredArtifacts: ['Scripts', 'devtoolsLogs'],
2526
};
2627
}
@@ -31,21 +32,16 @@ class UnminifiedJavaScript extends ByteEfficiencyAudit {
3132
*/
3233
static computeWaste(scriptContent, networkRecord) {
3334
const contentLength = scriptContent.length;
34-
let tokenLength = 0;
35-
let tokenLengthWithMangling = 0;
35+
let totalTokenLength = 0;
3636

3737
const tokens = esprima.tokenize(scriptContent);
3838
for (const token of tokens) {
39-
tokenLength += token.value.length;
40-
// assume all identifiers could be reduced to a single character
41-
tokenLengthWithMangling += token.type === 'Identifier' ? 1 : token.value.length;
39+
totalTokenLength += token.value.length;
4240
}
4341

44-
if (1 - tokenLength / contentLength < IGNORE_THRESHOLD_IN_PERCENT) return null;
45-
4642
const totalBytes = ByteEfficiencyAudit.estimateTransferSize(networkRecord, contentLength,
4743
'script');
48-
const wastedRatio = 1 - (tokenLength + tokenLengthWithMangling) / (2 * contentLength);
44+
const wastedRatio = 1 - totalTokenLength / contentLength;
4945
const wastedBytes = Math.round(totalBytes * wastedRatio);
5046

5147
return {
@@ -61,15 +57,17 @@ class UnminifiedJavaScript extends ByteEfficiencyAudit {
6157
* @return {!Audit.HeadingsResult}
6258
*/
6359
static audit_(artifacts, networkRecords) {
64-
const scriptsByUrl = artifacts.Scripts;
65-
6660
const results = [];
67-
for (const [url, scriptContent] of scriptsByUrl.entries()) {
61+
for (const [url, scriptContent] of artifacts.Scripts.entries()) {
6862
const networkRecord = networkRecords.find(record => record.url === url);
6963
if (!networkRecord || !scriptContent) continue;
7064

7165
const result = UnminifiedJavaScript.computeWaste(scriptContent, networkRecord);
72-
if (!result || result.wastedBytes < IGNORE_THRESHOLD_IN_BYTES) continue;
66+
67+
// If the ratio is minimal, the file is likely already minified, so ignore it.
68+
// If the total number of bytes to be saved is quite small, it's also safe to ignore.
69+
if (result.wastedRatio < IGNORE_THRESHOLD_IN_PERCENT ||
70+
result.wastedBytes < IGNORE_THRESHOLD_IN_BYTES) continue;
7371
results.push(result);
7472
}
7573

lighthouse-core/gather/gatherers/scripts.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const Gatherer = require('./gatherer');
99
const WebInspector = require('../../lib/web-inspector');
1010

1111
/**
12-
* @fileoverview Gets JavaScript content
12+
* @fileoverview Gets JavaScript file content
1313
*/
1414
class Scripts extends Gatherer {
1515
/**
@@ -22,13 +22,13 @@ class Scripts extends Gatherer {
2222

2323
const scriptContentMap = new Map();
2424
const scriptRecords = traceData.networkRecords
25-
.filter(record => record.resourceType() === WebInspector.resourceTypes.Script)
25+
.filter(record => record.resourceType() === WebInspector.resourceTypes.Script);
2626

2727
return scriptRecords.reduce((promise, record) => {
2828
return promise
2929
.then(() => {
3030
return driver.getRequestContent(record.requestId)
31-
.catch(err => null)
31+
.catch(_ => null)
3232
.then(content => {
3333
if (!content) return;
3434
scriptContentMap.set(record.url, content);
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/**
2+
* @license Copyright 2017 Google Inc. All Rights Reserved.
3+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
4+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
5+
*/
6+
'use strict';
7+
8+
const KB = 1024;
9+
const UnminifiedJavascriptAudit =
10+
require('../../../audits/byte-efficiency/unminified-javascript.js');
11+
const assert = require('assert');
12+
13+
/* eslint-env mocha */
14+
15+
describe('Page uses optimized responses', () => {
16+
it('fails when given unminified scripts', () => {
17+
const auditResult = UnminifiedJavascriptAudit.audit_({
18+
Scripts: new Map([
19+
[
20+
'foo.js',
21+
`
22+
var foo = new Set();
23+
foo.add(1);
24+
foo.add(2);
25+
26+
if (foo.has(2)) {
27+
console.log('hello!')
28+
}
29+
`,
30+
],
31+
[
32+
'other.js',
33+
`
34+
const foo = new Set();
35+
foo.add(1);
36+
37+
async function go() {
38+
await foo.has(1)
39+
console.log('yay esnext!')
40+
}
41+
`,
42+
],
43+
]),
44+
}, [
45+
{url: 'foo.js', _transferSize: 20 * KB, _resourceType: {_name: 'script'}},
46+
{url: 'other.js', _transferSize: 50 * KB, _resourceType: {_name: 'script'}},
47+
]);
48+
49+
assert.equal(auditResult.results.length, 2);
50+
assert.equal(auditResult.results[0].url, 'foo.js');
51+
assert.equal(Math.round(auditResult.results[0].wastedPercent), 57);
52+
assert.equal(Math.round(auditResult.results[0].wastedBytes / 1024), 11);
53+
assert.equal(auditResult.results[1].url, 'other.js');
54+
assert.equal(Math.round(auditResult.results[1].wastedPercent), 53);
55+
assert.equal(Math.round(auditResult.results[1].wastedBytes / 1024), 27);
56+
});
57+
58+
it('passes when scripts are already minified', () => {
59+
const auditResult = UnminifiedJavascriptAudit.audit_({
60+
Scripts: new Map([
61+
[
62+
'foo.js',
63+
'var f=new Set();f.add(1);f.add(2);if(f.has(2))console.log(1234)',
64+
],
65+
[
66+
'other.js',
67+
`
68+
const foo = new Set();
69+
foo.add(1);
70+
71+
async function go() {
72+
await foo.has(1)
73+
console.log('yay esnext!')
74+
}
75+
`,
76+
],
77+
[
78+
'invalid.js',
79+
'for{(wtf',
80+
],
81+
]),
82+
}, [
83+
{url: 'foo.js', _transferSize: 20 * KB, _resourceType: {_name: 'script'}},
84+
{url: 'other.js', _transferSize: 3 * KB, _resourceType: {_name: 'script'}},
85+
{url: 'invalid.js', _transferSize: 20 * KB, _resourceType: {_name: 'script'}},
86+
]);
87+
88+
assert.equal(auditResult.results.length, 0);
89+
});
90+
});

0 commit comments

Comments
 (0)