-
-
Notifications
You must be signed in to change notification settings - Fork 5k
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
Conversation
allBrowsers[browser] = semver.coerce(ModernBrowsers[browser]) | ||
return allBrowsers | ||
}, {}) | ||
const modernBrowsers = new Proxy(ModernBrowsers, { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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'"
]
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
||
export const isModernBrowser = (ua) => { | ||
if (!ua) { | ||
return false | ||
} | ||
const coerce = require('semver/functions/coerce') |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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 Report
@@ 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
Continue to review full report at Codecov.
|
Types of changes
Description
Checklist: