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

fix: eliminate side effect from modern utils #6953

Merged
merged 2 commits into from
Feb 9, 2020
Merged

fix: eliminate side effect from modern utils #6953

merged 2 commits into from
Feb 9, 2020

Conversation

clarkdo
Copy link
Member

@clarkdo clarkdo commented Feb 9, 2020

Types of changes

  • Bug fix (a non-breaking change which fixes an issue)
  • New feature (a non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Description

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly. (PR: #)
  • I have added tests to cover my changes (if not applicable, please state why)
  • All new and existing tests are passing.

@clarkdo clarkdo requested review from pimlie and pi0 February 9, 2020 11:02
allBrowsers[browser] = semver.coerce(ModernBrowsers[browser])
return allBrowsers
}, {})
const modernBrowsers = new Proxy(ModernBrowsers, {
Copy link
Member

Choose a reason for hiding this comment

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

Still, we are always initializing a Proxy. What if we instead use getModernBrowsers which in the first call evaluates the value and keep in _modernBrowsers for next calls?

Copy link
Member Author

Choose a reason for hiding this comment

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

Proxy get won't be called at initialization, it's same as using _modernBrowsers

Copy link

Choose a reason for hiding this comment

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

Would also prefer using memoization here. Seems cleaner then using a Proxy

Copy link
Member

Choose a reason for hiding this comment

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

I also like Proxy self-memo pattern :)

Copy link
Member

Choose a reason for hiding this comment

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

The only thing that should check later is that ensure rollup doesn't count this as a side-effect and tree-shaking works for the const variable.

Copy link

Choose a reason for hiding this comment

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

@clarkdo Doesnt seem to work:

 ERROR  Webpack build for lambda failed [                                                                                 11:56:45
  '../node_modules/@nuxt/utils-edge/dist/utils.js\n' +
    "Module not found: Error: Can't resolve 'semver/functions/coerce' in 'nuxt-lambda/node_modules/@nuxt/utils-edge/dist'",
  '../node_modules/@nuxt/utils-edge/dist/utils.js\n' +
    "Module not found: Error: Can't resolve 'semver/functions/gte' in 'nuxt-lambda/node_modules/@nuxt/utils-edge/dist'"
]

Copy link

Choose a reason for hiding this comment

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

@clarkdo This is because I had still stubbed semver btw, let me check what happens if i dont stub it

Copy link

Choose a reason for hiding this comment

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

When I dont stub semver I get a runtime error, have to check it. But nuxt/utils load is as follows:

image

Total time is 8.2ms for loading/parsing @nuxt/utils-edge. The webpack require is signal-exit, the small yellow block is escapeRegExp and the other block already shows the function.

Copy link
Member Author

@clarkdo clarkdo Feb 9, 2020

Choose a reason for hiding this comment

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

From log, the error is from isModernBrowser, semver is required at that time for browser detection if you're using modern server mode.

Copy link
Member Author

@clarkdo clarkdo Feb 9, 2020

Choose a reason for hiding this comment

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

@pimlie I've applied the changes from your and @pi0 review, but it's not released to edge. I'll trigger one


export const isModernBrowser = (ua) => {
if (!ua) {
return false
}
const coerce = require('semver/functions/coerce')
Copy link
Member

Choose a reason for hiding this comment

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

Is it ok require called per-request? (TBH no idea of perf impact when we have cache)

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 code is called in nodejs and I think require.cache is pretty fast

@codecov-io
Copy link

Codecov Report

Merging #6953 into dev will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##              dev    #6953   +/-   ##
=======================================
  Coverage   62.72%   62.72%           
=======================================
  Files          82       82           
  Lines        3305     3305           
  Branches      899      899           
=======================================
  Hits         2073     2073           
  Misses        989      989           
  Partials      243      243
Flag Coverage Δ
#unittests 62.72% <ø> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b5612a9...a784531. Read the comment docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants