site-kit-wp icon indicating copy to clipboard operation
site-kit-wp copied to clipboard

Use global instead of window in JS code

Open tofumatt opened this issue 7 years ago β€’ 6 comments

Now that we're using Jest, we are running our plugin code in both Node and Browser environments. This hasn't actually created any issues yet, but I think it'd be nice to rely on Webpack's global variable instead of using window in plugin code and global in our Jest codeβ€”as we currently do.

This is a somewhat aesthetic choice right now, though I think global more clearly expresses our usual intended use of window, and also makes the code a bit more flexible/idiomatic.

This would be a pretty easy search+replace of window in our code that is processed by Webpack.


Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

  • Use global over window in JS code that will be bundled by Webpack.

Implementation Brief

  • Add a no-restricted-globals rule to our eslintrc encouraging usage of global over window:
"no-restricted-globals": [
      "error",
      {
        "name": "window",
        "message": "Use 'global' instead."
      }
    ],
  • Convert all existing instances of window to global.

Changelog entry

  • N/A

tofumatt avatar Nov 27 '19 10:11 tofumatt

@tofumatt Webpack turns this into window when bundling right?

felixarntz avatar Jan 14 '20 14:01 felixarntz

Yup! Everything client-side will continue to work as-normal, but it means our code is a bit more portable for jest, etc. Plus it's just a bit more "standard" use of ES6. πŸ€·β€β™‚

tofumatt avatar Jan 14 '20 14:01 tofumatt

@tofumatt can we enforce this with an ESLint rule? I took a look at the core set but I don't see one for this. Is there maybe a third-party package that you know of that we could leverage?

aaemnnosttv avatar Jan 16 '20 10:01 aaemnnosttv

An easy way might just be to disable usage of window as a global. But I'll have a quick look for anything a bit more codified/pre-made πŸ˜„

tofumatt avatar Jan 16 '20 11:01 tofumatt

IB βœ…

felixarntz avatar Jan 17 '20 10:01 felixarntz

Regression tests on my local and Jurassic Ninja passed.

Tested

  • Setup all services (between Jurassic Ninja and my Local) image image

  • Deactivated services, disconnected SK. No issues to note.

Passed QA βœ…

cole10up avatar Jan 28 '20 07:01 cole10up