-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
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
feat: add polyfills
option to auto-imports
#30332
base: main
Are you sure you want to change the base?
Conversation
Run & review this pull request in StackBlitz Codeflow. |
WalkthroughThe pull request introduces modifications to the Nuxt imports module and related configuration files to enhance compatibility and provide more flexible import options. The changes primarily focus on adding a new optional Key modifications include:
The changes are implemented across multiple files:
The modifications aim to provide more granular control over auto-imports and compatibility features within the Nuxt framework, allowing developers to opt-in or opt-out of specific polyfill and compatibility imports as needed. Tip CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 3
🧹 Nitpick comments (1)
packages/schema/src/types/imports.ts (1)
35-40
: Enhance documentation for the polyfills optionThe documentation should provide a comprehensive list of all polyfills that will be included when this option is enabled. This helps developers make informed decisions about whether to enable or disable the feature.
Consider expanding the documentation like this:
/** - * Add polyfills for setInterval, requestIdleCallback, and others + * Add polyfills for native APIs to improve testing capabilities. + * Currently includes: + * - setInterval + * - requestIdleCallback + * - [list other included polyfills] + * + * When disabled, these APIs must be mocked manually in tests. * @default true */
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (5)
packages/nuxt/src/imports/module.ts
(1 hunks)packages/nuxt/src/imports/presets.ts
(1 hunks)packages/schema/src/types/imports.ts
(1 hunks)test/nuxt/client.test.ts
(2 hunks)vitest.nuxt.config.ts
(1 hunks)
🔇 Additional comments (4)
vitest.nuxt.config.ts (1)
24-26
: Configuration looks good
The polyfills: false
setting in the test configuration aligns with the PR objectives, allowing for manual mocking of native APIs during testing.
packages/nuxt/src/imports/presets.ts (1)
251-260
: LGTM! Clean separation of polyfills into a dedicated preset.
The implementation cleanly separates polyfills into their own preset array, making them optional through the new polyfills
configuration option.
Let's verify if there are any other native API polyfills that should be included:
✅ Verification successful
Let's search for the actual polyfill implementations to ensure we haven't missed any.
The polyfill list in appCompatPresets
is complete and correctly implemented
The implementation covers all the compatibility-related functionality in the codebase:
idle-callback.ts
: Polyfills forrequestIdleCallback
andcancelIdleCallback
interval.ts
: Server-safe wrapper forsetInterval
- Other files in the compat directory (
capi.ts
andvue-demi.ts
) are Vue-specific compatibility layers rather than browser API polyfills
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Search for other potential polyfills in the codebase
rg -g '!*.test.*' -g '!*.spec.*' "from.*compat.*"
Length of output: 1744
Script:
#!/bin/bash
# Search for polyfill implementations in the compat directory
fd "compat" --type d
fd . --full-path "*/compat/*" --type f --exec cat {}
Length of output: 205
Script:
#!/bin/bash
# List and show content of files in the compat directory
ls -la packages/nuxt/src/app/compat/
cat packages/nuxt/src/app/compat/*
Length of output: 2351
packages/nuxt/src/imports/module.ts (2)
12-12
: LGTM! Clean import of the new preset.
The import statement is correctly placed and follows the existing import style.
19-36
: LGTM! Well-implemented conditional polyfills inclusion.
The implementation:
- Maintains backwards compatibility by defaulting
polyfills
to true - Correctly implements conditional inclusion of polyfills
- Follows the existing code structure
Let's verify the default behaviour remains unchanged:
✅ Verification successful
Let me verify the polyfills implementation further by checking the imports configuration and usage.
Let me check one final aspect - the definition of appCompatPresets to fully understand what polyfills are being conditionally included.
Polyfills implementation verified and working as expected
The implementation correctly handles polyfills by:
- Conditionally including
requestIdleCallback
,cancelIdleCallback
, andsetInterval
from compatibility layer - Maintaining proper imports from
#app/compat/*
modules - Preserving existing functionality through default presets
- Allowing opt-out via
imports.polyfills
configuration
The implementation is safe as the polyfills are:
- Properly isolated in dedicated compatibility modules
- Used consistently throughout the codebase
- Have appropriate server-side handling
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Search for existing usages of these polyfills to ensure we don't break anything
rg -g '!*.test.*' -g '!*.spec.*' "requestIdleCallback|setInterval"
Length of output: 1741
Script:
#!/bin/bash
# Check the imports configuration and polyfills setup
ast-grep --pattern 'export const defaultPresets = $$$'
# Check for any conditional polyfills usage
rg -g '!*.test.*' -g '!*.spec.*' "imports\.polyfills" -A 2 -B 2
Length of output: 491
Script:
#!/bin/bash
# Check the definition of appCompatPresets
ast-grep --pattern 'export const appCompatPresets = $$$'
# Also check if there are any other references to these presets
rg -g '!*.test.*' -g '!*.spec.*' "appCompatPresets"
Length of output: 437
Script:
#!/bin/bash
# Let's see the actual content of the presets file to understand the polyfills
cat packages/nuxt/src/imports/presets.ts
Length of output: 6080
it('setInterval is not auto-imported', async () => { | ||
vi.useFakeTimers() | ||
|
||
const wrapper = mount(Component) | ||
|
||
vi.advanceTimersByTime(1000) | ||
|
||
await wrapper.vm.$nextTick() | ||
|
||
expect(wrapper.find('#child').exists()).toBe(true) | ||
|
||
vi.useRealTimers() | ||
}) |
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.
🛠️ Refactor suggestion
Improve test robustness and coverage
Several improvements could enhance the test:
- The test should clean up timers in an afterEach hook to prevent leaks if the test fails
- The test description should be more specific about what's being tested
- Consider adding a complementary test with polyfills enabled
Here's how to improve the test:
+ afterEach(() => {
+ vi.useRealTimers()
+ })
- it('setInterval is not auto-imported', async () => {
+ it('should toggle visibility after timeout when using native setInterval', async () => {
vi.useFakeTimers()
const wrapper = mount(Component)
vi.advanceTimersByTime(1000)
await wrapper.vm.$nextTick()
expect(wrapper.find('#child').exists()).toBe(true)
- vi.useRealTimers()
})
Consider adding this complementary test:
it('should work with polyfills enabled', async () => {
// TODO: Add test with imports.polyfills = true
// This would verify the feature works in both configurations
})
const visible = ref(false) | ||
setInterval(() => { | ||
visible.value = true | ||
}, 1000) |
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.
Add missing ref import
The code uses ref
but the import statement is missing.
Add this import at the top of the file:
-import { Suspense, defineComponent, h, toDisplayString, useAttrs } from 'vue'
+import { Suspense, defineComponent, h, toDisplayString, useAttrs, ref } from 'vue'
Committable suggestion skipped: line range outside the PR's diff.
describe('app/compat', () => { | ||
const Component = defineComponent({ | ||
setup () { | ||
const visible = ref(false) | ||
setInterval(() => { | ||
visible.value = true | ||
}, 1000) | ||
|
||
return () => h('div', {}, visible.value ? h('span', { id: 'child' }) : {}) | ||
}, | ||
}) | ||
it('setInterval is not auto-imported', async () => { | ||
vi.useFakeTimers() | ||
|
||
const wrapper = mount(Component) | ||
|
||
vi.advanceTimersByTime(1000) | ||
|
||
await wrapper.vm.$nextTick() | ||
|
||
expect(wrapper.find('#child').exists()).toBe(true) | ||
|
||
vi.useRealTimers() | ||
}) | ||
}) |
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.
💡 Codebase verification
Add test coverage for requestIdleCallback
polyfill
The codebase includes polyfills for both setInterval
and requestIdleCallback
, but only setInterval
is currently tested. A test for requestIdleCallback
is needed as it's the only other significant polyfill implemented in the compat layer.
Key points to test:
packages/nuxt/src/app/compat/idle-callback.ts
provides polyfill for browsers without native support- The polyfill includes both
requestIdleCallback
andcancelIdleCallback
- The implementation provides a fallback using
setTimeout
🔗 Analysis chain
Verify test coverage for other polyfilled APIs
The test suite only covers setInterval
. Consider adding tests for other APIs mentioned in the polyfills option, such as requestIdleCallback
.
Let's check what other APIs might need testing:
Let me gather more information about the polyfills and their test coverage.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Search for other polyfilled APIs in the codebase
rg -l "requestIdleCallback|setInterval" --type ts
Length of output: 379
Script:
#!/bin/bash
# Check the polyfills configuration and implementation
cat packages/nuxt/src/app/compat/idle-callback.ts
cat packages/nuxt/src/app/compat/interval.ts
cat packages/schema/src/types/imports.ts
# Check existing test coverage
rg -A 5 "describe.*compat" test/
Length of output: 3568
🔗 Linked issue
ref: nuxt/test-utils#1035
📚 Description
Mocking native APIs like
setInterval
could make it difficult to mock in tests.We would like to add a polyfills option to allow the choice of not overriding APIs like
setInterval
.