Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extension(tsc): add type checking to extension entry points #5346

Merged
merged 8 commits into from
May 29, 2018
Merged

Conversation

brendankenny
Copy link
Member

@brendankenny brendankenny commented May 24, 2018

to do this, needed to do a little import/export doctoring.

lighthouse-background.js

  • exports functions to window if not in node (this is how devtools uses it), otherwise exports function as a node module

lighthouse-ext-background.js

  • requires lighthouse-background.js in module form, allowing cross-file references to work in type checking and IDE navigation. Browserify handles the require normally (before it was still required but not as a module, but wrote its exports to window so they were available there)
  • exports functions to window, since this file is only ever used where there is a window, but also to a module so popup.js can access for type checking

popup.js

  • has a typedef import of lighthouse-ext-background.js only for types, so still works as before, but now types of calls to the background page are checked and IDE nav, etc works

git isn't picking up that lighthouse-ext-background.js moved to extension-background.js, so it's treating it as a totally new file. Sorry about that. I can revert the move and do that afterwards if that's easier to review. reverted file renaming for now

@@ -10,22 +10,11 @@ const Connection = require('./connection.js');
/* eslint-disable no-unused-vars */

/**
* @interface
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

turns out this was never working as an interface for tsc (@interface has closure support only). Making a typedef so can be imported elsewhere

@@ -1,29 +0,0 @@
/**
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think anyone uses this, correct? Just came with the extension generator :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i do sometimes but dont most of the time. happy to get rid of it.

@@ -12,8 +12,7 @@
"default_locale": "en",
"background": {
"scripts": [
"scripts/chromereload.js",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we just delete this are we using manifest canary?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm good with deleting, but we should probably delete the actual extension in that case? @paulirish

Copy link
Member

@paulirish paulirish May 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ill delete canary extension now
edit: done.

@brendankenny
Copy link
Member Author

ok, yeah, that's way better without the file renames :) will propose that in a future PR

let lighthouseIsRunning = false;
let latestStatusLog = [];

// /**
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was disabled in February 2017. I don't think we need to leave it in anymore just in case :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HERO

Copy link
Collaborator

@patrickhulce patrickhulce left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems mostly good to me! 👍

};
}

if (typeof module !== 'undefined' && module.exports) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we just comment that this is for browserify and that it's not really executed in node? this threw me a bit :)

* @param {ParentNode} context
* @return {HTMLElement}
*/
function find(query, context) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we seem to always use document, do we need context?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added default of document. WDYT?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure that sg 👍

*/
function lighthouse(url, flags = {}, configJSON) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a breaking change for lighthouse('http://example.com') in node, do we really need to do this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we support fully optional flags? I hadn't dug into it so I didn't pursue it ({} isn't currently castable to LH.Flags which is why I dropped it), but if so, agreed we should support that here.

I can add a manual cast of a {} here, maybe, and then follow up with solidifying the flags pipeline in a follow up?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would Partial<LH.Flags> cut it? I guess we could differentiate LH.Flags from LH.CLIFlags which is basically what's defined now

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would Partial<LH.Flags> cut it? I guess we could differentiate LH.Flags from LH.CLIFlags which is basically what's defined now

yeah, I think that's basically what we need to do. Handy to have output from yargs, but then our optional flags fed into Config is a different beast (and it looks like completely optional is fine).

Ok if we do a future "Make Flags Good" PR while leaving this with the {} default?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

const runnerResult = await background.runLighthouseForConnection(connection, url, options,
categoryIDs, updateBadgeUI);
if (runnerResult) {
// For now, should always be a runnerResult as the extension can't do `gatherMode`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we throw if no runner result? seems like a fatal condition

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SG

chrome.runtime.onInstalled.addListener(details => {
if (details.previousVersion) {
// eslint-disable-next-line no-console
console.log('previousVersion', details.previousVersion);
}
});
}

if (typeof module !== 'undefined' && module.exports) {
// We don't want tsc to infer an index type, so use exports intest of module.exports.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

who consumes this via require?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

who consumes this via require?

popup.js imports this for a typedef only. Added a comment to explain

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh just for typedefs gotcha 👍 yeah comment is good

@@ -1,29 +0,0 @@
/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i do sometimes but dont most of the time. happy to get rid of it.

let lighthouseIsRunning = false;
let latestStatusLog = [];

// /**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HERO

};
}

if (typeof module !== 'undefined' && module.exports) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

really dig this. thanks.

return window.runLighthouseForConnection(connection, url, options, categoryIDs)
.then(results => {
const endTime = Date.now();
results.timing = {total: endTime - startTime};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currently LR continues to expect a .timing.total prop.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currently LR continues to expect a .timing.total prop.

this is added in runner now, so doesn't need to be done here

} else {
hideRunningSubpage();
async function initPopup() {
chrome.tabs.query({active: true, lastFocusedWindow: true}, function(tabs) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is changing the order here deliberate?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is changing the order here deliberate?

yeah, it used to fire off the promise to retrieve the background page and immediately run this query in parallel. Since I switched the promise.then to await, the query wouldn't run until after the background page had been found and the rest of the init stuff had run. Since at least siteURL is used for the pre-populated github message in case of a thrown error, it made sense to make sure it was set before the stuff

@paulirish
Copy link
Member

👍 to a file-rename PR afterwards. :)

@brendankenny
Copy link
Member Author

@patrickhulce second (third?) look?

@patrickhulce patrickhulce merged commit 92728c9 into master May 29, 2018
@patrickhulce patrickhulce deleted the tsrun branch May 29, 2018 21:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants