Skip to content

Commit

Permalink
better cdt lib gen
Browse files Browse the repository at this point in the history
  • Loading branch information
connorjclark committed Dec 11, 2019
1 parent 3333c4f commit 0b655b6
Show file tree
Hide file tree
Showing 4 changed files with 568 additions and 487 deletions.
52 changes: 26 additions & 26 deletions build/build-cdt-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,23 @@
'use strict';

const fs = require('fs');
const {execFileSync} = require('child_process');
const glob = require('glob');

const files = [
'node_modules/chrome-devtools-frontend/front_end/sdk/SourceMap.js',
];
const outDir = 'lighthouse-core/lib/cdt/generated';

execFileSync('node_modules/.bin/tsc', [
'--allowJs',
'--outDir',
outDir,
...files,
]);
const outDir = `${__dirname}/../lighthouse-core/lib/cdt/generated`;
const files = {
'node_modules/chrome-devtools-frontend/front_end/sdk/SourceMap.js': 'SourceMap.js',
}

// eslint-disable-next-line no-console
console.log('making modifications ...');

for (const file of glob.sync(`${outDir}/**/*.js`)) {
const code = fs.readFileSync(file, 'utf-8');
for (const [input, output] of Object.entries(files)) {
const code = fs.readFileSync(input, 'utf-8');

let lines = code.match(/^.*(\r?\n|$)/mg) || [];
const cutoffIndex = lines.findIndex(line => line.includes('Legacy exported object'));
lines = lines.splice(0, cutoffIndex);

let deletionMode = false;
// let deletionMode = false;

const modifiedLines = lines.map((line, i) => {
// Don't modify jsdoc comments.
Expand All @@ -40,26 +31,35 @@ for (const file of glob.sync(`${outDir}/**/*.js`)) {
}

let newLine = line;
if (file.endsWith('SourceMap.js')) {
if (line.includes('TextSourceMap.load =')) deletionMode = true;
if (line.includes('prototype.sourceContentProvider =')) deletionMode = true;
if (line.includes(`url += Common.UIString('? [sm]');`)) newLine = '';
if (input.endsWith('SourceMap.js')) {
// if (line.includes('TextSourceMap.load =')) deletionMode = true;
// if (line.includes('prototype.sourceContentProvider =')) deletionMode = true;
// if (line.includes(`url += Common.UIString('? [sm]');`)) newLine = '';
newLine = line.replace(`Common.ParsedURL.completeURL(this._baseURL, href)`, `''`);
}
newLine = newLine.replace('exports["default"]', 'exports.default');
if (deletionMode) {
newLine = '';
if (line.startsWith(' };')) deletionMode = false;
// newLine = newLine.replace('exports["default"]', 'exports.default');
// if (deletionMode) {
// newLine = '';
// if (line.startsWith(' };')) deletionMode = false;
// }

let match = newLine.match(/export default class (\w*)/);
if (match) {
newLine = newLine.replace(match[0], `const ${match[1]} = module.exports = class ${match[1]}`);
}
match = newLine.match(/export class (\w*)/);
if (match) {
newLine = newLine.replace(match[0], `const ${match[1]} = module.exports.${match[1]} = class ${match[1]}`);
}

if (newLine !== line) {
// eslint-disable-next-line no-console
console.log(`${file}:${i}: ${line.trim()}`);
console.log(`${input}:${i}: ${line.trim()}`);
}
return newLine;
});
modifiedLines.unshift(`const Common = require('../Common.js')\n`);
modifiedLines.unshift('// generated by build-cdt-lib.js\n');
modifiedLines.unshift('// @ts-nocheck\n');
fs.writeFileSync(file, modifiedLines.join(''));
fs.writeFileSync(`${outDir}/${output}`, modifiedLines.join(''));
}
9 changes: 5 additions & 4 deletions lighthouse-core/computed/bundle-analysis.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const makeComputedArtifact = require('./computed-artifact.js');
const SDK = require('../lib/cdt/SDK.js');

/**
* @typedef {typeof SDK.TextSourceMap} SourceMap
* @typedef {import('../lib/cdt/generated/SourceMap.js').TextSourceMap} TextSourceMap
*/

/**
Expand All @@ -27,13 +27,13 @@ const SDK = require('../lib/cdt/SDK.js');
* @property {LH.Artifacts.RawSourceMap} rawMap
* @property {LH.Artifacts.ScriptElement} script
* @property {LH.Artifacts.NetworkRequest=} networkRecord
* @property {SourceMap} map
* @property {TextSourceMap} map
* @property {Sizes} sizes
*/

/**
* Calculate the number of bytes contributed by each source file
* @param {SourceMap} map
* @param {TextSourceMap} map
* @param {string} content
*/
function computeGeneratedFileSizes(map, content) {
Expand All @@ -55,6 +55,7 @@ function computeGeneratedFileSizes(map, content) {
const source = mapping.sourceURL;
const lineNum = mapping.lineNumber;
const colNum = mapping.columnNumber;
// @ts-ignore
const lastColNum = mapping.lastColumnNumber;

// Webpack sometimes emits null mappings.
Expand Down Expand Up @@ -124,7 +125,7 @@ class BundleAnalysis {
if (!scriptElement) continue;

// Lazily generate expensive things.
/** @type {SourceMap=} */
/** @type {TextSourceMap=} */
let map;
/** @type {Sizes=} */
let sizes;
Expand Down
2 changes: 2 additions & 0 deletions lighthouse-core/lib/cdt/SDK.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const SDK = {
const originalMappings = SDK.TextSourceMap.prototype.mappings;
SDK.TextSourceMap.prototype.mappings = function() {
const mappings = originalMappings.call(this);
// @ts-ignore
mappings.upperBound = upperBound.bind(mappings);
return mappings;
};
Expand Down Expand Up @@ -55,6 +56,7 @@ function upperBound(object, comparator, left, right) {
}

// Add `lastColumnNumber` to mappings.
// @ts-ignore
SDK.TextSourceMap.prototype.computeLastGeneratedColumns = function() {
const mappings = this.mappings();
// @ts-ignore: `lastColumnNumber` is not on types yet.
Expand Down
Loading

0 comments on commit 0b655b6

Please sign in to comment.